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

<pre><code>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}')
</code></pre>

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

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

<p>Try this:</p>

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