forked from Azure-Samples/piggymetrics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.sh
executable file
·213 lines (186 loc) · 7.02 KB
/
util.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
#!/bin/bash
MAX_RETRIES=299
wait_deployment_complete() {
deploymentName=$1
namespaceName=$2
cnt=0
kubectl get deployment ${deploymentName} -n ${namespaceName}
while [ $? -ne 0 ]
do
if [ $cnt -eq $MAX_RETRIES ]; then
echo "Timeout and exit due to the maximum retries reached."
return 1
fi
cnt=$((cnt+1))
echo "Unable to get the deployment ${deploymentName}, retry ${cnt} of ${MAX_RETRIES}..."
sleep 5
kubectl get deployment ${deploymentName} -n ${namespaceName}
done
cnt=0
read -r -a replicas <<< `kubectl get deployment ${deploymentName} -n ${namespaceName} -o=jsonpath='{.spec.replicas}{" "}{.status.readyReplicas}{" "}{.status.availableReplicas}{" "}{.status.updatedReplicas}{"\n"}'`
while [[ ${#replicas[@]} -ne 4 || ${replicas[0]} != ${replicas[1]} || ${replicas[1]} != ${replicas[2]} || ${replicas[2]} != ${replicas[3]} ]]
do
if [ $cnt -eq $MAX_RETRIES ]; then
echo "Timeout and exit due to the maximum retries reached."
return 1
fi
cnt=$((cnt+1))
# Delete pods in ImagePullBackOff status
podIds=`kubectl get pod -n ${namespaceName} | grep ImagePullBackOff | awk '{print $1}'`
read -r -a podIds <<< `echo $podIds`
for podId in "${podIds[@]}"
do
echo "Delete pod ${podId} in ImagePullBackOff status"
kubectl delete pod ${podId} -n ${namespaceName}
done
sleep 5
echo "Wait until the deployment ${deploymentName} completes, retry ${cnt} of ${MAX_RETRIES}..."
read -r -a replicas <<< `kubectl get deployment ${deploymentName} -n ${namespaceName} -o=jsonpath='{.spec.replicas}{" "}{.status.readyReplicas}{" "}{.status.availableReplicas}{" "}{.status.updatedReplicas}{"\n"}'`
done
echo "Deployment ${deploymentName} completed."
}
wait_service_available() {
serviceName=$1
namespaceName=$2
portIndex=$3
cnt=0
kubectl get svc ${serviceName} -n ${namespaceName}
while [ $? -ne 0 ]
do
if [ $cnt -eq $MAX_RETRIES ]; then
echo "Timeout and exit due to the maximum retries reached."
return 1
fi
cnt=$((cnt+1))
echo "Unable to get the service ${serviceName}, retry ${cnt} of ${MAX_RETRIES}..."
sleep 5
kubectl get svc ${serviceName} -n ${namespaceName}
done
cnt=0
appEndpoint=$(kubectl get svc ${serviceName} -n ${namespaceName} -o=jsonpath='{.status.loadBalancer.ingress[0].ip}:{.spec.ports['${portIndex}'].port}')
echo "Service ${serviceName} ip:port is ${appEndpoint}"
while [[ $appEndpoint = :* ]] || [[ -z $appEndpoint ]]
do
if [ $cnt -eq $MAX_RETRIES ]; then
echo "Timeout and exit due to the maximum retries reached."
return 1
fi
cnt=$((cnt+1))
sleep 5
echo "Wait until the IP address and port of the service ${serviceName} are available, retry ${cnt} of ${MAX_RETRIES}..."
appEndpoint=$(kubectl get svc ${serviceName} -n ${namespaceName} -o=jsonpath='{.status.loadBalancer.ingress[0].ip}:{.spec.ports['${portIndex}'].port}')
echo "Service ${serviceName} ip:port is ${appEndpoint}"
done
}
wait_ingress_available() {
ingressName=$1
namespaceName=$2
cnt=0
kubectl get ingress ${ingressName} -n ${namespaceName}
while [ $? -ne 0 ]
do
if [ $cnt -eq $MAX_RETRIES ]; then
echo "Timeout and exit due to the maximum retries reached."
return 1
fi
cnt=$((cnt+1))
echo "Unable to get the ingress ${ingressName}, retry ${cnt} of ${MAX_RETRIES}..."
sleep 5
kubectl get ingress ${ingressName} -n ${namespaceName}
done
cnt=0
ip=$(kubectl get ingress ${ingressName} -n ${namespaceName} -o=jsonpath='{.status.loadBalancer.ingress[0].ip}')
echo "Ingress ${ingressName} ip is ${ip}"
while [ -z $ip ]
do
if [ $cnt -eq $MAX_RETRIES ]; then
echo "Timeout and exit due to the maximum retries reached."
return 1
fi
cnt=$((cnt+1))
sleep 30
echo "Wait until the IP address of the ingress ${ingressName} is available, retry ${cnt} of ${MAX_RETRIES}..."
ip=$(kubectl get ingress ${ingressName} -n ${namespaceName} -o=jsonpath='{.status.loadBalancer.ingress[0].ip}')
echo "Ingress ${ingressName} ip is ${ip}"
done
}
Green='\033[0;32m'
Red='\033[0;31m'
Color_Off='\033[0m'
Check_Mark='\xE2\x9C\x94'
assert_equals () {
if [ "$1" = "$2" ]; then
echo -e "$Green $Check_Mark Success $Color_Off"
else
echo -e "$Red Failed $Color_Off"
echo -e "$Red Expected $1 to equal $2 $Color_Off"
exit 1
fi
}
get_json_value () {
echo $1 | jq -r $2
}
wait_app_ready() {
readinessCheckEndpoint=$1
accountsEndpoint=$2
username=$3
password=$4
cnt=0
statusCode=$(curl $readinessCheckEndpoint --insecure --silent --output /dev/null --write-out '%{http_code}')
while [ $statusCode -ne 200 ]
do
if [ $cnt -eq $MAX_RETRIES ]; then
echo "Timeout and exit due to the maximum retries reached."
exit 1
fi
cnt=$((cnt+1))
echo "Readiness check endpoint is not available, retry ${cnt} of ${MAX_RETRIES}..."
sleep 20
statusCode=$(curl $readinessCheckEndpoint --insecure --silent --output /dev/null --write-out '%{http_code}')
done
cnt=0
status=$(curl $readinessCheckEndpoint --insecure --silent | jq -r 'select(.status != null) | .status')
while [[ UP != $status ]]
do
if [ $cnt -eq $MAX_RETRIES ]; then
echo "Timeout and exit due to the maximum retries reached."
exit 1
fi
cnt=$((cnt+1))
echo "Readiness status is ${status}, retry ${cnt} of ${MAX_RETRIES}..."
sleep 20
status=$(curl $readinessCheckEndpoint --insecure --silent | jq -r 'select(.status != null) | .status')
done
echo "Readiness status is ${status}, wait 60s before creating testing account..."
sleep 60
cnt=0
statusCode=$(curl -X 'POST' \
"$accountsEndpoint" \
-H 'Content-Type: application/json' \
-d '{
"username": "'$username'",
"password": "'$password'"
}' \
--insecure \
--write-out '%{http_code}' --silent --output /dev/null)
while [ $statusCode -ne 201 ]
do
if [ $cnt -eq $MAX_RETRIES ]; then
echo "Timeout and exit due to the maximum retries reached."
exit 1
fi
cnt=$((cnt+1))
echo "Failed to create testing account ${username}, retry ${cnt} of ${MAX_RETRIES}..."
sleep 30
statusCode=$(curl -X 'POST' \
"$accountsEndpoint" \
-H 'Content-Type: application/json' \
-d '{
"username": "'$username'",
"password": "'$password'"
}' \
--insecure \
--write-out '%{http_code}' --silent --output /dev/null)
done
echo "Successfully created testing account ${username}."
}