
Helm Charts: Templates and Values
Build your own recipe. Master the Go templating engine, learn to use conditional logic and loops in your YAML, and create flexible charts that work in any environment.
Helm Templates and Values: Building the Dynamic Cluster
In the previous lesson, we learned that Helm is a "Recipe Book." But to be a master chef, you need to be able to write the recipes yourself.
A Helm Chart isn't just a static folder of YAML files. It's a dynamic program. It uses the Go Templating Language to decide exactly what the final YAML should look like based on the input from your values.yaml file.
Why is this powerful?
- You want an Ingress only in production? Use an
ifstatement. - You want to spin up 5 sidecar containers for 5 different AI metrics? Use a
rangeloop. - You want to standardize your labels across 50 different microservices? Use a Named Template.
In this lesson, we will master the Go Templates Syntax, learn the power of Built-in Objects, and build a professional-grade Helm chart from scratch for your AI agents.
1. The Core Syntax: Double Curlies {{ }}
In Helm, any text inside {{ }} is treated as code.
A. Accessing Values
The most common operation is pulling data from values.yaml.
replicas: {{ .Values.replicaCount }}
B. The Dot .
The dot represents the "Scope." In most cases, it refers to the "Root" of the chart.
2. Built-in Objects: The Magic Variables
Helm gives you several "Searchable" objects that provide data about the environment.
{{ .Release.Name }}: The name you gave the installation (e.g.my-app-v1).{{ .Release.Namespace }}: Where the chart is being installed.{{ .Chart.Version }}: The version of the chart itself.{{ .Capabilities.KubeVersion }}: The version of Kubernetes running in the cluster.
3. Conditional Logic: The if Statement
This allows you to create optional resources.
{{- if .Values.ingress.enabled }}
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: {{ .Release.Name }}-ingress
spec:
rules:
- host: {{ .Values.ingress.host }}
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: {{ .Release.Name }}-service
port:
number: 80
{{- end }}
Pro Tip: The dash in {{- removes the empty line that the code would otherwise generate, keeping your final YAML clean and readable.
4. Loops: The range Command
If you have a list of environment variables in your values.yaml, you don't want to type env: 50 times. Use a loop!
In values.yaml:
envVars:
- name: DB_URL
value: "localhost:5432"
- name: MODEL_NAME
value: "gpt-4"
In Deployment Template:
env:
{{- range .Values.envVars }}
- name: {{ .name }}
value: {{ .value | quote }}
{{- end }}
5. Visualizing the Template Engine
graph LR
V["values.yaml (Configuration)"] -- "Inputs" --> T["Engine (Go Templates)"]
D["deployment.yaml (Blueprint)"] -- "Inputs" --> T
subgraph "The Transformation"
T -- "Replace [.Values.x]" --> C["Cleaning whitespace"]
C -- "Check [if/else]" --> Logic["Final YAML Generation"]
end
Logic -- "kubectl apply" --> K8s["K8s Control Plane"]
6. Practical Example: The Global Labels Template
In a large company, every resource must have standard labels (owner, cost-center, env). Instead of repeating this in 50 files, we use a Named Template in _helpers.tpl.
In _helpers.tpl:
{{- define "mychart.labels" -}}
app.kubernetes.io/name: {{ .Chart.Name }}
app.kubernetes.io/instance: {{ .Release.Name }}
owner: ai-team
{{- end }}
In any other template:
metadata:
labels:
{{- include "mychart.labels" . | nindent 4 }}
7. AI Implementation: Templating "Inference Profiles"
When deploying AI models, the resource requirements vary wildly based on the model type.
The Flexible Chart Strategy:
In your values.yaml, define different profiles:
profiles:
light:
cpu: 1
memory: 2Gi
heavy:
cpu: 8
memory: 32Gi
gpu: 1
In your deployment template:
resources:
limits:
cpu: {{ index .Values.profiles .Values.activeProfile "cpu" }}
memory: {{ index .Values.profiles .Values.activeProfile "memory" }}
This allows you to change a single word in your values (activeProfile: heavy) and your entire deployment architecture shifts to support a massive GPU model.
8. Summary and Key Takeaways
- Templates: The skeletal structure of your application.
- Values: The flesh and parameters that change per environment.
- Go Templating: USes syntax like
if,range, andinclude. - Helpers: Store reusable patterns to keep your code DRY (Don't Repeat Yourself).
- Quotes & Indentation: Use functions like
quoteandnindentto ensure your final YAML is valid. - Dynamic Power: Build one chart that can be deployed as 1 replica in Dev and 100 replicas in Prod with different identities.
In the next lesson, we will learn how to automate the deployment of these charts using GitHub Actions.
9. SEO Metadata & Keywords
Focus Keywords: Helm templates tutorial K8s, how to use helm values.yaml, Go templating for Kubernetes, Helm if statement and range loop, writing reusable Helm charts, helm nindent and quote functions.
Meta Description: Go beyond basic YAML. Learn how to master the Helm templating engine to create dynamic, reusable, and intelligent Kubernetes blueprints. Discover how to use logic, loops, and named templates to manage your AI and web services professionally.