1

When I have a line in my values.yaml file:

condition: "eq .Release.Namespace dev"

Can I evaluate this in the template somehow?

{{- if .condition  }}
status: "development"
{{- end }}

I've tried to do the above but that is not working, is there something else I can do to achieve this?

Ravenix
  • 1,010
  • 4
  • 15
  • 40

1 Answers1

3

A better practice would be to have a Helm values file that explicitly stated these sorts of conditions, even if they're redundant.

# values.dev.yaml
status: development
# configmap.yaml
status: {{ .Values.status }}
helm install release-name . -f values.dev.yaml -n dev

This is technically possible, though. One of Helm's extension functions (not part of the core Go text/template library or Sprig) is tpl, which allows you to evaluate an arbitrary string as though it were a template string. You'd need to wrap that value in {{ ... }} to force it to be evaluated as an expression. But, this does work, and helm template outputs the correct values:

apiVersion: v1
kind: ConfigMap
metadata:
  name: demo
data:
  {{/* This is the raw condition string from values.yaml */}}
  condition: {{ .Values.condition }}

  {{/* This calls `tpl` on that string, and dumps out the result */}}
  evaluated: {{ tpl (printf "{{%s}}" .Values.condition) . }}

  {{/* `tpl` produces a string out, but you can compare it */}}
{{- if eq "true" (tpl (printf "{{%s}}" .Values.condition) .) }}
  status: development
{{- else }}
  status: production
{{- end }}

My experience with Helm templating is that it's possible to do some pretty complicated things in a template, but once you get up to writing serious code and dynamic evaluation there, you start to get into normal software lifecycle problems, and for example it's hard to unit-test this setup.

David Maze
  • 130,717
  • 29
  • 175
  • 215