Skip to content

Commit 771a8f4

Browse files
author
Jesus Carrillo
committed
add: e2e tests for port change in service
1 parent d78fb1f commit 771a8f4

3 files changed

Lines changed: 249 additions & 0 deletions

File tree

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
# yaml-language-server: $schema=https://raw.githubusercontent.com/kyverno/chainsaw/main/.schemas/json/test-chainsaw-v1alpha1.json
2+
apiVersion: chainsaw.kyverno.io/v1alpha1
3+
kind: Test
4+
metadata:
5+
name: lb-update-port
6+
labels:
7+
all:
8+
lke:
9+
spec:
10+
namespace: "lb-update-port"
11+
steps:
12+
- name: Create pods and services
13+
try:
14+
- apply:
15+
file: create-pods-services.yaml
16+
catch:
17+
- describe:
18+
apiVersion: v1
19+
kind: Pod
20+
- describe:
21+
apiVersion: v1
22+
kind: Service
23+
- name: Check that loadbalancer ip is assigned
24+
try:
25+
- assert:
26+
resource:
27+
apiVersion: v1
28+
kind: Service
29+
metadata:
30+
name: svc-test
31+
status:
32+
(loadBalancer.ingress[0].ip != null): true
33+
- name: Fetch loadbalancer ip and check both pods reachable
34+
try:
35+
- script:
36+
content: |
37+
set -e
38+
sleep 30
39+
IP=$(kubectl get svc svc-test -n $NAMESPACE -o json | jq -r .status.loadBalancer.ingress[0].ip)
40+
41+
podnames=()
42+
43+
for i in {1..10}; do
44+
if [[ ${#podnames[@]} -lt 2 ]]; then
45+
output=$(curl -s $IP:80 | jq -e .podName || true)
46+
47+
if [[ "$output" == *"test-"* ]]; then
48+
unique=true
49+
for i in "${array[@]}"; do
50+
if [[ "$i" == "$output" ]]; then
51+
unique=false
52+
break
53+
fi
54+
done
55+
if [[ "$unique" == true ]]; then
56+
podnames+=($output)
57+
fi
58+
fi
59+
else
60+
break
61+
fi
62+
sleep 10
63+
done
64+
65+
if [[ ${#podnames[@]} -lt 2 ]]; then
66+
echo "all pods failed to respond"
67+
else
68+
echo "all pods responded"
69+
fi
70+
check:
71+
($error == null): true
72+
(contains($stdout, 'all pods responded')): true
73+
- name: Update service
74+
try:
75+
- apply:
76+
file: update-port-service.yaml
77+
- name: Check pods reachable on new port
78+
try:
79+
- script:
80+
content: |
81+
set -e
82+
#wait for changes to propagate to the LB
83+
sleep 60
84+
IP=$(kubectl get svc svc-test -n $NAMESPACE -o json | jq -r .status.loadBalancer.ingress[0].ip)
85+
86+
podnames=()
87+
88+
for i in {1..10}; do
89+
if [[ ${#podnames[@]} -lt 2 ]]; then
90+
output=$(curl -s $IP:8080 | jq -e .podName || true)
91+
92+
if [[ "$output" == *"test-"* ]]; then
93+
unique=true
94+
for i in "${array[@]}"; do
95+
if [[ "$i" == "$output" ]]; then
96+
unique=false
97+
break
98+
fi
99+
done
100+
if [[ "$unique" == true ]]; then
101+
podnames+=($output)
102+
fi
103+
fi
104+
else
105+
break
106+
fi
107+
sleep 10
108+
done
109+
110+
if [[ ${#podnames[@]} -lt 2 ]]; then
111+
echo "all pods failed to respond"
112+
else
113+
echo "all pods responded"
114+
fi
115+
check:
116+
($error == null): true
117+
(contains($stdout, 'all pods responded')): true
118+
- name: Fetch firewall ID and check ports are updated
119+
try:
120+
- script:
121+
content: |
122+
set -e
123+
for i in {1..10}; do
124+
nbid=$(KUBECONFIG=$KUBECONFIG NAMESPACE=$NAMESPACE LINODE_TOKEN=$LINODE_TOKEN ../scripts/get-nb-id.sh)
125+
126+
fw=$(curl -s --request GET \
127+
-H "Authorization: Bearer $LINODE_TOKEN" \
128+
-H "Content-Type: application/json" \
129+
-H "accept: application/json" \
130+
"https://api.linode.com/v4/nodebalancers/${nbid}/firewalls" || true)
131+
echo "$fw" | jq -r '.data[].rules.inbound[]'
132+
if echo "$fw" | jq -r '.data[].rules.inbound[].ports' | grep 8080 ; then
133+
echo "firewall rule updated with new port"
134+
break
135+
fi
136+
sleep 10
137+
done
138+
check:
139+
($error == null): true
140+
(contains($stdout, 'firewall rule updated with new port')): true
141+
- name: Delete Pods
142+
try:
143+
- delete:
144+
ref:
145+
apiVersion: v1
146+
kind: Pod
147+
- name: Delete Service
148+
try:
149+
- delete:
150+
ref:
151+
apiVersion: v1
152+
kind: Service
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
---
2+
apiVersion: apps/v1
3+
kind: Deployment
4+
metadata:
5+
labels:
6+
app: lb-update-port
7+
name: test
8+
spec:
9+
replicas: 2
10+
selector:
11+
matchLabels:
12+
app: lb-update-port
13+
template:
14+
metadata:
15+
labels:
16+
app: lb-update-port
17+
spec:
18+
affinity:
19+
podAntiAffinity:
20+
preferredDuringSchedulingIgnoredDuringExecution:
21+
- podAffinityTerm:
22+
labelSelector:
23+
matchExpressions:
24+
- key: app
25+
operator: In
26+
values:
27+
- lb-update-port
28+
topologyKey: kubernetes.io/hostname
29+
weight: 100
30+
containers:
31+
- image: appscode/test-server:2.3
32+
name: test
33+
ports:
34+
- name: http-1
35+
containerPort: 8080
36+
protocol: TCP
37+
env:
38+
- name: POD_NAME
39+
valueFrom:
40+
fieldRef:
41+
apiVersion: v1
42+
fieldPath: metadata.name
43+
---
44+
apiVersion: v1
45+
kind: Service
46+
metadata:
47+
name: svc-test
48+
labels:
49+
app: lb-update-port
50+
annotations:
51+
service.beta.kubernetes.io/linode-loadbalancer-firewall-acl: |
52+
{
53+
"denyList": {
54+
"ipv4": ["8.8.8.8/32",
55+
"9.9.9.9/32"]
56+
}
57+
}
58+
spec:
59+
type: LoadBalancer
60+
selector:
61+
app: lb-update-port
62+
ports:
63+
- name: http-1
64+
protocol: TCP
65+
port: 80
66+
targetPort: 8080
67+
sessionAffinity: None
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
---
2+
apiVersion: v1
3+
kind: Service
4+
metadata:
5+
name: svc-test
6+
labels:
7+
app: lb-update-port
8+
annotations:
9+
service.beta.kubernetes.io/linode-loadbalancer-firewall-acl: |
10+
{
11+
"denyList": {
12+
"ipv4": ["8.8.8.8/32",
13+
"9.9.9.9/32"]
14+
}
15+
}
16+
spec:
17+
type: LoadBalancer
18+
selector:
19+
app: lb-update-port
20+
ports:
21+
- name: http-1
22+
protocol: TCP
23+
port: 80
24+
targetPort: 8080
25+
- name: http-2
26+
protocol: TCP
27+
port: 8080
28+
targetPort: 8080
29+
sessionAffinity: None
30+
...

0 commit comments

Comments
 (0)