50 Helm Template Cheatsheets
Basic Template Functions:
Access a Value from
values.yaml
:replicas: {{ .Values.replicaCount }}
Access Release Information:
releaseName: {{ .Release.Name }} releaseNamespace: {{ .Release.Namespace }}
Access Chart Information:
chartName: {{ .Chart.Name }} chartVersion: {{ .Chart.Version }}
Port Configuration:
containerPort: {{ .Values.containerPort }}
Conditional Blocks:
Conditional Rendering:
{{- if .Values.enableFeature }} featureEnabled: true {{- end }}
Conditional Rendering with Else Block:
{{- if .Values.enableFeature }} featureEnabled: true {{- else }} featureEnabled: false {{- end }}
Loops:
Loop Over a List:
{{- range .Values.ingress.hosts }} - host: {{ . }} {{- end }}
Loop Over a Map:
{{- range $key, $value := .Values.labels }} {{ $key }}: {{ $value }} {{- end }}
Accessing Values:
Access Nested Values:
username: {{ .Values.database.credentials.username }}
Quoting a Value:
message: {{ .Values.message | quote }}
Including Partial Templates:
- Include a Partial Template:
{{- include "partials.common" . | indent 2 }}
Template Functions:
Convert to YAML:
{{- toYaml .Values.config | nindent 4 }}
Convert to JSON:
jsonData: {{ toJson .Values.data }}
Using Sprig Functions:
password: {{ randAlphaNum 10 | quote }}
Helm Hook Templates:
Pre-install Hook:
{{- if .Values.preInstallJob }} # pre-install job definition {{- end }}
Post-install Hook:
{{- if .Values.postInstallJob }} # post-install job definition {{- end }}
Advanced Examples:
Creating a Kubernetes Secret:
apiVersion: v1 kind: Secret metadata: name: {{ .Release.Name }}-secret type: Opaque data: password: {{ .Values.secret.password | b64enc }}
Creating a Service:
apiVersion: v1 kind: Service metadata: name: {{ include "mychart.fullname" . }}
Defining a ConfigMap:
apiVersion: v1 kind: ConfigMap metadata: name: {{ include "mychart.fullname" . }} data: config.yaml: | {{ .Files.Get "config.yaml" | indent 4 }}
Creating an Ingress:
apiVersion: networking.k8s.io/v1 kind: Ingress metadata: name: {{ include "mychart.fullname" . }} spec: rules: - host: {{ .Values.ingress.host }}
Defining a PersistentVolumeClaim:
apiVersion: v1 kind: PersistentVolumeClaim metadata: name: {{ include "mychart.fullname" . }}-data
Setting Resource Limits:
resources: limits: cpu: {{ .Values.resources.cpu | quote }} memory: {{ .Values.resources.memory | quote }}
Creating a Job:
apiVersion: batch/v1 kind: Job metadata: name: {{ .Release.Name }}-job spec: template: spec: containers: - name: {{ .Chart.Name }}-job image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}" command: ["{{ .Values.command }}"]
Creating a CronJob:
apiVersion: batch/v1beta1 kind: CronJob metadata: name: {{ .Release.Name }}-cronjob spec: schedule: "*/1 * * * *" jobTemplate: spec: template: spec: containers: - name: {{ .Chart.Name }}-cronjob image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}" command: ["{{ .Values.command }}"]
Creating a StatefulSet:
apiVersion: apps/v1 kind: StatefulSet metadata: name: {{ .Release.Name }}-statefulset
Creating a Deployment with Liveness and Readiness Probes:
apiVersion: apps/v1 kind: Deployment metadata: name: {{ .Release.Name }}-deployment spec: template: spec: containers: - name: {{ .Chart.Name }}-container image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}" livenessProbe: httpGet: path: /healthz port: 80 initialDelaySeconds: 30 readinessProbe: httpGet: path: /readyz port: 80 initialDelaySeconds: 5
Creating a Horizontal Pod Autoscaler (HPA):
apiVersion: autoscaling/v2beta2 kind: HorizontalPodAutoscaler metadata: name: {{ .Release.Name }}-hpa spec: scaleTargetRef: apiVersion: apps/v1 kind: Deployment name: {{ .Release.Name }}-deployment minReplicas: 1 maxReplicas: 10 metrics: - type: Resource resource: name: cpu targetAverageUtilization: 50
Accessing Values:
Accessing Values from Dependencies:
dependencyValue: {{ .Values.dependencies.subchart.value }}
Accessing Values with Default:
port: {{ .Values.port | default 8080 }}
Advanced Conditional Blocks:
Conditional Block with Nested Conditions:
{{- if and .Values.condition1 .Values.condition2 }} # Block content {{- end }}
Conditional Block with OR Operator:
{{- if or .Values.condition1 .Values.condition2 }} # Block content {{- end }}
Advanced Loops:
Loop with Index:
{{- range $index, $element := .Values.list }} - index: {{ $index }}, value: {{ $element }} {{- end }}
Loop with Sorting:
{{- range sort .Values.list }} - value: {{ . }} {{- end }}
Advanced Template Functions:
Using
tpl
Function for Advanced Template Rendering:renderedTemplate: {{ tpl (.Files.Get "template.yaml") . | indent 4 }}
Using
include
with Dynamic Name:{{- include (printf "%s-config" .Values.serviceName) . | indent 2 }}
External Template Files:
- Including External Template File:
{{- include "external-template.tpl" . | indent 2 }}
Debugging:
- Printing Values for Debugging:
{{- printf "%#v" .Values | nindent 2 }}
Naming Conventions:
- Using Release Name and Chart Name for Naming Resources:
metadata: name: {{ .Release.Name }}-{{ .Chart.Name }}
Comments:
- Adding Comments in Templates:
# This is a comment
External Resources:
- Mounting External Files as ConfigMaps:
apiVersion: v1 kind: ConfigMap metadata: name: my-configmap data: {{ .Files.Get "config-file.yaml" | indent 2 }}
Combining Functions:
- Combining Functions for Complex Operations:
combinedValue: {{ trimPrefix "prefix-" (upper .Values.value) }}
Advanced StatefulSet Configuration:
- StatefulSet with Persistent Volume Claim:
volumeClaimTemplates: - metadata: name: data spec: accessModes: [ "ReadWriteOnce" ] resources: requests: storage: 1Gi
External Secrets:
- Referencing External Secrets:
apiVersion: v1 kind: Secret metadata: name: my-secret annotations: "helm.sh/resource-policy": keep type: Opaque data: password: {{ .Values.externalSecret.password | b64enc }}
Helm Release Specifics:
- Accessing Release Timestamp:
releaseDate: {{ .Release.Timestamp }}
Debugging with printf
:
- Debugging with
printf
:{{ printf "%#v" .Values | nindent 2 }}
Using quote
and unquote
Functions:
- Quoting and Unquoting Values:
quotedValue: {{ .Values.someValue | quote }} unquotedValue: {{ .Values.quotedValue | unquote }}
Advanced Resource Definitions:
- Advanced Pod Annotations:
annotations: myAnnotation: {{ .Values.annotations.myAnnotation | quote }}
Configurable Labels:
- Configurable Labels:
labels: myLabel: {{ .Values.labels.myLabel | quote }}
Conditional Pod Disruption Budget:
- Conditional Pod Disruption Budget:
{{- if .Values.enablePodDisruptionBudget }} apiVersion: policy/v1beta1 kind: PodDisruptionBudget metadata: name: {{ include "mychart.fullname" . }} spec: # PDB specification {{- end }}
Helm Template Debugging:
- Template Debugging with
required
Function:requiredValue: {{ required "Value is required" .Values.requiredValue }}
Multiple pipelines:
- Manipulate value with multiple pipelines:
domain: {{ .Values.domain | lower | trunc 9 | quote}}
Last updated 03 Jun 2024, 13:43 +0530 .