From d7472829dc5de46baf67ff7ab2780c1cf12246a4 Mon Sep 17 00:00:00 2001 From: zoumo Date: Tue, 26 Mar 2024 14:42:48 +0800 Subject: [PATCH] fix(canary): ensure canary objects are recycled post-release instead of deleting stable ones (#67) This commit corrects the release cleanup logic to recycle canary objects after the canary release phase is complete, rather than erroneously removing stable objects. The updated logic helps maintain service stability and ensures a proper transition from canary to stable environment. --- pkg/controllers/rolloutrun/control/control.go | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/pkg/controllers/rolloutrun/control/control.go b/pkg/controllers/rolloutrun/control/control.go index 52382c9..ca2b0c3 100644 --- a/pkg/controllers/rolloutrun/control/control.go +++ b/pkg/controllers/rolloutrun/control/control.go @@ -21,6 +21,7 @@ import ( "encoding/json" "errors" "fmt" + "strings" apierrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -213,11 +214,19 @@ func (c *CanaryReleaseControl) CreateOrUpdate(ctx context.Context, stable *workl return controllerutil.OperationResultUpdated, canaryInfo, nil } +func (c *CanaryReleaseControl) getCanaryName(stableName string) string { + return stableName + "-canary" +} + func (c *CanaryReleaseControl) getCanaryObject(cluster, namespace, name string) (client.Object, error) { + if strings.HasSuffix(name, "-canary") { + return nil, fmt.Errorf("input name should not end with -canary, got=%s", name) + } + canaryName := c.getCanaryName(name) canaryObj := c.workload.NewObject() err := c.client.Get( clusterinfo.WithCluster(context.TODO(), cluster), - client.ObjectKey{Namespace: namespace, Name: name}, + client.ObjectKey{Namespace: namespace, Name: canaryName}, canaryObj, ) return canaryObj, err @@ -225,8 +234,7 @@ func (c *CanaryReleaseControl) getCanaryObject(cluster, namespace, name string) func (c *CanaryReleaseControl) canaryObject(stable *workload.Info) (client.Object, bool, error) { // retrieve canary object - canaryName := stable.Name + "-canary" - canaryObj, err := c.getCanaryObject(stable.ClusterName, stable.Namespace, canaryName) + canaryObj, err := c.getCanaryObject(stable.ClusterName, stable.Namespace, stable.Name) if client.IgnoreNotFound(err) != nil { return nil, false, err } @@ -252,7 +260,7 @@ func (c *CanaryReleaseControl) canaryObject(stable *workload.Info) (client.Objec canaryObj.SetFinalizers(nil) canaryObj.SetManagedFields(nil) // set canary metadata - canaryObj.SetName(canaryName) + canaryObj.SetName(c.getCanaryName(stable.Name)) } return canaryObj, found, nil