Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Helm Conditional Templates

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?

like image 919
Eduardo Avatar asked Sep 10 '19 22:09

Eduardo


People also ask

What does {{ }} mean in Helm?

The Helm template syntax is based on the Go programming language's text/template package. The braces {{ and }} are the opening and closing brackets to enter and exit template logic.

Does Helm use Go templates?

While we talk about the "Helm template language" as if it is Helm-specific, it is actually a combination of the Go template language, some extra functions, and a variety of wrappers to expose certain objects to the templates.

What are Helm templates?

Templates generate manifest files, which are YAML-formatted resource descriptions that Kubernetes can understand. We'll look at how templates are structured, how they can be used, how to write Go templates, and how to debug your work. This guide focuses on the following concepts: The Helm template language.

Does Helm use Jinja?

Helm uses Go templates to define Kubernetes (yaml) manifests. We were already unsatisfied by using Jinja and we did not see a huge improvement from our previous system, the main reason being: YAML files are not suitable to be managed by text templating frameworks.


2 Answers

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 }}
like image 149
Eduardo Avatar answered Sep 21 '22 19:09

Eduardo


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.

like image 33
Utku Özdemir Avatar answered Sep 23 '22 19:09

Utku Özdemir