diff --git a/wait-for-deployment b/wait-for-deployment index 8b606a8..ca4c800 100755 --- a/wait-for-deployment +++ b/wait-for-deployment @@ -45,20 +45,20 @@ get_updated_replicas() { get_deployment_jsonpath '{.status.updatedReplicas}' } -get_available_replicas() { - get_deployment_jsonpath '{.status.availableReplicas}' +get_ready_replicas() { + get_deployment_jsonpath '{.status.readyReplicas}' } get_deployment_jsonpath() { local -r jsonpath="$1" - kubectl --namespace "${namespace}" get deployment "${deployment}" -o "jsonpath=${jsonpath}" + kubectl --namespace "${namespace}" get "${deployment}" -o "jsonpath=${jsonpath}" } display_usage_and_exit() { - echo "Usage: $(basename "$0") [-n ] [-t ] " >&2 + echo "Usage: $(basename "$0") [-n ] [-t ] [deployment|statefulset]/" >&2 echo "Arguments:" >&2 - echo "deployment REQUIRED: The name of the deployment the script should wait on" >&2 + echo "[deployment/statefulset]/resource-name REQUIRED: The type and name of the resource the script should wait on. Currently deployment and statefulset are supported." >&2 echo "-n OPTIONAL: The namespace the deployment exists in, defaults is the 'default' namespace" >&2 echo "-t OPTIONAL: How long to wait for the deployment to be available, defaults to ${DEFAULT_TIMEOUT} seconds, must be greater than 0" >&2 exit 1 @@ -77,16 +77,26 @@ do done shift $((OPTIND-1)) -if [ "$#" -ne 1 ] ; then +if [ "$#" -ne 1 ] ; then display_usage_and_exit fi -readonly deployment="$1" +deployment="$1" if [[ ${timeout} -le 0 ]]; then display_usage_and_exit fi -echo "Waiting for deployment of ${deployment} in namespace ${namespace} with a timeout ${timeout} seconds" +# add deployment as type if no type specified (backwards compatibility) +if ! [[ $deployment =~ .*/.* ]]; then + deployment="deployment/$deployment" +fi +readonly deployment="$deployment" + +if [[ ! $deployment =~ deployment\/.* ]] && [[ ! $deployment =~ statefulset\/.* ]]; then + display_usage_and_exit +fi + +echo "Waiting for ${deployment} in namespace ${namespace} with a timeout ${timeout} seconds" monitor_timeout $$ & readonly timeout_monitor_pid=$! @@ -96,7 +106,7 @@ trap 'kill -- -${timeout_monitor_pid}' EXIT #Stop timeout monitor generation=$(get_generation); readonly generation current_generation=$(get_observed_generation) -echo "Expected generation for deployment ${deployment}: ${generation}" +echo "Expected generation for ${deployment}: ${generation}" while [[ ${current_generation} -lt ${generation} ]]; do sleep .5 echo "Currently observed generation: ${current_generation}" @@ -109,14 +119,14 @@ echo "Specified replicas: ${specified_replicas}" current_replicas=$(get_replicas) updated_replicas=$(get_updated_replicas) -available_replicas=$(get_available_replicas) +ready_replicas=$(get_ready_replicas) -while [[ ${updated_replicas} -lt ${specified_replicas} || ${current_replicas} -gt ${updated_replicas} || ${available_replicas} -lt ${updated_replicas} ]]; do - sleep .5 - echo "current/updated/available replicas: ${current_replicas}/${updated_replicas}/${available_replicas}, waiting" +while [[ ${updated_replicas} -lt ${specified_replicas} || ${current_replicas} -gt ${updated_replicas} || ${ready_replicas} -lt ${updated_replicas} ]]; do + sleep 5 + echo "${deployment} current/updated/ready replicas: ${current_replicas}/${updated_replicas}/${ready_replicas}, waiting" current_replicas=$(get_replicas) updated_replicas=$(get_updated_replicas) - available_replicas=$(get_available_replicas) + ready_replicas=$(get_ready_replicas) done -echo "Deployment ${deployment} successful. All ${available_replicas} replicas are ready." +echo "Update of ${deployment} successful. All ${ready_replicas} replicas are ready."