【问题标题】:Helm template not able to read ip address - can't evaluate field ipAddress in type stringHelm 模板无法读取 IP 地址 - 无法评估字符串类型中的字段 ipAddress
【发布时间】:2022-08-16 00:35:18
【问题描述】:
我正在尝试为 Istio 的 ServiceEntry 创建一个 helm 模板,其中包含静态 IP 地址的地址列表。在 values.yaml 中,我有
- name: test-se
namespace: test-se-ns
egressUrls:
- mydbhost.com
port: 32306
protocol: TCP
ipAddress: 10.2.2.2
在 .tpl 文件中,我试图将 ipAddress 的值添加到列表中
{{- with .ipAddress }}
addresses:
- {{ .ipAddress | quote }}
{{- end }}
总是失败并出现异常
模板/_service_entry.tpl:18:13:在 <.ipAddress> 处执行 \"common.serviceentry.tpl\":无法评估字符串类型中的字段 ipAddress
知道我做错了什么吗?
标签:
kubernetes
kubernetes-helm
go-templates
【解决方案1】:
如果您使用with,则将您用作with 的东西制作为该块内的上下文。
所以,用点来指代它。
{{- with .ipAddress }}
addresses:
- {{ . | quote }}
{{- end }}
来自docs:
{{with pipeline}} T1 {{end}}
If the value of the pipeline is empty, no output is generated;
otherwise, dot is set to the value of the pipeline and T1 is
executed.
在这种情况下,if 似乎也很合适,因为您对新上下文的操作并不多。
{{- if .ipAddress }}
addresses:
- {{ .ipAddress | quote }}
{{- end }}
【解决方案2】:
当您在 Helm 中使用 with 时,您会更改 . 的范围,因此 Helm 会查找对象而不是字符串,您可以在 docs 中了解更多信息。
但无论如何,我认为在你的情况下,你需要使用range 而不是with,你可以看一个例子here