-
Notifications
You must be signed in to change notification settings - Fork 18
/
vrrp_check.sh
executable file
·68 lines (56 loc) · 1.62 KB
/
vrrp_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
#!/bin/bash
# Name: vrrp_check.sh
# VRRP check script to perform various health checks for Kubernetes nodes
DEFAULT_TIMEOUT=5
build_kubeapi_url() {
local path=$1
echo "https://${KUBERNETES_SERVICE_HOST}:${KUBERNETES_SERVICE_PORT}${path}"
}
do_curl() {
local url=$1
local timeout=$2
local token=$(cat /var/run/secrets/kubernetes.io/serviceaccount/token)
local headers=(-H "Authorization: bearer ${token}")
local options=(-fksS -m ${timeout})
local curl_args=("${options[@]}" "${headers[@]}" ${url})
curl "${curl_args[@]}"
}
is_kubeapi_ready() {
local url=$(build_kubeapi_url /healthz)
do_curl "$url" "$DEFAULT_TIMEOUT"
}
is_node_ready() {
local nodename=${NODE_NAME}
local url=$(build_kubeapi_url /api/v1/nodes/$nodename)
local res=$(do_curl "$url" "$DEFAULT_TIMEOUT")
if [[ $? -ne 0 ]]; then
echo $res
return 1
fi
local ready=$(jq -r '.status.conditions[] | select(.type=="Ready") | .status' <<< "$res")
if [[ $ready == "True" ]]; then
return 0
else
echo $ready
return 1
fi
}
ARG_TYPE=$1
case $ARG_TYPE in
"URL_CHECK") checkurl=$2
timeout=${3:-$DEFAULT_TIMEOUT}
res=$(do_curl "$checkurl" "$timeout")
exit
;;
"API_CHECK") timeout=${2:-$DEFAULT_TIMEOUT}
checkurl=$(build_kubeapi_url /healthz)
res=$(do_curl "$checkurl" "$timeout")
exit
;;
"NODE_READY") is_node_ready
exit
;;
*) echo "unknown type"
exit 1
;;
esac