Monitoring a kubernetes job
<kube master>/apis/batch/v1/namespaces/default/jobs
endpoint lists status of the jobs. I have parsed this json and retrieved the name of the latest running job that starts with "deploy...".
Then we can hit
<kube master>/apis/batch/v1/namespaces/default/jobs/<job name retrieved above>
And monitor the status field value which is as below when the job succeeds
"status": { "conditions": [ { "type": "Complete", "status": "True", "lastProbeTime": "2016-09-22T13:59:03Z", "lastTransitionTime": "2016-09-22T13:59:03Z" } ], "startTime": "2016-09-22T13:56:42Z", "completionTime": "2016-09-22T13:59:03Z", "succeeded": 1 }
So we keep polling this endpoint till it completes. Hope this helps someone.
I found that the JobStatus does not get updated while polling using job.getStatus()Even if the status changes while checking from the command prompt using kubectl.
To get around this, I reload the job handler:
client.extensions().jobs() .inNamespace(myJob.getMetadata().getNamespace()) .withName(myJob.getMetadata().getName()) .get();
My loop to check the job status looks like this:
KubernetesClient client = new DefaultKubernetesClient(config); Job myJob = client.extensions().jobs() .load(new FileInputStream("/path/x.yaml")) .create(); boolean jobActive = true; while(jobActive){ myJob = client.extensions().jobs() .inNamespace(myJob.getMetadata().getNamespace()) .withName(myJob.getMetadata().getName()) .get(); JobStatus myJobStatus = myJob.getStatus(); System.out.println("=================="); System.out.println(myJobStatus.toString()); if(myJob.getStatus().getActive()==null){ jobActive = false; } else { System.out.println(myJob.getStatus().getActive()); System.out.println("Sleeping for a minute before polling again!!"); Thread.sleep(60000); } } System.out.println(myJob.getStatus().toString());
Hope this helps