forked from GoogleCloudPlatform/gke-networking-demos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathverify-functions.sh
321 lines (299 loc) · 7.85 KB
/
verify-functions.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
#!/bin/bash -e
# Copyright 2018 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Library of functions used by the validate the project
# Check if a resource quota is met
# Globals:
# None
# Arguments:
# PROJECT
# METRICS
# QUOTA
# Returns:
# status code
function meets_quota() {
local PROJECT="$1"
local METRIC="$2"
local QUOTA="$3"
local LIMIT
LIMIT=$(gcloud compute project-info describe --project "$PROJECT" \
--format=json | jq --arg METRIC "$METRIC" '.quotas[] | select(.metric==$METRIC).limit')
if [[ "${LIMIT}" -ge "$QUOTA" ]]; then
return 0
fi
echo ""
echo "$METRIC quota of $QUOTA is not met"
echo ""
return 1
}
# Check if a given deployment exists
# Globals:
# None
# Arguments:
# PROJECT
# DEPLOY
# Returns:
# status code
# 0: the deployment should exist, except that it does not
# 1: the deployment exists
# 2: no trace of the deployment left, it can be an indicator of successful clean up earlier
function deployment_exists() {
local PROJECT="${1}"
local DEPLOY="${2}"
local EXISTS
EXISTS=$(gcloud deployment-manager deployments list --project "${PROJECT}" \
--filter="name=${DEPLOY} AND operation.status=DONE" --format "value(operation.error.errors)")
if [[ "${EXISTS}" != "" ]]; then
if [[ "${EXISTS}" != "[]" ]]; then
echo "ERROR ${DEPLOY}: ${EXISTS}"
return 0
fi
else ## EXISTS = "" the resource cannot be found at all
return 2
fi
# default exit code, where the resource can be found and there are no operation.error.errors
return 1
}
# Delete a deployment, including retry logic where necessary
# Globals:
# None
# Arguments:
# PROJECT
# DEPLOY
# RETRY - number of retrys, default to 3
# Returns:
# status code
function deployment_deletes() {
local PROJECT="${1:-}"
local DEPLOY="${2:-}"
local RETRY="${3:-3}"
while [ ${RETRY} -gt 0 ]; do
echo "Trying to delete ${DEPLOY}"
gcloud deployment-manager deployments delete "${DEPLOY}" --quiet --project "${PROJECT}"
deployment_exists "${PROJECT}" "${DEPLOY}"
if [[ "$?" != "2" ]]; then
echo "failed deleting ${DEPLOY}. Retrying ..."
sleep 20
RETRY=$((RETRY-1))
else
break
fi
done
# Fail the deletion if there are still traces of deployment left after multiple attempts of deletion
deployment_exists "${PROJECT}" "${DEPLOY}"
if [[ "$?" != "2" ]]; then
return 1
fi
return 0
}
# Check if a given network exists
# Globals:
# None
# Arguments:
# PROJECT
# NETWORK
# Returns:
# status code
function network_exists() {
local PROJECT="${1}"
local NETWORK="${2}"
local EXISTS
EXISTS=$(gcloud compute networks list --project "${PROJECT}" \
--filter="name=${NETWORK}" --format "value(name)")
if [[ "${EXISTS}" != "" ]]; then
echo "${NETWORK} network exists"
return 0
fi
return 1
}
# Check if a given vpn exists
# Globals:
# None
# Arguments:
# PROJECT
# VPN
# Returns:
# status code
function vpn_exists() {
local PROJECT="${1}"
local VPN="${2}"
local EXISTS
EXISTS=$(gcloud compute vpn-tunnels list --project "${PROJECT}" \
--filter="name=${VPN} and status=ESTABLISHED" --format "value(name)")
if [[ "${EXISTS}" != "" ]]; then
echo "${VPN} vpn exists"
return 0
fi
return 1
}
# Check if a given network peering exists
# Globals:
# None
# Arguments:
# PROJECT
# NETWORK
# Returns:
# status code
# 0: the network peering exists
# 1: the network peering does not exist
function network_peering_exists() {
local PROJECT="${1}"
local NETWORK="${2}"
local EXISTS
EXISTS=$(gcloud compute networks peerings list --project "${PROJECT}" \
--filter="name=${NETWORK}" --format "value(name)")
if [[ "${EXISTS}" != "" ]]; then
echo "${NETWORK} peering exists"
return 0
fi
return 1
}
# Delete a network peering, including retry logic where necessary
# Globals:
# None
# Arguments:
# PROJECT
# NETWORK
# PEERING
# RETRY - number of retrys, default to 3
# Returns:
# status code
function network_peering_deletes() {
local PROJECT="${1:-}"
local NETWORK="${2:-}"
local PEERING="${3:-}"
local RETRY="${4:-3}"
while [ ${RETRY} -gt 0 ]; do
gcloud compute networks peerings delete "${PEERING}" --network "${NETWORK}" --project "${PROJECT}" --quiet
network_peering_exists "${PROJECT}" "${NETWORK}"
if [[ "$?" != "1" ]]; then
sleep 10
RETRY=$((RETRY-1))
else
break
fi
done
# Fail the deletion if there are still traces of deployment left after multiple attempts of deletion
network_peering_exists "${PROJECT}" "${NETWORK}"
if [[ "$?" != "0" ]]; then
return 1
fi
return 0
}
# Verify cidr range
# Globals:
# None
# Arguments:
# PROJECT
# SUBNET
# RANGE
# Returns:
# status code
function verify_cidr_range() {
local PROJECT="${1}"
local SUBNET="${2}"
local CIDR="${3}"
local RANGE
RANGE=$(gcloud compute networks subnets list --project "${PROJECT}" \
--filter="name=${SUBNET}" --format "value(RANGE)")
if [[ "${RANGE}" == "${CIDR}" ]]; then
echo "Subnet ${SUBNET} has the ip range ${RANGE}"
return 0
fi
return 1
}
# Check if a cluster exists
# Globals:
# None
# Arguments:
# PROJECT
# CLUSTER
# Returns:
# status code
function cluster_running() {
local PROJECT="${1}"
local CLUSTER="${2}"
local RUNNING
RUNNING=$(gcloud container clusters list --project "${PROJECT}" \
--filter="name=${CLUSTER} AND status:RUNNING" --format "value(name)")
if [[ "${RUNNING}" == "${CLUSTER}" ]]; then
echo "Cluster ${CLUSTER} is running"
return 0
fi
return 1
}
# Check if service ip is available
# Globals:
# None
# Arguments:
# PROJECT
# CLUSTER
# SERVICE
# RETRY_COUNT - Number of times to retry
# INTERVAL - Amount of time to sleep between retries
# NAMESPACE - k8s namespace the service lives in
# Returns:
# status code
function access_service () {
local PROJECT="${1}"
local CLUSTER="${2}"
local SERVICE="${3}"
local RETRY_COUNT="15"
local SLEEP="15"
local NAMESPACE="default"
local SERVICE_IP
for ((i=0; i<"${RETRY_COUNT}"; i++)); do
SERVICE_IP=$(kubectl get -n "${NAMESPACE}" --cluster "${CLUSTER}" \
service "${SERVICE}" -o jsonpath='{.status.loadBalancer.ingress[0].ip}')
if [ "${SERVICE_IP}" == "" ] ; then
echo "Attempt $((i + 1)): IP not yet allocated for service ${SERVICE}" >&1
else
echo "$SERVICE_IP has been allocated for service ${SERVICE} in ${CLUSTER}" >&1
return 0
fi
sleep "${SLEEP}"
done
echo "Timed out waiting for service ${SERVICE} to be allocated an IP address." >&1
return 1
}
# Check if service backends exist
# Globals:
# None
# Arguments:
# PROJECT
# NAME
# RETRY_COUNT - Number of times to retry
# INTERVAL - Amount of time to sleep between retries
# NAMESPACE - k8s namespace the service lives in
# Returns:
# status code
function backends_exists () {
local PROJECT="${1}"
local NAME="${2}"
local RETRY_COUNT="50"
local SLEEP="10"
local BACKEND
for ((i=0; i<"${RETRY_COUNT}"; i++)); do
BACKEND=$(gcloud compute backend-services list --project "$PROJECT" \
--format "value(backends.group)" | grep "${NAME}")
if [ "${BACKEND}" == "" ] ; then
return 0
else
echo "Attempt $((i + 1)): Checking if service backends are removed" >&1
fi
sleep "${SLEEP}"
done
echo "Timed out waiting for service backends to be removed." >&1
return 1
}