Created
August 15, 2023 01:29
-
-
Save xorima/ab179cc4e8623ef143b224e927c31e3e to your computer and use it in GitHub Desktop.
check pod is ready
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# Initial delay in seconds | |
DELAY=1 | |
# Maximum number of retries | |
MAX_RETRIES=5 | |
# Automatically set Pod name and namespace | |
POD_NAME=$HOSTNAME | |
NAMESPACE=$(cat /var/run/secrets/kubernetes.io/serviceaccount/namespace) | |
# Kubernetes API endpoint to check pod status | |
API_ENDPOINT="https://kubernetes.default.svc.cluster.local/api/v1/namespaces/$NAMESPACE/pods/$POD_NAME" | |
retry_count=0 | |
while [ $retry_count -lt $MAX_RETRIES ]; do | |
# Use wget to call the Kubernetes API | |
readiness=$(wget -O - --header="Authorization: Bearer $(cat /var/run/secrets/kubernetes.io/serviceaccount/token)" --no-check-certificate "$API_ENDPOINT" 2>/dev/null | grep -o '"ready":[true,false]*' | cut -d':' -f2) | |
if [ "$readiness" = "true" ]; then | |
# Pod is ready, exit with 0 | |
exit 0 | |
else | |
# Pod is not ready, wait for the exponential backoff delay | |
echo "Pod is not ready. Retrying in $DELAY seconds..." | |
sleep $DELAY | |
# Double the delay for the next iteration | |
DELAY=$((DELAY * 2)) | |
retry_count=$((retry_count + 1)) | |
fi | |
done | |
# If we've reached here, all retries have failed | |
echo "Pod readiness check failed after $MAX_RETRIES retries." | |
exit 1 | |
In this version, I've replaced the awk commands with grep and cut, which should be more compatible with BusyBox's limited toolset. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment