-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrollout_demo_curl.sh
executable file
·103 lines (81 loc) · 3.02 KB
/
rollout_demo_curl.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
#!/bin/sh
#NAME=ckad-demo
NAME=web
[ ! -z "$1" ] && NAME=$1
KUBECTL="kubectl -n demo"
kubectl get ns demo || kubectl create ns demo
GET_SVC_IP() {
IP=$($KUBECTL get svc $NAME --no-headers | awk '{ print $3; }')
}
GET_SVC_IP
CURL_OPTS="--connect-timeout 2 --max-time 4"
WGET_OPTS="-qO - --timeout 4 --tries 1"
GET_CMD="curl $CURL_OPTS"
GET_CMD="wget $WGET_OPTS"
#IMAGE_NAME=alpine
IMAGE_NAME=busybox
#IMAGE_NAME=ubuntu
LOOP_CONNECT_FROM_LOCAL_NODE() {
while true; do
if [ -z "$IP" ]; then
GET_SVC_IP
else
curl $CURL_OPTS ${IP}:80/1;
[ $? -ne 0 ] && GET_SVC_IP
fi
sleep 1;
done
}
FROM_POD() {
set -x
$KUBECTL describe pod testpod 2>&1 | grep Image: | grep " ${IMAGE_NAME}$"
if [ $? -eq 0 ]; then
$KUBECTL exec -it testpod -- sh -c "while true; do $GET_CMD web/1; sleep 1; done"
$KUBECTL label testpod hide=k1spy
else
#$KUBECTL run testpod --rm -it --image ${IMAGE_NAME} -- sh -c "while true; do $GET_CMD web/1; sleep 1; done"
#$KUBECTL run testpod --dry-run=client -o yaml --rm -it --image ${IMAGE_NAME} -- sh -c "while true; do $GET_CMD web/1; sleep 1; done" |
$KUBECTL run testpod --dry-run=client -o yaml --image ${IMAGE_NAME} -- sh -c "while true; do $GET_CMD web/1; sleep 1; done" |
sed -e 's/run: testpod/hide: k1spy/' | $KUBECTL apply -f -
while true; do $KUBECTL logs testpod -f; sleep 2; done
fi
}
ON_CLUSTER_NODE_P() {
echo "Checking if running on Cluster node:"
echo; echo "---- Looking to see if a kubelet is running on this node:"
ps -fade | grep -v grep | grep 'kubelet ' || {
echo; echo "---- Not a cluster node: No kubelet running locally"
return 1
}
echo; echo "---- Trying to obtain current context"
CONTEXT=$( $KUBECTL config get-contexts -o name --context current )
[ -z "$CONTEXT" ] && {
echo; echo "---- Not a cluster node: failed to get cluster context"
return 1
}
echo; echo "---- Trying to obtain apiserver ip address of current context"
IP=$( $KUBECTL config view --context $CONTEXT | grep -m 1 server: | sed -e 's?.*https://??' -e 's/:.*//' )
HOST_IP=$( hostname -i )
[ -z "$IP" ] && {
echo; echo "---- Not a cluster node: failed to get apiserver ip address"
return 1
}
[ -z "$HOST_IP" ] && {
echo; echo "---- Not a cluster node: failed to get hostname ip address"
return 1
}
echo; echo "---- Comparing apiserver ip address with address of this node"
[ "$IP" != "$HOST_IP" ] && {
echo; echo "---- Not a cluster node: apiserver ip ($IP) != host ip address ($HOST_IP)"
return 1
}
echo; echo "---- Looks like we're running on the active control plane node"
return 0
}
if ON_CLUSTER_NODE_P ; then
echo; echo "Looks like we're running on the main control-plane node - using curl directly"
LOOP_CONNECT_FROM_LOCAL_NODE
else
echo; echo "Looks like we're running on a remote node - using wget from a Pod"
FROM_POD
fi