Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 580
Chillax Avatar asked Dec 20 '18 09:12

Chillax


People also ask

How do you comment in Helm chart?

Both YAML and Helm Templates have comment markers. Inside of templates, YAML comments may be used when it is useful for Helm users to (possibly) see the comments during debugging. The comment above is visible when the user runs helm install --debug , while comments specified in {{- /* */}} sections are not.

How do I find the value of a Helm chart?

You can use helm -n <namespace> get values <release-name> to just get the values install used/computed rather than the whole chart and everything, or helm -n <namespace> get manifest <release-name> to view the generated resource configurations††.

How do you use variables in Helm?

In Helm templates, a variable is a named reference to another object. It follows the form $name . Variables are assigned with a special assignment operator: := . We can rewrite the above to use a variable for Release.Name .

How do I change the value of a Helm chart?

Using the --values flag You can use a --values flag in your Helm commands to override the values in a chart and pass in a new file. Specify the name of the new file after the --values flag in the Helm command. Example: helm upgrade --install <service> -f values.


3 Answers

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.

like image 119
Ryan Dawson Avatar answered Sep 29 '22 19:09

Ryan Dawson


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
like image 26
edbighead Avatar answered Sep 29 '22 21:09

edbighead


Define the required values on top of your manifest as variables utilizing the required function.

E.g. deployment.yaml:

{{- $name := .Values.name | required ".Values.name is required." -}}

---
apiVersion: apps/v1
kind: Deployment
metadata: 
  name: {{ $name }}
 ....
like image 4
pijemcolu Avatar answered Sep 29 '22 21:09

pijemcolu