From 25754a3f03a87ba4118d537a3f584c01d5766f06 Mon Sep 17 00:00:00 2001 From: Sanskar Jaiswal Date: Tue, 16 May 2023 17:39:12 +0530 Subject: [PATCH] resume target scaler during finalization Resume target scaler during finalization so that targetRef deployment does not get stuck at 0 replicas after canary has been deleted. Signed-off-by: Sanskar Jaiswal --- pkg/apis/flagger/v1beta1/canary.go | 5 +++-- pkg/controller/finalizer.go | 28 +++++++++++++++++++++++++++- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/pkg/apis/flagger/v1beta1/canary.go b/pkg/apis/flagger/v1beta1/canary.go index 304284532..6c67fbb8b 100644 --- a/pkg/apis/flagger/v1beta1/canary.go +++ b/pkg/apis/flagger/v1beta1/canary.go @@ -437,14 +437,15 @@ type LocalObjectReference struct { type AutoscalerRefernce struct { // API version of the scaler - // +optional + // +required APIVersion string `json:"apiVersion,omitempty"` // Kind of the scaler - // +optional + // +required Kind string `json:"kind,omitempty"` // Name of the scaler + // +required Name string `json:"name"` // PrimaryScalerQueries maps a unique id to a query for the primary diff --git a/pkg/controller/finalizer.go b/pkg/controller/finalizer.go index 94b8852e3..9781b511c 100644 --- a/pkg/controller/finalizer.go +++ b/pkg/controller/finalizer.go @@ -18,9 +18,12 @@ package controller import ( "context" + "errors" "fmt" + "time" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/util/wait" "k8s.io/client-go/util/retry" flaggerv1 "github.com/fluxcd/flagger/pkg/apis/flagger/v1beta1" @@ -52,6 +55,16 @@ func (c *Controller) finalize(old interface{}) error { c.recordEventInfof(canary, "Terminating canary %s.%s", canary.Name, canary.Namespace) } + // Resume target scaler so that the targetRef Deployment is not stuck at 0 replicas. + if canary.Spec.AutoscalerRef != nil { + scalerReconciler := c.canaryFactory.ScalerReconciler(canary.Spec.AutoscalerRef.Kind) + if scalerReconciler != nil { + if err = scalerReconciler.ResumeTargetScaler(canary); err != nil { + return fmt.Errorf("failed to resume target scaler during finalizing: %w", err) + } + } + } + // Revert the Kubernetes deployment or daemonset err = canaryController.Finalize(canary) if err != nil { @@ -61,7 +74,20 @@ func (c *Controller) finalize(old interface{}) error { // Ensure that targetRef has met a ready state c.logger.Infof("Checking if canary is ready %s.%s", canary.Name, canary.Namespace) - _, err = canaryController.IsCanaryReady(canary) + backoff := wait.Backoff{ + Duration: time.Second, + Factor: 2, + Cap: canary.GetAnalysisInterval(), + } + retry.OnError(backoff, func(err error) bool { + return err.Error() == "retriable error" + }, func() error { + retriable, err := canaryController.IsCanaryReady(canary) + if err != nil && retriable { + return errors.New("retriable error") + } + return err + }) if err != nil { return fmt.Errorf("canary not ready during finalizing: %w", err) }