This repository has been archived by the owner on Oct 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 36
/
readiness-probe.sh
67 lines (56 loc) · 2.19 KB
/
readiness-probe.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
#!/usr/bin/env bash
# This is the default readiness probe, which will look at all monit monitored
# processes and check that they are ready.
# It may optionally be launched with other scripts as arguments; for each
# argument, it will run it as a command, and report not ready if any one returns
# a non-zero exit status.
# If the enviroment variable `FISSILE_ACTIVE_CHECK` is set, that is assumed to
# be a command which, when run, will report if this pod should be placed in the
# set of pods accepting traffic. An exit status of zero in that case is assumed
# to mean the pod is ready for traffic.
###
set -o errexit -o nounset -o pipefail
# Set up the readiness flag ahead of time, so if we error out we mark this pod
# as not ready
readiness=false
update_readiness() {
local svcacct=/var/run/secrets/kubernetes.io/serviceaccount
curl --silent \
--cacert "${svcacct}/ca.crt" \
--header "Authorization: bearer $(cat "${svcacct}/token")" \
--header 'Content-Type: application/merge-patch+json' \
--output /dev/null \
--request 'PATCH' \
"https://${KUBERNETES_SERVICE_HOST}:${KUBERNETES_SERVICE_PORT}/api/v1/namespaces/$(cat "${svcacct}/namespace")/pods/${HOSTNAME}" \
--data '{
"metadata": {
"labels": {
"skiff-role-active": "'"${readiness}"'"
}
}
}'
return $?
}
if test -n "${FISSILE_ACTIVE_PASSIVE_PROBE:-}" ; then
trap update_readiness EXIT
fi
if [ ! -r /etc/monitrc ] ; then
echo "Waiting for monit to start"
exit 1
fi
# Grab monit port
monit_port=$(awk '/httpd port/ { print $4 }' /etc/monitrc)
# Check that monit thinks everything is ready
curl -s -u admin:"${MONIT_PASSWORD}" http://127.0.0.1:"${monit_port}"/_status | gawk '
$1 == "status" && $2 != "running" && $2 != "accessible" { print "Waiting for monit to be ready"; exit 1 }
'
# Check that any additional readiness checks are ready
for command in "${@}" ; do
/usr/bin/env bash -c "${command}"
done
# If this is an active/passive role, do that check
if test -n "${FISSILE_ACTIVE_PASSIVE_PROBE:-}" ; then
if eval "${FISSILE_ACTIVE_PASSIVE_PROBE}" ; then
readiness=true
fi
fi