Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 25 additions & 15 deletions wait-for-deployment
Original file line number Diff line number Diff line change
Expand Up @@ -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}'
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain why you changed this from available to ready? Apologies, it's been a while since I looked into the exact logic.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's because readyReplicas is available for both deployment and stateful sets and availableReplicas only for deployments. So using readyReplicas allows us checking deployments and stateful sets the same way.

}

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 <namespace>] [-t <timeout>] <deployment>" >&2
echo "Usage: $(basename "$0") [-n <namespace>] [-t <timeout>] [deployment|statefulset]/<resource-name>" >&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
Expand All @@ -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"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you re-apply the readonly declaration after line 92 (after which we do not need to mutate the variable again AFAIU)?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed :)

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=$!
Expand All @@ -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}"
Expand All @@ -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."