Knative: Operator’s Handbook

Script to wait until deployment is ready

If you need to automate deploying a Revision and waiting until Revision readiness, you would need a tool.

If you use the official Knative command-line tool (kn), by default it wait 600 seconds for deployment to be ready (configurable with --wait-timeout). However, kn cannot deploy YAML manifests.

If you use kubectl, you can use a bash script like this to achieve the same result:

#!/usr/bin/env bash
set -euo pipefail

##
## USAGE:
##   ./wait-ready.sh SERVICE_FILE [TIMEOUT default:600]
##

pid="$$"
file="${1:?SERVICE_FILE argument not specified}" || usage
wait_secs="${2:-600}"

status() {
    kubectl get revision "${1:?REVISION empty}" \
        --output=jsonpath="{.status.conditions[?(@.type=='Ready')].status}"
}

reason() {
    kubectl get revision "${1:?REVISION empty}" \
        --output=jsonpath="{.status.conditions[?(@.type=='Ready')].reason}"
}

wait_ready() {
    local rev
    rev="${1:?REVISION empty}"
    while :; do
        cur="$(status "${rev}")"
        echo >&2 "Ready: ${cur}"
        if [[ "$cur" == True ]]; then
            exit 0
        elif [[ "$cur" == False ]]; then
            echo >&2 "Reason: $(reason "${rev}")"
            exit 1
        fi
        sleep 2
    done
}

start_timer() {
    sleep "${1:?SECS empty}"
    echo "not ready after ${1}s."
    kill -KILL -- "$pid" &>/dev/null
}

rev_name="$(kubectl get -f "${file}" \
    --output=jsonpath='{.status.latestCreatedRevisionName}')"

start_timer "$wait_secs" &
timer_pid="$!"
trap "kill -KILL -- $timer_pid" EXIT

wait_ready "${rev_name}"