kubernetesopenshiftjsonpath

Get name of most recently created pod


In OpenShift, is there a more elegant way of obtaining the name of the most recently created pod in application my_app than this one?

name=$(oc get pods -l app=my_app -o=jsonpath='{range.items[*]}{.status.startTime}{"\t"}{.metadata.name}{"\n"}{end}' | sort -r | head -1 | awk '{print $2}')

The idea is to sort by .status.startTime and to output one .metadata.name. So far, I have not been successful in using oc get with both options --sort-by and -o jsonpath at the same time, so I have fallen back to Unix pipes in this version.

I am using OpenShift v3.9. I am also tagging this question for Kubernetes because it presumably applies to kubectl (instead of oc) in an analogous manner (without the -l app=my_app).


Solution

  • Try this:

    kubectl get pods --sort-by=.metadata.creationTimestamp -o jsonpath="{.items[0].metadata.name}"