19

Is there any resource out there that gives an overview of all the possible status conditions a kubernetes job can have?

I'm wondering because I would like to check, when I run a job if it is already running and if so, exit the new job.

I came across until kubectl get jobs myjob -o jsonpath='{.status.conditions[?(@.type=="Complete")].status}' | grep True ; do sleep 1 ; done quite a few times but I want to know if it is running, not if it is already complete. Would prefer not to wait (ha) for kubectl 1.11 wait functionality

wesvb
  • 309
  • 1
  • 2
  • 10

5 Answers5

18

The kubernetes API docs for JobCondition imply that the only type values are “Complete” and “Failed”, and that they may have a ”True” or ”False” status.

In addition to the job status conditions array, you may also find it informative to look at the job status active count, and the startTime and completionTime if you’re just interested in whether it’s finished.

Brad Koch
  • 19,267
  • 19
  • 110
  • 137
David Maze
  • 130,717
  • 29
  • 175
  • 215
  • 4
    What does status "True" or "False" mean? `completionTime` time is not populated if the job finished unsuccessfully, so it is not very useful for determining if the job has finished. – Andrew Savinykh Jan 02 '20 at 20:04
4
kubectl get jobs <myjob> --namespace <mynamespae> -o jsonpath='{.status.conditions[?(@.type=="Succeeded")].status}'
Zhang Chen
  • 89
  • 3
3

I believe this can help to get all job statuses for a given selector and after the result can be parsed in bash or CI to wait until some specific statuses:

kubectl get job -n myspace --selector=appName=myapplication -o json | jq -r '.items[] | .metadata.name + ":" + (.status.conditions[] | select(.status == "True") .type + ":" + .status)'
myapplication-job-1558097758:Complete:True
myapplication-job-1558101228:Failed:True

Also here I found another example for bash:

Wait for kubernetes job to complete on either failure/success using command line

By @ruazn2:

until [[ $SECONDS -gt $end ]] || [[ $(kubectl get jobs $job_name -o jsonpath='{.status.conditions[?(@.type=="Failed")].status}') == "True" ]] || [[ $(kubectl get jobs $job_name -o jsonpath='{.status.conditions[?(@.type=="Complete")].status}') == "True" ]]; do
kivagant
  • 1,849
  • 2
  • 24
  • 33
1

This one has been successfully tested and returns "True" or "False":

kubectl get jobs <myjob> --namespace <mynamespace> -o jsonpath='{.status.conditions[?(@.type=="Complete")].status}'
Fabrice Jammes
  • 2,275
  • 1
  • 26
  • 39
  • 1
    almost exactly what I wanted, all I care is the status, so `-o=jsonpath='{.status.conditions[].type}'`. thank you for this – Eugene Oct 21 '21 at 02:41
0

I don't know if anyone else had the same trouble as me... Here's something you can pipe through xargs and operate on. This outputs all failed jobs:

kubectl get jobs \
    -n [your namespace] \
    -o jsonpath='{range .items[?(.status.failed == 1)]} {.metadata.name}{"\n"} {end}'

And an explanation...

  • -n is the namespace you're searching
  • -o specifies the format of your output
  • range [iterable] specifies that you want to loop over something. It goes in curly braces {range [iterable]} and must have an {end} at the end. Range lets you operate on each item over which you're iterating, so between range and end you're working with the individual items.
  • .items[*] would return all items in the json output given by kubectl get job [job name] -n [namespace] -o json, and wow, what a handy reference that is. This is where you'll find all the paths to items in the job description.
  • ?() let's you FILTER. This is the really powerful bit. If something matches the condition then it's included in the result
  • ?(.status.failed == 1) specifies that we want to include all results that have a status of "failed"... that's those results that have json path .status.failed set to 1.
  • .metadata.name specifies that we want to output the name of the job we're matching.
  • "\n" says to add a newline between matched jobs.

Here's a reference if you want to learn more: https://kubernetes.io/docs/reference/kubectl/jsonpath/

KeatsKelleher
  • 10,015
  • 4
  • 45
  • 52