@@ -18,6 +18,7 @@ package router
18
18
19
19
import (
20
20
"context"
21
+ "encoding/json"
21
22
"fmt"
22
23
23
24
corev1 "k8s.io/api/core/v1"
@@ -30,6 +31,7 @@ import (
30
31
"k8s.io/apimachinery/pkg/api/errors"
31
32
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
32
33
"k8s.io/apimachinery/pkg/runtime/schema"
34
+ types "k8s.io/apimachinery/pkg/types"
33
35
"k8s.io/client-go/kubernetes"
34
36
35
37
flaggerv1 "github.com/fluxcd/flagger/pkg/apis/flagger/v1beta1"
@@ -275,7 +277,8 @@ func (gr *GlooRouter) createFlaggerUpstream(canary *flaggerv1.Canary, upstreamNa
275
277
if err != nil {
276
278
return fmt .Errorf ("service %s.%s get query error: %w" , svcName , canary .Namespace , err )
277
279
}
278
- _ , err = upstreamClient .Get (context .TODO (), upstreamName , metav1.GetOptions {})
280
+ curUpstream , err := upstreamClient .Get (context .TODO (), upstreamName , metav1.GetOptions {})
281
+
279
282
if errors .IsNotFound (err ) {
280
283
glooUpstreamWithConfig , err := gr .getGlooConfigUpstream (canary )
281
284
if err != nil {
@@ -288,7 +291,42 @@ func (gr *GlooRouter) createFlaggerUpstream(canary *flaggerv1.Canary, upstreamNa
288
291
}
289
292
} else if err != nil {
290
293
return fmt .Errorf ("upstream %s.%s get query error: %w" , upstreamName , canary .Namespace , err )
294
+ } else {
295
+ return gr .syncUpstreamSpec (curUpstream , canary )
296
+ }
297
+ return nil
298
+ }
299
+
300
+ func (gr * GlooRouter ) syncUpstreamSpec (curUpstream * gloov1.Upstream , canary * flaggerv1.Canary ) error {
301
+ glooUpstreamWithConfig , err := gr .getGlooConfigUpstream (canary )
302
+ if err != nil {
303
+ return err
291
304
}
305
+
306
+ if glooUpstreamWithConfig == nil {
307
+ return nil
308
+ }
309
+
310
+ glooUpstreamLB := glooUpstreamWithConfig .Spec .LoadBalancerConfig
311
+ loadBalancerDiff := cmp .Diff (glooUpstreamLB , curUpstream .Spec .LoadBalancerConfig )
312
+
313
+ if loadBalancerDiff != "" {
314
+ gr .logger .Debugf ("detect diff in upstream spec %s.%s %s" , curUpstream .Name , canary .Namespace , loadBalancerDiff )
315
+
316
+ patchUpstream := gloov1.Upstream {}
317
+ patchUpstream .Spec = gloov1.UpstreamSpec {}
318
+ patchUpstream .Spec .LoadBalancerConfig = glooUpstreamLB
319
+ patchBytes , err := json .Marshal (patchUpstream )
320
+ if err != nil {
321
+ return fmt .Errorf ("unable to marshal patch upstream from %s.%s with error: %w" , glooUpstreamWithConfig .Name , glooUpstreamWithConfig .Namespace , err )
322
+ }
323
+
324
+ _ , err = gr .glooClient .GlooV1 ().Upstreams (canary .Namespace ).Patch (context .TODO (), curUpstream .Name , types .MergePatchType , patchBytes , metav1.PatchOptions {})
325
+ if err != nil {
326
+ return fmt .Errorf ("upstream %s.%s spec patch error: %w" , curUpstream .Name , canary .Namespace , err )
327
+ }
328
+ }
329
+
292
330
return nil
293
331
}
294
332
0 commit comments