I'm installing up a helm chart using helm install
command. I have values.yaml
which takes a few inputs from the user. One of the keys in values.yaml
is action, which can only take three predefined values (let's say action1, action2 and action3) as an input. Any other value other than this is invalid.
When a user provides the value to action field in values.yaml
and trigger the helm install
command, the first thing I need to check is that if the action key has a valid value or not. If the action value is invalid, I want the release to be failed with a proper error message.
e.g.: In case the user has given action: action4
, this is not valid and release should fail as .Values.action
can only be action1, action2, or action3.
How I can achieve this use case and which file should be best to handle this validation considering the helm structure?
With HelmV3 there is now an easier way. Just specify a schema that includes your values.
For example:
title: Values
type: object
properties:
action:
description: Some action
type: string
pattern: "^(action1|action2|action3)quot;
I was able to achieve the use case with below changes. Added the following code in _helpers.tpl
{{- define "actionValidate" -}}
{{ $action := .Values.actions }}
{{- if or (eq $action "action1") (eq $action "action2") (eq $action "action3") -}}
true
{{- end -}}
{{- end -}}
Invoked this function from a .tpl file like this:-
{{ include "actionValidate" . | required "Action value is incorrect. The valid values are 'action1', 'action2', 'action3' " }}
Helm uses Go templating, so you can add some custom tricks. Read the following resource: https://github.com/helm/helm/blob/master/docs/charts_tips_and_tricks.md. You can check if some already provided functions are good enough for you, for example, required
.