How to check if kubernetes job is successful or failure using client go library

10/11/2018

There are lot of fields out there in the library but it basically talks about job finished or not, but how to check if a job is finished and successful or job is finished and failure

if con.Type == v1.JobComplete && con.Status == corev1.ConditionTrue && job.Status.Succeeded > 0 {
    fmt.Printf("Job: %v Completed Successfully: %v\n", name, con)
    break
} else if con.Type == v1.JobFailed && con.Status == corev1.ConditionTrue {
    if job.Status.Active == 0 && job.Status.Succeeded == 0 {
        fmt.Printf("Job: %v Failed: %v\n", name, con)
        break
    }
}

This is how I am checking now, I am not completely sure this is correct

-- Coding Ninja
client-go
go
kubernetes

1 Answer

10/24/2018

You can use just the Active, Succeeded and Failed variables to know the status of the job.

Example:

    batchClient := kubernetesapi.BatchV1()
    jobClient := batchClient.Jobs(NameSpace)


    job, _ := jobClient.Get(jobName, metav1.GetOptions{})

    if job.Status.Active > 0 {
    return "Job is still running"

    } else {
      if job.Status.Succeeded > 0 {
       return "Job Successful"
       } 
       return "Job failed"
    }

This should take care of any number of retries to the job if you are using backOffLimit as more than 1.

-- Bharath
Source: StackOverflow