This article explores using conditionals in Helm charts to include or exclude template sections based on values in the values file.
In this article, we explore how to use conditionals in Helm charts to include or exclude sections of your templates based on values specified in the values file. With these techniques, you can add optional configurations such as custom labels, control whitespace, and conditionally render entire objects like service accounts—all of which add flexibility to your deployments.
Consider a basic Helm chart with a simple service template. Initially, you might have a values.yaml file with minimal configuration and a corresponding service.yaml template:
Now, suppose different releases require different labels—for example, adding an organizational label to group objects. You can update your configuration as follows:
However, when the orgLabel value is optional, you want these label lines to appear only when the value is provided. Similar to conditionally executing code in programming languages such as Python:
Copy
Ask AI
orgLabel = "payroll"if orgLabel: print(orgLabel)
In Helm charts, you can achieve the same behavior using if blocks. Here’s an updated version of the service.yaml file that conditionally adds the organization label:
When Helm renders your templates, it may leave unintended whitespace between directives. To avoid this, use a dash after the opening braces—and before the closing braces, if needed—as shown below:
Copy
Ask AI
{{- if .Values.orgLabel }} labels: org: {{ .Values.orgLabel }}{{- end }}
This simple trick ensures that your generated YAML files remain clean and free of extra spaces.
Helm templates support conditional logic that includes if, else if, and else constructs. Similar to Python’s conditional logic, you can use helper functions such as eq for equality comparisons. Consider this Python pseudocode:
You can achieve similar logic in a Helm template as follows:
Copy
Ask AI
# service.yamlapiVersion: v1kind: Servicemetadata: name: {{ .Release.Name }}-nginx {{- if .Values.orgLabel }} labels: org: {{ .Values.orgLabel }} {{- else if eq .Values.orgLabel "hr" }} labels: org: human resources {{- end }}spec: ports: - port: 80 name: http selector: app: hello-world
In this template, if orgLabel is provided, its value is used. If it specifically equals "hr", the label is set to “human resources”. This approach leverages Helm’s helper functions for clear and concise conditional logic.
A common use case for Helm conditionals is to control the creation of certain Kubernetes objects based on configuration. For example, you might want to create a ServiceAccount only if it is explicitly enabled in your values.yaml file.In your default values.yaml, you can add a section like this:
Copy
Ask AI
# Default values for nginx-chart.serviceAccount: # Specifies whether a ServiceAccount should be created create: true
Then, wrap the entire ServiceAccount template in a conditional block to ensure it is created only when needed:
Copy
Ask AI
{{- if .Values.serviceAccount.create }}apiVersion: v1kind: ServiceAccountmetadata: name: {{ .Release.Name }}-robot-sa{{- end }}
This technique provides significant flexibility, allowing users to control which resources are deployed according to their specific requirements.This concludes our deep dive into using conditionals in Helm charts. We demonstrated how to add optional labels, trim whitespace in rendered templates, employ if-else logic using helper functions, and conditionally create resources like service accounts. By leveraging these techniques, you can create more dynamic, efficient, and maintainable Helm charts.Happy templating!For more information, explore the official Helm Documentation.