Introduction
- Helm uses Go templating to dynamically generate Kubernetes manifests. By leveraging Helm templates, you can create reusable, flexible configurations for your Kubernetes applications. In this blog, we will explore:
- How to use values from values.yaml
- Helm template objects like .Release and .Chart
- Commonly used functions in Helm templating
- Examples of using functions like lower and replace
- Conditional statements (if-else) in Helm
Using Values from values.yaml
Values from values.yaml can be referenced in template files using {{ .Values }}
.
Example
: Accessing Values from values.yaml
values.yaml
1
2
3
| app:
name: my-app
replicaCount: 2
|
deployment.yaml
(inside templates/
folder)
1
2
3
4
5
6
| apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ .Values.app.name }}
spec:
replicas: {{ .Values.app.replicaCount }}
|
When you run helm template .
, Helm replaces placeholders with values from values.yaml
.
Using .Release and .Chart Objects
- .Release Object
- .Chart Object
Helm Templating Functions
- Helm includes several built-in functions for manipulating data. Some of the commonly used ones are:
Function | Description |
---|
lower | Converts a string to lowercase |
upper | Converts a string to uppercase |
replace | Replaces a substring in a string |
default | Provides a default value if a variable is empty |
quote | Wraps a string in quotes |
ternary | Returns one of two values based on a condition |
range | Iterates over lists and maps |
- Example 1: Using
lower
Function
1
2
| metadata:
name: {{ .Values.app.name | lower }}
|
- If
app.name
in values.yaml is MyApp
, this will render:
1
2
| metadata:
name: myapp
|
- Example 2: Using
replace
Function
1
2
3
| metadata:
annotations:
description: "{{ .Values.app.name | replace "-" "_" }}"
|
- If
app.name
is my-app
, the result will be:
1
2
3
| metadata:
annotations:
description: "my_app"
|
Example: Using range to Loop Through a List
- If you have a list of environment variables in values.yaml:
1
2
3
4
5
| env:
- name: ENV
value: production
- name: DEBUG
value: "false"
|
- You can iterate over them in your template:
1
2
3
4
5
| env:
{{- range .Values.env }}
- name: {{ .name }}
value: {{ .value }}
{{- end }}
|
1
2
3
4
5
| env:
- name: ENV
value: production
- name: DEBUG
value: "false"
|
- If you need to iterate over key-value pairs in a dictionary:
1
2
3
| config:
app_name: MyApp
debug: "true"
|
1
2
3
| {{- range $key, $value := .Values.config }}
{{ $key }}: {{ $value }}
{{- end }}
|
1
2
| app_name: MyApp
debug: true
|
Conditional Statements (if-else)
Helm allows conditional rendering using if, else, and end
.
Example: Using if-else to Enable Ingress
values.yaml
1
2
| ingress:
enabled: true
|
ingress.yaml
1
2
3
4
5
6
7
8
9
10
11
| {{- if .Values.ingress.enabled }}
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: {{ .Release.Name }}-ingress
spec:
rules:
- host: {{ .Release.Name }}.example.com
{{- else }}
# Ingress is disabled
{{- end }}
|
- If ingress.enabled is true, the ingress resource is created. Otherwise, the comment # Ingress is disabled appears in the rendered YAML.
Conditional Operators for Template Logic
Operator | Description | Example |
---|
eq | Checks if two values are equal | {{ if eq .Values.env "production" }} |
ne | Checks if two values are not equal | {{ if ne .Values.env "development" }} |
lt | Checks if the first value is less than the second | {{ if lt .Values.replicas 3 }} |
gt | Checks if the first value is greater than the second | {{ if gt .Values.replicas 1 }} |
le | Checks if the first value is less than or equal to the second | {{ if le .Values.replicas 5 }} |
ge | Checks if the first value is greater than or equal to the second | {{ if ge .Values.replicas 2 }} |
and | Logical AND condition | {{ if and .Values.enabled .Values.ingress.enabled }} |
or | Logical OR condition | {{ if or .Values.enabled .Values.backup.enabled }} |
Conclusion
Go templating in Helm provides powerful features for dynamic configuration. By using values from values.yaml, built-in objects like .Release and .Chart, common functions, and conditionals, you can create flexible Helm charts that adapt to different environments.
Mastering Helm templating will help you build robust and reusable Kubernetes deployments.