|
| 1 | +#!/bin/bash |
| 2 | +# This script is used by k8s to check the readiness of containers |
| 3 | +# Check if the container is readiness or not, exit code 0 means readiness, others mean not readiness |
| 4 | + |
| 5 | +#### exit code contract, k8s only cares zero or not none-zero, but we want to use none-zero code to indicate different error |
| 6 | +# 0: readiness |
| 7 | +# 1: if the hook script is python code, the default crash exit code is 1 |
| 8 | +# 2: supervisor start service doesn't exit normally |
| 9 | +# other exit code: returned by post_check_script, define in the post_check_script, should not include 1,2 |
| 10 | + |
| 11 | +# check if the start service exists |
| 12 | +# if the start service doesn't exist, do nothing |
| 13 | +# if the start service exists, check if it exits normally |
| 14 | +# if the start service doesn't exit normally, exit with code 2 |
| 15 | +pre_check_service_name="start" |
| 16 | +no_process_string="ERROR (no such process)" |
| 17 | +service_status=$(supervisorctl status $pre_check_service_name) |
| 18 | +if [[ $service_status != *"$no_process_string"* ]] && [[ $(echo $service_status |awk '{print $2}') != 'EXITED' ]]; then |
| 19 | + exit 2 |
| 20 | +fi |
| 21 | + |
| 22 | +# feature owner can add their own readiness check script |
| 23 | +# check if the post_check_script exists |
| 24 | +# if the post_check_script exists, run it |
| 25 | +# if the post_check_script exits with non-zero code, exit with the code |
| 26 | +post_check_script="/usr/bin/readiness_probe_hook" |
| 27 | +if [ -x $post_check_script ]; then |
| 28 | + $post_check_script |
| 29 | + post_check_result=$? |
| 30 | + if [ $post_check_result != 0 ]; then |
| 31 | + exit $post_check_result |
| 32 | + fi |
| 33 | +fi |
| 34 | + |
| 35 | +exit 0 |
0 commit comments