<p>I have a configmap where I have defined the following key-value mapping in the <code>data</code> section:</p>

<pre><code>apiVersion: v1
kind: ConfigMap
metadata:
  namespace: test
  name: test-config
data:
  TEST: "CONFIGMAP_VALUE"
</code></pre>

<p>then in the definition of my container (in the deployment / statefulset manifest) I have the following:</p>

<pre><code>        env:
        - name: TEST
          value: "ANOTHER_VALUE"
        envFrom:
        - configMapRef:
            name: test-config
</code></pre>

<p>When doing this I was expecting that the value from the configmap (TEST="CONFIGMAP_VALUE") will override the (default) value specified in the container spec (TEST="ANOTHER_VALUE"), but this is not the case (TEST always gets the value from the container spec). I couldn't find any relevant documentation about this - is it possible to achieve such env variable value overriding?</p>

asenovm
<p>From <a href="https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.27/#container-v1-core" rel="nofollow noreferrer">Kubernetes API reference</a>:</p>
<blockquote>
<p><code>envFrom</code> : List of sources to populate environment variables in the container. The keys defined within a source must be a C_IDENTIFIER. All invalid keys will be reported as an event when the container is starting. When a key exists in multiple sources, the value associated with the last source will take precedence. Values defined by an Env with a duplicate key will take precedence. Cannot be updated.</p>
</blockquote>
<p>So above clearly states the <strong>env</strong> will take precedence than <strong>envFrom</strong>.</p>
<blockquote>
<p>When a key exists in multiple sources, the value associated with the last source will take precedence.</p>
</blockquote>
<p>So, for overriding, don't use <code>envFrom</code>, but define the value twice within <code>env</code>, see below:</p>
<pre><code>apiVersion: v1
kind: ConfigMap
metadata:
  namespace: default
  name: test-config
data:
  TEST: &quot;CONFIGMAP_VALUE&quot;
---
apiVersion: v1
kind: Pod
metadata:
  name: busy
  namespace: default
spec:
  containers:
  - name: busybox
    image: busybox
    env:
    - name: TEST
      value: &quot;DEFAULT_VAULT&quot;
    - name: TEST
      valueFrom:
        configMapKeyRef:
          name: test-config
          key: TEST
    command:
    - &quot;sh&quot;
    - &quot;-c&quot;
    - &gt;
      while true; do
        echo &quot;$(TEST)&quot;;
        sleep 3600;
      done
</code></pre>
<p>Check:</p>
<pre><code>kubectl logs busy -n default
CONFIGMAP_VALUE
</code></pre>
