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

折月煮酒 提交于 2020-05-27 05:17:47

问题


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?


回答1:


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



回答2:


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.




回答3:


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


来源:https://stackoverflow.com/questions/55478581/how-to-fail-a-helm-release-based-on-inputs-in-values-yaml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!