DEV Community

Cover image for Advanced Concepts in Helm Templates
Manjunath Kotabal
Manjunath Kotabal

Posted on

1

Advanced Concepts in Helm Templates

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 }}
Enter fullscreen mode Exit fullscreen mode

values.yaml:

envVars:
  APP_MODE: production
  LOG_LEVEL: debug
Enter fullscreen mode Exit fullscreen mode

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 }}
Enter fullscreen mode Exit fullscreen mode

values.yaml:

service:
  enabled: true
  type: ClusterIP
  port: 80
  targetPort: 8080
Enter fullscreen mode Exit fullscreen mode

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 }}
Enter fullscreen mode Exit fullscreen mode

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 }}
Enter fullscreen mode Exit fullscreen mode

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 }}
Enter fullscreen mode Exit fullscreen mode

values.yaml:

configurations:
  APP_ENV: production
  DEBUG_MODE: "false"
Enter fullscreen mode Exit fullscreen mode

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 }}
Enter fullscreen mode Exit fullscreen mode

values.yaml:

secrets:
  DB_PASSWORD: my_secure_password
  API_KEY: my_api_key
Enter fullscreen mode Exit fullscreen mode

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 }}
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read full post →

Top comments (1)

Collapse
 
rimjhim_singh profile image
Rimjhim Singh

Amazing!

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more