<p>We have cluster with Istio and also Jenkins job to get &quot;stable&quot; pods, which uses this <code>kubectl</code> query:</p>
<pre class="lang-sh prettyprint-override"><code>kubectl get po -o=jsonpath=&quot;{range .items[?(@.status.containerStatuses[-1].ready==true)]}{.spec.containers[0].image}{'\\n'}{end}&quot;
</code></pre>
<pre><code>registry/my-proj/admin:2.0.0.000123
registry/my-proj/foo:2.0.0.000123
registry/my-proj/bar:2.0.0.000123
</code></pre>
<p>This query fetches pods where last container (application) is ready, because we also have Istio sidecar containers. But here is tricky thing, it looks like array is built using alphabet, so if Istio container will be last - it fetches it as ready pod, because last container is ready.</p>
<p>I've tried to use <code>go-template</code> also, but the best thing I've managed to do</p>
<pre class="lang-sh prettyprint-override"><code>kubectl get po -o go-template='{{range .items}}{{range .status.containerStatuses}}{{if eq .ready true }}{{end}}{{end}}{{.metadata.name}}{{println}}{{end}}
</code></pre>
<pre><code>registry/my-proj/admin:2.0.0.000123
registry/my-proj/admin:2.0.0.000123
registry/my-proj/foo:2.0.0.000123
registry/my-proj/foo:2.0.0.000123
registry/my-proj/bar:2.0.0.000123
</code></pre>
<p>It fetches <strong>2</strong> times pods where 2 containers are ready and only <strong>1</strong> if <strong>1</strong> container is ready.</p>
<p><strong>TL;DR;</strong></p>
<p>I am looking for ultimate query which can fetch pods where <strong>all containers are ready</strong>, thanks</p>

<p>What about something like this?</p>
<pre><code>kubectl get po -o go-template='{{range .items}}{{.metadata.name}}{{&quot;\n&quot;}}{{end}}{{range .items}}{{$pod := .}}{{range .status.containerStatuses}}{{if eq .ready false}}{{$pod.metadata.name}}{{&quot;\n&quot;}}{{end}}{{end}}{{end}}' | sort | uniq -u
</code></pre>
<p>What's happening here:</p>
<ol>
<li>We are getting all existing pod names, delimited by newline</li>
<li>Appending all pod names, which have at least one not ready container, delimited by newline</li>
<li>Sorting output alphabetically</li>
<li>Getting unique lines, while excluding duplicate ones.</li>
</ol>
<p>The trick is that <code>-u</code> key excludes all duplicate entries, so all that is left is running pods.
<code>{{ $pod := .}}</code> is used to save outer scope to print pod name inside inner loop. &quot;Get all pods&quot; is coming after &quot;get not ready pods&quot; to reduce risk of possible race condition, when some pods may have become ready by the time we did a &quot;get all pods&quot; query.</p>
<p>I do believe that something like this can easily be achieved with jsonpath too, but I don't think that you'll be able to do it with kubectl only, without the usage of <code>sort</code> and <code>uniq</code>.</p>
