How to show all deployments/daemonsets which mount specific configmap/secret?

8/8/2020

Sometimes, I want to explore all deployments/daemonsets which mount specific configmap/secret.

Is there any way can achieve this by kubectl?

-- Stephen Chen
kubectl
kubernetes

1 Answer

8/8/2020

You need to have jq to do such a complex queries. Here you go:

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}]'

The jq command de-constructed:

[ // 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
]

Example output:

[
  {
    "kind": "Deployment",
    "name": "telemetry-agent",
    "configMap": "telemetry-config-map"
  },
  {
    "kind": "DaemonSet",
    "name": "fluent-bit",
    "configMap": "fluent-bit"
  },
  {
    "kind": "DaemonSet",
    "name": "telegraf",
    "configMap": "telegraf"
  }
]

N.B. If you want to find specific configMap, just replace the select() clause .configMap != null to .configMap.name == "specific-configmap". Also, feel free to add --all-namespaces to the kubectl get command if you want to query from all namespaces

-- Lukman
Source: StackOverflow