controllers

package
v0.1.0-alpha Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 3, 2021 License: Apache-2.0 Imports: 33 Imported by: 0

Documentation

Index

Constants

View Source
const (
	PhysBackupTimeLimitDefault = 60 * time.Minute
	StatusReady                = "Ready"
	StatusInProgress           = "InProgress"

	RestoreInProgress = "Restore" + StatusInProgress
	CreateInProgress  = "Create" + StatusInProgress
)
View Source
const (

	// OperatorName is the default operator name.
	OperatorName = "operator"
)

Variables

View Source
var (
	// SvcName is a string template for service names.
	SvcName = "%s-svc"
	// AgentSvcName is a string template for agent service names.
	AgentSvcName = "%s-agent-svc"
	// DbdaemonSvcName is a string template for dbdaemon service names.
	DbdaemonSvcName = "%s-dbdaemon-svc"
	// SvcEndpoint is a string template for service endpoints.
	SvcEndpoint = "%s.%s" // SvcName.namespaceName

	// StsName is a string template for Database stateful set names.
	StsName = "%s-sts"
	// AgentDeploymentName is a string template for agent deployment names.
	AgentDeploymentName = "%s-agent-deployment"
	// PvcMountName is a string template for pvc names.
	PvcMountName = "%s-pvc-%s" // inst.name-pvc-mount, e.g. mydb-pvc-u02
	// CmName is a string template for config map names.
	CmName = "%s-cm"
	// DatabasePodAppLabel is the 'app' label assigned to db pod.
	DatabasePodAppLabel = "db-op"
)
View Source
var CheckStatusInstanceFunc = func(ctx context.Context, instName, cdbName, clusterIP, DBDomain string, log logr.Logger) (string, error) {
	log.Info("resources/checkStatusInstance", "inst name", instName, "clusterIP", clusterIP)

	ctx, cancel := context.WithTimeout(ctx, dialTimeout)
	defer cancel()

	conn, err := grpc.Dial(fmt.Sprintf("%s:%d", clusterIP, consts.DefaultConfigAgentPort), grpc.WithInsecure())
	if err != nil {
		log.Error(err, "resources/checkStatusInstance: failed to create a conn via gRPC.Dial")
		return "", err
	}
	defer conn.Close()

	caClient := capb.NewConfigAgentClient(conn)
	cdOut, err := caClient.CheckStatus(ctx, &capb.CheckStatusRequest{
		Name:            instName,
		CdbName:         cdbName,
		CheckStatusType: capb.CheckStatusRequest_INSTANCE,
		DbDomain:        DBDomain,
	})
	if err != nil {
		return "", fmt.Errorf("resource/checkStatusInstance: failed on CheckStatus gRPC call: %v", err)
	}
	log.Info("resource/CheckStatusInstance: DONE with this output", "out", cdOut)

	return string(cdOut.Status), nil
}

checkStatusInstance attempts to determine a state of an database instance. In particular:

  • has provisioning finished?
  • is Instance up and accepting connection requests?
View Source
var ExecCmdFunc = func(p ExecCmdParams, cmd string) (string, error) {
	var cmdOut, cmdErr bytes.Buffer

	cmdShell := []string{"sh", "-c", cmd}

	req := p.Client.CoreV1().RESTClient().Post().Resource("pods").Name(p.Pod).
		Namespace(p.Ns).SubResource("exec")

	req.VersionedParams(&corev1.PodExecOptions{
		Container: p.Con.Name,
		Command:   cmdShell,
		Stdout:    true,
		Stderr:    true,
	}, scheme.ParameterCodec)

	exec, err := remotecommand.NewSPDYExecutor(p.RestConfig, "POST", req.URL())
	if err != nil {
		return "", fmt.Errorf("failed to init executor: %v", err)
	}

	// exec.Stream might return timout error, use a backoff with 4 retries
	// 100ms, 500ms, 2.5s, 12.5s
	var backoff = wait.Backoff{
		Steps:    4,
		Duration: 100 * time.Millisecond,
		Factor:   5.0,
		Jitter:   0.1,
	}
	if err := retry.OnError(backoff, func(error) bool { return true }, func() error {
		e := exec.Stream(remotecommand.StreamOptions{
			Stdout: &cmdOut,
			Stderr: &cmdErr,
			Tty:    false,
		})
		if e != nil {
			log.Error(fmt.Sprintf("exec.Stream failed, retrying, err: %v, stderr: %v, stdout: %v",
				e, cmdErr.String(), cmdOut.String()))
		}
		return e
	}); err != nil {
		return "", fmt.Errorf("failed to run a command [%v], err: %v, stderr: %v, stdout: %v",
			cmd, err, cmdErr.String(), cmdOut.String())
	}

	if cmdErr.Len() > 0 {
		return "", fmt.Errorf("stderr: %v", cmdErr.String())
	}

	return cmdOut.String(), nil
}

ExecCmdFunc invokes pod/exec.

Functions

func Contains

func Contains(array []string, elem string) bool

Contains check whether given "elem" presents in "array"

func DeleteLROOperation

func DeleteLROOperation(caClientFactory ConfigAgentClientFactory, ctx context.Context, r client.Reader, namespace, id, instName string) error

DeleteLROOperation deletes LRO operation for the specified namespace instance and operation id.

func DiskSpecs

