<p>I have the following environment variable in my Kubernetes template:</p>
<pre><code>envFrom:
  - configMapRef:
      name: configmap

env:
- name: MACHINENAME
  valueFrom:
    fieldRef:
      fieldPath: spec.nodeName
</code></pre>
<p>I would like to use the value from 'fieldRef' in a config map instead. Would this kind of modification be possible?
In other words, I want to add the 'MACHINENAME' environment variable to the config map, so I don't have to use the 'env:' block.</p>

Ohad
<p>You cannot do this in the way you describe.</p>
<p>A <a href="https://kubernetes.io/docs/concepts/configuration/configmap/" rel="nofollow noreferrer">ConfigMap</a> only contains fixed string-key/string-value pairs.  You cannot embed a more complex structure into a ConfigMap, or say that a ConfigMap value will be resolved using the downward API when a Pod is created.  The node name of the pod, and most of the other downward API information, will be different for each pod using the ConfigMap (and likely even for each replica of the same deployment) and so there is no fixed value you can put into a ConfigMap.</p>
<p>You tagged this question with the <a href="https://helm.sh" rel="nofollow noreferrer">Helm</a> deployment tool.  If you're using Helm, and you're simply trying to avoid repeating this boilerplate in every Deployment spec, you can write a helper template that includes this definition</p>
<pre class="lang-yaml prettyprint-override"><code>{{/* templates/_helpers.tpl */}}
{{- define &quot;machinename&quot; -}}
- name: MACHINENAME
  valueFrom:
    fieldRef:
      fieldPath: spec.nodeName
{{- end -}}
</code></pre>
<p>Now in your Deployment spec, you can include this template rather than retyping the whole YAML block.</p>
<pre class="lang-yaml prettyprint-override"><code>containers:
  - envFrom:
      - configMapRef:
        name: configmap
    env:
{{ include &quot;machinename . | indent 6 }}
</code></pre>
<p>(The exact <code>indent</code> value will depend on the context where you include it, and should be two more than the number of spaces at the start of the <code>env:</code> line.  It is important that the line containing <code>indent</code> not itself be indented.)</p>
