injoy
injoy

Reputation: 4373

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

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?

Upvotes: 0

Views: 611

Answers (2)

Matt
Matt

Reputation: 74620

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

Upvotes: 2

user3546408
user3546408

Reputation:

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

Upvotes: 1

Related Questions