<p>In a Helm Chart I have to following values</p>

<pre><code>dataCenters:
  - name: a
    replicas: 3
  - name: b
    replicas: 2
</code></pre>

<p>When generating the template I would like my output to be like the following </p>

<pre><code>server.1 = a-1
server.2 = a-2
server.3 = a-3
server.4 = b-1
server.5 = b-2
</code></pre>

<p>I tried this code</p>

<pre><code>{{- $index := 0 -}}
{{ range $dc := .Values.cluster.dataCenters -}}
{{ range $seq := (int $dc.replicas | until) -}}
{{- $index := (add $index 1) -}}
server.{{ $index }}={{ $dc.name }}-{{ $seq }}
{{ end -}}
{{ end -}}
</code></pre>

<p>however in helm templates I don't thing you can reassign the value of the index as my 4th line is attempting and because of that I get out</p>

<pre><code>server.1 = a-1
...
server.1 = b-2
</code></pre>

<p>How does one calculates the global index <strong>0 to 4</strong> (1 to 5 in my situation) using the Sprig/Helm templating language?</p>

Dan M
<p>I have a way to do it that involves some trickery, heavily inspired by functional programming experience.</p>

<p>A Go/Helm template takes a single parameter, but the <a href="http://masterminds.github.io/sprig/" rel="nofollow noreferrer">sprig</a> library gives you the ability to create lists, and the <a href="https://godoc.org/text/template" rel="nofollow noreferrer">text/template</a> <code>index</code> function lets you pick things out of a list.  That lets you write a "function" template that takes multiple parameters, packed into a list.</p>

<p>Say we want to write out a single line of this output.  We need to keep track of which server number we're at (globally), which replica number we're at (within the current data center), the current data center record, and the records we haven't emitted yet.  If we're past the end of the current list, then print the records for the rest of the data centers; otherwise print a single line for the current replica and repeat for the next server/replica index.</p>

<pre><code>{{ define "emit-dc" -}}
  {{ $server := index . 0 -}}
  {{ $n := index . 1 -}}
  {{ $dc := index . 2 -}}
  {{ $dcs := index . 3 -}}
  {{ if gt $n (int64 $dc.replicas) -}}
    {{ template "emit-dcs" (list $server $dcs) -}}
  {{ else -}}
server.{{ $server }}: {{ $dc.name }}-{{ $n }}
{{ template "emit-dc" (list (add1 $server) (add1 $n) $dc $dcs) -}}
  {{ end -}}
{{ end -}}
</code></pre>

<p>At the top level, we know the index of the next server number, plus the list of data centers.  If that list is empty, we're done.  Otherwise we can start emitting rows from the first data center in the list.</p>

<pre><code>{{ define "emit-dcs" -}}
  {{ $server := index . 0 -}}
  {{ $dcs := index . 1 -}}
  {{ if ne 0 (len $dcs) -}}
    {{ template "emit-dc" (list $server 1 (first $dcs) (rest $dcs)) -}}
  {{ end -}}
{{ end -}}
</code></pre>

<p>Then in your actual resource definition (say, your ConfigMap definition) you can invoke this template with the first server number:</p>

<pre><code>{{ template "emit-dcs" (list 1 .Values.dataCenters) -}}
</code></pre>

<p>Copy this all into a dummy Helm chart and you can verify the output:</p>

<pre><code>% helm template .
---
# Source: x/templates/test.yaml
server.1: a-1
server.2: a-2
server.3: a-3
server.4: b-1
server.5: b-2
</code></pre>

<p>I suspect this trick won't work well if the number of servers goes much above the hundreds (the Go templating engine almost certainly isn't <em>tail recursive</em>), and this is somewhat trying to impose standard programming language methods on a templating language that isn't quite designed for it.  But...it works.</p>
