In our previous blogs, we covered the basics of Helm and how to set up your environment and create your first Helm chart. In this blog, we’ll dive deeper into Helm’s templating capabilities, focusing on advanced techniques that make Helm charts dynamic, reusable, and powerful.
Purpose of Advanced Helm Templating
Advanced templating allows you to:
- Use loops and conditionals to create resources dynamically.
- Create reusable logic through helper templates.
- Dynamically configure ConfigMaps, Secrets, and environment variables.
Using Loops and Conditional Statements
1. Iterating Over a List of Values
Loops in Helm are used to dynamically generate resources for a list of items. This is achieved using the range
function.
Example: Generating a list of environment variables for a ConfigMap:
apiVersion: v1
kind: ConfigMap
metadata:
name: example-configmap
namespace: {{ .Release.Namespace }}
data:
{{- range $key, $value := .Values.envVars }}
{{ $key }}: {{ $value | quote }}
{{- end }}
values.yaml:
envVars:
APP_MODE: production
LOG_LEVEL: debug
The above template will create a ConfigMap with the keys and values from the envVars
list in values.yaml
.
2. Conditional Creation of Resources
Helm allows conditional logic using if
, else
, and else if
statements to determine whether or not a resource should be created.
Example: Conditionally creating a Service:
{{- if .Values.service.enabled }}
apiVersion: v1
kind: Service
metadata:
name: {{ .Release.Name }}-service
spec:
type: {{ .Values.service.type }}
ports:
- port: {{ .Values.service.port }}
targetPort: {{ .Values.service.targetPort }}
selector:
app: {{ .Release.Name }}
{{- end }}
values.yaml:
service:
enabled: true
type: ClusterIP
port: 80
targetPort: 8080
This approach ensures that a Service is only created if service.enabled
is set to true
.
Implementing Helper Templates
Helper templates are reusable snippets of logic defined in the _helpers.tpl
file. They simplify repetitive tasks and improve code readability.
Example: Defining and using a helper template for resource labels:
_helpers.tpl:
{{- define "common.labels" -}}
app: {{ .Chart.Name }}
release: {{ .Release.Name }}
env: {{ .Values.environment | default "dev" }}
{{- end }}
Using the helper in a Deployment:
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ .Release.Name }}-deployment
labels:
{{ include "common.labels" . | nindent 4 }}
spec:
replicas: {{ .Values.replicaCount }}
selector:
matchLabels:
app: {{ .Chart.Name }}
template:
metadata:
labels:
{{ include "common.labels" . | nindent 8 }}
spec:
containers:
- name: {{ .Release.Name }}
image: {{ .Values.image.repository }}:{{ .Values.image.tag }}
Helper templates make your chart modular and easier to maintain.
Handling ConfigMaps, Secrets, and Environment Variables Dynamically
1. Dynamic ConfigMaps
ConfigMaps can be generated dynamically using loops and values defined in values.yaml
.
Example:
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ .Release.Name }}-configmap
data:
{{- range $key, $value := .Values.configurations }}
{{ $key }}: {{ $value | quote }}
{{- end }}
values.yaml:
configurations:
APP_ENV: production
DEBUG_MODE: "false"
2. Managing Secrets
Secrets can also be templated dynamically for sensitive data.
Example:
apiVersion: v1
kind: Secret
metadata:
name: {{ .Release.Name }}-secret
data:
{{- range $key, $value := .Values.secrets }}
{{ $key }}: {{ $value | b64enc }}
{{- end }}
values.yaml:
secrets:
DB_PASSWORD: my_secure_password
API_KEY: my_api_key
This template ensures all secret values are base64-encoded.
3. Environment Variables in Multi-Container Pods
Helm makes it easy to configure environment variables for multi-container pods dynamically.
Example:
spec:
containers:
{{- range $index, $container := .Values.containers }}
- name: {{ $container.name }}
image: {{ $container.image }}
env:
{{- range $key, $value := $container.env }}
- name: {{ $key }}
value: {{ $value | quote }}
{{- end }}
{{- end }}
values.yaml:
containers:
- name: app-container
image: nginx:latest
env:
NODE_ENV: production
LOG_LEVEL: debug
- name: sidecar-container
image: busybox:latest
env:
MONITORING: enabled
This configuration dynamically generates the container definitions and their respective environment variables.
Conclusion
Helm’s advanced templating features make it an invaluable tool for Kubernetes deployments. By leveraging loops, conditional statements, helper templates, and dynamic configurations, you can create reusable, flexible, and efficient charts tailored to your application’s needs. In the next blog, we’ll explore building deployment-ready Helm charts with best practices for production environments.
Checkout article on building deployment ready helm charts : https://dev.to/manjunath_kotabal_3d1e736/building-deployment-ready-helm-charts-3b08
Top comments (1)
Amazing!