Find created POD name from JOB name using Kubernetes/OpenShift API

10/4/2019

I want given a Job name find name of created Pod by Job controller using the OpenShift or Kubernetes REST API.

Using the CLI, this is possible by doing:

$ oc describe job testapi | grep -A 4 Events
Events:
  Type    Reason            Age   From            Message
  ----    ------            ----  ----            -------
  Normal  SuccessfulCreate  6m    job-controller  Created pod: testapi-gff8c

Can we do same operation using the REST API?

I would like to get Pod names to get logs of all pods generated by a Job.

-- scoulomb
kubernetes
openshift

1 Answer

10/4/2019

The describe command is aggregating multiple results to provide its output so you will need to do a bit of extra work.

Call the events endpoint and parse the results using a tool like jq or can be pulled out in part by grep.

Heres an example assuming the job name is testapi and the namespace is myproject:

curl -H "Authorization: Bearer $(oc whoami -t)" -k https://127.0.0.1:8443/api/v1/namespaces/myproject/events?fieldSelector=involvedObject.name%3Dtestapi%2CinvolvedObject.namespace%3Dmyproject%2CinvolvedObject.kind%3DJob | grep Created

-- PhilipGough
Source: StackOverflow