eramos
eramos

Reputation: 324

Helm template function to detect node within values.yaml

I will try to reproduce a very strange behaviour (or my misunderstanding probably) of a helm template function which basically detects the existence of a node within values.yaml file. Actually is not so simple but it is the simplest way to show the problem:

I'm using v2.16.1 helm version, but it is reproduced on newer 3.3. Follow the next steps:

  1. Create an example chart
helm create myChart
  1. Append the function to myChart helpers
cat << EOF >> myChart/templates/_helpers.tpl

{{- define "myChart.hasNodeA" -}}
    {{- \$found := false -}}
    {{- if .Values.A -}}
        {{- \$found := true -}}
    {{- end -}}
    {{- print \$found -}}
{{- end -}}
EOF

(note the dollar escapes, that's only to ease you append the function with the cat/EOF)

  1. Append the rendered condition within deployment:
cat << EOF >> myChart/templates/deployment.yaml
{{- if eq (include "myChart.hasNodeA" .) "true" }}
  hasNodeA: "yes"
{{- end }}
{{- if eq (include "myChart.hasNodeA" .) "false" }}
  hasNodeA: "no"
{{- end }}
EOF
  1. Render without and with the node at values:
helm template myChart | grep hasNodeA

This shows "no", and although it is "correct", it is because the function always returns 'false'

Adding the node:

cat << EOF >> myChart/values.yaml
A:
  whatever: 555
EOF

You will see the problem:

helm template myChart | grep hasNodeA

THIS SHOW "no" again.

The thing is that the function condition is being fulfilled, indeed, if you test this one (edit the _helpers.tpl file moving the printout to the middle):

{{- define "myChart.hasNodeA" -}}
    {{- $found := false -}}
    {{- if .Values.A -}}
        {{- $found := true -}}
        {{- print $found -}}
    {{- end -}}
{{- end -}}

You will see that the condition is really passed:

$> helm template myChart | grep hasNodeA
hasNodeA: "yes"

Obviously this does not work for negative cases. The strange thing here is that the variable configured within the condition seems to be ignored when printed at the end.

Any idea about this behaviour ? Thank U

Upvotes: 0

Views: 967

Answers (1)

In Helm, the use of the operator := creates a new variable inside its context. In this case, you are creating a new $found variable. In Helm v3 you can solve this using = operator.

If you're using Helm v2 then this will be solve using a lot of ifs.

BR

Upvotes: 1

Related Questions