How best to say a value is required in a helm chart?

时光毁灭记忆、已成空白 提交于 2019-12-23 15:30:00

问题


I am doing this now:

value: {{ required "A valid .Values.foo entry required!" .Values.foo }}

But to give this same message for all required values in the templates is cumbersome and clutters the templates in my opinion.

Is there a better way where we could define it outside the template \ or a cleaner way to do it within the template itself?


回答1:


You could do something by taking advantage of range and the fact that null will fail the required check. So in your values.yaml you could have this section for required env vars:

reqEnv:
 - name: "VAR1"
   value: null
 - name: "VAR2"
   value: null

And in the env section of the Deployment you then have:

{{- range .Values.reqEnv }}
          {{ .name }}: {{ required "A value must be entered for all reqEnv entries" .value }}
{{- end }}

Then the user gets an error unless they set all required values of the reqEnv section in their values file or as paramters. Unfortunately what you lose by doing this is the detail of which var is missing. This could be why the official helm charts seem to prefer using required in the way that you already are.




回答2:


You can use helm lint with --strict flag to check undefined values

$ helm lint --strict . 
==> Linting .
[INFO] Chart.yaml: icon is recommended
[ERROR] templates/: render error in "mychart/templates/service.yaml": template: mychart/templates/service.yaml:10:19: executing "mychart/templates/service.yaml" at <.Values.foo>: map has no entry for key "foo"

Error: 1 chart(s) linted, 1 chart(s) failed


来源:https://stackoverflow.com/questions/53866196/how-best-to-say-a-value-is-required-in-a-helm-chart

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!