<p>I would love to list deployments having <code>mongodb</code> environment value along with their phase status. Is there anyway to do so?</p>
<p>With this command, I get the deployments name which carries a specific environment value</p>
<pre><code>kubectl get deploy -o=custom-columns=&quot;NAME:.metadata.name,SEC:.spec.template.spec.containers[*].env[*].value&quot; | grep mongodb | cut -f 1 -d ' '
</code></pre>
<p>Output:</p>
<pre><code>app1
app2
app3
app4
</code></pre>
<p>Output I want to get:</p>
<pre><code>NAME                                     READY   UP-TO-DATE   AVAILABLE   AGE
app1                                     1/1     1            1           125d
app2                                     1/1     1            1           248d
app3                                     1/1     1            1           248d
app4                                     1/1     1            1           248d
</code></pre>
<p>Or it can be pods as well. I'd appreciate your help.</p>
<p>Thank you!</p>

<p>I had a go at a solution using <code>kubectl</code> but was unsuccessful.</p>
<p>I suspect (!?) you'll need to use additional tools to parse|process the results to get what you want. Perhaps using <a href="https://stedolan.github.io/jq/" rel="noreferrer"><code>jq</code></a>?</p>
<p>For Deployments, you can filter the results based on environment variable names using, e.g.:</p>
<pre class="lang-sh prettyprint-override"><code>FILTER=&quot;{.items[*].spec.template.spec.containers[*].env[?(@.name==\&quot;mongodb\&quot;)]}&quot;

kubectl get deployments \
--namespace=${NAMESPACE} \
--output=jsonpath=&quot;${FILTER}&quot;
</code></pre>
<p>But this only returns the filtered path (i.e. <code>items[*].spec.template.spec.containers[*].env</code>).</p>
<p>With JSONPath, you ought (!) be able to apply the filter to the item but I this isn't supported (by <code>kubectl</code>'s implementation) i.e.:</p>
<pre class="lang-sh prettyprint-override"><code>FILTER=&quot;{.items[?(@.spec.template.spec.containers[?(@.env[?(@.name==\&quot;mongodb\&quot;)])])]}&quot;
</code></pre>
<p>With <code>jq</code>, I think you'll be able to select the <code>env.name</code>, return the <code>item</code>'s status and get the raw <code>status</code> values that you need. Something like:</p>
<pre class="lang-sh prettyprint-override"><code>FILTER='
.items[]
|select(.spec.template.spec.containers[].env[].name == &quot;mongodb&quot;)
|{&quot;name&quot;:.metadata.name, &quot;ready&quot;:.status.readyReplicas}
'

kubectl get deployments \
--output=json \
| jq -r &quot;${FILTER}&quot;
</code></pre>
