<p>In Kubernetes (using a filter) we can limit the output from k8s on interesting resource and I wonder whether it is possible to list top 5 latest created pods using only filter.</p>
<p>The current example mostly list all pods and pipe to another unix (<code>head</code> command)</p>
<pre><code>kubectl get pods --sort-by=.metadata.creationTimestamp | head -n 5
</code></pre>
<p>But I guess it takes quite long time to get all first from server side, then lists first 5.</p>
<p>Can I use special filter will make it more efficiency?</p>

<p>There are several aspects which prevent you to solve this question using only <code>filter</code>:</p>
<ol>
<li><p><code>Filter</code> itself:</p>
<blockquote>
<p>Field selectors are essentially resource filters. By default, no
selectors/filters are applied, meaning that all resources of the
specified type are selected. This makes the kubectl queries kubectl
get pods and kubectl get pods --field-selector &quot;&quot; equivalent.</p>
</blockquote>
<p><a href="https://kubernetes.io/docs/concepts/overview/working-with-objects/field-selectors/" rel="nofollow noreferrer">Reference - Field selectors</a>.</p>
<p>And its limitations on <a href="https://kubernetes.io/docs/concepts/overview/working-with-objects/field-selectors/#supported-operators" rel="nofollow noreferrer">supported operations</a>:</p>
<blockquote>
<p>You can use the =, ==, and != operators with field selectors (= and ==
mean the same thing). This kubectl command, for example, selects all
Kubernetes Services that aren't in the default namespace:</p>
<p>kubectl get services  --all-namespaces --field-selector
metadata.namespace!=default</p>
</blockquote>
<p>It can't compare value like <code>&gt;</code> or <code>&lt;</code> even if a number of total pods would have been available.</p>
</li>
<li><p>I compared requests with <code>--v=8</code> to see which exact response is performed when <code>kubectl get pods</code> is executed with different options:</p>
<pre><code>$ kubectl get pods -A --sort-by=.metadata.creationTimestamp --v=8
I1007 12:40:45.296727     562 round_trippers.go:432] GET https://10.186.0.2:6443/api/v1/pods?includeObject=Object
</code></pre>
<p>and</p>
<pre><code>$ kubectl get pods -A --field-selector=metadata.namespace=kube-system --v=8
I1007 12:41:42.873609    1067 round_trippers.go:432] GET https://10.186.0.2:6443/api/v1/pods?fieldSelector=metadata.namespace%3Dkube-system&amp;limit=500
</code></pre>
<p>The difference when <code>--field-selector</code> <code>kubectl</code> used is it adds <code>&amp;limit=500</code> to the request, so it's a point where some data inconsistency can appear if using <code>kubectl</code> from terminal. While <code>--sort-by</code> get all data from <code>api server</code> to the client.</p>
</li>
<li><p>Using <code>-o jsonpath</code> works the same way as regular <code>kubectl get pods</code> request and has again the limit of 500 results which may lead to data inconsistency:</p>
<pre><code>$ kubectl get pods -A --v=7 -o jsonpath='{range.items[*]}{.metadata.creationTimestamp}{&quot;\t&quot;}{.metadata.name}{&quot;\n&quot;}{end}'
I1007 12:52:25.505109    6943 round_trippers.go:432] GET https://10.186.0.2:6443/api/v1/pods?limit=500
</code></pre>
<p>Also even developers using another linux commands (<code>jq</code>, <code>grep</code>, <code>sort</code>, <code>|</code>) to work with initial results which are got from kubernetes api. See <a href="https://kubernetes.io/docs/reference/kubectl/cheatsheet/#viewing-finding-resources" rel="nofollow noreferrer">examples of Viewing, finding resources</a>.</p>
</li>
</ol>
<p>So to confirm <a href="https://stackoverflow.com/questions/69408472/how-to-get-top-n-latest-created-pods-inside-kubernetes-based-on-filter#comment122686547_69408472">@P...'s comment</a>, you will need to get data first to the client and only then work on it.</p>
