Skip to content

Commit

Permalink
deploymentcontroller: make it possible to pick conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
bertinatto committed Mar 6, 2024
1 parent 99c950c commit 5fafaba
Showing 1 changed file with 58 additions and 32 deletions.
90 changes: 58 additions & 32 deletions pkg/operator/deploymentcontroller/deployment_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ type ManifestHookFunc func(*opv1.OperatorSpec, []byte) ([]byte, error)
//
// This controller supports removable operands, as configured in pkg/operator/management.
//
// This controller produces the following conditions:
// This controller optionally produces the following conditions:
// <name>Available: indicates that the deployment controller was successfully deployed and at least one Deployment replica is available.
// <name>Progressing: indicates that the Deployment is in progress.
// <name>Degraded: produced when the sync() method returns an error.
Expand All @@ -46,6 +46,7 @@ type DeploymentController struct {
deployInformer appsinformersv1.DeploymentInformer
optionalInformers []factory.Informer
recorder events.Recorder
conditions []string
// Optional hook functions to modify the deployment manifest.
// This helps in modifying the manifests before it deployment
// is created from the manifest.
Expand Down Expand Up @@ -79,6 +80,10 @@ func NewDeploymentController(
operatorClient,
kubeClient,
deployInformer,
).WithConditions(
opv1.OperatorStatusTypeAvailable,
opv1.OperatorStatusTypeProgressing,
opv1.OperatorStatusTypeDegraded,
).WithExtraInformers(
optionalInformers...,
).WithManifestHooks(
Expand Down Expand Up @@ -122,22 +127,28 @@ func (c *DeploymentController) WithDeploymentHooks(hooks ...DeploymentHookFunc)
return c
}

func (c *DeploymentController) WithConditions(conditions ...string) *DeploymentController {
c.conditions = conditions
return c
}

func (c *DeploymentController) ToController() factory.Controller {
informers := append(
c.optionalInformers,
c.operatorClient.Informer(),
c.deployInformer.Informer(),
)
return factory.New().WithInformers(
controller := factory.New().WithInformers(
informers...,
).WithSync(
c.sync,
).ResyncEvery(
time.Minute,
).WithSyncDegradedOnError(
c.operatorClient,
).ToController(
)
if containsCondition(c.conditions, opv1.OperatorStatusTypeDegraded) {
controller = controller.WithSyncDegradedOnError(c.operatorClient)
}
return controller.ToController(
c.name,
c.recorder.WithComponentSuffix(strings.ToLower(c.name)+"-deployment-controller-"),
)
Expand Down Expand Up @@ -194,42 +205,48 @@ func (c *DeploymentController) syncManaged(ctx context.Context, opSpec *opv1.Ope
return err
}

availableCondition := opv1.OperatorCondition{
Type: c.name + opv1.OperatorStatusTypeAvailable,
Status: opv1.ConditionTrue,
}

if deployment.Status.AvailableReplicas > 0 {
availableCondition.Status = opv1.ConditionTrue
} else {
availableCondition.Status = opv1.ConditionFalse
availableCondition.Message = "Waiting for Deployment"
availableCondition.Reason = "Deploying"
}

progressingCondition := opv1.OperatorCondition{
Type: c.name + opv1.OperatorStatusTypeProgressing,
Status: opv1.ConditionFalse,
updateStatusFuncs := []v1helpers.UpdateStatusFunc{
func(newStatus *opv1.OperatorStatus) error {
// TODO: set ObservedGeneration (the last stable generation change we dealt with)
resourcemerge.SetDeploymentGeneration(&newStatus.Generations, deployment)
return nil
},
}

if ok, msg := isProgressing(deployment); ok {
progressingCondition.Status = opv1.ConditionTrue
progressingCondition.Message = msg
progressingCondition.Reason = "Deploying"
// Set Available Condition
if containsCondition(c.conditions, opv1.OperatorStatusTypeAvailable) {
availableCondition := opv1.OperatorCondition{
Type: c.name + opv1.OperatorStatusTypeAvailable,
Status: opv1.ConditionTrue,
}
if deployment.Status.AvailableReplicas > 0 {
availableCondition.Status = opv1.ConditionTrue
} else {
availableCondition.Status = opv1.ConditionFalse
availableCondition.Message = "Waiting for Deployment"
availableCondition.Reason = "Deploying"
}
updateStatusFuncs = append(updateStatusFuncs, v1helpers.UpdateConditionFn(availableCondition))
}

updateStatusFn := func(newStatus *opv1.OperatorStatus) error {
// TODO: set ObservedGeneration (the last stable generation change we dealt with)
resourcemerge.SetDeploymentGeneration(&newStatus.Generations, deployment)
return nil
// Set Progressing Condition
if containsCondition(c.conditions, opv1.OperatorStatusTypeProgressing) {
progressingCondition := opv1.OperatorCondition{
Type: c.name + opv1.OperatorStatusTypeProgressing,
Status: opv1.ConditionFalse,
}
if ok, msg := isProgressing(deployment); ok {
progressingCondition.Status = opv1.ConditionTrue
progressingCondition.Message = msg
progressingCondition.Reason = "Deploying"
}
updateStatusFuncs = append(updateStatusFuncs, v1helpers.UpdateConditionFn(progressingCondition))
}

_, _, err = v1helpers.UpdateStatus(
ctx,
c.operatorClient,
updateStatusFn,
v1helpers.UpdateConditionFn(availableCondition),
v1helpers.UpdateConditionFn(progressingCondition),
updateStatusFuncs...,
)

return err
Expand Down Expand Up @@ -292,3 +309,12 @@ func isProgressing(deployment *appsv1.Deployment) (bool, string) {
}
return false, ""
}

func containsCondition(conditions []string, condition string) bool {
for i := range conditions {
if conditions[i] == condition {
return true
}
}
return false
}

0 comments on commit 5fafaba

Please sign in to comment.