Search code examples
kuberneteskubectl

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?


Solution

  • 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