Go Templates & Values
๐ฅ Vibe Prompt
"Helm template with conditionals, loops, and named templates. Generate multi-environment configs."
Template Functions
# Values
{{ .Values.replicaCount }}
{{ .Release.Name }}
{{ .Chart.Name }}
{{ .Release.Namespace }}
# Conditionals
{{- if .Values.ingress.enabled }}
ingress:
host: {{ .Values.ingress.host | quote }}
{{- end }}
# Loops
{{- range .Values.envSecrets }}
- name: {{ .name }}
valueFrom:
secretKeyRef:
name: {{ .secretName }}
key: {{ .secretKey }}
{{- end }}
# Default values
{{ .Values.image.tag | default "latest" | quote }}
# Named templates (from _helpers.tpl)
{{- define "myapp.labels" }}
app: {{ .Chart.Name }}
release: {{ .Release.Name }}
{{- end }}
# Use it
labels:
{{- include "myapp.labels" . | nindent 4 }}
Multi-Environment
# Override values per env
helm install prod ./myapp -f values/prod.yaml
diff --git a/values/dev.yaml b/values/prod.yaml
--- replicaCount: 1
+++ replicaCount: 5
--- resources: {limits: {cpu: "0.5"}}
+++ resources: {limits: {cpu: "2", memory: "2Gi"}}
Advanced Functions
# Type coercion
{{ .Values.replicaCount | int }}
{{ .Values.duration | toString }}
# String operations
{{ .Values.name | upper | lower | title }}
{{ .Values.host | trimSuffix ".com" }}
# JSON/YAML
{{ .Values.config | toJson }}
{{ .Values.config | toYaml }}
Best Practices
- Always use
{{-to trim whitespace - Use
_helpers.tplfor reusable snippets - Validate with
helm lintandhelm template - Document values.yaml with comments
Chapter Summary
- Understand core concepts and principles
- Master implementation methods and techniques
- Familiar with common issues and solutions
- Able to apply in real projects
Further Reading
- Official documentation and API references
- Open source examples on GitHub
- Technical books and online courses
- Community discussions and tech blogs
Implementation Example
Basic Example
# This section provides a complete implementation example
Steps
- Setup: Configure development environment
- Data: Prepare required data
- Implementation: Build core functionality
- Testing: Verify correctness
- Optimization: Improve performance
Common Errors
| Error Type | Cause | Solution | |------------|-------|----------| | Compilation | Syntax | Check code syntax | | Runtime | Environment | Verify dependencies installed | | Logic | Algorithm | Step-by-step debugging | | Performance | Efficiency | Use profilers |
Code Example
import sys
def main():
print("Hello, World!")
if __name__ == "__main__":
main()
References
- Official documentation
- API reference
- Open source examples
- Community discussions
Template Functions and Pipelines
Helm provides many built-in template functions and supports pipelining (chaining functions with |).
String Functions
| Function | Purpose | Example |
|----------|---------|--------|
| upper | Convert to uppercase | {{ .Values.name | upper }} |
| lower | Convert to lowercase | {{ .Values.name | lower }} |
| title | Title case | {{ "hello world" | title }} |
| trim | Remove whitespace | {{ .Values.name | trim }} |
| quote | Wrap in double quotes | {{ .Values.name | quote }} |
| nospace | Remove all spaces | {{ .Values.name | nospace }} |
| repeat | Repeat string N times | {{ .Values.name | repeat 3 }} |
Default Values
# In values.yaml
image:
repository: nginx
# tag: 1.25 <-- this field is optional
# In deployment.yaml
image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default "latest" }}"
Required Values (Fail if Missing)
# Fails installation if .Values.database.url is missing
- name: DATABASE_URL
value: {{ required "database.url is required!" .Values.database.url }}
Flow Control
If-Else
{{- if .Values.persistence.enabled }}
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: {{ include "myapp.fullname" . }}
spec:
accessModes:
- {{ .Values.persistence.accessMode | default "ReadWriteOnce" | quote }}
resources:
requests:
storage: {{ .Values.persistence.size | default "8Gi" }}
{{- end }}
With (Scope Change)
# Without "with"
env:
- name: NODE_ENV
value: {{ .Values.env.nodeEnv }}
- name: PORT
value: {{ .Values.env.port | quote }}
# With "with" โ shorter!
{{- with .Values.env }}
env:
- name: NODE_ENV
value: {{ .nodeEnv }}
- name: PORT
value: {{ .port | quote }}
{{- end }}
Range (Loop)
# values.yaml
envVars:
- name: NODE_ENV
value: production
- name: LOG_LEVEL
value: info
# template
env:
{{- range .Values.envVars }n - name: {{ .name }}
value: {{ .value | quote }}
{{- end }}
Values Precedence
Helm values come from multiple sources with a clear priority order:
| Priority | Source | Example |
|----------|--------|--------|
| 1 (highest) | --set flags | --set image.tag=v2 |
| 2 | --values file | -f custom-values.yaml |
| 3 | Parent chart values | From umbrella chart |
| 4 | Chart's values.yaml | Default values in chart |
Subchart Values
Access subchart values with the global prefix or direct values.
Global Values
# Parent values.yaml
global:
imageRegistry: my-registry.io
database:
host: primary-db.example.com
# Subchart accesses it as:
# {{ .Values.global.database.host }}
Exporting Values
# Subchart values.yaml
exports:
data:
url: http://my-service:8080
# Parent chart accesses:
# {{ .Values.subchartName.exports.data.url }}
Summary
Go templates in Helm provide powerful functions, flow control, and a flexible values system. Mastering these patterns makes charts maintainable and reusable.
Key takeaways:
- Use
defaultfor optional values with sensible defaults | - Use
requiredto fail fast on missing critical values | - Pipelines chain functions:
{{ val | upper | quote }}| if/elsecontrols conditional resource creation |withchanges the scope (shortens dot references) |rangeiterates over lists and maps |--setoverrides have highest precedence |- Global values propagate to all subcharts |
What's Next: Dependencies and Subcharts
The next chapter covers managing chart dependencies and subcharts.