我有一个模板,可以使用以下命令渲染多次range
迭代,我可以访问变量外部变量,例如$.Release.Name
没有问题。但是,当我包含模板时,我无法让它工作:
{{ range $key, $val := $.Values.resources }}
...
annotations:
checksum/config: {{ include (print $.Template.BasePath "/secrets.yaml") . | sha256sum }}
{{ end }}
And in secrets.yaml
:
apiVersion: "v1"
kind: "Secret"
metadata:
name: {{ $.Release.Name }}-secrets
我收到这个错误:
Error: render error in "botfront-project/templates/deployment.yaml": template: [filename] :19:28: executing [filename] at <include (print $.Template.BasePath "/secrets.yaml") .>: error calling include: template: .../secrets.yaml:4:19: executing ".../secrets.yaml" at <$.Release.Name>: nil pointer evaluating interface {}.Name
如何访问包含的模板内的变量?
TL;DR;
只需更换.
with $
使用全局范围而不是您创建的本地范围。
Example:
{{- include "my-chart.labels" $ | nindent 4 }}
说明
根据文档,https://helm.sh/docs/chart_template_guide/control_structs/#modifying-scope-using-with https://helm.sh/docs/chart_template_guide/control_structures/#modifying-scope-using-with:
我们可以使用 $ 从父级访问对象 Release.Name
范围。
当模板执行开始时,$被映射到根范围
并且在模板执行期间它不会改变
With range
我们改变循环内的范围。的确,{{- include "my-chart.labels" . | nindent 4 }}
将调用当前范围.
.
因此,如果您深入研究 helm 文档中的“范围”内容,您最终会找到这部分:https://helm.sh/docs/chart_template_guide/variables/ https://helm.sh/docs/chart_template_guide/variables/
以此为例:
{{- range .Values.tlsSecrets }}
apiVersion: v1
kind: Secret
metadata:
name: {{ .name }}
labels:
# Many helm templates would use `.` below, but that will not work,
# however `$` will work here
app.kubernetes.io/name: {{ template "fullname" $ }}
# I cannot reference .Chart.Name, but I can do $.Chart.Name
helm.sh/chart: "{{ $.Chart.Name }}-{{ $.Chart.Version }}"
app.kubernetes.io/instance: "{{ $.Release.Name }}"
# Value from appVersion in Chart.yaml
app.kubernetes.io/version: "{{ $.Chart.AppVersion }}"
app.kubernetes.io/managed-by: "{{ $.Release.Service }}"
type: kubernetes.io/tls
data:
tls.crt: {{ .certificate }}
tls.key: {{ .key }}
---
{{- end }}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)