26

I found that we can create subcharts and conditionally include them as described here: Helm conditionally install subchart

I have just one template that I want conditionally include in my chart but I could not find anything in the docs. Is there such feature?

Eduardo
  • 5,645
  • 4
  • 49
  • 57

2 Answers2

44

I discovered that empty templates are not loaded. I solved it by wrapping my yaml file content in an if condition.

{{ if .Values.something }}
content of yaml file
{{ end }}
Eduardo
  • 5,645
  • 4
  • 49
  • 57
  • How would I check if a Variable is Null. For instance, how can I create a Template only if a variable `.Values.something` is Null – Kiran Jun 23 '20 at 08:02
  • 2
    According to [helm docs](https://helm.sh/docs/chart_template_guide/control_structures/), _a pipeline is evaluated as false if the value is a nil (empty or null)_ @Kiran – Eduardo Jun 23 '20 at 21:24
  • When I use this aproach with a secret, it deletes the secret on upgrade – PhoneixS Jul 01 '20 at 15:59
35

You simply wrap the template resource at the first and last lines with the check you want to do. Let's take the official Grafana chart as example:

In its values.yaml, it has a flag called ingress.enabled, which looks like the following:

ingress:
  enabled: false

Then in its ingress template resource, this flag is checked:

{{- if .Values.ingress.enabled -}}
...
apiVersion: extensions/v1beta1
kind: Ingress
...
{{- end }}

As a result, ingress object will only be created if ingress.enabled is set to true.

Utku Özdemir
  • 7,390
  • 2
  • 52
  • 49