<p>Sometimes, I want to explore all deployments/daemonsets which mount specific configmap/secret.</p>
<p>Is there any way can achieve this by <code>kubectl</code>?</p>

Stephen Chen
<p>You need to have <code>jq</code> to do such a complex queries.
Here you go:</p>
<pre><code>kubectl get -o json deploy,daemonset | jq '[.items[] | . as $parent | .spec.template.spec.volumes[]? | select(.configMap != null) | {kind:$parent.kind, name:$parent.metadata.name, configMap:.configMap.name}]'
</code></pre>
<p>The <code>jq</code> command de-constructed:</p>
<pre><code>[ // output as array
  .items[] // iterate over root key 'items'
  | 
  . as $parent // store the current entry as $parent to refer to it later
  | 
  .spec.template.spec.volumes[]? // iterate over its volumes (the ? to prevent error if there is no volume
  | 
  select(.configMap != null) // select only those volumes with configMap key 
  | 
  {kind:$parent.kind, name:$parent.metadata.name, configMap:.configMap.name} // now construct the output using $parent's kind and name and the configMap's name
]
</code></pre>
<p>Example output:</p>
<pre><code>[
  {
    &quot;kind&quot;: &quot;Deployment&quot;,
    &quot;name&quot;: &quot;telemetry-agent&quot;,
    &quot;configMap&quot;: &quot;telemetry-config-map&quot;
  },
  {
    &quot;kind&quot;: &quot;DaemonSet&quot;,
    &quot;name&quot;: &quot;fluent-bit&quot;,
    &quot;configMap&quot;: &quot;fluent-bit&quot;
  },
  {
    &quot;kind&quot;: &quot;DaemonSet&quot;,
    &quot;name&quot;: &quot;telegraf&quot;,
    &quot;configMap&quot;: &quot;telegraf&quot;
  }
]
</code></pre>
<p>N.B. If you want to find specific configMap, just replace the <code>select()</code> clause <code>.configMap != null</code> to <code>.configMap.name == &quot;specific-configmap&quot;</code>. Also, feel free to add <code>--all-namespaces</code> to the <code>kubectl get</code> command if you want to query from all namespaces</p>
