-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test-with-ingress-tls.sh
executable file
·163 lines (118 loc) · 4.62 KB
/
test-with-ingress-tls.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
#!/usr/bin/env bash
set -ex
export PS4='+($(date +%H:%M:%S) ${BASH_SOURCE}:${LINENO})'
exec &> >(tee ./test-with-ingress-tls.log)
REGISTRY_PORT=5000
EXTERNAL_PORT=8001
# comment this to keep the cluster running on completion.
STOP_CLUSTER_ON_EXIT=1
docker_tag() {
local from="$1"
local to="$2"
IMG=$(docker images --no-trunc --quiet "$from")
docker tag $IMG $to
docker push $to
}
# start the test cluster (3 workers, 3 masters)
# short options:
#./kind_helper.py -s -w 3 -i ${EXTERNAL_PORT}:443 -v -t 120 -p ${REGISTRY_PORT}
# the same with long options
./kind_helper.py --start --masters 1 --workers 3 --timeout 120 --ingress ${EXTERNAL_PORT}:443 --verbose --registry-port ${REGISTRY_PORT}
cleanup() {
echo "*** cleanup ***"
set +e
./kind_helper.py -c 'version'
./kind_helper.py -c 'get events'
./kind_helper.py -c 'get ing'
./kind_helper.py -c 'get deployment'
./kind_helper.py -c 'get ing test-echo-server'
./kind_helper.py -c 'describe ing test-echo-server'
./kind_helper.py -c 'get deployment test-echo-server'
./kind_helper.py -c 'get pods -l test-echo-server'
./kind_helper.py -c 'get logs -l test-echo-server'
for n in $(./kind_helper.py -c 'get ns' | grep Active | awk '{print $1;}'); do
echo "namespace $n"
./kind_helper.py -c 'get pods -n '$n
if [[ $n == "default" ]] || [[ $n == "ingress-nginx" ]]; then
for p in $(./kind_helper.py -c "get pods -n $n -o jsonpath=\"{range .items[*]}{' '}{.metadata.name}{end}\""); do
./kind_helper.py -c "describe pod -n $n $p"
done
fi
done
# kill the cluster on exit
if [[ $STOP_CLUSTER_ON_EXIT != "" ]]; then
./kind_helper.py --stop
fi
}
trap "cleanup" EXIT SIGINT
# check if the nodes are up and ready
NODES=$(./kind_helper.py -c 'get nodes')
READY_NODES=$(echo "$NODES" | grep -c Ready)
if [[ $READY_NODES != 4 ]]; then
echo "Not all nodes up ${READY_NODES}/6"
exit 1
fi
WORKER_NODES=$(echo "$NODES" | grep -c kind-worker)
if [[ $WORKER_NODES != 3 ]]; then
echo "Not enough worker nodes ${WORKER_NODES}/3"
exit 1
fi
MASTER_NODES=$(echo "$NODES" | grep -c kind-control-plane)
if [[ $MASTER_NODES != 1 ]]; then
echo "Not enough worker nodes ${MASTER_NODES}/3"
exit 1
fi
set +e
docker rmi aaa/mm/kind-test-pod localhost
docker rmi localhost:${REGISTRY_PORT}/kind-test-pod
set -e
# create self signed certificate for ingress
KEY_FILE=test/ingress-key.pem
CERT_FILE=test/ingress-cert.pem
HOST=localhost
openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout ${KEY_FILE} -out ${CERT_FILE} -subj "/CN=${HOST}/O=${HOST}"
# create secret object that refers to certificate
SECRET_NAME=ingress-secret-cert
./kind_helper.py -c "create secret tls ${SECRET_NAME} --key ${KEY_FILE} --cert ${CERT_FILE}"
# build image for test pod
docker build -f test/Dockerfile.test -t aaa/mm/kind-test-pod .
# put docker into the local kind registry
docker_tag aaa/mm/kind-test-pod localhost:${REGISTRY_PORT}/kind-test-pod
# adjust port number in container image name
sed -e s/PORTNUM/${REGISTRY_PORT}/ test/deployment.yaml >test/deployment-port.yaml
# create pod in registry that refers to kind registry
./kind_helper.py -c 'create -f test/service_account.yaml'
./kind_helper.py -c 'create -f test/role.yaml'
./kind_helper.py -c 'create -f test/role_binding.yaml'
./kind_helper.py -c 'create -f test/deployment-port.yaml'
./kind_helper.py -c 'create -f test/service.yaml'
./kind_helper.py -c 'create -f test/ingress-tls.yaml'
./kind_helper.py -c 'wait -f test/deployment.yaml --for condition=available'
echo "*** deployment available ***"
./kind_helper.py -c 'get pods -o wide'
echo "*** wait for ingress object to be active (be attached to load balancer) ***"
COUNT=0
while [[ true ]]; do
HAS_LB=$(./kind_helper.py -c 'get ingresses test-echo-server -n default -o json' | jq .status.loadBalancer.ingress)
echo "${HAS_LB}"
if [[ $HAS_LB != "null" ]] && [[ $HAS_LB != "{}" ]]; then
break
fi
sleep 3
if [[ $COUNT -gt 200 ]]; then
echo "waiting too long for ingress to be available, exit with error..."
./kind_helper.py -c 'get ingresses test-echo-server -n default -o json'
./kind_helper.py -c 'get get pods -n default'
exit 1
fi
((COUNT+=1))
done
./kind_helper.py -c 'get ingresses test-echo-server -n default -o yaml'
RESPONSE=$(curl -k -v https://localhost:${EXTERNAL_PORT}/test-echo-server)
echo "${RESPONSE}"
HAS_RESPONSE=$(echo "${RESPONSE}" | grep -c '<h2>echo response</h2>')
if [[ $HAS_RESPONSE != 1 ]]; then
echo "Sorry, could not reach the service"
exit 1
fi
echo "*** test completed ***"