'How the pod can reflect the application exit code of K8S Job

I'm running a K8S job, with the following flags:

apiVersion: batch/v1
kind: Job
metadata:
  name: my-EP
spec:
  template:
    metadata:
      labels:
        app: EP
    spec:
      restartPolicy: "Never"
      containers:
      - name: EP
        image: myImage

The Job starts, runs my script that runs some application that sends me an email and then terminates. The application returns the exit code to the bash script. when I run the command: kubectl get pods, I get the following:

NAME               READY   STATUS      RESTARTS   AGE
my-EP-94rh8   0/1     Completed   0          2m2s

Sometimes there are issues, and the network not connected or no license available. I would like that to be visible to the pod user. My question is, can I propagate the script exit code to be seen when I run the above get pods command? I.E instead of the "Completed" status, I would like to see my application exit code - 0, 1, 2, 3.... or maybe there is a way to see it in the Pods Statuses, in the describe command? currently I see:

Pods Statuses:  0 Running / 1 Succeeded / 0 Failed

Is this possible?



Solution 1:[1]

The a non-zero exit code on k8s jobs will fall into the Failed pod status. There really isn't a way for you to have the exit code shown with kubectl get pods but you could output the pod status with -ojson and then pipe it into jq looking for the exit code. Something like the following from this post might work

 kubectl get pod pod_name -c container_name-n namespace -ojson | jq .status.containerStatuses[].state.terminated.exitCode

or this, with the items[] in the json

kubectl get pods -ojson | jq .items[].status.containerStatuses[].state.terminated.exitCode

Alternatively, as u/blaimi mentioned, you can do it without jq, like this:

kubectl get pod pod_name -o jsonpath --template='{.status.containerStatuses[*].state.terminated.exitCode}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1