Skip to content

Commit 47fd837

Browse files
committed
add failing test for VS cleanup on delegation
1 parent 27daa2c commit 47fd837

File tree

2 files changed

+75
-3
lines changed

2 files changed

+75
-3
lines changed

pkg/router/istio_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -599,6 +599,39 @@ func TestIstioRouter_Delegate(t *testing.T) {
599599
err := router.Reconcile(mocks.canary)
600600
require.Error(t, err)
601601
})
602+
603+
t.Run("syncs when enabled", func(t *testing.T) {
604+
mocks := newFixture(nil)
605+
mocks.canary.Spec.Service.Hosts = []string{}
606+
mocks.canary.Spec.Service.Gateways = []string{}
607+
mocks.canary.Spec.Service.PortDiscovery = false
608+
609+
router := &IstioRouter{
610+
logger: mocks.logger,
611+
flaggerClient: mocks.flaggerClient,
612+
istioClient: mocks.meshClient,
613+
kubeClient: mocks.kubeClient,
614+
}
615+
616+
err := router.Reconcile(mocks.canary)
617+
require.NoError(t, err)
618+
619+
vs, err := mocks.meshClient.NetworkingV1beta1().VirtualServices("default").Get(context.TODO(), "podinfo", metav1.GetOptions{})
620+
require.NoError(t, err)
621+
require.Len(t, vs.Spec.Http, 1)
622+
require.Len(t, vs.Spec.Http[0].Route, 2)
623+
624+
// enable delegation
625+
mocks.canary.Spec.Service.Delegation = true
626+
627+
err = router.Reconcile(mocks.canary)
628+
require.NoError(t, err)
629+
630+
vs, err = mocks.meshClient.NetworkingV1beta1().VirtualServices("default").Get(context.TODO(), "podinfo", metav1.GetOptions{})
631+
require.NoError(t, err)
632+
assert.Len(t, vs.Spec.Gateways, 0)
633+
assert.Len(t, vs.Spec.Hosts, 0)
634+
})
602635
}
603636

604637
func TestIstioRouter_Finalize(t *testing.T) {

test/istio/test-delegation.sh

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ spec:
5252
namespace: test
5353
EOF
5454

55-
echo '>>> Initialising canary for delegate'
55+
echo '>>> Initialising canary'
5656
cat <<EOF | kubectl apply -f -
5757
apiVersion: flagger.app/v1beta1
5858
kind: Canary
@@ -68,8 +68,7 @@ spec:
6868
service:
6969
port: 80
7070
targetPort: 9898
71-
portDiscovery: true
72-
delegation: true
71+
delegation: false # start with delegation disabled to make sure it works properly after it's enabled
7372
analysis:
7473
interval: 15s
7574
threshold: 15
@@ -102,6 +101,46 @@ done
102101

103102
echo '✔ Canary initialization test passed'
104103

104+
echo '>>> Enabling delegation on canary'
105+
kubectl patch canary podinfo -n test --type=merge -p '{"spec":{"service":{"delegation":true}}}'
106+
107+
echo '>>> Waiting for VirtualService to be updated with delegation...'
108+
109+
retries=30
110+
count=0
111+
ok=false
112+
113+
until ${ok}; do
114+
VS_YAML=$(kubectl get virtualservice/podinfo -n test -o yaml 2>/dev/null)
115+
116+
# A VirtualService is considered INVALID if it contains a 'hosts:' or 'gateways:' key
117+
# that is NOT immediately followed by an empty array '[]'.
118+
if [[ -n "${VS_YAML}" ]] && ! echo "${VS_YAML}" | grep -vE '^ (hosts|gateways): \[\]$' | grep -qE '^ (hosts|gateways):'; then
119+
echo "✔ Validation Passed: 'gateways' and 'hosts' are either absent or empty."
120+
ok=true
121+
else
122+
# If ok is not true, print a status message.
123+
echo "VirtualService not ready yet ('gateways' or 'hosts' key is present). Retrying..."
124+
ok=false
125+
fi
126+
127+
if ${ok}; then
128+
break
129+
fi
130+
131+
count=$(($count + 1))
132+
if [[ ${count} -eq ${retries} ]]; then
133+
kubectl -n istio-system logs deployment/flagger
134+
kubectl get virtualservice/podinfo -n test -o yaml
135+
echo "No more retries left"
136+
exit 1
137+
fi
138+
139+
sleep 5
140+
done
141+
142+
echo '✔ VirtualService delegation enabled'
143+
105144
echo '>>> Triggering canary deployment'
106145
kubectl -n test set image deployment/podinfo podinfod=ghcr.io/stefanprodan/podinfo:6.0.1
107146

0 commit comments

Comments
 (0)