<p>Is there a mechanism in a template (yml) file in k8s for generating multiple items? Like a for loop.</p>
<p>For example , I have a template used my multiple projects. But some need 1 database others maybe 2, 3 or more. I don't want to define all variables by hands, and edit my file if a new projects comes in and needs a new variable.</p>
<p>Example of what I do today:</p>
<pre><code>  template:
      (...)
      containers:
        (...)
        env:
        - name: DATABASE_NAME
          value: &quot;{{prj.database.name}}&quot;
        - name: DATABASE_NAME_2
          value: &quot;{{prj.database.name.second}}&quot;
        - name: DATABASE_USER
          valueFrom:
            secretKeyRef:
              name: &quot;{{ k8s.deploy.app.secret.name }}&quot;
              key: database_user
        - name: DATABASE_USER_2
          valueFrom:
            secretKeyRef:
              name: &quot;{{ k8s.deploy.app.secret.name }}&quot;
              key: database_user_2
</code></pre>
<p>As you can see I have to copy paste code for each user + password + database name. In this file and also in secrets. I use XLDeploy to insert data in my YML files.</p>
<p>What i'm looking for:</p>
<pre><code>  template:
      (...)
      containers:
        (...)
        env:
        ---- for i in {{prj.database.number}} ----
        - name: DATABASE_NAME_{i}
          value: &quot;{{prj.database.name.{i]}}&quot;
        - name: DATABASE_USER_{i}
          valueFrom:
            secretKeyRef:
              name: &quot;{{ k8s.deploy.app.secret.name }}&quot;
              key: database_user_{i}
        ---- end for loop ----
</code></pre>
<p>So I could insert in XLDeploy my number of databases to use for each projects. And fill the values with XLD too.</p>
<p>Is that possible, or I need to use a script langague to generate a YML file from a &quot;YML template&quot; ?</p>

BeGreen
<p>You could use <a href="https://helm.sh/" rel="nofollow noreferrer">helm</a> for this if you have other yamls related to this file. Helm allows for syntax like this:</p>
<pre class="lang-yaml prettyprint-override"><code>{{- range .Values.testvalues }}
---
apiVersion: v1
kind: Service
metadata:
  annotations: {}
  labels: {}
  name: {{ . }}
spec:
  ports:
  - name: tcp-80-8080
    port: 80
    protocol: TCP
    targetPort: 8080
  selector: 
    app: rss-{{ . }}
  type: ClusterIP
{{- end }}
</code></pre>
<p>So with a list for <code>testvalues</code> in the <code>values.yaml</code> like this:</p>
<pre class="lang-yaml prettyprint-override"><code>testvalues:
- a
- b
- c
- d
</code></pre>
<p>Helm would generate four service declarations -- one for each item. The first looking like this:</p>
<pre class="lang-yaml prettyprint-override"><code>---
apiVersion: v1
kind: Service
metadata:
  annotations: {}
  labels: {}
  name: a
spec:
  ports:
  - name: tcp-80-8080
    port: 80
    protocol: TCP
    targetPort: 8080
  selector: 
    app: rss-a
  type: ClusterIP
</code></pre>
<p>The other option if it's just a single file, is to use Jinja and something like Python to build the file using the <a href="https://pypi.org/project/Jinja2/" rel="nofollow noreferrer">jinja2</a> module</p>
