<p>I am looking for a way to get environment variable in data section of configmap.
In the below yml configuration, I have assigned $NODE_NAME which didn't help me. Is there any way to get this work</p>
<pre><code>apiVersion: v1
kind: ConfigMap
metadata:
  name: config
  namespace: kube-system
data:
  test.conf: |
    {
        &quot;push&quot;: 5,
        &quot;test&quot;: $NODE_NAME
    }
</code></pre>

<p>One way to achieve this would be by using the <a href="https://stackoverflow.com/a/14157575/12201084">envsubst</a> as following:</p>
<pre><code>$ export NODE_NAME=my-node-name
$ cat &lt;&lt; EOF | envsubst | kubectl apply -f-
apiVersion: v1
kind: ConfigMap
metadata:
  name: config
  namespace: kube-system
data:
  test.conf: |
    {
        &quot;push&quot;: 5,
        &quot;test&quot;: $NODE_NAME
    }
EOF
</code></pre>
<hr />
<p>But sth tells me that you want to use this in a pod and populate config with environment variable.</p>
<p>Have a look at this example:</p>
<pre><code>apiVersion: v1
kind: ConfigMap
metadata:
  name: config
  namespace: kube-system
data:
  test.conf: |
    {
        &quot;push&quot;: 5,
        &quot;test&quot;: $NODE_NAME
    }

---
apiVersion: v1
kind: Pod
metadata:
  labels:
    run: example-pod
  name: example-pod
spec:
  initContainers:
  - args:
    - sh
    - -c
    - cat /test.conf | envsubst &gt; /data/test.conf
    image: bhgedigital/envsubst
    name: envsubst
    env:
      - name: NODE_NAME
        valueFrom:
          fieldRef:
            fieldPath: spec.nodeName
    volumeMounts:
    - mountPath: /data
      name: data-volume
    - mountPath: /test.conf
      subPath: test.conf
      name: config-volume
  containers:
  - image: busybox
    name: busybox
    args:
    - sleep 
    - &quot;1000&quot;
    volumeMounts:
    - mountPath: /data
      name: data-volume 
  volumes:
  - name: data-volume
    emptyDir: {}
  - name: config-volume
    configMap:
      name: config
</code></pre>
<p>when you apply the above yaml you can check if the file was substituted correctly as following:</p>
<pre><code>$ kubectl exec -it example-pod -- cat /data/test.conf                      
{
    &quot;push&quot;: 5,
    &quot;test&quot;: minikube
}
</code></pre>
<p>As you can see I was testing it in minikube (hence nodeName = minikube in my case)</p>
