How to fail a helm release based on inputs in values.yaml

4/2/2019

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?

-- Saurabh
kubernetes
kubernetes-helm

3 Answers

11/29/2019

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;
-- Daniel Habenicht
Source: StackOverflow

4/3/2019

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' " }}
-- Saurabh
Source: StackOverflow

4/3/2019

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.

-- RafaƂ Leszko
Source: StackOverflow