@@ -22,18 +22,33 @@ const PreStopHookScriptConfigKey = "pre-stop-hook-script.sh"
2222const PreStopHookScript = `#!/usr/bin/env bash
2323
2424# This script will wait for up to $MAX_WAIT_SECONDS for $POD_IP to disappear from DNS record,
25- # then it will wait additional $ADDN_WAIT_SECONDS and exit. This slows down the process shutdown
25+ # then it will wait additional $ADDITIONAL_WAIT_SECONDS and exit. This slows down the process shutdown
2626# and allows to make changes to the pool gracefully, without blackholing traffic when DNS
27- # contains IP that is already inactive. Assumes $SERVICE_NAME and $POD_IP env variables are defined.
27+ # contains IP that is already inactive. Assumes $HEADLESS_SERVICE_NAME and $POD_IP env variables are defined.
2828
29- MAX_WAIT_SECONDS=20 # max time to wait for pods IP to disappear from DNS
30- ADDN_WAIT_SECONDS=1 # additional wait, allows queries to successfully use IP from DNS from before pod termination
29+ # max time to wait for pods IP to disappear from DNS. As this runs in parallel to grace period
30+ # (defaulting to 30s) after which process is SIGKILLed, it should be set to allow enough time
31+ # for the process to gracefully terminate.
32+ MAX_WAIT_SECONDS=20
33+
34+ # additional wait, allows queries to successfully use IP from DNS from before pod termination
35+ # this gives a little bit more time for clients that resolved DNS just before DNS record
36+ # was updated.
37+ ADDITIONAL_WAIT_SECONDS=1
38+
39+ START_TIME=$(date +%s)
40+ while true; do
41+ ELAPSED_TIME=$(($(date +%s) - $START_TIME))
42+
43+ if [ $ELAPSED_TIME -gt $MAX_WAIT_SECONDS ]; then
44+ exit 1
45+ fi
46+
47+ if ! getent hosts $HEADLESS_SERVICE_NAME | grep $POD_IP; then
48+ sleep $ADDITIONAL_WAIT_SECONDS
49+ exit 0
50+ fi
3151
32- for i in {1..$MAX_WAIT_SECONDS}
33- do
34- getent hosts $SERVICE_NAME | grep $POD_IP || sleep $ADDN_WAIT_SECONDS && exit 0
3552 sleep 1
3653done
37-
38- exit 1
3954`
0 commit comments