Skip to content

Commit a437af0

Browse files
authored
Merge pull request #1617 from sopida-chotwanwirach/gloo-reconcile-upstream-spec-change
fix(gloo): Update reconciler to detect change in gloo upstream spec
2 parents c45be96 + e3a529e commit a437af0

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed

pkg/router/gloo.go

+39-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package router
1818

1919
import (
2020
"context"
21+
"encoding/json"
2122
"fmt"
2223

2324
corev1 "k8s.io/api/core/v1"
@@ -30,6 +31,7 @@ import (
3031
"k8s.io/apimachinery/pkg/api/errors"
3132
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3233
"k8s.io/apimachinery/pkg/runtime/schema"
34+
types "k8s.io/apimachinery/pkg/types"
3335
"k8s.io/client-go/kubernetes"
3436

3537
flaggerv1 "github.com/fluxcd/flagger/pkg/apis/flagger/v1beta1"
@@ -275,7 +277,8 @@ func (gr *GlooRouter) createFlaggerUpstream(canary *flaggerv1.Canary, upstreamNa
275277
if err != nil {
276278
return fmt.Errorf("service %s.%s get query error: %w", svcName, canary.Namespace, err)
277279
}
278-
_, err = upstreamClient.Get(context.TODO(), upstreamName, metav1.GetOptions{})
280+
curUpstream, err := upstreamClient.Get(context.TODO(), upstreamName, metav1.GetOptions{})
281+
279282
if errors.IsNotFound(err) {
280283
glooUpstreamWithConfig, err := gr.getGlooConfigUpstream(canary)
281284
if err != nil {
@@ -288,7 +291,42 @@ func (gr *GlooRouter) createFlaggerUpstream(canary *flaggerv1.Canary, upstreamNa
288291
}
289292
} else if err != nil {
290293
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
291304
}
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+
292330
return nil
293331
}
294332

test/gloo/test-canary.sh

+22
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,28 @@ done
117117

118118
echo '✔ Canary initialization test passed'
119119

120+
echo '>>> Waiting for primary spec to be updated'
121+
122+
# Update gloo upstream on slow start config which will trigger update on flagger upstreams
123+
kubectl -n gloo-system patch upstream config-upstream --type json --patch='[ { "op": "replace", "path": "/spec/loadBalancerConfig/roundRobin/slowStartConfig/minWeightPercent", "value": 20 } ]'
124+
125+
retries=50
126+
count=0
127+
ok=false
128+
until ${ok}; do
129+
kubectl -n test get upstream/test-podinfo-canaryupstream-80 -ojson | jq '.spec.loadBalancerConfig.roundRobin.slowStartConfig.minWeightPercent' | grep '20' && ok=true || ok=false
130+
131+
sleep 5
132+
count=$(($count + 1))
133+
if [[ ${count} -eq ${retries} ]]; then
134+
kubectl -n gloo-system logs deployment/flagger
135+
echo "No more retries left"
136+
exit 1
137+
fi
138+
done
139+
140+
echo '✔ Canary reconcilation test passed'
141+
120142
echo '>>> Triggering canary deployment'
121143
kubectl -n test set image deployment/podinfo podinfod=ghcr.io/stefanprodan/podinfo:6.0.1
122144

0 commit comments

Comments
 (0)