<p>I have 10 applications to deploy to Kubernetes. Each of the deployments depends on an init container that is basically identical except for a single parameter (and it doesn't make conceptual sense for me to decouple this init container from the application). So far I've been copy-pasting this init container into each deployment.yaml file, but I feel like that's got to be a better way of doing this!</p>

<p>I haven't seen a great solution from my research, though the only thing I can think of so far is to use something like Helm to package up the init container and deploy it as part of some dependency-based way (Argo?).</p>

<p>Has anyone else with this issue found a solution they were satisfied with?</p>

Mike
<p>A Helm template can contain an arbitrary amount of text, just so long as when all of the macros are expanded it produces a valid YAML Kubernetes manifest.  ("Valid YAML" is trickier than it sounds because the indentation matters.)</p>

<p>The simplest way to do this would be to write a shared Helm template that included the definition for the init container:</p>

<p><strong>_init_container.tpl:</strong></p>

<pre><code>{{- define "common.myinit" -}}
name: myinit
image: myname/myinit:{{ .Values.initTag }}
# Other things from a container spec
{{ end -}}
</code></pre>

<p>Then in your deployment, include this:</p>

<p><strong>deployment.yaml:</strong></p>

<pre><code>apiVersion: v1
kind: Deployment
spec:
  template:
    spec:
      initContainers:
        - {{ include "common.myinit" . | indent 10 | strip }}
</code></pre>

<p>Then you can copy the <code>_init_container.tpl</code> file into each of your individual services.</p>

<p>If you want to avoid the copy-and-paste (reasonable enough) you can create a Helm chart that contains only templates and no actual Kubernetes resources.  You need to set up some sort of repository to hold this chart.  Put the <code>_init_container.tpl</code> into that shared chart, declare it as a dependency is the chart metadata, and reference the template in your deployment YAML in the same way (Go template names are shared across all included charts).</p>
