Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enhanced script to be able to support waiting for stateful sets #15

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
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
32 changes: 17 additions & 15 deletions wait-for-deployment
Original file line number Diff line number Diff line change
Expand Up @@ -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}'
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 "${type}" "${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
Copy link
Owner

Choose a reason for hiding this comment

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

Instead of requiring another parameter that would break existing users, how about expecting callers to define the type as a slash-delimited prefix to the resource name like

[<resource type>/]<resource name>

where <resource type> is optional and defaults to deployment. This would also be in line with what you see in k8s land / kubectl.

Copy link
Author

Choose a reason for hiding this comment

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

I like this idea. Unfortunately I don't have time to implement and test it in the moment, it will take a while.

Copy link
Owner

Choose a reason for hiding this comment

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

No hurry on my end (how could I expect that anyway ;) ), take your time!

Copy link
Author

Choose a reason for hiding this comment

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

I've pushed a new commit, changing the script back to using one commandline argument only. This should do it :)

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
Expand All @@ -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"
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 :)

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