Luca Prete | 1b823d6 | 2018-12-13 17:33:47 -0800 | [diff] [blame] | 1 | #!/usr/bin/env bash |
| 2 | |
Joey Armstrong | 0476e91 | 2024-02-09 16:00:26 -0500 | [diff] [blame] | 3 | # Copyright 2018-2024 Open Networking Foundation (ONF) and the ONF Contributors |
Luca Prete | 1b823d6 | 2018-12-13 17:33:47 -0800 | [diff] [blame] | 4 | # |
| 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 | |
| 21 | set -eu -o pipefail |
| 22 | fail_wfj=0 |
| 23 | |
| 24 | # Set these to configure maximum timeout, and interval for checks |
| 25 | JOBS_TIMEOUT=${JOBS_TIMEOUT:-600} |
| 26 | CHECK_INTERVAL=${CHECK_INTERVAL:-5} |
| 27 | KUBECTL_ARGS=${KUBECTL_ARGS:-} |
| 28 | |
| 29 | # calculate timeout time |
| 30 | START_TIME=$(date +%s) |
| 31 | END_TIME=$((START_TIME + JOBS_TIMEOUT)) |
| 32 | |
| 33 | echo "wait_for_jobs.sh - Waiting up to ${JOBS_TIMEOUT} seconds for all Kubernetes jobs to complete" |
| 34 | echo "Number printed is number of currently active jobs" |
| 35 | |
| 36 | prev_job_count=0 |
| 37 | |
| 38 | while true; do |
Joey Armstrong | 0476e91 | 2024-02-09 16:00:26 -0500 | [diff] [blame] | 39 | NOW=$(date +%s) |
Luca Prete | 1b823d6 | 2018-12-13 17:33:47 -0800 | [diff] [blame] | 40 | |
Joey Armstrong | 0476e91 | 2024-02-09 16:00:26 -0500 | [diff] [blame] | 41 | # 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 Prete | 1b823d6 | 2018-12-13 17:33:47 -0800 | [diff] [blame] | 48 | |
Joey Armstrong | 0476e91 | 2024-02-09 16:00:26 -0500 | [diff] [blame] | 49 | # 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 Prete | 1b823d6 | 2018-12-13 17:33:47 -0800 | [diff] [blame] | 53 | |
Joey Armstrong | 0476e91 | 2024-02-09 16:00:26 -0500 | [diff] [blame] | 54 | # 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 Prete | 1b823d6 | 2018-12-13 17:33:47 -0800 | [diff] [blame] | 57 | |
Joey Armstrong | 0476e91 | 2024-02-09 16:00:26 -0500 | [diff] [blame] | 58 | # 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 Prete | 1b823d6 | 2018-12-13 17:33:47 -0800 | [diff] [blame] | 66 | |
Joey Armstrong | 0476e91 | 2024-02-09 16:00:26 -0500 | [diff] [blame] | 67 | # 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 Prete | 1b823d6 | 2018-12-13 17:33:47 -0800 | [diff] [blame] | 75 | |
Joey Armstrong | 0476e91 | 2024-02-09 16:00:26 -0500 | [diff] [blame] | 76 | # print number of remaining jobs every $CHECK_INTERVAL |
| 77 | echo -n "$active_job_count " |
| 78 | sleep "$CHECK_INTERVAL" |
Luca Prete | 1b823d6 | 2018-12-13 17:33:47 -0800 | [diff] [blame] | 79 | done |
| 80 | |
| 81 | echo "" |
| 82 | echo "Job Status - Name | Start Time | Completion Time" |
| 83 | # shellcheck disable=SC2086 |
| 84 | kubectl get jobs $KUBECTL_ARGS -o=jsonpath='{range .items[*]}{.metadata.name}{"\t| "}{.status.startTime}{" | "}{.status.completionTime}{"\n"}{end}' |
| 85 | |
| 86 | exit ${fail_wfj} |