<p>I am currently working on a project where I need to set a bigger number (approx. 20) of environment variables.</p>

<p>Now these variable should be shared with multiple containers inside of a pod. I am using a Configmap to define the values. Now my problem is this. I have to set every Environment variable seperately and also have to do this for every container which just is pretty ugly and alot of unnecessary code. Basically it's looking something like this</p>

<pre><code>kind: Pod
apiVersion: v1
...
containers:
- name: container1
  image: my-image:latest
  env:
  - name: VAR1
    valueFrom:
      configMapKeyRef:
        name: my-config
        key: my-var1
    ...
- name: container2
  image: some-other-image:stable
  env:
  - name: VAR1
    valueFrom:
      configMapKeyRef:
        name: my-config
        key: my-var1
    ...
</code></pre>

<p>What I want is to automatically add all the values from my configMap as and environment variable to the container. My first approach is to mount a configMap as volume and then run a shell script on startup. Similar to this</p>

<p><strong>configMap</strong></p>

<pre><code>kind: ConfigMap
apiVersion: v1
...
data:
  envCat.sh: |
    #!/bin/sh
    export $(grep -v '^#' /etc/pod-config/envVars | xargs)
    cat
  envVars
    MY_VAR1="HELLO WORLD!!"
    MY_VAR2="I AM HERE"
</code></pre>

<p><strong>Pod</strong></p>

<pre><code>kind: Pod
apiVersion: v1
...
spec:
  volumes:
  - name: config-volume:
    configMap:
      name: my-config
  containers:
  - name: container1
    image: my-image:latest
    volumeMounts:
    - name: config-volume
      mountPath: /etc/pod-config
    command:
    - /etc/pod-config/envCat.sh
</code></pre>

<p>Unfortunately the export command works just fine when I run it manually in the container but running the shell script or running /bin/sh /etc/pod-config/envCat.sh is not working.</p>

<p>Any suggestions to achieve what I want to do?</p>

relief.melone
<p>You should be using envFrom to load all key:value pairs as environment variables inside the container </p>
