图表模板中的嵌套循环

use*_*963 2 go-templates kubernetes kubernetes-helm

我无法为 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
Run Code Online (Sandbox Code Playgroud)

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

# number of nodes / shards
nodeCount: 5
replicaCount: 3
Run Code Online (Sandbox Code Playgroud)

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

    <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>
Run Code Online (Sandbox Code Playgroud)

Emr*_*ain 5

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

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

参考:helm.sh