forked from philrud/istio-multicluster-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.sh
executable file
·452 lines (391 loc) · 16.5 KB
/
common.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
#!/bin/bash -e
### script arguments
# ISTIO_VERSION: e.g. "1.1.3" or "release-1.1-20190417-09-16"
ISTIO_VERSION="${ISTIO_VERSION:-release-1.1-latest-daily}"
# KUBECONFIG: path to a kubeconfig file
KUBECONFIG1="${KUBECONFIG1:-${HOME}/.kube/config}"
KUBECONFIG2="${KUBECONFIG2:-${HOME}/.kube/config}"
# KUBECONTEXT: empty value defaults to "current" context of given kubeconfig file
KUBECONTEXT1="${KUBECONTEXT1:-$(kubectl --kubeconfig=${KUBECONFIG1} config current-context)}"
KUBECONTEXT2="${KUBECONTEXT2:-$(kubectl --kubeconfig=${KUBECONFIG2} config current-context)}"
###
### script arguments sanity checks
if [[ $KUBECONFIG1 == $KUBECONFIG2 ]] && [[ $KUBECONTEXT1 == $KUBECONTEXT2 ]]; then
echo
echo " [FAIL] KUBECONFIG{1,2}/KUBECONTEXT{1,2} pairs refer to the same cluster"
echo " this configuration requires two distinct clusters, terminating..."
echo
exit 1
fi
###
function download_istio_dist {
local ISTIO_VERSION="${1:?required argument is not set or empty}"
local ISTIO_DIST_DIR="${2:?required argument is not set or empty}"
mkdir -p $ISTIO_DIST_DIR
local BUCKET_CONTENTS
local BUCKET_URL
# looking for a RELEASE
local RELEASE_BUCKET_URL="https://storage.googleapis.com/istio-release/?prefix=releases/${ISTIO_VERSION}/"
if [[ -z "$BUCKET_URL" ]]; then
BUCKET_CONTENTS=$(curl --location --fail $RELEASE_BUCKET_URL)
if [[ $BUCKET_CONTENTS == *"charts/index.yaml"* ]]; then
BUCKET_URL=$(echo $RELEASE_BUCKET_URL | sed 's/?.*//')
fi
fi
# looking for a DAILY BUILD
local SNAPSHOT_BUCKET_URL="https://storage.googleapis.com/istio-prerelease/?prefix=daily-build/${ISTIO_VERSION}/"
if [[ -z "$BUCKET_URL" ]]; then
BUCKET_CONTENTS=$(curl --location --fail $SNAPSHOT_BUCKET_URL)
if [[ $BUCKET_CONTENTS == *"charts/index.yaml"* ]]; then
BUCKET_URL=$(echo $SNAPSHOT_BUCKET_URL | sed 's/?.*//')
fi
fi
if [[ -z "$BUCKET_URL" ]]; then
echo "couldn't find specified Istio release"
return 1
fi
echo $BUCKET_URL
# distribution found, downloading artifacts
local ISTIO_URL=$(sed -ne '/.*/{s/.*<Key>\([^<>]*charts\/istio-[0-9][^<>]*\.tgz\)<\/Key>.*/\1/p;q;}' <<< "$BUCKET_CONTENTS")
local ISTIO_INIT_URL=$(sed -ne '/.*/{s/.*<Key>\([^<>]*charts\/istio-init-[^<>]*\.tgz\)<\/Key>.*/\1/p;q;}' <<< "$BUCKET_CONTENTS")
: "${ISTIO_URL:?failed to locate istio-<RELEASE>.tgz}"
: "${ISTIO_INIT_URL:?failed to locate istio-init-<RELEASE>.tgz}"
curl --location --fail "${BUCKET_URL}${ISTIO_URL}" -o "${ISTIO_DIST_DIR}/istio.tgz"
curl --location --fail "${BUCKET_URL}${ISTIO_INIT_URL}" -o "${ISTIO_DIST_DIR}/istio-init.tgz"
tar xzf "${ISTIO_DIST_DIR}/istio.tgz" -C "${ISTIO_DIST_DIR}"
tar xzf "${ISTIO_DIST_DIR}/istio-init.tgz" -C "${ISTIO_DIST_DIR}"
}
function ensure_namespace_exists {
local KUBECTL="${1:?required argument is not set or empty}"
$KUBECTL create namespace "istio-system" || true
}
function install_istio_init {
local KUBECTL="${1:?required argument is not set or empty}"
local ISTIO_INIT_DIR="${2:?required argument is not set or empty}"
helm template $ISTIO_INIT_DIR --name istio-init --namespace istio-system | $KUBECTL apply -f -
until [[ "$($KUBECTL get crds | grep 'istio.io\|certmanager.k8s.io' | wc -l)" -gt 52 ]]; do
echo "awaiting CRDs creation..."
sleep 3
done
}
function install_certs {
local KUBECTL1="${1:?required argument is not set or empty}"
local KUBECTL2="${2:?required argument is not set or empty}"
local CERTS_DIR="${3:?required argument is not set or empty}"
mkdir -p $CERTS_DIR
cat > $CERTS_DIR/ca-cert.pem <<EOF
-----BEGIN CERTIFICATE-----
MIIDnzCCAoegAwIBAgIJAON1ifrBZ2/BMA0GCSqGSIb3DQEBCwUAMIGLMQswCQYD
VQQGEw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Rw/6ClP5+cCG7fGocPaZh+c+4Nxm9suMuZBZCtNOeYOMIfvCPcCS+8
PQ/0hC4/0J3WJKzGBssaaMufJxzgFPPtDJ998kY8rlROghdSaVt423/jXIAYnP3Y
05n8TGERBj7TLdtIVbtUIx3JHAo3PWJywA6mEDovFMJhJERp9sDHIr1BbhXK1TFN
Z6HNH6gInkSSMtvC4Ptejb749PTaePRPF7ID//eq/3AH8UK50F3TQcLjEqWUsJUn
aFKltOc+RAjzDklcUPeG4Y6eMA==
-----END CERTIFICATE-----
EOF
$KUBECTL1 create secret generic cacerts -n istio-system \
--from-file=$CERTS_DIR/ca-cert.pem \
--from-file=$CERTS_DIR/ca-key.pem \
--from-file=$CERTS_DIR/root-cert.pem \
--from-file=$CERTS_DIR/cert-chain.pem || true
$KUBECTL2 create secret generic cacerts -n istio-system \
--from-file=$CERTS_DIR/ca-cert.pem \
--from-file=$CERTS_DIR/ca-key.pem \
--from-file=$CERTS_DIR/root-cert.pem \
--from-file=$CERTS_DIR/cert-chain.pem || true
}
function install_istio {
local KUBECTL="${1:?required argument is not set or empty}"
local ISTIO_DIR="${2:?required argument is not set or empty}"
helm template $ISTIO_DIR --name istio \
--namespace istio-system \
--set global.controlPlaneSecurityEnabled=true \
--set global.mtls.enabled=true \
--set security.selfSigned=false \
| $KUBECTL apply -f -
}
function await_istio_rollout {
local KUBECTL="${1:?required argument is not set or empty}"
$KUBECTL -n istio-system rollout status deployment istio-ingressgateway
}
function configure_dns {
local KUBECTL="${1:?required argument is not set or empty}"
$KUBECTL -n istio-system rollout status deployment istiocoredns
$KUBECTL apply -f - <<EOF
apiVersion: v1
kind: ConfigMap
metadata:
name: kube-dns
namespace: kube-system
data:
stubDomains: |
{"global": ["$($KUBECTL get svc -n istio-system istiocoredns -o jsonpath={.spec.clusterIP})"]}
EOF
}
function install_slave_cluster_secrets {
local KUBECONFIG_MASTER="${1:?required argument is not set or empty}"
local KUBECONTEXT_MASTER="${2:?required argument is not set or empty}"
local KUBECONFIG_SLAVE="${3:?required argument is not set or empty}"
local KUBECONTEXT_SLAVE="${4:?required argument is not set or empty}"
local KUBECTL_MASTER="kubectl --kubeconfig=${KUBECONFIG_MASTER} --context=${KUBECONTEXT_MASTER}"
local KUBECTL_SLAVE="kubectl --kubeconfig=${KUBECONFIG_SLAVE} --context=${KUBECONTEXT_SLAVE}"
local CLUSTER_NAME=$($KUBECTL_SLAVE config view -o jsonpath="{.contexts[?(@.name == \"${KUBECONTEXT_SLAVE}\")].context.cluster}")
local SERVER=$($KUBECTL_SLAVE config view -o jsonpath="{.clusters[?(@.name == \"${CLUSTER_NAME}\")].cluster.server}")
local NAMESPACE=istio-system
local SERVICE_ACCOUNT=istio-pilot-service-account
local SECRET_NAME=$($KUBECTL_SLAVE get sa ${SERVICE_ACCOUNT} -n ${NAMESPACE} -o jsonpath="{.secrets[].name}")
local CA_DATA=$($KUBECTL_SLAVE get secret ${SECRET_NAME} -n ${NAMESPACE} -o jsonpath="{.data['ca\.crt']}")
local TOKEN=$($KUBECTL_SLAVE get secret ${SECRET_NAME} -n ${NAMESPACE} -o jsonpath="{.data['token']}" | base64 --decode)
local TEMP_DIR=$(mktemp -d)
local KUBECFG_FILE=$TEMP_DIR/kubeconfig
cat > $KUBECFG_FILE <<EOF
apiVersion: v1
kind: Config
clusters:
- cluster:
certificate-authority-data: ${CA_DATA}
server: ${SERVER}
name: ${CLUSTER_NAME}
contexts:
- context:
cluster: ${CLUSTER_NAME}
user: ${CLUSTER_NAME}
name: ${CLUSTER_NAME}
current-context: ${CLUSTER_NAME}
preferences: {}
users:
- name: ${CLUSTER_NAME}
user:
token: ${TOKEN}
EOF
local SECRET_NAME=$(cat /dev/urandom | env LC_CTYPE=C tr -dc 'a-z0-9' | fold -w 32 | head -n 1)
$KUBECTL_MASTER create secret generic ${SECRET_NAME} --from-file ${KUBECFG_FILE} -n ${NAMESPACE}
$KUBECTL_MASTER label secret ${SECRET_NAME} istio/multiCluster=true -n ${NAMESPACE}
rm -rf $TEMP_DIR
sleep 5
}
function install_probe {
local KUBECTL="${1:?required argument is not set or empty}"
$KUBECTL create ns test || true
$KUBECTL label --overwrite=true ns test istio-injection=enabled || true
$KUBECTL apply -f - <<EOF
apiVersion: v1
kind: Service
metadata:
name: test-srv
namespace: test
spec:
ports:
- port: 8080
name: http-echo
selector:
app: test-srv
---
apiVersion: apps/v1beta1
kind: Deployment
metadata:
name: test-srv
namespace: test
spec:
template:
metadata:
labels:
app: test-srv
spec:
containers:
- name: fortio-server
image: fortio/fortio
ports:
- containerPort: 8080
args:
- server
EOF
$KUBECTL -n test rollout status deployment test-srv
sleep 3
}
function delete_probe {
local KUBECTL="${1:?required argument is not set or empty}"
$KUBECTL delete -f - <<EOF
apiVersion: v1
kind: Service
metadata:
name: test-srv
namespace: test
spec:
ports:
- port: 8080
name: http-echo
selector:
app: test-srv
---
apiVersion: apps/v1beta1
kind: Deployment
metadata:
name: test-srv
namespace: test
spec:
template:
metadata:
labels:
app: test-srv
spec:
containers:
- name: fortio-server
image: fortio/fortio
ports:
- containerPort: 8080
args:
- server
EOF
$KUBECTL delete ns test || true
}
function check_probe {
local KUBECTL="${1:?required argument is not set or empty}"
local FORTIO_ADDR="${2:-test-srv.test.svc.cluster.local:8080}"
local POD=$($KUBECTL -n test get pod -l app=test-srv -o jsonpath="{.items[0].metadata.name}")
$KUBECTL -n test exec -it $POD -c fortio-server -- fortio curl http://${FORTIO_ADDR}/debug?env=dump
}
function check_probe_load_balancer {
local KUBECTL="${1:?required argument is not set or empty}"
local POD=$($KUBECTL -n test get pod -l app=test-srv -o jsonpath="{.items[0].metadata.name}")
local SVCADDR1=$($KUBECTL -n test exec -it $POD -c fortio-server -- \
fortio curl http://test-srv.test.svc.cluster.local:8080/debug?env=dump \
| grep "^TEST_SRV_SERVICE_HOST=")
echo "received response from: $SVCADDR1"
for i in {1..10}; do
local SVCADDR2=$($KUBECTL -n test exec -it $POD -c fortio-server -- \
fortio curl http://test-srv.test.svc.cluster.local:8080/debug?env=dump \
| grep "^TEST_SRV_SERVICE_HOST=")
if [[ $SVCADDR1 != $SVCADDR2 ]]; then
echo "received response from: $SVCADDR2"
return 0
fi
done
echo "never received response from any other host"
return 1
}
function fortio_addr {
local KUBECTL="${1:?required argument is not set or empty}"
local IP_ADDR=$($KUBECTL get svc test-srv -n test -o json | jq -r ".spec.clusterIP")
local PORT_NUMBER=$($KUBECTL get svc test-srv -n test -o json | jq -r ".spec.ports[0].port")
echo "$IP_ADDR:$PORT_NUMBER"
}
function check_probe_vpn {
local KUBECTL1="${1:?required argument is not set or empty}"
local KUBECTL2="${2:?required argument is not set or empty}"
echo -e "\n [*] get the local fortio address... \n"
FORTIO_ADDR1=$(fortio_addr "$KUBECTL1")
FORTIO_ADDR2=$(fortio_addr "$KUBECTL2")
echo -e "\n [OK] get the local fortio addresses: Cluster 1: $FORTIO_ADDR1, Cluster 2: $FORTIO_ADDR2\n"
echo -e "\n [*] check if Cluster 1 fortio can access Cluster 2 fortio... \n"
check_probe "$KUBECTL1" "$FORTIO_ADDR2" 2&>1 /dev/null
if [[ $? -eq 0 ]]; then
echo -e "\n [OK] check if Cluster 1 fortio can access Cluster 2 fortio \n"
else
echo -e "\n [ERROR] check if Cluster 1 fortio can access Cluster 2 fortio \n"
fi
check_probe "$KUBECTL2" "$FORTIO_ADDR1" 2&>1 /dev/null
if [[ $? -eq 0 ]]; then
echo -e "\n [OK] check if Cluster 2 fortio can access Cluster 1 fortio \n"
else
echo -e "\n [ERROR] check if Cluster 2 fortio can access Cluster 1 fortio \n"
fi
return 0
}
function istio_mc_secrets {
local KUBECTL="${1:?required argument is not set or empty}"
$KUBECTL get secret -n istio-system -l istio/multiCluster=true
}