<p>I have a deployment with a pod that has his configuration defined through a lot of environment variables. Now I want to add a sidecar container that requires exactly the same environment variables as the already defined container. Instead of just copy/pasting all the variables I'd like to stick to the DRY principle.</p>
<p>The current definition looks something like this:</p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: apps/v1
kind: Deployment
spec:
  template:
    spec:
      containers:
      - name: container1
        env:
        - name: MYSQL_HOST
          value: {{ template &quot;mariadb.primary.fullname&quot; .Subcharts.mariadb }}
        - name: MYSQL_DATABASE
          value: {{ .Values.mariadb.auth.database | quote }}
        - name: MYSQL_USER
          valueFrom:
            secretKeyRef:
              name: {{ .Values.secretName | default (printf &quot;%s-%s&quot; .Release.Name &quot;db&quot;) }}
              key: {{ .Values.usernameKey | default &quot;db-username&quot; }}
        - name: MYSQL_PASSWORD
          valueFrom:
            secretKeyRef:
              name: {{ .Values.secretName | default (printf &quot;%s-%s&quot; .Release.Name &quot;db&quot;) }}
              key: {{ .Values.passwordKey | default &quot;db-password&quot; }}
</code></pre>
<p>The recommended way to reuse environment variables is through a ConfigMap.
Now I can create this:</p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: v1
kind: ConfigMap
metadata:
  name: config
data:
  MYSQL_HOST: {{ template &quot;mariadb.primary.fullname&quot; .Subcharts.mariadb }}
  MYSQL_DATABASE: {{ .Values.mariadb.auth.database | quote }}
---
apiVersion: apps/v1
kind: Deployment
spec:
  template:
    spec:
      containers:
      - name: container1
        envFrom:
        - configMapRef:
            name: config
      - name: container2
        envFrom:
        - configMapRef:
            name: config
</code></pre>
<p>But now I would still need to repeat myself for <code>MYSQL_USER</code> and <code>MYSQL_PASSWORD</code> (and for the 20 other env variables that are like this).</p>
<p>Is it possible to reuse the existing secrets <code>MYSQL_USER</code> and <code>MYSQL_PASSWORD</code> as environment variables without creating duplicates on <code>container1</code> and <code>container2</code>?</p>

<p>You can create a <a href="https://docs.helm.sh/docs/chart_template_guide/named_templates/" rel="nofollow noreferrer">named template</a> to do this.  If you're only going to be using this template within a single Kubernetes object (multiple containers within the same Deployment) you could put it in that object's <code>deployment.yml</code> file, or into the shared <code>_helpers.tpl</code> file.</p>
<pre class="lang-yaml prettyprint-override"><code>{{- define &quot;env.mysql&quot; -}}
- name: MYSQL_HOST
  value: {{ template &quot;mariadb.primary.fullname&quot; .Subcharts.mariadb }}
- name: MYSQL_DATABASE
  value: {{ .Values.mariadb.auth.database | quote }}
- name: ET
  value: cetera
{{- end -}}
</code></pre>
<p>Then wherever you need to use that block of variables, you can <code>include</code> it, and <code>indent</code> it to the correct level.</p>
<pre class="lang-yaml prettyprint-override"><code>spec:
  template:
    spec:
      containers:
      - name: container1
        env:
{{- include &quot;env.mysql&quot; . | indent 10 }}
          - name: OTHER_ENVIRONMENT_VARIABLES
            value: are okay too
</code></pre>
<p>Having <code>define</code>d the template once, you can <code>include</code> it as many times as you need to.</p>
