Giorgio Tarozzi
Giorgio Tarozzi

Reputation: 43

Openshift - Correlation between confimap and pods

there's a quick way/oc command to get which deployement use one or more configmap ?

In my case, I need to know which deployment I need to rollout to apply configmap update.

No evidence on dc YAML file.

Thanks in advance!

Upvotes: 0

Views: 411

Answers (1)

soulmanos
soulmanos

Reputation: 26

From guide in OpenShift 3.9

In DeploymentConfig this is the Stanza to pull all environment variables from a ConfigMap.

spec:
  containers:
    ...
      envFrom: 
        - configMapRef:
            name: env-config
    ...

In might be worth grepping all your deploymentConfigs for the keyword configMap in a particular namespace/project to check you've got configMaps attached

$ oc get dc -o json -n $(oc project -q) | grep -A2 configMap
                                        "configMapRef": {
                                            "name": "printenv-config"
                                        }

Assuming injecting environment variables from configmaps, there might be an easier way of doing this? - but you could also pipe into jq and filter based on the envFrom array key being not null

oc get dc -o json | jq -r '[.items[] | select(.spec.template.spec.containers[].envFrom[]? != null)]' | jq -c '.[] | {namespace: .metadata.namespace, dcname: .metadata.name, configMap: .spec.template.spec.containers[].envFrom[].configMapRef.name}'

Results in:

{"namespace":"aps-env","dcname":"openshift-tasks","configMap":"another-config-map"}
{"namespace":"aps-env","dcname":"printenv","configMap":"printenv-config"}

Alternatively you might be mounting your configMap via a volume mount, in which case the Stanza is different, and you'd need to adjust the above accordingly

"volumes": [
...
    {
        "configMap": {
            "defaultMode": 420,
            "name": "gogs"
        },
        "name": "config-volume"
    }
...

Different Query based on Volume mounted configMap

$ oc get dc -o json | jq -r '[.items[] | select(.spec.template.spec.volumes[]?.configMap != null)]' | jq -c '.[] | {dcname: .metadata.name, configMapName: .spec.template.spec.volumes[].configMap.name}' | grep -v null

Results in:

{"dcname":"gogs","configMapName":"gogs-configmap"}

Upvotes: 1

Related Questions