wait-for-k8s-node-ready/wait-for-k8s-node-ready.sh

55 lines
1.3 KiB
Bash
Executable file

#!/bin/bash
# Script Name: wait-for-k8s-node-ready.sh
# Description:
# v1.0.0 - 2026-06-08: Wait until a Kubernetes node reaches Ready or NotReady.
#
# Usage:
# wait-for-k8s-node-ready.sh <node> <ready|notready> [timeout_seconds]
set -euo pipefail
log() {
echo "[$(date +'%Y-%m-%dT%H:%M:%S%z')] $*"
}
usage() {
echo "Usage: $0 <node> <ready|notready> [timeout_seconds]"
}
NODE="${1:-}"
DESIRED="${2:-}"
TIMEOUT="${3:-600}"
if [ -z "${NODE}" ] || [ -z "${DESIRED}" ]; then
usage
exit 1
fi
case "${DESIRED}" in
ready|Ready|READY) WANT="True"; LABEL="Ready" ;;
notready|NotReady|NOTREADY) WANT="False"; LABEL="NotReady" ;;
*) echo "Desired state must be ready or notready"; exit 1 ;;
esac
log "Waiting for ${NODE} to become ${LABEL} (timeout: ${TIMEOUT}s)..."
end=$((SECONDS + TIMEOUT))
while [ "${SECONDS}" -lt "${end}" ]; do
current=$(kubectl get node "${NODE}" -o jsonpath='{.status.conditions[?(@.type=="Ready")].status}' 2>/dev/null || echo "Unknown")
if [ "${WANT}" = "False" ] && { [ "${current}" = "False" ] || [ "${current}" = "Unknown" ]; }; then
log "Node ${NODE} is ${LABEL}."
exit 0
fi
if [ "${WANT}" = "True" ] && [ "${current}" = "True" ]; then
log "Node ${NODE} is ${LABEL}."
exit 0
fi
printf "."
sleep 2
done
echo
log "Timed out waiting for ${NODE} to become ${LABEL}."
exit 1