Helm require value without using it

2/12/2020

Is it possible to have a required .Value without using it in the template.

For example in my case I want to require to write a password for a subchart of mongodb but I won't use it on my templates so can I have something like bellow in a template:

{{- required 'You must set a mongodb password' .Values.mongodb.mongodbPassword | noPrint -}}
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "cloud.fullname" . }}
  labels:
    {{- include "cloud.labels" . | nindent 4 }}
    app.kubernetes.io/component: cloud
spec:
  replicas: {{ .Values.cloud.minReplicaCount }}
  selector:
....

And the result would be something like:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: blablablabla
...
-- PhoneixS
kubernetes-helm
mongodb
templates

2 Answers

2/12/2020

Yes, it is possible. Let's consider the below Values.yaml file:

Values.yaml:

mongodb:
  mongodbPassword: "AbDEX***"

So, you want to generate the deployment file only if the password is set. You can do by using if-block of go-templating. If the length of the password field is greater than zero, the deployment yaml will be generated otherwise not.

{{- if  .Values.mongodb.mongodbPassword}}
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "cloud.fullname" . }}
  labels:
    {{- include "cloud.labels" . | nindent 4 }}
    app.kubernetes.io/component: cloud
spec:
  replicas: {{ .Values.cloud.minReplicaCount }}
  selector:
....
{{- end }}

Reference:

{{if pipeline}} T1 {{end}}
    If the value of the pipeline is empty, no output is generated;
    otherwise, T1 is executed. The empty values are false, 0, any nil pointer or
    interface value, and any array, slice, map, or string of length zero.
    Dot is unaffected.
-- Kamol Hasan
Source: StackOverflow

2/13/2020

Possibly the most direct way is to use sprig's fail function.

{{- if not .Values.mongodb.mongodbPassword -}}
{{- fail "You must set a mongodb password" -}}
{{- end -}}

Assigning the required expression to a variable (that you never use) will probably also have the desired effect.

{{- $unused := required "You must set a mongodb password" .Values.mongodb.mongodbPassword -}}
-- David Maze
Source: StackOverflow