我建议使用命名模板进行验证,您可以在安装说明中执行,而不是常规模板文件。此外,通过对单个值使用故障安全验证函数(与 不同require
),您可以在helm
命令执行失败之前验证多个值。
Bitnami 图表使用这种策略。
设置
在部分文件中,例如templates/_helpers.tpl
,定义命名模板以验证提供的值:
{{/* Compile all validation warnings into a single message and call fail. */}}
{{- define "mychart.validateValues" -}}
{{- $messages := list -}}
{{- $messages = append $messages (include "mychart.validateValues.foo" .) -}}
{{- $messages = append $messages (include "mychart.validateValues.bar" .) -}}
{{- $messages = without $messages "" -}}
{{- $message := join "\n" $messages -}}
{{- if $message -}}
{{- printf "\nVALUES VALIDATION:\n%s" $message | fail -}}
{{- end -}}
{{- end -}}
{{/* Validate value of foo */}}
{{- define "mychart.validateValues.foo" -}}
{{- if not (and .Values.foo (kindIs "string" .Values.foo)) -}}
mychart: foo
`foo` is required and should be a non-empty string
{{- end -}}
{{- end -}}
{{/* Validate the value of bar */}}
{{- define "mychart.validateValues.bar" -}}
{{- if not (and .Values.bar (kindIs "string" .Values.bar)) -}}
mychart: bar
`bar` is required and should be a non-empty string
{{- end -}}
{{- end -}}
在这种情况下,命名模板mychart.validateValues
将运行多个验证(即mychart.validateValues.foo
)mychart.validateValues.bar
。如果一个或多个验证产生验证警告,它将调用fail
警告摘要。
在templates/NOTES.txt
中,评估命名模板以进行验证:
{{- include "mychart.validateValues" . }}
测试
使用以下values.yaml
文件:
bar: 24
模板化、安装或升级图表将失败并显示以下消息:
Error: execution error at (test/templates/NOTES.txt:1:4):
VALUES VALIDATION:
mychart: foo
`foo` is required and should be a non-empty string
mychart: bar
`bar` is required and should be a non-empty string