<h1>TL;DR</h1>
<blockquote>
<p>Can I reference a previously-defined key-value pair in a ConfigMap?</p>
</blockquote>
<h1>Full Version</h1>
<p>I'm writing a deployment specification for an application which accepts
its configuration from environment variables at startup. Some of the
environment variables are derived from others. If I were setting
them up as file to be sourced at application startup, I would simply
do:</p>
<pre><code>[me@myserver ~]$ cat ./myenv.sh
export FOO=foo
export BAR=bar
export FOOBAR=$FOO$BAR
[me@myserver ~]$ . ./myenv.sh
[me@myserver ~]$ printenv FOOBAR
foobar
</code></pre>
<p>However, the analagous way to do this in a ConfigMap by referencing
previously-defined key-value pairs doesn't work (see sample ConfigMap
and Pod, below). Here are the results:</p>
<pre><code>[me@myserver ~]$ kubectl create -f my-app-config.yaml -f my-app-pod.yaml
configmap &quot;my-app&quot; created
pod &quot;my-app&quot; created
[me@myserver ~]$ kubectl exec -it my-app -- printenv | grep MY_CONFIGMAP
MY_CONFIGMAP_FOO=foo
MY_CONFIGMAP_FOOBAR=$(MY_CONFIGMAP_FOO)$(MY_CONFIGMAP_BAR)
MY_CONFIGMAP_BAR=bar
</code></pre>
<p>Hoped-for value of <code>MY_CONFIGMAP_FOOBAR</code> is <code>foobar</code>.</p>
<p>I attempted to cross-apply the following instructions under
<a href="https://kubernetes.io/docs/tasks/configure-pod-container/configure-pod-configmap/#use-configmap-defined-environment-variables-in-pod-commands" rel="nofollow noreferrer">Use ConfigMap-defined environment variables in Pod commands</a>
from the documentation:</p>
<blockquote>
<p>You can use ConfigMap-defined environment variables in the <strong><code>command</code></strong>
section of the Pod specification using the <strong><code>$(VAR_NAME)</code></strong>
Kubernetes substitution syntax.</p>
</blockquote>
<p>However, this obviously did not work, and I've been unable to find an
answer in my research.</p>
<p>I suspect I already know the answer, but is there any way to accomplish
this?</p>
<h1><a href="https://stackoverflow.com/help/mcve">MCVE</a></h1>
<h2>ConfigMap</h2>
<pre><code>apiVersion: v1
kind: ConfigMap
metadata:
  name: my-app

data:
  MY_CONFIGMAP_FOO: foo
  MY_CONFIGMAP_BAR: bar
  
  # Desired concatenation would be `foobar`
  MY_CONFIGMAP_FOOBAR: $(MY_CONFIGMAP_FOO)$(MY_CONFIGMAP_BAR)
</code></pre>
<h2>Pod</h2>
<pre><code>apiVersion: v1
kind: Pod
metadata:
  name: my-app

spec:
  containers:
  - name: my-app
    image: alpine
    imagePullPolicy: IfNotPresent
    envFrom:
      - configMapRef:
          name: my-app
          
    command:
      - /bin/sh
      
    args:
      - -c
      - &quot;while true; do sleep 3600; done&quot;
</code></pre>
<h1>Epilogue: Twenty-Nine Months Later</h1>
<p>After working with Kubernetes for an additional two-and-a-half years, my ultimate solution (and recommendation) is to use a Helm chart, preferrably with an Ansible-managed deployment.</p>

<p>You can achieve your expected result with help of this tool: <a href="https://www.gnu.org/software/gettext/manual/html_node/envsubst-Invocation.html" rel="nofollow noreferrer">envsubst</a>.</p>

<p>Create the <code>ConfigMap</code> following way,</p>

<pre><code>envsubst &lt; my-app-config.yaml | kubectl apply -f -
</code></pre>

<p>Now, all environment variables on your <code>ConfigMap</code> will be replaced with resolved value.</p>
