Search code examples
kubernetes-helm

Helm condition check on two values and return useful message


I want to check the following values and would like to give the user of the chart a useful message during rendering/installing. My condition is already working but its not exiting its just does not render if condition is failing.

clusterabc:
  - name: dc1
    replica: 2
    ip: ["xx", "y"]
# my condition is to check the length of ip array and the replica should be same if not then use should get a message to check... this mismatch

This is working but it throws not a error {{- if and (eq (len $value.ip | int) ($value.replica | int))) (eq $.Values.dc $value.name }}

How can I give the user a error like the required function


Solution

  • fail

    Unconditionally returns an empty string and an error with the specified text. This is useful in scenarios where other conditionals have determined that template rendering should fail.

    fail "Please accept the end user license agreement"


    demo

    values.yaml

    clusterabc:
      - name: dc1
        replica: 3
        ip: ["xx", "y"]
    

    template/NOTES.txt

    Check the length of ip array and the replica should be same
    {{- $flag := true -}}
    {{- range $i, $val := $.Values.clusterabc -}}
    {{- if ne (len $val.ip | int) ($val.replica | int) -}}
    {{- $flag = false }}
    {{- end -}}
    {{- end -}}
    {{- if not $flag -}}
    {{ fail "check clusterabc fail..." }}
    {{- else }}
    check clusterabc success...
    {{- end -}}
    

    helm install xxxx .

    output:

    Error: execution error at (xxx/templates/NOTES.txt:xx:x): check clusterabc fail...