func DiskSpecs(inst *v1alpha1.Instance, config *v1alpha1.Config) []commonv1alpha1.DiskSpec

func GetDBDomain

func GetDBDomain(inst *v1alpha1.Instance) string

GetDBDomain figures out DBDomain from DBUniqueName and DBDomain.

func GetLROOperation

func GetLROOperation(caClientFactory ConfigAgentClientFactory, ctx context.Context, r client.Reader, namespace, id, instName string) (*lropb.Operation, error)

GetLROOperation returns LRO operation for the specified namespace instance and operation id.

func GetLogLevelArgs

func GetLogLevelArgs(config *v1alpha1.Config) map[string][]string

GetLogLevelArgs returns agent args for log level.

func GetPVCNameAndMount

func GetPVCNameAndMount(instName, diskName string) (string, string)

GetPVCNameAndMount returns PVC names and their corresponding mount.

func IsAlreadyExistsError

func IsAlreadyExistsError(err error) bool

IsAlreadyExistsError returns true if given error is caused by object already exists.

func IsLROOperationDone

func IsLROOperationDone(caClientFactory ConfigAgentClientFactory, ctx context.Context, r client.Reader, namespace, id, instName string) (bool, error)

Check for LRO job status Return (true, nil) if LRO is done without errors. Return (true, err) if LRO is done with an error. Return (false, nil) if LRO still in progress. Return (false, err) if other error occurred.

func IsNotFoundError

func IsNotFoundError(err error) bool

IsNotFoundError returns true if given error is caused by object not found.

func NewAgentDeployment

func NewAgentDeployment(agentDeployment AgentDeploymentParams) (*appsv1.Deployment, error)

NewAgentDeployment returns the agent deployment.

func NewAgentSvc

func NewAgentSvc(inst *v1alpha1.Instance, scheme *runtime.Scheme) (*corev1.Service, error)

NewAgentSvc returns the service for the agent.

func NewConfigMap

func NewConfigMap(inst *v1alpha1.Instance, scheme *runtime.Scheme, cmName string) (*corev1.ConfigMap, error)

NewConfigMap returns the config map for database env variables.

func NewDBDaemonSvc

func NewDBDaemonSvc(inst *v1alpha1.Instance, scheme *runtime.Scheme) (*corev1.Service, error)

NewDBDaemonSvc returns the service for the database daemon server.

func NewPVCs

NewPVCs returns PVCs.

func NewPodTemplate

func NewPodTemplate(sp StsParams, cdbName, DBDomain string) corev1.PodTemplateSpec

NewPodTemplate returns the pod template for the database statefulset.

func NewSnapshotInst

func NewSnapshotInst(inst *v1alpha1.Instance, scheme *runtime.Scheme, pvcName, snapName, volumeSnapshotClassName string) (*snapv1.VolumeSnapshot, error)

NewSnapshot returns the snapshot for the given instance and pv.

func NewSts

NewSts returns the statefulset for the database pod.

func NewSvc

func NewSvc(inst *v1alpha1.Instance, scheme *runtime.Scheme, lb string) (*corev1.Service, error)

NewSvc returns the service for the database.

func SvcURL

func SvcURL(svc *corev1.Service, port int32) string

SvcURL returns the URL for the database service.

Types

type AgentDeploymentParams

type AgentDeploymentParams struct {
	Config         *v1alpha1.Config
	Inst           *v1alpha1.Instance
	Scheme         *runtime.Scheme
	Images         map[string]string
	PrivEscalation bool
	Name           string
	Log            logr.Logger
	Args           map[string][]string
	Services       []commonv1alpha1.Service
}

AgentDeploymentParams stores parameters for creating a agent deployment.

type ConfigAgentClientFactory

type ConfigAgentClientFactory interface {
	// New returns new Client.
	// connection close function should be invoked by the caller if
	// error is nil.
	New(ctx context.Context, r client.Reader, namespace, instName string) (capb.ConfigAgentClient, ConnCloseFunc, error)
}

ConfigAgentClientFactory is a GRPC implementation of ConfigAgentClientFactory. Exists for test mock.

type ConnCloseFunc

type ConnCloseFunc func()

type ExecCmdParams

type ExecCmdParams struct {
	Pod        string
	Ns         string
	Con        *corev1.Container
	Sch        *runtime.Scheme
	RestConfig *rest.Config
	Client     kubernetes.Interface
}

ExecCmdParams stores parameters for invoking pod/exec.

type GrpcConfigAgentClientFactory

type GrpcConfigAgentClientFactory struct {
	// contains filtered or unexported fields
}

ConfigAgentClientFactory is a GRPC implementation of ConfigAgentClientFactory. Exists for test mock.

func (*GrpcConfigAgentClientFactory) New

New returns a new config agent client.

type StsParams

type StsParams struct {
	Inst           *v1alpha1.Instance
	Scheme         *runtime.Scheme
	Namespace      string
	Images         map[string]string
	SvcName        string
	StsName        string
	PrivEscalation bool
	ConfigMap      *corev1.ConfigMap
	Restore        *v1alpha1.RestoreSpec
	Disks          []commonv1alpha1.DiskSpec
	Config         *v1alpha1.Config
	Log            logr.Logger
	Services       []commonv1alpha1.Service
}

StsParams stores parameters for creating a database stateful set.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL