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

12/20/2018

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?

-- Chillax
kubernetes-helm

3 Answers

12/20/2018

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
-- edbighead
Source: StackOverflow

12/24/2018

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.

-- Ryan Dawson
Source: StackOverflow

2/28/2020

To expose name of missing item to required text you can do something like this:

{{- range $field, $my_key := $data }}
 {{- if hasKey $dic1 $my_key }}
  {{ $field }}: {{ index $dic1 $my_key | b64enc}}
 {{- else if hasKey $dic2 $my_key }}
  {{ $field }}: {{ index $dic2 $my_key | b64enc}}
 {{- else }}
  {{ $field }}: {{ required (printf "key %s is missing" $my_key) nil }}
 {{- end }}
{{- end }}
-- Essential15
Source: StackOverflow