Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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).

like image 937
rookie099 Avatar asked Aug 13 '18 19:08

rookie099


1 Answers

Try this:

kubectl get pods --sort-by=.metadata.creationTimestamp -o jsonpath="{.items[0].metadata.name}"
like image 190
Alexey Yakunin Avatar answered Sep 21 '22 13:09

Alexey Yakunin