Skip to content

Commit

Permalink
Set ObservedGeneration after condition init
Browse files Browse the repository at this point in the history
This patch implements the pattern that watches at the ObservedGeneration
to influence the DeploymentReadyCondition. The ObservedGeneration is
bumped in the main controller after the conditions Init, and the
deployment compare the generation with the ObservedGeneration to make
sure we look at the last version of the Deployment spec.

Signed-off-by: Francesco Pantano <[email protected]>
  • Loading branch information
fmount committed Apr 16, 2024
1 parent 8e29e92 commit b1cd9f2
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions controllers/horizon_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ func (r *HorizonReconciler) Reconcile(ctx context.Context, req ctrl.Request) (re
)

instance.Status.Conditions.Init(&cl)
instance.Status.ObservedGeneration = instance.Generation

// If we're not deleting this and the service object doesn't have our finalizer, add it.
if instance.DeletionTimestamp.IsZero() && controllerutil.AddFinalizer(instance, helper.GetFinalizer()) || isNewInstance {
Expand Down Expand Up @@ -717,7 +718,12 @@ func (r *HorizonReconciler) reconcileNormal(ctx context.Context, instance *horiz
return ctrlResult, nil
}
instance.Status.ReadyCount = depl.GetDeployment().Status.ReadyReplicas
if instance.Status.ReadyCount > 0 {
// Mark the Deployment as Ready only if the number of Replicas is equals
// to the Deployed instances (ReadyCount), but mark it as True is Replicas
// is zero. In addition, make sure the controller sees the last Generation
// by comparing it with the ObservedGeneration set in the StateFulSet.
if (instance.Status.ReadyCount == *instance.Spec.Replicas) &&
(depl.GetDeployment().Generation == depl.GetDeployment().Status.ObservedGeneration) {
instance.Status.Conditions.MarkTrue(condition.DeploymentReadyCondition, condition.DeploymentReadyMessage)
} else {
instance.Status.Conditions.Set(condition.FalseCondition(
Expand All @@ -728,8 +734,6 @@ func (r *HorizonReconciler) reconcileNormal(ctx context.Context, instance *horiz
}
// create Deployment - end

// Update the lastObserved generation before evaluating conditions
instance.Status.ObservedGeneration = instance.Generation
// We reached the end of the Reconcile, update the Ready condition based on
// the sub conditions
if instance.Status.Conditions.AllSubConditionIsTrue() {
Expand Down

0 comments on commit b1cd9f2

Please sign in to comment.