@@ -27,6 +27,7 @@ import (
27
27
"k8s.io/client-go/tools/clientcmd"
28
28
"k8s.io/client-go/tools/remotecommand"
29
29
"k8s.io/client-go/util/homedir"
30
+ k8sRetry "k8s.io/client-go/util/retry"
30
31
)
31
32
32
33
const (
@@ -301,14 +302,27 @@ func WaitForPodDaemonset(ctx context.Context, clientset *kubernetes.Clientset, n
301
302
}
302
303
303
304
func MustUpdateReplica (ctx context.Context , deploymentsClient typedappsv1.DeploymentInterface , deploymentName string , replicas int32 ) {
304
- deployment , err := deploymentsClient .Get (ctx , deploymentName , metav1.GetOptions {})
305
- if err != nil {
306
- panic (errors .Wrapf (err , "could not get deployment %s" , deploymentName ))
307
- }
305
+ retryErr := k8sRetry .RetryOnConflict (k8sRetry .DefaultRetry , func () error {
306
+ // Get the latest Deployment resource.
307
+ deployment , getErr := deploymentsClient .Get (ctx , deploymentName , metav1.GetOptions {})
308
+ if getErr != nil {
309
+ return fmt .Errorf ("failed to get deployment: %w" , getErr )
310
+ }
311
+
312
+ // Modify the number of replicas.
313
+ deployment .Spec .Replicas = Int32ToPtr (replicas )
314
+
315
+ // Attempt to update the Deployment.
316
+ _ , updateErr := deploymentsClient .Update (ctx , deployment , metav1.UpdateOptions {})
317
+ if updateErr != nil {
318
+ return fmt .Errorf ("failed to update deployment: %w" , updateErr )
319
+ }
320
+
321
+ return nil // No error, operation succeeded.
322
+ })
308
323
309
- deployment .Spec .Replicas = Int32ToPtr (replicas )
310
- if _ , err := deploymentsClient .Update (ctx , deployment , metav1.UpdateOptions {}); err != nil {
311
- panic (errors .Wrapf (err , "could not update deployment %s" , deploymentName ))
324
+ if retryErr != nil {
325
+ panic (errors .Wrapf (retryErr , "could not update deployment %s" , deploymentName ))
312
326
}
313
327
}
314
328
0 commit comments