简体   繁体   中英

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

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.

eg: 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?

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' " }}

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)$"

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 .

Not enough rep to comment. However, in response to @Saurabh, you now must YAML comment out the function invocation. Otherwise, Go Template will encounter an invalid return type runtime error from that random true value existing in the middle of the invocation file.

Example

# validation.yaml
---
#{{ include "actionValidate" .  | required "Action value is incorrect. The valid values are 'action1', 'action2', 'action3' " }}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM