From d1fdde2761b5180c31283dbd097912fbe871a9cc Mon Sep 17 00:00:00 2001 From: Lucas Fernandez Date: Tue, 7 Sep 2021 20:15:52 +0200 Subject: [PATCH] Added readiness probe script --- readinessProbe.sh | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100755 readinessProbe.sh diff --git a/readinessProbe.sh b/readinessProbe.sh new file mode 100755 index 0000000..53e5432 --- /dev/null +++ b/readinessProbe.sh @@ -0,0 +1,37 @@ +#!/bin/bash + +function is_container_running() { + [ $(curl -s -o /dev/null -w ''%{http_code}'' localhost:8081) == "302" ] +}; + +function is_leader_election_ready() { + [ $(curl -s -o /dev/null -w ''%{http_code}'' localhost:4040) == "200" ] +} + +function get_leader() { + echo "$(curl http://localhost:4040 2> /dev/null | python3 -c "import sys, json; print(json.load(sys.stdin)['name'])")" +} + +function get_pod_name() { + echo ${HOSTNAME} +} + +function is_container_leader() { + [ "$(get_leader)" == "$(get_pod_name)" ] +}; + +if ! is_leader_election_ready; then + # Leader election is not ready yet + exit 0 +fi + +if ! is_container_running; then + # Pod is waiting to become leader + exit 0 +elif is_container_leader; then + # Pod is the leader + exit 0 +else + # Pod is still running but it is not the leader + exit 1 +fi