<p>You can use <code>kubectl get -o json</code> and parse the output using <a href="https://github.com/stedolan/jq" rel="nofollow noreferrer"><code>jq</code></a> to iterate over the deployments, matching any desired <code>configMapKeyRef.name</code> and returning a <code>uniq</code> list of deployments.</p>
<p>This should work:</p>
<pre><code>kubectl get deployments --all-namespaces -o json | jq -r '.items | map(select(.spec.template.spec.containers[]?.env[]?.valueFrom.configMapKeyRef.name == &quot;london-tz-config&quot; ) | .metadata.name) | .[]' | uniq
</code></pre>

CC BY-SA 4.0
2020-10-20T21:04:53.150Z
2020-10-20T21:04:53.150Z
Eduardo Baitello
<p>Suppose I have a <code>ConfigMap</code>:</p>
<pre><code>apiVersion: v1
kind: ConfigMap
metadata:
  name: london-tz-config
data:
  SERVER_TIMEZONE: &quot;Europe/London&quot;
</code></pre>
<p>I also have about 50 distinct Deployments. Let's say that 20 of them use the <code>london-tz-config</code> ConfigMap to set their environments, like this</p>
<pre><code>  template:
    metadata:
      labels:
        app: echo-app
    spec:
      containers:
        - name: echo-app-container
          image: ealen/echo-server:latest
          ports:
            - containerPort: 8080
          env:
            - name: SERVER_TIMEZONE
              valueFrom:
                configMapKeyRef:
                  name: london-tz-config    # Reference our ConfigMap here
                  key: SERVER_TIMEZONE
</code></pre>
<p>I don't know which Deployments use which ConfigMaps at any given time without manually inspecting the cluster.</p>
<p>Is there a way to ask Kubernetes to show me all Deployments that contain references to a given ConfigMap by its <code>metadata.name</code> attribute?</p>
<p>Either <code>kubectl</code> or the Kubernetes API would be okay.</p>

<kubernetes><kubectl>
Get all Deployments referencing given ConfigMap by the ConfigMap's name
kalatabe
# Get all Deployments referencing given ConfigMap by the ConfigMap's name

(Asked by: username_0 on 2020-10-20)

Suppose I have a ConfigMap:
apiVersion: v1
kind: ConfigMap
metadata:
  name: london-tz-config
data:
  SERVER_TIMEZONE: "Europe/London"

I also have about 50 distinct Deployments. Let's say that 20 of them use the london-tz-config ConfigMap to set their environments, like this
  template:
    metadata:
      labels:
        app: echo-app
    spec:
      containers:
        - name: echo-app-container
          image: ealen/echo-server:latest
          ports:
            - containerPort: 8080
          env:
            - name: SERVER_TIMEZONE
              valueFrom:
                configMapKeyRef:
                  name: london-tz-config    # Reference our ConfigMap here
                  key: SERVER_TIMEZONE

I don't know which Deployments use which ConfigMaps at any given time without manually inspecting the cluster.
Is there a way to ask Kubernetes to show me all Deployments that contain references to a given ConfigMap by its metadata.name attribute?
Either kubectl or the Kubernetes API would be okay.

**Tags:** kubernetes, kubectl


---

## 1 answers

### Answer by username_1 on 2020-10-20. Score: 3 | Accepted answer

You can use kubectl get -o json and parse the output using jq to iterate over the deployments, matching any desired configMapKeyRef.name and returning a uniq list of deployments.
This should work:
kubectl get deployments --all-namespaces -o json | jq -r '.items | map(select(.spec.template.spec.containers[]?.env[]?.valueFrom.configMapKeyRef.name == "london-tz-config" ) | .metadata.name) | .[]' | uniq


---

