1

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.

0

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
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.