How to check a job is completed before it's been created using kubectl?

4/25/2020

I can use kubectl wait --for=condition=complete --timeout=<some time> job/<job-name> to wait for a job in completed state. However, if the job has not yet been created (sometimes, it's due to k8s takes some time to schedule the job), kubectl will exit with error immediately.

Is there a way to wait for the job been created and then transit into completed state? What's the most common way to do this in industry?

-- injoy
kubectl
kubernetes

2 Answers

4/25/2020
kubectl wait --for=condition=created --timeout=<some time> job/<job-name>

Edit: If I'm not mistaken, kubectl wait is still experimental anyway - but the condition you name should match whatever you're expecting in a standard status output.

Second Edit: wrong character update

-- Bryan Heden
Source: StackOverflow

4/25/2020

kubectl wait does not include the functionality to wait on a non existent resource yet.

For anything complex try and use a kube API client. Run a watch on a resource group and you receive a stream of events for it, and continue on when the event criteria has been met.

If you are stuck in shell land, kubectl doesn't seem to respect SIGPIPE signals when when handling the output of a kubectl get x --watch so maybe a simple loop...

timeout=$(( $(date +%s) + 60 )) 
while ! kubectl get job whatever 2>/dev/null; do
  [ $(date +%s) -gt $timeout ] && exit 1
  sleep 5
done
-- Matt
Source: StackOverflow