Skip to content

Instantly share code, notes, and snippets.

@polster
Last active November 29, 2019 11:42
Show Gist options
  • Save polster/0e5bba04a7df35d43212ac59f23ebc3c to your computer and use it in GitHub Desktop.
Save polster/0e5bba04a7df35d43212ac59f23ebc3c to your computer and use it in GitHub Desktop.
Kubernetes pod deployment status polling script for ci-pipelines
#!/bin/bash
# #############################################################################
#
# Helper script used to poll one or more pods for their running status.
#
# #############################################################################
DEFAULT_POLL_MAX_COUNT=20
DEFAULT_POLL_SLEEP_IN_SECONDS=5
function usage(){
echo "Poll for the running status of one or more pods."
echo "usage: $0 -c kubernetes context file -n name space -s service name [-h]"
}
function error {
echo -e "$1" >&2
exit 1
}
while [[ "$#" -gt 0 ]]; do case $1 in
-c|--config-file) config_file="$2"; shift;;
-n|--namespace) namespace="$2"; shift;;
-s|--service-name) service_name="$2"; shift;;
-h|--help) usage; exit 0;;
*) error "Unknown parameter passed: $1";;
esac; shift; done
[[ -z "$config_file" ]] && error "Config file for Kubernetes cluster not set, aborting!"
[[ -z "$service_name" ]] && error "Service name not set, aborting!"
[[ -z "$namespace" ]] && namespace="default" && echo "Target namespace not set, will use [default]!"
command_get_pods="kubectl get pods --namespace $namespace --kubeconfig $config_file | grep \"$service_name\""
command_notready_count="$command_get_pods | grep \"0/\" | wc -l"
command_crashed="$command_get_pods | grep \"Crash\""
command_crashed_count="$command_crashed | wc -l"
notready=$(eval $command_notready_count)
crashed=$(eval $command_crashed_count)
iteration=0
while (( $notready > 0 )) && (( $iteration < $DEFAULT_POLL_MAX_COUNT )); do
echo "#Pods not ready: $notready / Crashed: $crashed"
eval $command_crashed
sleep $DEFAULT_POLL_SLEEP_IN_SECONDS
notready=$(eval $command_notready_count)
crashed=$(eval $command_crashed_count)
iteration=$(( iteration + 1 ))
done
echo "#Pods not ready: $notready / Crashed: $crashed"
if (( $notready > 0 )); then
echo "Timeout while waiting for pod(s)"
eval $command_crashed
exit -1
else
eval $command_get_pods
echo "All pods are ready, have fun!"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment