What is the best way to wait for kubernetes job to be complete? I noticed a lot of suggestions to use:
kubectl wait --for=condition=complete job/myjob
>
Run the first wait condition as a subprocess and capture its PID. If the condition is met, this process will exit with an exit code of 0.
kubectl wait --for=condition=complete job/myjob &
completion_pid=$!
Do the same for the failure wait condition. The trick here is to add && exit 1
so that the subprocess returns a non-zero exit code when the job fails.
kubectl wait --for=condition=failed job/myjob && exit 1 &
failure_pid=$!
Then use the Bash builtin wait -n $PID1 $PID2
to wait for one of the conditions to succeed. The command will capture the exit code of the first process to exit:
wait -n $completion_pid $failure_pid
Finally, you can check the actual exit code of wait -n
to see whether the job failed or not:
exit_code=$?
if (( $exit_code == 0 )); then
echo "Job completed"
else
echo "Job failed with exit code ${exit_code}, exiting..."
fi
exit $exit_code
Complete example:
# wait for completion as background process - capture PID
kubectl wait --for=condition=complete job/myjob &
completion_pid=$!
# wait for failure as background process - capture PID
kubectl wait --for=condition=failed job/myjob && exit 1 &
failure_pid=$!
# capture exit code of the first subprocess to exit
wait -n $completion_pid $failure_pid
# store exit code in variable
exit_code=$?
if (( $exit_code == 0 )); then
echo "Job completed"
else
echo "Job failed with exit code ${exit_code}, exiting..."
fi
exit $exit_code