-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkubetest1-check.sh
executable file
·164 lines (117 loc) · 4.44 KB
/
kubetest1-check.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
#!/bin/sh
set -e
#
# Do simple checks on kubetest1 deployment
#
KUBETEST_MANIFEST=kubetest1-manifest.yaml
MYSQL_ROOT_PASSWORD=kubetest
MYSQL_DB_NAME=kubetest
MYSQL_TABLE_NAME=kubetest
MYSQL_KUBETEST_CHECK_VALUE='KUBETEST-DB-CHECK'
# functions
on_exit()
{
rm -f "$KUBETEST_MANIFEST".expanded
cat <<EOF
KUBETEST REPORT
===============
manifest: ${KUBETEST_MANIFEST}
cluster nodes: $(echo ${kube_nodes} | sed 's/[[:space:]]\+/ /g')
kubernetes master: ${kube_master_node}
multi-node cluster: ${MULTI_NODE}
${kubetest_pod_name:-kubetest-pod}: ${kubetest_pod_hostip}
${mysql_pod_name:-mysql-pod}: ${mysql_pod_hostip}
$(echo "${KUBETEST_FINAL_RESULT:-KUBETEST CHECK: WEB ERROR}" | \
sed -n 's/^.\+$/ &/p')
EOF
}
# start
trap on_exit EXIT
# cleanup old services and deployments
echo 'KUBETEST INFO: First cleanup previous deployments (if any)'
kubectl delete services kubetest-service mysql-service || true
kubectl delete deployments kubetest-deployment mysql-deployment || true
echo 'KUBETEST INFO: Waiting for pods to disappear...'
while [ -n "$(kubectl get pods -o jsonpath='{.items[*]}')" ] ; do
sleep 2s
done
# gather info about kubernetes cluster
echo 'KUBETEST INFO: Get nodes ip addresses'
kube_nodes=$(kubectl get nodes -o json | jq -cr '.items[] | .status.addresses[] | select(.type == "InternalIP") | .address')
kube_nodes_count=$(echo "$kube_nodes" | wc -w)
if [ "$kube_nodes_count" -eq 0 ] ; then
echo 'KUBETEST ERROR: No nodes in cluster'
exit 1
elif [ "$kube_nodes_count" -eq 1 ] ; then
MULTI_NODE=no
ANTI_AFFINITY=''
else
MULTI_NODE=yes
ANTI_AFFINITY=Anti
fi
echo 'KUBETEST INFO: Find master node ip'
kube_master_node=$(kubectl get nodes -o wide | awk '{if ($3 == "master") print $6;}' | sort -u | head -n 1)
ip_match=no
for ip in $kube_nodes ; do
if [ "$ip" = "$kube_master_node" ] ; then
ip_match=yes
break
fi
done
if [ "$ip_match" != yes ] ; then
echo 'KUBETEST ERROR: Cannot detect master node ip'
exit 1
fi
# deploy pods and services
echo 'KUBETEST INFO: (Re)deploy kubetest'
cat "$KUBETEST_MANIFEST" > "$KUBETEST_MANIFEST".expanded
sed -i -e 's/[$]{ANTI_AFFINITY}/'"$ANTI_AFFINITY"'/g' \
-e 's/[$]{PUBLIC_IP}/'"$kube_master_node"'/g' \
-e 's/[$]{MYSQL_ROOT_PASSWORD}/'"$MYSQL_ROOT_PASSWORD"'/g' \
-e 's/[$]{MYSQL_DB_NAME}/'"$MYSQL_DB_NAME"'/g' \
-e 's/[$]{MYSQL_TABLE_NAME}/'"$MYSQL_TABLE_NAME"'/g' \
-e 's/[$]{MYSQL_KUBETEST_CHECK_VALUE}/'"$MYSQL_KUBETEST_CHECK_VALUE"'/g' \
"$KUBETEST_MANIFEST".expanded
kubectl apply -f "$KUBETEST_MANIFEST".expanded
echo 'KUBETEST INFO: Wait for mysql pod...'
while [ "$(kubectl get pods --selector=app=mysql_pod -o json \
| jq '.items[0].status.containerStatuses[0].ready')" != true ] ;
do
sleep 2s
done
echo 'KUBETEST INFO: Wait for kubetest pod...'
while [ "$(kubectl get pods --selector=app=kubetest_pod -o json \
| jq '.items[0].status.containerStatuses[0].ready')" != true ] ;
do
sleep 2s
done
# gather info about pods
echo 'KUBETEST INFO: Get kubetest pod ip address and name'
kubetest_pod_hostip=$(kubectl get pods --selector=app=kubetest_pod -o jsonpath='{.items[*].status.hostIP}')
kubetest_pod_name=$(kubectl get pods --selector=app=kubetest_pod -o jsonpath='{.items[*].metadata.name}')
echo 'KUBETEST INFO: Get mysql pod ip address and name'
mysql_pod_hostip=$(kubectl get pods --selector=app=mysql_pod -o jsonpath='{.items[*].status.hostIP}')
mysql_pod_name=$(kubectl get pods --selector=app=mysql_pod -o jsonpath='{.items[*].metadata.name}')
if [ -z "$kubetest_pod_hostip" ] || [ -z "$mysql_pod_hostip" ] ; then
echo 'KUBETEST ERROR: Could not get hostIP of a pod'
exit 1
fi
if [ "$MULTI_NODE" = yes ] && [ "$kubetest_pod_hostip" = "$mysql_pod_hostip" ] ; then
echo 'KUBETEST ERROR: Pods are colocated on one node'
exit 1
fi
# populate mysql
echo 'KUBETEST INFO: Populate mysql'
kubectl exec "$mysql_pod_name" -it -- mysql -u root -p${MYSQL_ROOT_PASSWORD} <<EOF
CREATE DATABASE IF NOT EXISTS ${MYSQL_DB_NAME};
USE ${MYSQL_DB_NAME};
DROP TABLE IF EXISTS ${MYSQL_TABLE_NAME};
CREATE TABLE ${MYSQL_TABLE_NAME} (
kubetest_check_value varchar(60) DEFAULT NULL
);
INSERT INTO ${MYSQL_TABLE_NAME}(kubetest_check_value) VALUES ('${MYSQL_KUBETEST_CHECK_VALUE}');
EOF
# Check published kubetest service
echo 'KUBETEST INFO: Check kubetest service on http port (80) on master node'
KUBETEST_FINAL_RESULT=$(curl -Ls "${kube_master_node}:80" || true)
exit 0