<p>I have 4 Kubernetes/Helm deployments (web, emailworker, jobworker, sync) which all need to share exactly the same <code>spec.template.spec.containers[].env</code> key. The env keys are quite large and I'd like to avoid copy/pasting it in each deployment, e.g.:</p>

<pre><code># ...
env:
- name: NODE_ENV
  value: "{{ .Values.node_env }}"
- name: BASEURL
  value: "{{ .Values.base_url }}"
- name: REDIS_HOST
  valueFrom:
    secretKeyRef:
      name: secret-redis
      key: host
- name: KUE_PREFIX
  value: "{{ .Values.kue_prefix }}"
- name: DATABASE_NAME
  value: "{{ .Values.database_name }}"
- name: DATABASE_HOST
  valueFrom:
    secretKeyRef:
      name: secret-postgres
      key: host
- name: DATABASE_USER
  valueFrom:
    secretKeyRef:
      name: secret-postgres
      key: username
- name: DATABASE_PASSWORD
  valueFrom:
    secretKeyRef:
      name: secret-postgres
      key: password
- name: AWS_KEY
  valueFrom:
    secretKeyRef:
      name: secret-bucket
      key: key
- name: AWS_SECRET
  valueFrom:
    secretKeyRef:
      name: secret-bucket
      key: secret
- name: AWS_S3_BUCKET
  valueFrom:
    secretKeyRef:
      name: secret-bucket
      key: bucket
- name: AWS_S3_ENDPOINT
  value: "{{ .Values.s3_endpoint }}"
- name: INSTAGRAM_CLIENT_ID
  valueFrom:
    secretKeyRef:
      name: secret-instagram
      key: clientID
# ...
</code></pre>

<p>Is this possible to achieve with either yaml, Helm or Kubernetes?</p>

<p>So I found a solution with Helm named templates: <a href="https://github.com/kubernetes/helm/blob/master/docs/chart_template_guide/named_templates.md" rel="nofollow noreferrer">https://github.com/kubernetes/helm/blob/master/docs/chart_template_guide/named_templates.md</a></p>

<p>I created a file <code>templates/_env.yaml</code> with the following content:</p>

<pre><code>{{ define "env" }}
            - name: NODE_ENV
              value: "{{ .Values.node_env }}"
            - name: BASEURL
              value: "{{ .Values.base_url }}"
            - name: REDIS_HOST
              valueFrom:
                secretKeyRef:
                  name: secret-redis
                  key: host
            - name: KUE_PREFIX
              value: "{{ .Values.kue_prefix }}"
            - name: DATABASE_NAME
              value: "{{ .Values.database_name }}"
            - name: DATABASE_HOST
              valueFrom:
                secretKeyRef:
                  name: secret-postgres
                  key: host
            - name: DATABASE_USER
              valueFrom:
                secretKeyRef:
                  name: secret-postgres
                  key: username
            - name: DATABASE_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: secret-postgres
                  key: password
            - name: AWS_KEY
              valueFrom:
                secretKeyRef:
                  name: secret-bucket
                  key: key
            - name: AWS_SECRET
              valueFrom:
                secretKeyRef:
                  name: secret-bucket
                  key: secret
            - name: AWS_S3_BUCKET
              valueFrom:
                secretKeyRef:
                  name: secret-bucket
                  key: bucket
            - name: AWS_S3_ENDPOINT
              value: "{{ .Values.s3_endpoint }}"
            - name: INSTAGRAM_CLIENT_ID
              valueFrom:
                secretKeyRef:
                  name: secret-instagram
                  key: clientID
{{ end }}
</code></pre>

<p>And here's how I use it in a <code>templates/deployment.yaml</code> files:</p>

<pre><code>apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: somedeployment
  # ...
spec:
  template:
    # ...
    metadata:
      name: somedeployment
    spec:
      # ...
      containers:
        - name: container-name
          image: someimage
          # ...
          env:
            {{- template "env" . }}
</code></pre>
