From e1ba1b0ea638c2a60837752f27a461f2795b57e7 Mon Sep 17 00:00:00 2001 From: Wei-Xiang Sun Date: Thu, 1 Sep 2022 14:39:35 +0800 Subject: [PATCH] update canary status after the first deployment of rollout (#72) Signed-off-by: mingzhou.swx Signed-off-by: mingzhou.swx Co-authored-by: mingzhou.swx --- pkg/controller/rollout/status.go | 27 ++++++++++++++++++++++++++- pkg/util/controller_finder.go | 28 ++++++++++++++++++++-------- 2 files changed, 46 insertions(+), 9 deletions(-) diff --git a/pkg/controller/rollout/status.go b/pkg/controller/rollout/status.go index c3396f2..738e732 100644 --- a/pkg/controller/rollout/status.go +++ b/pkg/controller/rollout/status.go @@ -93,12 +93,37 @@ func (r *RolloutReconciler) updateRolloutStatus(rollout *rolloutv1alpha1.Rollout newStatus.Phase = rolloutv1alpha1.RolloutPhaseHealthy newStatus.Message = "rollout is healthy" case rolloutv1alpha1.RolloutPhaseHealthy: - // from healthy to progressing if workload.InRolloutProgressing { + // from healthy to progressing klog.Infof("rollout(%s/%s) status phase from(%s) -> to(%s)", rollout.Namespace, rollout.Name, rolloutv1alpha1.RolloutPhaseHealthy, rolloutv1alpha1.RolloutPhaseProgressing) newStatus.Phase = rolloutv1alpha1.RolloutPhaseProgressing cond := util.NewRolloutCondition(rolloutv1alpha1.RolloutConditionProgressing, corev1.ConditionFalse, rolloutv1alpha1.ProgressingReasonInitializing, "Rollout is in Progressing") util.SetRolloutCondition(&newStatus, *cond) + } else if workload.IsInStable && newStatus.CanaryStatus == nil { + // The following logic is to make PaaS be able to judge whether the rollout is ready + // at the first deployment of the Rollout/Workload. For example: generally, a PaaS + // platform can use the following code to judge whether the rollout progression is completed: + // ``` + // if getRolloutID(workload, rollout) == rollout.Status.CanaryStatus.ObservedRolloutID && + // rollout.Status.CanaryStatus.CurrentStepState == "Completed" { + // // do something after rollout + // } + //``` + // But at the first deployment of Rollout/Workload, CanaryStatus isn't set due to no rollout progression, + // and PaaS platform cannot judge whether the deployment is completed base on the code above. So we have + // to update the status just like the rollout was completed. + + newStatus.CanaryStatus = &rolloutv1alpha1.CanaryStatus{ + CanaryReplicas: workload.CanaryReplicas, + CanaryReadyReplicas: workload.CanaryReadyReplicas, + ObservedRolloutID: getRolloutID(workload, rollout), + ObservedWorkloadGeneration: workload.Generation, + PodTemplateHash: workload.PodTemplateHash, + CanaryRevision: workload.CanaryRevision, + CurrentStepIndex: int32(len(rollout.Spec.Strategy.Canary.Steps)), + CurrentStepState: rolloutv1alpha1.CanaryStepStateCompleted, + } + newStatus.Message = "workload deployment is completed" } case rolloutv1alpha1.RolloutPhaseProgressing: cond := util.GetRolloutCondition(newStatus, rolloutv1alpha1.RolloutConditionProgressing) diff --git a/pkg/util/controller_finder.go b/pkg/util/controller_finder.go index c36880c..5ae6af8 100644 --- a/pkg/util/controller_finder.go +++ b/pkg/util/controller_finder.go @@ -54,6 +54,8 @@ type Workload struct { // Revision hash key RevisionLabelKey string + // Is it in stable and no need to publish + IsInStable bool // Is it in rollback phase IsInRollback bool // indicate whether the workload can enter the rollout process @@ -140,13 +142,15 @@ func (r *ControllerFinder) getKruiseCloneSet(namespace string, ref *rolloutv1alp PodTemplateHash: cloneSet.Status.UpdateRevision[strings.LastIndex(cloneSet.Status.UpdateRevision, "-")+1:], IsStatusConsistent: true, } - // not in rollout progressing - if _, ok = workload.Annotations[InRolloutProgressingAnnotation]; !ok { - return workload, nil - } // no need to progress if cloneSet.Status.Replicas == cloneSet.Status.UpdatedReplicas && cloneSet.Status.CurrentRevision == cloneSet.Status.UpdateRevision { + workload.IsInStable = true + return workload, nil + } + + // not in rollout progressing + if _, ok = workload.Annotations[InRolloutProgressingAnnotation]; !ok { return workload, nil } @@ -192,6 +196,12 @@ func (r *ControllerFinder) getDeployment(namespace string, ref *rolloutv1alpha1. CanaryRevision: ComputeHash(&stable.Spec.Template, nil), RevisionLabelKey: apps.DefaultDeploymentUniqueLabelKey, } + + // deployment is stable + if stable.Status.Replicas == stable.Status.UpdatedReplicas { + workload.IsInStable = true + } + // not in rollout progressing if _, ok = workload.Annotations[InRolloutProgressingAnnotation]; !ok { return workload, nil @@ -256,13 +266,15 @@ func (r *ControllerFinder) getStatefulSetLikeWorkload(namespace string, ref *rol PodTemplateHash: workloadInfo.Status.UpdateRevision, IsStatusConsistent: true, } - // not in rollout progressing - if _, ok := workload.Annotations[InRolloutProgressingAnnotation]; !ok { - return workload, nil - } // no need to progress if workloadInfo.Status.Replicas == workloadInfo.Status.UpdatedReplicas && workloadInfo.Status.StableRevision == workloadInfo.Status.UpdateRevision { + workload.IsInStable = true + return workload, nil + } + + // not in rollout progressing + if _, ok := workload.Annotations[InRolloutProgressingAnnotation]; !ok { return workload, nil }