blob: dbb35dab6efd1c58bacf7f494d8b7dae9601c058 [file] [log] [blame]
Luca Prete1b823d62018-12-13 17:33:47 -08001#!/usr/bin/env bash
2
Joey Armstrong0476e912024-02-09 16:00:26 -05003# Copyright 2018-2024 Open Networking Foundation (ONF) and the ONF Contributors
Luca Prete1b823d62018-12-13 17:33:47 -08004#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17# wait_for_jobs.sh
18# waits for all kubernetes jobs to complete before exiting
19# inspired by similar scripts in Kolla-Kubernetes and Openstack Helm
20
21set -eu -o pipefail
22fail_wfj=0
23
24# Set these to configure maximum timeout, and interval for checks
25JOBS_TIMEOUT=${JOBS_TIMEOUT:-600}
26CHECK_INTERVAL=${CHECK_INTERVAL:-5}
27KUBECTL_ARGS=${KUBECTL_ARGS:-}
28
29# calculate timeout time
30START_TIME=$(date +%s)
31END_TIME=$((START_TIME + JOBS_TIMEOUT))
32
33echo "wait_for_jobs.sh - Waiting up to ${JOBS_TIMEOUT} seconds for all Kubernetes jobs to complete"
34echo "Number printed is number of currently active jobs"
35
36prev_job_count=0
37
38while true; do
Joey Armstrong0476e912024-02-09 16:00:26 -050039 NOW=$(date +%s)
Luca Prete1b823d62018-12-13 17:33:47 -080040
Joey Armstrong0476e912024-02-09 16:00:26 -050041 # handle timeout without completion
42 if [ "$NOW" -gt "$END_TIME" ]
43 then
44 echo "Jobs didn't complete before timeout of ${JOBS_TIMEOUT} seconds"
45 fail_wfj=1
46 break
47 fi
Luca Prete1b823d62018-12-13 17:33:47 -080048
Joey Armstrong0476e912024-02-09 16:00:26 -050049 # get list of active jobs, and count of them
50 # jsonpath is picky about string vs comparison quoting, so have to have:
51 # shellcheck disable=SC2026,SC2086
52 active_jobs=$(kubectl get jobs $KUBECTL_ARGS -o=jsonpath='{range .items[?(@.status.active=='1')]}{.metadata.name}{"\n"}{end}')
Luca Prete1b823d62018-12-13 17:33:47 -080053
Joey Armstrong0476e912024-02-09 16:00:26 -050054 # this always is 1 or more, as echo leaves a newline in the output which wc
55 # counts as a line
56 active_job_count=$(echo -n "${active_jobs}" | wc -l)
Luca Prete1b823d62018-12-13 17:33:47 -080057
Joey Armstrong0476e912024-02-09 16:00:26 -050058 # if no jobs active, print runtime and break
59 if [ -z "$active_jobs" ]
60 then
61 runtime=$((NOW - START_TIME))
62 echo ""
63 echo "All jobs completed in $runtime seconds"
64 break
65 fi
Luca Prete1b823d62018-12-13 17:33:47 -080066
Joey Armstrong0476e912024-02-09 16:00:26 -050067 # deal with changes in number of jobs
68 if [ "$active_job_count" -ne "$prev_job_count" ]
69 then
70 echo ""
71 echo "Number of active jobs changed - current jobs:"
72 echo "$active_jobs"
73 fi
74 prev_job_count=$active_job_count
Luca Prete1b823d62018-12-13 17:33:47 -080075
Joey Armstrong0476e912024-02-09 16:00:26 -050076 # print number of remaining jobs every $CHECK_INTERVAL
77 echo -n "$active_job_count "
78 sleep "$CHECK_INTERVAL"
Luca Prete1b823d62018-12-13 17:33:47 -080079done
80
81echo ""
82echo "Job Status - Name | Start Time | Completion Time"
83# shellcheck disable=SC2086
84kubectl get jobs $KUBECTL_ARGS -o=jsonpath='{range .items[*]}{.metadata.name}{"\t| "}{.status.startTime}{" | "}{.status.completionTime}{"\n"}{end}'
85
86exit ${fail_wfj}