Nested loops in chart templates

10/31/2018

I'm having trouble rendering a template for a Helm Chart that needs to have easily expandable amounts of nodes and replicas. I get the below error message. What's odd is I do not get the below error message if I remove the inner loop to not nest loops. I'm completely new to go, but this seems valid. I'm at a loss.

Error:

$ helm install . --dry-run --debug
Error: render error in "app-on-k8s/templates/configmap_configd.yaml": template: app-on-k8s/templates/configmap_configd.yaml:18:77: executing "app-on-k8s/templates/configmap_configd.yaml" at <.Values.nodeCount>: can't evaluate field Values in type int

This is the relevant section from my values.yaml file:

# number of nodes / shards
nodeCount: 5
replicaCount: 3

And the relevant section from my template file:

    <default>
        {{range $i, $e := until (atoi (printf "%d" (int64 .Values.nodeCount))) }}
                <node>
                {{range $j, $k := until (atoi (printf "%d" (int64 .Values.replicaCount))) }}   #line 18
                    <replica>
                        <host>{{ $.Release.Name }}-{{$j}}</host>
                        <port>{{ $.Values.service.rpc_port }}</port>
                    </replica>
                {{end}}    
                </node>
        {{end}}
    </default>
-- user797963
go-templates
kubernetes
kubernetes-helm

1 Answer

10/31/2018

The problem is, when you are using .Values.replicaCount in the second loop,. scope has been changed and now pointing at .Values.nodeCount. So .Values.replicaCount now pointing to .Values.nodeCount.Values.replicaCount. Since, there is no such field in values.yaml file you are getting this error.

Use $.Values.replicaCount instead of .Values.replicaCount in second loop.

Ref: helm.sh

-- Emruz Hossain
Source: StackOverflow