@@ -26,6 +26,7 @@ import (
2626 "k8s.io/apimachinery/pkg/api/errors"
2727 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2828 "k8s.io/apimachinery/pkg/runtime/schema"
29+ "k8s.io/apimachinery/pkg/types"
2930 "k8s.io/client-go/kubernetes"
3031 "k8s.io/client-go/util/retry"
3132
@@ -154,12 +155,10 @@ func (c *DeploymentController) ScaleToZero(cd *flaggerv1.Canary) error {
154155 return fmt .Errorf ("deployment %s.%s get query error: %w" , targetName , cd .Namespace , err )
155156 }
156157
157- depCopy := dep .DeepCopy ()
158- depCopy .Spec .Replicas = int32p (0 )
159-
160- _ , err = c .kubeClient .AppsV1 ().Deployments (dep .Namespace ).Update (context .TODO (), depCopy , metav1.UpdateOptions {})
158+ patch := []byte (fmt .Sprintf (`{"spec":{"replicas": %d}}` , 0 ))
159+ _ , err = c .kubeClient .AppsV1 ().Deployments (dep .Namespace ).Patch (context .TODO (), dep .GetName (), types .MergePatchType , patch , metav1.PatchOptions {})
161160 if err != nil {
162- return fmt .Errorf ("deployment %s.%s update query error: %w" , targetName , cd .Namespace , err )
161+ return fmt .Errorf ("deployment %s.%s patch query error: %w" , targetName , cd .Namespace , err )
163162 }
164163 return nil
165164}
@@ -210,12 +209,11 @@ func (c *DeploymentController) ScaleFromZero(cd *flaggerv1.Canary) error {
210209 }
211210 }
212211 }
213- depCopy := dep .DeepCopy ()
214- depCopy .Spec .Replicas = replicas
215212
216- _ , err = c .kubeClient .AppsV1 ().Deployments (dep .Namespace ).Update (context .TODO (), depCopy , metav1.UpdateOptions {})
213+ patch := []byte (fmt .Sprintf (`{"spec":{"replicas": %d}}` , * replicas ))
214+ _ , err = c .kubeClient .AppsV1 ().Deployments (dep .Namespace ).Patch (context .TODO (), dep .GetName (), types .MergePatchType , patch , metav1.PatchOptions {})
217215 if err != nil {
218- return fmt .Errorf ("scaling up %s.%s to %v failed: %v" , depCopy .GetName (), depCopy .Namespace , replicas , err )
216+ return fmt .Errorf ("scaling up %s.%s to %d failed: %v" , dep .GetName (), dep .Namespace , * replicas , err )
219217 }
220218 return nil
221219}
@@ -388,11 +386,10 @@ func (c *DeploymentController) scale(cd *flaggerv1.Canary, replicas int32) error
388386 return fmt .Errorf ("deployment %s.%s query error: %w" , targetName , cd .Namespace , err )
389387 }
390388
391- depCopy := dep .DeepCopy ()
392- depCopy .Spec .Replicas = int32p (replicas )
393- _ , err = c .kubeClient .AppsV1 ().Deployments (dep .Namespace ).Update (context .TODO (), depCopy , metav1.UpdateOptions {})
389+ patch := []byte (fmt .Sprintf (`{"spec":{"replicas": %d}}` , replicas ))
390+ _ , err = c .kubeClient .AppsV1 ().Deployments (dep .Namespace ).Patch (context .TODO (), dep .GetName (), types .MergePatchType , patch , metav1.PatchOptions {})
394391 if err != nil {
395- return fmt .Errorf ("scaling %s.%s to %v failed: %w" , depCopy .GetName (), depCopy .Namespace , replicas , err )
392+ return fmt .Errorf ("scaling %s.%s to %d failed: %w" , dep .GetName (), dep .Namespace , replicas , err )
396393 }
397394 return nil
398395}
0 commit comments