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
).
Try this:
kubectl get pods --sort-by=.metadata.creationTimestamp -o jsonpath="{.items[0].metadata.name}"
On Kubernetes front, kubectl get po --sort-by=.status.startTime
is supposed to work, except in K8s 1.7: it was fixed for 1.8.
"Kubernetes sort pods by age" also mentions
kubectl get pods --sort-by=.metadata.creationTimestamp
Since Openshift 3.9 (March 2018) is fairly recent, those kubectl
commands should work even if the oc
one is not fully compatible.