How to know if Kubernetes job is done?

4/16/2019

I use Python kubernetes-client, and want to wait if the job is done:

api_instance.create_namespaced_job("default", body, pretty=True)

This call just makes a submit job, it will return the response even though the job is still running. How can I wait for the job to finish?

-- Võ Trường Duy
kubernetes

2 Answers

4/16/2019

The only way I managed to do this is calling in the loop:

api_instance.read_namespaced_job_status()

...and check the result status.

You can reach the same by calling kubectl wait but it's not python. You can clone kubectl sources (in Go) and find how the hell they do it there.

-- smrt28
Source: StackOverflow

4/16/2019

I found the solution. You can recognize the job is complete by watching the jobs and observing the events:

from kubernetes import client, config, watch

config.load_kube_config()
api_client = client.BatchV1Api()
print("INFO:    Waiting for event to come up...")
w = watch.Watch()
for event in w.stream(api_client.list_job_for_all_namespaces):
    o = event['object']
    print(o)
    if (o.status.... = "Complete"): ....
-- smrt28
Source: StackOverflow