我有一个带有 2 个子图表的父图表。父图表有 global.myflag 而子图表有 myflag 字段,在它们各自的 values.yaml 中。我想要灵活性,可以独立部署子图表。因此,我在子图表 _helper.tpl 中添加了一个模板函数,我想在其中检查 - 如果 global.myflag 存在,则使用该值 - 否则使用子图表中 myflag 的值
模板将返回真/假。像这样的东西——
{{- define "isFlagEnabled" -}}
{{- $flag := false -}}
{{- if .Values.myflag -}}
{{- $flag := .Values.myflag -}}
{{- end -}}
{{- if .Values.global.myflag -}}
{{- $flag := .Values.global.myflag -}}
{{- end -}}
{{- printf "%s" $flag -}}
{{- end -}}
Run Code Online (Sandbox Code Playgroud)
并使用这个值(真/假),我想在我的 config.yaml 中设置一些值。
{{- if eq (value from template) true -}}
Run Code Online (Sandbox Code Playgroud)
我在这里有两个问题 - 1. 我们可以对模板值设置“if”条件吗?如何?2. 有没有更好的方法来做到这一点?
修饰和清理你的功能
{{- define "isFlagEnabled" -}}
{{- if .Values.global -}} <-- check parent exists to avoid nil pointer evaluating interface {}.myflag
{{- if .Values.global.myflag -}}
{{- .Values.global.myflag -}}
{{- end -}}
{{- else if .Values.myflag -}} <-- make sure its else if so you wont override if both defined
{{- .Values.myflag -}}
{{- end -}}
{{- else -}}
{{- printf "false" }}
{{- end -}}
Run Code Online (Sandbox Code Playgroud)
在 golang 模板语法中使用模板时,您需要使用圆括号将它们转义:
{{- define "flagUsage" -}}
{{- if eq (include "isFlagEnabled" .) "true" -}}
{{- printf "%s" (include "isFlagEnabled" .) -}}
{{- end -}}
{{- end -}}
Run Code Online (Sandbox Code Playgroud)
注意模板在示例中被使用了两次,一次作为 if 运算符的操作数,另一次作为标签的文本
{{- if eq (include "isFlagEnabled" .) "true" -}} <--- operand used in spring function surrounded by `{{ }}`
apiVersion: v1
kind: Service
metadata:
name: {{ include "my-chart.fullname" . }}
labels:
my-meta-label: {{ include "isFlagEnabled" . }} <---- plain text
spec:
type: {{ .Values.service.type }}
ports:
- port: {{ .Values.service.port }}
targetPort: http
protocol: TCP
name: http
selector:
{{- include "my-chart.selectorLabels" . | nindent 4 }}
{{- end }}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
15524 次 |
最近记录: |