Skip to content

Instantly share code, notes, and snippets.

@wknapik
Last active April 23, 2020 06:13
Show Gist options
  • Save wknapik/315f52a20a2af43da35be5412625794a to your computer and use it in GitHub Desktop.
Save wknapik/315f52a20a2af43da35be5412625794a to your computer and use it in GitHub Desktop.
Wait for kubernetes deployments to complete
Wait for kubernetes deployments to complete.
Two variants - s1.sh and s2.sh.
Usage:
./sX.sh
./sX.sh some-namespace
Both variants make just one pass through the list of deployments, so if another
deployment is started while the script is running, the result may not be
correct.
Variant #1 may behave incorrectly if one of the pids gets reused. E.g. if the
first deployment check instantly fails and the next check gets the same pid and
succeeds, the script will report success. Or if the pid is reused by a process
that's not a child of the current shell, wait will behave as if the deployment
check failed. This is quite unlikely to happen, but it is possible.
Variant #2 is slower.
The return code is 0 on success, or whatever `timeout ... kubectl rollout status`
returned for a timed out/failed deployment. Variant #2 could also return 1 on a
timeout that occured between calls to kubectl.
#!/usr/bin/env bash
set -eEo pipefail
shopt -s inherit_errexit >/dev/null 2>&1 || true
# Wait for all deployments to namespace $1 (or "default"), time out after 60s.
wait_for_deployments() {
local -r ns="${1:-default}" timeout=60
local -a deployments pids
local pid
read -ra deployments <<<"$(kubectl get deployment --namespace "$ns" -o jsonpath='{.items[*].metadata.name}')"
for deployment in "${deployments[@]}"; do
timeout "$timeout" kubectl rollout status -w "deployment/$deployment" --namespace "$ns" >/dev/null &
pids+=("$!")
done
for pid in "${pids[@]}"; do
wait "$pid" || return "$?"
done
}
wait_for_deployments "$1"
#!/usr/bin/env bash
set -eEo pipefail
shopt -s inherit_errexit >/dev/null 2>&1 || true
# Wait for all deployments to namespace $1 (or "default"), time out after 60s.
wait_for_deployments() {
local -r ns="${1:-default}" timeout=60
local -a deployments
local start_time countdown
read -ra deployments <<<"$(kubectl get deployment --namespace "$ns" -o jsonpath='{.items[*].metadata.name}')"
start_time="$SECONDS"
for deployment in "${deployments[@]}"; do
countdown=$((timeout - SECONDS + start_time))
if [[ "$countdown" -gt 0 ]]; then
timeout "$countdown" kubectl rollout status -w "deployment/$deployment" --namespace "$ns" >/dev/null || return "$?"
else
return 1
fi
done
}
wait_for_deployments "$1"
@sacherus
Copy link

sacherus commented Jan 9, 2019

I came up with very something similar:

...
for yaml in ${directory_patched}/*rollouts*/*; do
    timeout ${ROLLOUT_WAIT_TIME} kubectl rollout status -f ${yaml} --namespace ${PROJECT_NAME}
done

For my use case is pretty enough... But if you want to wait for all of them then you should use countdown. Yeah!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment