<p>I have a set of environment variables in my <code>deployment</code> using <code>EnvFrom</code> and <code>configMapRef</code>. The environment variables held in these configMaps were set by kustomize originally from json files.</p>
<pre><code>spec.template.spec.containers[0].

envFrom:
  - secretRef:
      name: eventstore-login
  - configMapRef:
      name: environment
  - configMapRef:
      name: eventstore-connection
  - configMapRef:
      name: graylog-connection
  - configMapRef:
      name: keycloak
  - configMapRef:
      name: database
</code></pre>
<p>The issue is that it's not possible for me to access the specific environment variables directly.</p>
<p>Here is the result of running <code>printenv</code> in the pod:</p>
<pre><code>...
eventstore-login={
  &quot;EVENT_STORE_LOGIN&quot;: &quot;admin&quot;,
  &quot;EVENT_STORE_PASS&quot;: &quot;changeit&quot;
}

evironment={
  &quot;LOTUS_ENV&quot;:&quot;dev&quot;,
  &quot;DEV_ENV&quot;:&quot;dev&quot;
}

eventstore={
  &quot;EVENT_STORE_HOST&quot;: &quot;eventstore-cluster&quot;,
  &quot;EVENT_STORE_PORT&quot;: &quot;1113&quot;
}

graylog={
  &quot;GRAYLOG_HOST&quot;:&quot;&quot;,
  &quot;GRAYLOG_SERVICE_PORT_GELF_TCP&quot;:&quot;&quot;
}
...
</code></pre>
<p>This means that from my nodejs app I need to do something like this</p>
<pre><code>&gt; process.env.graylog
'{\n  &quot;GRAYLOG_HOST&quot;:&quot;&quot;,\n  &quot;GRAYLOG_SERVICE_PORT_GELF_TCP&quot;:&quot;&quot;\n}\n'
</code></pre>
<p>This only returns the json string that corresponds to my original json file. But I want to be able to do something like this:</p>
<pre><code>process.env.GRAYLOG_HOST
</code></pre>
<p>To retrieve my environment variables. But I don't want to have to modify my deployment to look something like this:</p>
<pre><code>          env:
            - name: NODE_ENV
              value: dev
            - name: EVENT_STORE_HOST
              valueFrom:
                secretKeyRef:
                  name: eventstore-secret
                  key: EVENT_STORE_HOST
            - name: EVENT_STORE_PORT
              valueFrom:
                secretKeyRef:
                  name: eventstore-secret
                  key: EVENT_STORE_PORT
            - name: KEYCLOAK_REALM_PUBLIC_KEY
              valueFrom:
                configMapKeyRef:
                  name: keycloak-local
                  key: KEYCLOAK_REALM_PUBLIC_KEY

</code></pre>
<p>Where every variable is explicitly declared. I could do this but this is more of a pain to maintain.</p>

<h2>Short answer:</h2>
<p>You will need to define variables explicitly or change configmaps so they have <code>1 environment variable = 1 value</code> structure, this way you will be able to refer to them using <code>envFrom</code>. E.g.:</p>
<pre><code>&quot;apiVersion&quot;: &quot;v1&quot;,
&quot;data&quot;: {
    &quot;EVENT_STORE_LOGIN&quot;: &quot;admin&quot;,
    &quot;EVENT_STORE_PASS&quot;: &quot;changeit&quot;
},
&quot;kind&quot;: &quot;ConfigMap&quot;,
</code></pre>
<h2>More details</h2>
<p><code>Configmaps</code> are key-value pairs that means for one key there's only one value, <code>configmaps</code> can get <code>string</code> as data,  but they can't work with <code>map</code>.</p>
<p>I tried edited manually the <code>configmap</code> to confirm the above and got following:</p>
<pre><code>invalid type for io.k8s.api.core.v1.ConfigMap.data: got &quot;map&quot;, expected &quot;string&quot;
</code></pre>
<p>This is the reason why environment comes up as one string instead of structure.</p>
<p>For example this is how <code>configmap.json</code> looks:</p>
<pre><code>$ kubectl describe cm test2

Name:         test2
Namespace:    default
Labels:       &lt;none&gt;
Annotations:  &lt;none&gt;

Data
====
test.json:
----
environment={
  &quot;LOTUS_ENV&quot;:&quot;dev&quot;,
  &quot;DEV_ENV&quot;:&quot;dev&quot;
}
</code></pre>
<p>And this is how it's stored in kubernetes:</p>
<pre><code>$ kubectl get cm test2 -o json

{
    &quot;apiVersion&quot;: &quot;v1&quot;,
    &quot;data&quot;: {
        &quot;test.json&quot;: &quot;evironment={\n  \&quot;LOTUS_ENV\&quot;:\&quot;dev\&quot;,\n  \&quot;DEV_ENV\&quot;:\&quot;dev\&quot;\n}\n&quot;
    },
</code></pre>
<p>In other words observed behaviour is expected.</p>
<h2>Useful links:</h2>
<ul>
<li><a href="https://kubernetes.io/docs/concepts/configuration/configmap/" rel="nofollow noreferrer">ConfigMaps</a></li>
<li><a href="https://kubernetes.io/docs/tasks/configure-pod-container/configure-pod-configmap/" rel="nofollow noreferrer">Configure a Pod to Use a ConfigMap</a></li>
</ul>
