@@ -29,6 +29,7 @@ import (
29
29
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
30
30
"k8s.io/apimachinery/pkg/runtime/schema"
31
31
"k8s.io/client-go/kubernetes"
32
+ "k8s.io/client-go/util/retry"
32
33
33
34
flaggerv1 "github.com/fluxcd/flagger/pkg/apis/flagger/v1beta1"
34
35
clientset "github.com/fluxcd/flagger/pkg/client/clientset/versioned"
@@ -84,54 +85,72 @@ func (c *DeploymentController) Promote(cd *flaggerv1.Canary) error {
84
85
targetName := cd .Spec .TargetRef .Name
85
86
primaryName := fmt .Sprintf ("%s-primary" , targetName )
86
87
87
- canary , err := c .kubeClient .AppsV1 ().Deployments (cd .Namespace ).Get (context .TODO (), targetName , metav1.GetOptions {})
88
- if err != nil {
89
- return fmt .Errorf ("deployment %s.%s get query error: %w" , targetName , cd .Namespace , err )
90
- }
88
+ err := retry .RetryOnConflict (retry .DefaultRetry , func () error {
89
+ canary , err := c .kubeClient .AppsV1 ().Deployments (cd .Namespace ).Get (context .TODO (), targetName , metav1.GetOptions {})
90
+ if err != nil {
91
+ return fmt .Errorf ("deployment %s.%s get query error: %w" , targetName , cd .Namespace , err )
92
+ }
91
93
92
- label , labelValue , err := c .getSelectorLabel (canary )
93
- primaryLabelValue := fmt .Sprintf ("%s-primary" , labelValue )
94
- if err != nil {
95
- return fmt .Errorf ("getSelectorLabel failed: %w" , err )
96
- }
94
+ label , labelValue , err := c .getSelectorLabel (canary )
95
+ primaryLabelValue := fmt .Sprintf ("%s-primary" , labelValue )
96
+ if err != nil {
97
+ return fmt .Errorf ("getSelectorLabel failed: %w" , err )
98
+ }
97
99
98
- primary , err := c .kubeClient .AppsV1 ().Deployments (cd .Namespace ).Get (context .TODO (), primaryName , metav1.GetOptions {})
99
- if err != nil {
100
- return fmt .Errorf ("deployment %s.%s get query error: %w" , primaryName , cd .Namespace , err )
101
- }
100
+ primary , err := c .kubeClient .AppsV1 ().Deployments (cd .Namespace ).Get (context .TODO (), primaryName , metav1.GetOptions {})
101
+ if err != nil {
102
+ return fmt .Errorf ("deployment %s.%s get query error: %w" , primaryName , cd .Namespace , err )
103
+ }
102
104
103
- // promote secrets and config maps
104
- configRefs , err := c .configTracker .GetTargetConfigs (cd )
105
- if err != nil {
106
- return fmt .Errorf ("GetTargetConfigs failed: %w" , err )
107
- }
108
- if err := c .configTracker .CreatePrimaryConfigs (cd , configRefs , c .includeLabelPrefix ); err != nil {
109
- return fmt .Errorf ("CreatePrimaryConfigs failed: %w" , err )
110
- }
105
+ // promote secrets and config maps
106
+ configRefs , err := c .configTracker .GetTargetConfigs (cd )
107
+ if err != nil {
108
+ return fmt .Errorf ("GetTargetConfigs failed: %w" , err )
109
+ }
110
+ if err := c .configTracker .CreatePrimaryConfigs (cd , configRefs , c .includeLabelPrefix ); err != nil {
111
+ return fmt .Errorf ("CreatePrimaryConfigs failed: %w" , err )
112
+ }
111
113
112
- primaryCopy := primary .DeepCopy ()
113
- primaryCopy .Spec .ProgressDeadlineSeconds = canary .Spec .ProgressDeadlineSeconds
114
- primaryCopy .Spec .MinReadySeconds = canary .Spec .MinReadySeconds
115
- primaryCopy .Spec .RevisionHistoryLimit = canary .Spec .RevisionHistoryLimit
116
- primaryCopy .Spec .Strategy = canary .Spec .Strategy
114
+ primaryCopy := primary .DeepCopy ()
115
+ primaryCopy .Spec .ProgressDeadlineSeconds = canary .Spec .ProgressDeadlineSeconds
116
+ primaryCopy .Spec .MinReadySeconds = canary .Spec .MinReadySeconds
117
+ primaryCopy .Spec .RevisionHistoryLimit = canary .Spec .RevisionHistoryLimit
118
+ primaryCopy .Spec .Strategy = canary .Spec .Strategy
117
119
118
- // update spec with primary secrets and config maps
119
- primaryCopy .Spec .Template .Spec = c .getPrimaryDeploymentTemplateSpec (canary , configRefs )
120
+ // update spec with primary secrets and config maps
121
+ primaryCopy .Spec .Template .Spec = c .getPrimaryDeploymentTemplateSpec (canary , configRefs )
120
122
121
- // update pod annotations to ensure a rolling update
122
- annotations , err := makeAnnotations (canary .Spec .Template .Annotations )
123
- if err != nil {
124
- return fmt .Errorf ("makeAnnotations failed: %w" , err )
125
- }
123
+ // update pod annotations to ensure a rolling update
124
+ annotations , err := makeAnnotations (canary .Spec .Template .Annotations )
125
+ if err != nil {
126
+ return fmt .Errorf ("makeAnnotations failed: %w" , err )
127
+ }
126
128
127
- primaryCopy .Spec .Template .Annotations = annotations
128
- primaryCopy .Spec .Template .Labels = makePrimaryLabels (canary .Spec .Template .Labels , primaryLabelValue , label )
129
+ primaryCopy .Spec .Template .Annotations = annotations
130
+ primaryCopy .Spec .Template .Labels = makePrimaryLabels (canary .Spec .Template .Labels , primaryLabelValue , label )
129
131
130
- // apply update
131
- _ , err = c .kubeClient .AppsV1 ().Deployments (cd .Namespace ).Update (context .TODO (), primaryCopy , metav1.UpdateOptions {})
132
+ if cd .Spec .Deployment .Primary != nil {
133
+ if len (primaryCopy .ObjectMeta .Annotations ) == 0 {
134
+ primaryCopy .ObjectMeta .Annotations = make (map [string ]string )
135
+ }
136
+ for k , v := range cd .Spec .Deployment .Primary .Annotations {
137
+ primaryCopy .ObjectMeta .Annotations [k ] = v
138
+ }
139
+ if len (primaryCopy .ObjectMeta .Labels ) == 0 {
140
+ primaryCopy .ObjectMeta .Labels = make (map [string ]string )
141
+ }
142
+ for k , v := range cd .Spec .Deployment .Primary .Labels {
143
+ primaryCopy .ObjectMeta .Labels [k ] = v
144
+ }
145
+ }
146
+
147
+ // apply update
148
+ _ , err = c .kubeClient .AppsV1 ().Deployments (cd .Namespace ).Update (context .TODO (), primaryCopy , metav1.UpdateOptions {})
149
+ return err
150
+ })
132
151
if err != nil {
133
152
return fmt .Errorf ("updating deployment %s.%s template spec failed: %w" ,
134
- primaryCopy . GetName (), primaryCopy .Namespace , err )
153
+ primaryName , cd .Namespace , err )
135
154
}
136
155
137
156
// update HPA
@@ -364,18 +383,45 @@ func (c *DeploymentController) reconcilePrimaryHpa(cd *flaggerv1.Canary, init bo
364
383
if ! init && primaryHpa != nil {
365
384
diffMetrics := cmp .Diff (hpaSpec .Metrics , primaryHpa .Spec .Metrics )
366
385
diffBehavior := cmp .Diff (hpaSpec .Behavior , primaryHpa .Spec .Behavior )
367
- if diffMetrics != "" || diffBehavior != "" || int32Default (hpaSpec .MinReplicas ) != int32Default (primaryHpa .Spec .MinReplicas ) || hpaSpec .MaxReplicas != primaryHpa .Spec .MaxReplicas {
386
+ diffLabels := cmp .Diff (hpa .ObjectMeta .Labels , primaryHpa .ObjectMeta .Labels )
387
+ diffAnnotations := cmp .Diff (hpa .ObjectMeta .Annotations , primaryHpa .ObjectMeta .Annotations )
388
+ if diffMetrics != "" || diffBehavior != "" || diffLabels != "" || diffAnnotations != "" || int32Default (hpaSpec .MinReplicas ) != int32Default (primaryHpa .Spec .MinReplicas ) || hpaSpec .MaxReplicas != primaryHpa .Spec .MaxReplicas {
368
389
fmt .Println (diffMetrics , diffBehavior , hpaSpec .MinReplicas , primaryHpa .Spec .MinReplicas , hpaSpec .MaxReplicas , primaryHpa .Spec .MaxReplicas )
369
- hpaClone := primaryHpa .DeepCopy ()
370
- hpaClone .Spec .MaxReplicas = hpaSpec .MaxReplicas
371
- hpaClone .Spec .MinReplicas = hpaSpec .MinReplicas
372
- hpaClone .Spec .Metrics = hpaSpec .Metrics
373
- hpaClone .Spec .Behavior = hpaSpec .Behavior
374
390
375
- _ , err := c .kubeClient .AutoscalingV2beta2 ().HorizontalPodAutoscalers (cd .Namespace ).Update (context .TODO (), hpaClone , metav1.UpdateOptions {})
391
+ err = retry .RetryOnConflict (retry .DefaultRetry , func () error {
392
+ primaryHpa , err := c .kubeClient .AutoscalingV2beta2 ().HorizontalPodAutoscalers (cd .Namespace ).Get (context .TODO (), primaryHpaName , metav1.GetOptions {})
393
+ if err != nil {
394
+ return err
395
+ }
396
+
397
+ hpaClone := primaryHpa .DeepCopy ()
398
+ hpaClone .Spec .MaxReplicas = hpaSpec .MaxReplicas
399
+ hpaClone .Spec .MinReplicas = hpaSpec .MinReplicas
400
+ hpaClone .Spec .Metrics = hpaSpec .Metrics
401
+ hpaClone .Spec .Behavior = hpaSpec .Behavior
402
+
403
+ if cd .Spec .Autoscaler .Primary != nil {
404
+ if len (hpaClone .ObjectMeta .Annotations ) == 0 {
405
+ hpaClone .ObjectMeta .Annotations = make (map [string ]string )
406
+ }
407
+ for k , v := range cd .Spec .Autoscaler .Primary .Annotations {
408
+ hpaClone .ObjectMeta .Annotations [k ] = v
409
+ }
410
+ if len (hpaClone .ObjectMeta .Labels ) == 0 {
411
+ hpaClone .ObjectMeta .Labels = make (map [string ]string )
412
+ }
413
+ for k , v := range cd .Spec .Autoscaler .Primary .Labels {
414
+ hpaClone .ObjectMeta .Labels [k ] = v
415
+ }
416
+ }
417
+
418
+ _ , err = c .kubeClient .AutoscalingV2beta2 ().HorizontalPodAutoscalers (cd .Namespace ).Update (context .TODO (), hpaClone , metav1.UpdateOptions {})
419
+ return err
420
+ })
421
+
376
422
if err != nil {
377
423
return fmt .Errorf ("updating HorizontalPodAutoscaler %s.%s failed: %w" ,
378
- hpaClone .Name , hpaClone .Namespace , err )
424
+ primaryHpa .Name , primaryHpa .Namespace , err )
379
425
}
380
426
c .logger .With ("canary" , fmt .Sprintf ("%s.%s" , cd .Name , cd .Namespace )).
381
427
Infof ("HorizontalPodAutoscaler %s.%s updated" , primaryHpa .GetName (), cd .Namespace )
0 commit comments