From 0e79e00d56dd87377480fdafe402a1e602d78319 Mon Sep 17 00:00:00 2001 From: Christian Groth Date: Fri, 22 Feb 2019 14:18:46 +0100 Subject: [PATCH 1/3] enhanced script to be able to support waiting for stateful sets --- wait-for-deployment | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/wait-for-deployment b/wait-for-deployment index 8b606a8..44ac8fb 100755 --- a/wait-for-deployment +++ b/wait-for-deployment @@ -45,20 +45,21 @@ 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 "${type}" "${deployment}" -o "jsonpath=${jsonpath}" } display_usage_and_exit() { - echo "Usage: $(basename "$0") [-n ] [-t ] " >&2 + echo "Usage: $(basename "$0") [-n ] [-t ] " >&2 echo "Arguments:" >&2 - echo "deployment REQUIRED: The name of the deployment the script should wait on" >&2 + echo "deployment|statefulset REQUIRED: the type of kubernetes resource to be waited for: deployment or statefulset, other types are not supported" >&2 + echo "resource-name REQUIRED: The name of the deployment the script should wait on" >&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 +78,17 @@ do done shift $((OPTIND-1)) -if [ "$#" -ne 1 ] ; then +if [ "$#" -ne 2 ] ; then display_usage_and_exit fi -readonly deployment="$1" +readonly type="$1" +readonly deployment="$2" if [[ ${timeout} -le 0 ]]; then display_usage_and_exit fi -echo "Waiting for deployment of ${deployment} in namespace ${namespace} with a timeout ${timeout} seconds" +echo "Waiting for ${type} ${deployment} in namespace ${namespace} with a timeout ${timeout} seconds" monitor_timeout $$ & readonly timeout_monitor_pid=$! @@ -96,7 +98,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 ${type} ${deployment}: ${generation}" while [[ ${current_generation} -lt ${generation} ]]; do sleep .5 echo "Currently observed generation: ${current_generation}" @@ -109,14 +111,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 "Deployment/update of ${type} ${deployment} successful. All ${ready_replicas} replicas are ready." From 26404d2b312cfbe69a452cc1e5e1af8700940223 Mon Sep 17 00:00:00 2001 From: Christian Groth Date: Mon, 8 Apr 2019 09:13:13 +0200 Subject: [PATCH 2/3] refactored back to only one commandline parameter including type and resource name --- wait-for-deployment | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/wait-for-deployment b/wait-for-deployment index 44ac8fb..079a2b9 100755 --- a/wait-for-deployment +++ b/wait-for-deployment @@ -52,14 +52,13 @@ get_ready_replicas() { get_deployment_jsonpath() { local -r jsonpath="$1" - kubectl --namespace "${namespace}" get "${type}" "${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|statefulset REQUIRED: the type of kubernetes resource to be waited for: deployment or statefulset, other types are not supported" >&2 - echo "resource-name 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 the supported types are 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 @@ -78,17 +77,25 @@ do done shift $((OPTIND-1)) -if [ "$#" -ne 2 ] ; then +if [ "$#" -ne 1 ] ; then display_usage_and_exit fi -readonly type="$1" -readonly deployment="$2" +deployment="$1" if [[ ${timeout} -le 0 ]]; then display_usage_and_exit fi -echo "Waiting for ${type} ${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 + +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=$! @@ -98,7 +105,7 @@ trap 'kill -- -${timeout_monitor_pid}' EXIT #Stop timeout monitor generation=$(get_generation); readonly generation current_generation=$(get_observed_generation) -echo "Expected generation for ${type} ${deployment}: ${generation}" +echo "Expected generation for ${deployment}: ${generation}" while [[ ${current_generation} -lt ${generation} ]]; do sleep .5 echo "Currently observed generation: ${current_generation}" @@ -121,4 +128,4 @@ while [[ ${updated_replicas} -lt ${specified_replicas} || ${current_replicas} -g ready_replicas=$(get_ready_replicas) done -echo "Deployment/update of ${type} ${deployment} successful. All ${ready_replicas} replicas are ready." +echo "Update of ${deployment} successful. All ${ready_replicas} replicas are ready." From 7c59ca30d2e69f58c9a1cadf3043fcda32281a70 Mon Sep 17 00:00:00 2001 From: Christian Groth Date: Thu, 11 Apr 2019 07:32:17 +0200 Subject: [PATCH 3/3] minor fixes after review --- wait-for-deployment | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/wait-for-deployment b/wait-for-deployment index 079a2b9..ca4c800 100755 --- a/wait-for-deployment +++ b/wait-for-deployment @@ -58,7 +58,7 @@ get_deployment_jsonpath() { display_usage_and_exit() { echo "Usage: $(basename "$0") [-n ] [-t ] [deployment|statefulset]/" >&2 echo "Arguments:" >&2 - echo "[deployment/statefulset]/resource-name REQUIRED: The type and name of the resource the script should wait on. Currently the supported types are deployment and statefulset are supported." >&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 @@ -90,8 +90,9 @@ fi if ! [[ $deployment =~ .*/.* ]]; then deployment="deployment/$deployment" fi +readonly deployment="$deployment" -if [[ ! $deployment =~ deployment/.* ]] && [[ ! $deployment =~ statefulset/.* ]]; then +if [[ ! $deployment =~ deployment\/.* ]] && [[ ! $deployment =~ statefulset\/.* ]]; then display_usage_and_exit fi