Post

Go Templating in Helm Charts

Explore Go Templating in Helm Charts

Go Templating in Helm Charts

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
    • The .Release object provides metadata about the Helm release.

      1
      2
      3
      
      metadata:
        name: {{ .Release.Name }}
        namespace: {{ .Release.Namespace }}
      
    • .Release.Name → Name of the Helm release
    • .Release.Namespace → Namespace in which the chart is deployed
  • .Chart Object
    • The .Chart object contains metadata from Chart.yaml.

      1
      2
      3
      
      metadata:
        labels:
          chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
      
    • .Chart.Name → Name of the chart
    • .Chart.Version → Chart version

Helm Templating Functions

  • Helm includes several built-in functions for manipulating data. Some of the commonly used ones are:
FunctionDescription
lowerConverts a string to lowercase
upperConverts a string to uppercase
replaceReplaces a substring in a string
defaultProvides a default value if a variable is empty
quoteWraps a string in quotes
ternaryReturns one of two values based on a condition
rangeIterates 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 }}
  • This will generate:
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"
  • You can use:
1
2
3
{{- range $key, $value := .Values.config }}
{{ $key }}: {{ $value }}
{{- end }}
  • This will output:
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

OperatorDescriptionExample
eqChecks if two values are equal{{ if eq .Values.env "production" }}
neChecks if two values are not equal{{ if ne .Values.env "development" }}
ltChecks if the first value is less than the second{{ if lt .Values.replicas 3 }}
gtChecks if the first value is greater than the second{{ if gt .Values.replicas 1 }}
leChecks if the first value is less than or equal to the second{{ if le .Values.replicas 5 }}
geChecks if the first value is greater than or equal to the second{{ if ge .Values.replicas 2 }}
andLogical AND condition{{ if and .Values.enabled .Values.ingress.enabled }}
orLogical 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.

This post is licensed under CC BY 4.0 by the author.