图表模板中的嵌套循环

Nested loops in chart templates

我在为 Helm Chart 呈现模板时遇到问题,该模板需要具有易于扩展的节点和副本数量。我收到以下错误消息。奇怪的是,如果我删除内部循环而不是嵌套循环,我不会收到以下错误消息。我是全新的,但这似乎是有效的。我很茫然。

错误:

$ 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

这是我的 values.yaml 文件中的相关部分:

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

以及我的模板文件中的相关部分:

    <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>

问题是,当您在第二个循环中使用 .Values.replicaCount 时,. 范围已更改,现在指向 .Values.nodeCount。所以 .Values.replicaCount 现在指向 .Values.nodeCount.Values.replicaCount。因为 values.yaml 文件中没有这样的字段,所以你会收到此错误。

在第二个循环中使用 $.Values.replicaCount 而不是 .Values.replicaCount

参考:helm.sh