<p>Assume I have this manifest:</p>

<pre><code>apiVersion: batch/v1
kind: Job
metadata:
  name: initialize-assets-fixtures
spec:
  template:
    spec:
      initContainers:
      - name: wait-for-minio
        image: bitnami/minio-client
        env:
          - name: MINIO_SERVER_ACCESS_KEY
            valueFrom:
              secretKeyRef:
                name: minio
                key: access-key
          - name: MINIO_SERVER_SECRET_KEY
            valueFrom:
              secretKeyRef:
                name: minio
                key: secret-key
          - name: MINIO_SERVER_HOST
            value: minio
          - name: MINIO_SERVER_PORT_NUMBER
            value: "9000"
          - name: MINIO_ALIAS
            value: minio
        command: 
          - /bin/sh
          - -c 
          - |
            mc config host add ${MINIO_ALIAS} http://${MINIO_SERVER_HOST}:${MINIO_SERVER_PORT_NUMBER} ${MINIO_SERVER_ACCESS_KEY} ${MINIO_SERVER_SECRET_KEY}
      containers:
      - name: initialize-assets-fixtures
        image: bitnami/minio
        env:
          - name: MINIO_SERVER_ACCESS_KEY
            valueFrom:
              secretKeyRef:
                name: minio
                key: access-key
          - name: MINIO_SERVER_SECRET_KEY
            valueFrom:
              secretKeyRef:
                name: minio
                key: secret-key
          - name: MINIO_SERVER_HOST
            value: minio
          - name: MINIO_SERVER_PORT_NUMBER
            value: "9000"
          - name: MINIO_ALIAS
            value: minio
        command:
          - /bin/sh
          - -c
          - |
            mc config host add ${MINIO_ALIAS} http://${MINIO_SERVER_HOST}:${MINIO_SERVER_PORT_NUMBER} ${MINIO_SERVER_ACCESS_KEY} ${MINIO_SERVER_SECRET_KEY}

            for category in `ls`; do
              for f in `ls $category/*` ; do
                mc cp $f ${MINIO_ALIAS}/$category/$(basename $f)
              done
            done
      restartPolicy: Never
</code></pre>

<p>You see I have here one <code>initContainer</code> and one <code>container</code>. In both containers, I have the same configuration, i.e. the same <code>env</code> section. </p>

<p>Assume I have yet another <code>Job</code> manifest where I use the very same <code>env</code> section again. </p>

<p>It's a lot of duplicated configuration that I bet I can simplify drastically, but I don't know how to do it. Any hint? Any link to some documentation? After some googling, I was not able to come up with anything useful. Maybe with kustomize, but I'm not sure. Or maybe I'm doing it the wrong way with all those environment variables, but I don't think I have a choice, depending on the service I'm using (here it's minio, but I want to do the same kind of stuff with other services which might not be as flexible as minio).</p>

Laurent Michel
<p>Based on my knowledge you have those 3 options</p>

<ul>
<li><a href="https://kustomize.io/" rel="nofollow noreferrer">Kustomize</a></li>
<li><a href="https://helm.sh/" rel="nofollow noreferrer">Helm</a></li>
<li><a href="https://matthewpalmer.net/kubernetes-app-developer/articles/ultimate-configmap-guide-kubernetes.html" rel="nofollow noreferrer">ConfigMap</a></li>
</ul>

<hr>

<p><strong>ConfigMap</strong></p>

<hr>

<p>You can use either kubectl create configmap or a ConfigMap generator in kustomization.yaml to create a ConfigMap.</p>

<p>The data source corresponds to a key-value pair in the ConfigMap, where</p>

<p><strong>key</strong> = the file name or the key you provided on the command line</p>

<p><strong>value</strong> = the file contents or the literal value you provided on the command line.</p>

<p>More about how to use it in pod <a href="https://kubernetes.io/docs/tasks/configure-pod-container/configure-pod-configmap/" rel="nofollow noreferrer">here</a> </p>

<hr>

<p><strong>Helm</strong></p>

<hr>

<p>As @Matt mentionted in comments you can use helm</p>

<blockquote>
  <p><a href="https://helm.sh/" rel="nofollow noreferrer">helm</a> lets you template the yaml with values. Also once you get into it there are ways to create and include <a href="https://helm.sh/docs/chart_template_guide/named_templates/" rel="nofollow noreferrer">partial templates</a> – Matt</p>
</blockquote>

<p>By the way, helm has it's own created <a href="https://github.com/helm/charts/tree/master/stable/minio" rel="nofollow noreferrer">minio chart</a>, you might take a look how it is created there.</p>

<hr>

<p><strong>Kustomize</strong></p>

<hr>

<p>It's well described <a href="https://github.com/kubernetes-sigs/kustomize/blob/master/examples/wordpress/README.md" rel="nofollow noreferrer">here</a> and <a href="https://blog.stack-labs.com/code/kustomize-101/" rel="nofollow noreferrer">here</a>  how could you do that in kustomize. </p>

<p>Let me know if you have any more questions.</p>
