-
Notifications
You must be signed in to change notification settings - Fork 621
/
Copy pathtest-lb-service.sh
executable file
·793 lines (707 loc) · 25.9 KB
/
test-lb-service.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
#!/usr/bin/env bash
# This script is used for the CI job cloud-provider-openstack-test-occm.
#
# Prerequisites:
# - This script is supposed to be running on the CI host which is running devstack.
# - kubectl is ready to talk with the kubernetes cluster.
# - jq is needed.
# - This script will delete all the resources created during testing if there is any test case fails.
set -x
TIMEOUT=${TIMEOUT:-300}
FLOATING_IP=${FLOATING_IP:-""}
NAMESPACE="octavia-lb-test"
GATEWAY_IP=${GATEWAY_IP:-""}
DEVSTACK_OS_RC=${DEVSTACK_OS_RC:-"/home/zuul/devstack/openrc"}
CLUSTER_TENANT=${CLUSTER_TENANT:-"demo"}
CLUSTER_USER=${CLUSTER_USER:-"demo"}
LB_SUBNET_NAME=${LB_SUBNET_NAME:-"private-subnet"}
AUTO_CLEAN_UP=${AUTO_CLEAN_UP:-"true"}
function delete_resources() {
ERROR_CODE="$?"
if [[ ${AUTO_CLEAN_UP} != "true" ]]; then
exit ${ERROR_CODE}
fi
printf "\n>>>>>>> Deleting k8s services\n"
kubectl -n ${NAMESPACE} get svc -o name | xargs -r kubectl -n $NAMESPACE delete
printf "\n>>>>>>> Deleting k8s deployments\n"
kubectl -n ${NAMESPACE} get deploy -o name | xargs -r kubectl -n $NAMESPACE delete
printf "\n>>>>>>> Deleting openstack load balancer \n"
openstack loadbalancer delete test_shared_user_lb --cascade
if [[ "$ERROR_CODE" != "0" ]]; then
printf "\n>>>>>>> Dump openstack-cloud-controller-manager logs \n"
pod_name=$(kubectl -n kube-system get pod -l k8s-app=openstack-cloud-controller-manager -o name | awk 'NR==1 {print}')
kubectl -n kube-system logs ${pod_name}
fi
exit ${ERROR_CODE}
}
trap "delete_resources" EXIT;
function _check_lb_tags {
local lbID=$1
local svcName=$2
local tags=$3
if [ -z "$tags" ]; then
tags=$(openstack loadbalancer show $lbID -f value -c tags)
tags=$(echo $tags)
fi
if [[ ! "$tags" =~ (^|[[:space:]])kube_service_(.+?)$svcName($|[[:space:]]) ]]; then
return 1
fi
return 0
}
function _check_service_lb_annotation {
local svcName=$1
for i in {1..3}; do
lbID=$(kubectl -n $NAMESPACE get svc ${svcName} -o jsonpath="{.metadata.annotations.loadbalancer\.openstack\.org/load-balancer-id}")
if [ -n "$lbID" ]; then
echo "$lbID"
return 0
fi
sleep 5
done
printf "\n>>>>>>> FAIL: Service annotation loadbalancer.openstack.org/load-balancer-id not found for service %s \n" $svcName
kubectl -n $NAMESPACE get svc ${service1} -o yaml
exit 1
}
function set_openstack_credentials {
local XTRACE
XTRACE=$(set +o | grep xtrace)
set +x; source $DEVSTACK_OS_RC $CLUSTER_TENANT $CLUSTER_USER
$XTRACE
}
########################################################################
## Name: wait_for_service
## Desc: Waits for a k8s service until it gets a valid IP address
## Params:
## - (required) A k8s service name
########################################################################
function wait_for_service_address {
local service_name=$1
end=$(($(date +%s) + ${TIMEOUT}))
while true; do
ipaddr=$(kubectl -n $NAMESPACE describe service ${service_name} | grep 'LoadBalancer Ingress' | awk -F":" '{print $2}' | tr -d ' ')
if [ "x${ipaddr}" != "x" ]; then
printf "\n>>>>>>> Service ${service_name} is created successfully, IP: ${ipaddr}\n"
export ipaddr=${ipaddr}
break
fi
sleep 10
now=$(date +%s)
[ $now -gt $end ] && printf "\n>>>>>>> FAIL: Timeout when waiting for the Service ${service_name} created\n" && exit 1
done
}
########################################################################
## Name: wait_address_accessible
## Desc: Waits for the IP address accessible (port 80).
## Params:
## - (required) An IP address
########################################################################
function wait_address_accessible {
local addr=$1
end=$(($(date +%s) + ${TIMEOUT}))
while true; do
curl -sS http://${ipaddr}
if [ $? -eq 0 ]; then
break
fi
sleep 5
now=$(date +%s)
[ $now -gt $end ] && printf "\n>>>>>>> FAIL: Timeout when waiting for the IP address ${addr} accessible\n" && exit 1
done
}
########################################################################
## Name: wait_for_loadbalancer
## Desc: Waits for the load balancer to be ACTIVE
## Params:
## - (required) The load balancer ID.
########################################################################
function wait_for_loadbalancer {
local lbid=$1
local i=0
sleep 5
end=$(($(date +%s) + ${TIMEOUT}))
while true; do
status=$(openstack loadbalancer show $lbid -f value -c provisioning_status)
if [[ $status == "ACTIVE" ]]; then
if [[ $i == 2 ]]; then
printf "\n>>>>>>> Load balancer ${lbid} is ACTIVE\n"
break
fi
let i++
else
i=0
fi
sleep 10
now=$(date +%s)
[ $now -gt $end ] && printf "\n>>>>>>> FAIL: Timeout when waiting for the load balancer ${lbid} ACTIVE\n" && exit 1
done
}
########################################################################
## Name: wait_for_service_deleted
## Desc: Waits for a k8s service deleted.
## Params:
## - (required) A k8s service name
########################################################################
function wait_for_service_deleted {
local service_name=$1
end=$(($(date +%s) + ${TIMEOUT}))
while true; do
svc=$(kubectl -n $NAMESPACE get service | grep ${service_name})
if [[ "x${svc}" == "x" ]]; then
printf "\n>>>>>>> Service ${service_name} deleted\n"
break
fi
sleep 3
now=$(date +%s)
[ $now -gt $end ] && printf "\n>>>>>>> FAIL: Failed to wait for the Service ${service_name} deleted\n" && exit 1
done
}
########################################################################
## Name: create_namespace
## Desc: Makes sure the namespace exists.
## Params: None
########################################################################
function create_namespace {
printf "\n>>>>>>> Create Namespace ${NAMESPACE}\n"
kubectl -n $NAMESPACE get deploy echoserver > /dev/null 2>&1
cat <<EOF | kubectl apply -f -
apiVersion: v1
kind: Namespace
metadata:
name: ${NAMESPACE}
EOF
}
########################################################################
## Name: create_deployment
## Desc: Makes sure the echoserver service Deployment is running
## Params: None
########################################################################
function create_deployment {
printf "\n>>>>>>> Create a Deployment\n"
cat <<EOF | kubectl apply -f -
apiVersion: apps/v1
kind: Deployment
metadata:
name: echoserver
namespace: $NAMESPACE
labels:
run: echoserver
spec:
replicas: 1
selector:
matchLabels:
run: echoserver
template:
metadata:
labels:
run: echoserver
spec:
containers:
- name: echoserver
image: gcr.io/google-containers/echoserver:1.10
imagePullPolicy: IfNotPresent
ports:
- containerPort: 8080
EOF
}
########################################################################
## Name: test_basic
## Desc: Create a k8s service and send request to the service external
## IP
## Params: None
########################################################################
function test_basic {
local service="test-basic"
printf "\n>>>>>>> Create Service ${service}\n"
cat <<EOF | kubectl apply -f -
kind: Service
apiVersion: v1
metadata:
name: ${service}
namespace: $NAMESPACE
spec:
type: LoadBalancer
loadBalancerIP: ${FLOATING_IP}
selector:
run: echoserver
ports:
- protocol: TCP
port: 80
targetPort: 8080
EOF
printf "\n>>>>>>> Waiting for the Service ${service} creation finished\n"
wait_for_service_address ${service}
wait_address_accessible $ipaddr
printf "\n>>>>>>> Sending request to the Service ${service}\n"
podname=$(curl -sS http://${ipaddr} | grep Hostname | awk -F':' '{print $2}' | cut -d ' ' -f2)
if [[ "$podname" =~ "echoserver" ]]; then
printf "\n>>>>>>> Expected: Get correct response from Service ${service}\n"
else
printf "\n>>>>>>> FAIL: Get incorrect response from Service ${service}, expected: echoserver, actual: $podname\n"
curl -sS http://${ipaddr}
exit 1
fi
printf "\n>>>>>>> Delete Service ${service}\n"
kubectl -n $NAMESPACE delete service ${service}
}
########################################################################
## Name: test_forwarded
## Desc: Create a k8s service that gets the original client IP
## Params: None
########################################################################
function test_forwarded {
local service="test-x-forwarded-for"
local public_ip=$(curl -sS ifconfig.me)
local local_ip=$(ip route get 8.8.8.8 | head -1 | awk '{print $7}')
if [[ ${OCTAVIA_PROVIDER} == "ovn" ]]; then
printf "\n>>>>>>> Skipping Service ${service} test for OVN provider\n"
return 0
fi
printf "\n>>>>>>> Create the Service ${service}\n"
cat <<EOF | kubectl apply -f -
kind: Service
apiVersion: v1
metadata:
name: ${service}
namespace: $NAMESPACE
annotations:
loadbalancer.openstack.org/x-forwarded-for: "true"
spec:
type: LoadBalancer
loadBalancerIP: ${FLOATING_IP}
selector:
run: echoserver
ports:
- protocol: TCP
port: 80
targetPort: 8080
EOF
printf "\n>>>>>>> Waiting for the Service ${service} creation finished\n"
wait_for_service_address ${service}
wait_address_accessible $ipaddr
printf "\n>>>>>>> Sending request to the Service ${service}\n"
ip_in_header=$(curl -sS http://${ipaddr} | grep x-forwarded-for | awk -F'=' '{print $2}')
if [[ "${ip_in_header}" != "${local_ip}" && "${ip_in_header}" != "${public_ip}" && "${ip_in_header}" != "${GATEWAY_IP}" ]]; then
printf "\n>>>>>>> FAIL: Get incorrect response from Service ${service}, ip_in_header: ${ip_in_header}, local_ip: ${local_ip}, gateway_ip: ${GATEWAY_IP}, public_ip: ${public_ip}\n"
curl -sS http://${ipaddr}
exit 1
else
printf "\n>>>>>>> Expected: Get correct response from Service ${service}\n"
fi
printf "\n>>>>>>> Delete Service ${service}\n"
kubectl -n $NAMESPACE delete service ${service}
}
########################################################################
## Name: test_update_port
## Desc: Create a k8s service and update the service port/nodeport.
## Params: None
########################################################################
function test_update_port {
local service="test-update-port"
printf "\n>>>>>>> Creating Service %s \n" "${service}"
cat <<EOF | kubectl apply -f -
kind: Service
apiVersion: v1
metadata:
name: ${service}
namespace: $NAMESPACE
annotations:
service.beta.kubernetes.io/openstack-internal-load-balancer: "true"
spec:
type: LoadBalancer
selector:
run: echoserver
ports:
- name: port1
protocol: TCP
port: 80
targetPort: 8080
- name: port2
protocol: TCP
port: 8080
targetPort: 8080
EOF
printf "\n>>>>>>> Waiting for the Service %s created \n" "${service}"
wait_for_service_address ${service}
printf "\n>>>>>>> Validating openstack load balancer \n"
lbid=$(openstack loadbalancer list -c id -c name | grep "octavia-lb-test_${service}" | awk '{print $2}')
if [[ -z $lbid ]]; then
printf "\n>>>>>>> FAIL: Load balancer not found for Service ${service}\n"
exit 1
fi
lb_info=$(openstack loadbalancer status show $lbid)
listener_count=$(echo $lb_info | jq '.loadbalancer.listeners | length')
member_ports=$(echo $lb_info | jq '.loadbalancer.listeners | .[].pools | .[].members | .[].protocol_port' | uniq | tr '\n' ' ')
service_nodeports=$(kubectl -n $NAMESPACE get svc $service -o json | jq '.spec.ports | .[].nodePort' | tr '\n' ' ')
if [[ ${listener_count} != 2 ]]; then
printf "\n>>>>>>> FAIL: Unexpected number of listeners(${listener_count}) created for service ${service}\n"
exit 1
fi
if [[ "${member_ports}" != "${service_nodeports}" ]]; then
printf "\n>>>>>>> FAIL: Load balancer member ports %s and service nodeports %s not match\n" "${member_ports}" "${service_nodeports}"
kubectl -n $NAMESPACE get svc $service -o yaml
exit 1
fi
printf "\n>>>>>>> Expected: NodePorts ${member_ports} before updating service.\n"
printf "\n>>>>>>> Removing port2 and update NodePort of port1.\n"
kubectl -n $NAMESPACE patch svc $service --type json -p '[{"op": "remove","path": "/spec/ports/1"},{"op": "remove","path": "/spec/ports/0/nodePort"}]'
printf "\n>>>>>>> Waiting for load balancer $lbid ACTIVE.\n"
wait_for_loadbalancer $lbid
printf "\n>>>>>>> Validating openstack load balancer after updating the service.\n"
lb_info=$(openstack loadbalancer status show $lbid)
listener_count=$(echo $lb_info | jq '.loadbalancer.listeners | length')
member_port=$(echo $lb_info | jq '.loadbalancer.listeners | .[].pools | .[].members | .[].protocol_port' | uniq)
service_nodeport=$(kubectl -n $NAMESPACE get svc $service -o json | jq '.spec.ports | .[].nodePort')
if [[ ${listener_count} != 1 ]]; then
printf "\n>>>>>>> FAIL: Unexpected number of listeners(${listener_count}) for service.\n"
exit 1
fi
if [[ $(echo ${member_port} | wc -l) != 1 ]]; then
printf "\n>>>>>>> FAIL: Unexpected number of member port(${member_port}) for service.\n"
exit 1
fi
if [[ ${member_port} != ${service_nodeport} ]]; then
printf "\n>>>>>>> FAIL: Member ports ${member_port} and service nodeport ${service_nodeport} not match.\n"
exit 1
fi
if [[ ${member_port} == ${member_ports} ]]; then
printf "\n>>>>>>> FAIL: NodePort ${member_port} not changed.\n"
exit 1
fi
printf "\n>>>>>>> Expected: NodePort ${member_port} after updating service.\n"
printf "\n>>>>>>> Deleting Service ${service}\n"
kubectl -n $NAMESPACE delete service ${service}
}
########################################################################
## Name: test_shared_lb
## Desc: The steps in this test case:
## 1. Create service-1, lb-1 created.
## 2. Create service-2 with lb-1.
## 3. Update service-2.
## 4. Delete service-2.
## 5. Create service-3 with lb-1
## 6. Create service-4 with lb-1, but with same port as service-3, should fail.
## 7. Delete service-4.
## 8. Delete service-1.
## 9. Delete service-3.
########################################################################
function test_shared_lb {
local service1="test-shared-1"
printf "\n>>>>>>> Create Service ${service1}\n"
cat <<EOF | kubectl apply -f -
kind: Service
apiVersion: v1
metadata:
name: ${service1}
namespace: $NAMESPACE
annotations:
service.beta.kubernetes.io/openstack-internal-load-balancer: "true"
loadbalancer.openstack.org/enable-health-monitor: "false"
spec:
type: LoadBalancer
selector:
run: echoserver
ports:
- protocol: TCP
port: 80
targetPort: 8080
EOF
printf "\n>>>>>>> Waiting for the Service %s creation finished \n" ${service1}
wait_for_service_address ${service1}
printf "\n>>>>>>> Checking Service %s annotation\n" ${service1}
lbID=$(_check_service_lb_annotation "${service1}")
printf "\n>>>>>>> Validating tags of openstack load balancer %s \n" "$lbID"
tags=$(openstack loadbalancer show $lbID -f value -c tags)
tags=$(echo $tags)
_check_lb_tags $lbID $service1 "$tags"
if [ $? -ne 0 ]; then
printf "\n>>>>>>> FAIL: $service1 not found in load balancer tags ($tags) \n"
exit 1
fi
service2="test-shared-2"
printf "\n>>>>>>> Create Service ${service2}\n"
cat <<EOF | kubectl apply -f -
kind: Service
apiVersion: v1
metadata:
name: ${service2}
namespace: $NAMESPACE
annotations:
service.beta.kubernetes.io/openstack-internal-load-balancer: "true"
loadbalancer.openstack.org/enable-health-monitor: "false"
loadbalancer.openstack.org/load-balancer-id: "$lbID"
spec:
type: LoadBalancer
selector:
run: echoserver
ports:
- protocol: TCP
port: 8080
targetPort: 8080
EOF
printf "\n>>>>>>> Waiting for the Service ${service2} creation finished\n"
wait_for_service_address ${service2}
printf "\n>>>>>>> Checking Service %s annotation\n" ${service2}
svc2lbID=$(_check_service_lb_annotation "${service2}")
if [[ "$svc2lbID" != "$lbID" ]]; then
printf "\n>>>>>>> FAIL: Service annotation loadbalancer.openstack.org/load-balancer-id not equal. $lbID != $svc2lbID\n"
kubectl -n $NAMESPACE get svc ${service2} -o yaml
exit 1
fi
printf "\n>>>>>>> Validating tags of openstack load balancer %s \n" "$lbID"
tags=$(openstack loadbalancer show $lbID -f value -c tags)
tags=$(echo $tags)
_check_lb_tags $lbID $service1 "$tags"
if [ $? -ne 0 ]; then
printf "\n>>>>>>> FAIL: $service1 not found in load balancer tags ($tags) \n"
exit 1
fi
_check_lb_tags $lbID $service2 "$tags"
if [ $? -ne 0 ]; then
printf "\n>>>>>>> FAIL: $service2 not found in load balancer tags ($tags) \n"
exit 1
fi
service2="test-shared-2"
printf "\n>>>>>>> Updating Service ${service2} port\n"
cat <<EOF | kubectl apply -f -
kind: Service
apiVersion: v1
metadata:
name: ${service2}
namespace: $NAMESPACE
annotations:
service.beta.kubernetes.io/openstack-internal-load-balancer: "true"
loadbalancer.openstack.org/enable-health-monitor: "false"
loadbalancer.openstack.org/load-balancer-id: "$lbID"
spec:
type: LoadBalancer
selector:
run: echoserver
ports:
- protocol: TCP
port: 8081
targetPort: 8080
EOF
printf "\n>>>>>>> Waiting for the load balancer ${lbID} update finished\n"
wait_for_loadbalancer $lbID
printf "\n>>>>>>> Checking the listener number for the load balancer $lbID\n"
listenerNum=$(openstack loadbalancer status show $lbID | jq '.loadbalancer.listeners | length')
if [[ $listenerNum != 2 ]]; then
printf "\n>>>>>>> FAIL: The listener number should be 2 for load balancer $lbID, actual: $listenerNum\n"
exit 1
fi
printf "\n>>>>>>> Deleting Service ${service2}\n"
kubectl -n $NAMESPACE delete svc ${service2}
printf "\n>>>>>>> Waiting for the load balancer $lbID updated\n"
wait_for_loadbalancer $lbID
printf "\n>>>>>>> Validating tags of openstack load balancer %s \n" "$lbID"
tags=$(openstack loadbalancer show $lbID -f value -c tags)
tags=$(echo $tags)
_check_lb_tags $lbID $service1 "$tags"
if [ $? -ne 0 ]; then
printf "\n>>>>>>> FAIL: $service1 not found in load balancer tags ($tags) \n"
exit 1
fi
_check_lb_tags $lbID $service2 "$tags"
if [ $? -eq 0 ]; then
printf "\n>>>>>>> FAIL: $service2 found in load balancer tags ($tags) \n"
exit 1
fi
printf "\n>>>>>>> Checking the listener number for the load balancer $lbID\n"
listenerNum=$(openstack loadbalancer status show $lbID | jq '.loadbalancer.listeners | length')
if [[ $listenerNum != 1 ]]; then
printf "\n>>>>>>> FAIL: The listener number should be 1 for load balancer $lbID, actual: $listenerNum\n"
exit 1
fi
service3="test-shared-3"
printf "\n>>>>>>> Creating Service ${service3}\n"
cat <<EOF | kubectl apply -f -
kind: Service
apiVersion: v1
metadata:
name: ${service3}
namespace: $NAMESPACE
annotations:
service.beta.kubernetes.io/openstack-internal-load-balancer: "true"
loadbalancer.openstack.org/enable-health-monitor: "false"
loadbalancer.openstack.org/load-balancer-id: "$lbID"
spec:
type: LoadBalancer
selector:
run: echoserver
ports:
- protocol: TCP
port: 8080
targetPort: 8080
EOF
printf "\n>>>>>>> Waiting for the Service ${service3} creation finished \n"
wait_for_service_address ${service3}
printf "\n>>>>>>> Validating tags of openstack load balancer %s \n" "$lbID"
tags=$(openstack loadbalancer show $lbID -f value -c tags)
tags=$(echo $tags)
_check_lb_tags $lbID $service3 "$tags"
if [ $? -ne 0 ]; then
printf "\n>>>>>>> FAIL: $service3 not found in load balancer tags ($tags) \n"
exit 1
fi
service4="test-shared-4"
printf "\n>>>>>>> Creating Service ${service4} with port collision \n"
cat <<EOF | kubectl apply -f -
kind: Service
apiVersion: v1
metadata:
name: ${service4}
namespace: $NAMESPACE
annotations:
service.beta.kubernetes.io/openstack-internal-load-balancer: "true"
loadbalancer.openstack.org/enable-health-monitor: "false"
loadbalancer.openstack.org/load-balancer-id: "$lbID"
spec:
type: LoadBalancer
selector:
run: echoserver
ports:
- protocol: TCP
port: 8080
targetPort: 8080
EOF
sleep 10
printf "\n>>>>>>> Validating tags of openstack load balancer %s \n" "$lbID"
tags=$(openstack loadbalancer show $lbID -f value -c tags)
tags=$(echo $tags)
_check_lb_tags $lbID $service1 "$tags"
if [ $? -ne 0 ]; then
printf "\n>>>>>>> FAIL: $service1 not found in load balancer tags ($tags) \n"
exit 1
fi
_check_lb_tags $lbID $service3 "$tags"
if [ $? -ne 0 ]; then
printf "\n>>>>>>> FAIL: $service3 not found in load balancer tags ($tags) \n"
exit 1
fi
_check_lb_tags $lbID $service4 "$tags"
if [ $? -eq 0 ]; then
printf "\n>>>>>>> FAIL: $service4 found in load balancer tags ($tags) \n"
exit 1
fi
printf "\n>>>>>>> Deleting Service ${service4}\n"
kubectl -n $NAMESPACE delete svc ${service4}
sleep 5
printf "\n>>>>>>> Validating tags of openstack load balancer %s \n" "$lbID"
tags=$(openstack loadbalancer show $lbID -f value -c tags)
tags=$(echo $tags)
_check_lb_tags $lbID $service1 "$tags"
if [ $? -ne 0 ]; then
printf "\n>>>>>>> FAIL: $service1 not found in load balancer tags ($tags) \n"
exit 1
fi
_check_lb_tags $lbID $service3 "$tags"
if [ $? -ne 0 ]; then
printf "\n>>>>>>> FAIL: $service3 not found in load balancer tags ($tags) \n"
exit 1
fi
printf "\n>>>>>>> Deleting Service ${service1}\n"
kubectl -n $NAMESPACE delete svc ${service1}
printf "\n>>>>>>> Waiting for the load balancer ${lbID} update finished\n"
wait_for_loadbalancer $lbID
printf "\n>>>>>>> Validating tags of openstack load balancer %s \n" "$lbID"
tags=$(openstack loadbalancer show $lbID -f value -c tags)
tags=$(echo $tags)
_check_lb_tags $lbID $service1 "$tags"
if [ $? -eq 0 ]; then
printf "\n>>>>>>> FAIL: $service1 found in load balancer tags ($tags) \n"
exit 1
fi
_check_lb_tags $lbID $service3 "$tags"
if [ $? -ne 0 ]; then
printf "\n>>>>>>> FAIL: $service3 not found in load balancer tags ($tags) \n"
exit 1
fi
printf "\n>>>>>>> Checking the listener number for the load balancer $lbID\n"
listenerNum=$(openstack loadbalancer status show $lbID | jq '.loadbalancer.listeners | length')
if [[ $listenerNum != 1 ]]; then
printf "\n>>>>>>> FAIL: The listener number should be 1 for load balancer $lbID, actual: $listenerNum\n"
exit 1
fi
printf "\n>>>>>>> Deleting Service ${service3}\n"
kubectl -n $NAMESPACE delete svc ${service3}
}
########################################################################
## Name: test_shared_user_lb
## Desc: The steps in this test case:
## 1. Create a load balancer lb-1 in Octavia.
## 2. Create service-1 with lb-1.
## 3. Delete service-1.
########################################################################
function test_shared_user_lb {
# Get subnet ID for creating the load balancer
subid=$(openstack subnet show ${LB_SUBNET_NAME} -f value -c id)
if [ $? -ne 0 ]; then
printf "\n>>>>>>> FAIL: failed to get subnet ${LB_SUBNET_NAME}\n"
exit 1
fi
printf "\n>>>>>>> Creating openstack load balancer: --vip-subnet-id $subid \n"
lbID=$(openstack loadbalancer create --vip-subnet-id $subid --name test_shared_user_lb -f value -c id)
if [ $? -ne 0 ]; then
printf "\n>>>>>>> FAIL: failed to create load balancer\n"
exit 1
fi
printf "\n>>>>>>> Waiting for openstack load balancer $lbID ACTIVE \n"
wait_for_loadbalancer $lbID
printf "\n>>>>>>> Creating openstack load balancer listener \n"
openstack loadbalancer listener create --protocol HTTP --protocol-port 80 $lbID
printf "\n>>>>>>> Waiting for openstack load balancer $lbID ACTIVE after creating listener \n"
wait_for_loadbalancer $lbID
local service1="test-shared-user-lb"
printf "\n>>>>>>> Create Service ${service1}\n"
cat <<EOF | kubectl apply -f -
kind: Service
apiVersion: v1
metadata:
name: ${service1}
namespace: $NAMESPACE
annotations:
loadbalancer.openstack.org/load-balancer-id: "$lbID"
service.beta.kubernetes.io/openstack-internal-load-balancer: "true"
loadbalancer.openstack.org/enable-health-monitor: "false"
spec:
type: LoadBalancer
selector:
run: echoserver
ports:
- protocol: TCP
port: 8080
targetPort: 8080
EOF
printf "\n>>>>>>> Waiting for the Service ${service1} creation finished\n"
wait_for_service_address ${service1}
printf "\n>>>>>>> Checking Service %s annotation\n" ${service1}
lbID=$(_check_service_lb_annotation "${service1}")
printf "\n>>>>>>> Validating tags of openstack load balancer %s \n" "$lbID"
tags=$(openstack loadbalancer show $lbID -f value -c tags)
tags=$(echo $tags)
_check_lb_tags $lbID $service1 "$tags"
if [ $? -ne 0 ]; then
printf "\n>>>>>>> FAIL: $service1 not found in load balancer tags ($tags) \n"
exit 1
fi
printf "\n>>>>>>> Deleting Service ${service1}\n"
kubectl -n $NAMESPACE delete svc ${service1}
printf "\n>>>>>>> Waiting for Service ${service1} deleted \n"
wait_for_service_deleted ${service1}
printf "\n>>>>>>> Validating tags of openstack load balancer %s \n" "$lbID"
tags=$(openstack loadbalancer show $lbID -f value -c tags)
tags=$(echo $tags)
_check_lb_tags $lbID $service1 "$tags"
if [ $? -eq 0 ]; then
printf "\n>>>>>>> FAIL: $service1 still found in load balancer tags ($tags) \n"
exit 1
fi
}
create_namespace
create_deployment
set_openstack_credentials
test_basic
test_forwarded
test_update_port
test_shared_lb
test_shared_user_lb