title | weight |
---|---|
50 Helm Template Cheatsheets |
7 |
-
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 Rendering:
{{- if .Values.enableFeature }} featureEnabled: true {{- end }}
-
Conditional Rendering with Else Block:
{{- if .Values.enableFeature }} featureEnabled: true {{- else }} featureEnabled: false {{- end }}
-
Loop Over a List:
{{- range .Values.ingress.hosts }} - host: {{ . }} {{- end }}
-
Loop Over a Map:
{{- range $key, $value := .Values.labels }} {{ $key }}: {{ $value }} {{- end }}
-
Access Nested Values:
username: {{ .Values.database.credentials.username }}
-
Quoting a Value:
message: {{ .Values.message | quote }}
- Include a Partial Template:
{{- include "partials.common" . | indent 2 }}
-
Convert to YAML:
{{- toYaml .Values.config | nindent 4 }}
-
Convert to JSON:
jsonData: {{ toJson .Values.data }}
-
Using Sprig Functions:
password: {{ randAlphaNum 10 | quote }}
-
Pre-install Hook:
{{- if .Values.preInstallJob }} # pre-install job definition {{- end }}
-
Post-install Hook:
{{- if .Values.postInstallJob }} # post-install job definition {{- end }}
-
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 from Dependencies:
dependencyValue: {{ .Values.dependencies.subchart.value }}
-
Accessing Values with Default:
port: {{ .Values.port | default 8080 }}
-
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 }}
-
Loop with Index:
{{- range $index, $element := .Values.list }} - index: {{ $index }}, value: {{ $element }} {{- end }}
-
Loop with Sorting:
{{- range sort .Values.list }} - value: {{ . }} {{- end }}
-
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 }}
- Including External Template File:
{{- include "external-template.tpl" . | indent 2 }}
- Printing Values for Debugging:
{{- printf "%#v" .Values | nindent 2 }}
- Using Release Name and Chart Name for Naming Resources:
metadata: name: {{ .Release.Name }}-{{ .Chart.Name }}
- Adding Comments in Templates:
# This is a comment
- Mounting External Files as ConfigMaps:
apiVersion: v1 kind: ConfigMap metadata: name: my-configmap data: {{ .Files.Get "config-file.yaml" | indent 2 }}
- Combining Functions for Complex Operations:
combinedValue: {{ trimPrefix "prefix-" (upper .Values.value) }}
- StatefulSet with Persistent Volume Claim:
volumeClaimTemplates: - metadata: name: data spec: accessModes: [ "ReadWriteOnce" ] resources: requests: storage: 1Gi
- 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 }}
- Accessing Release Timestamp:
releaseDate: {{ .Release.Timestamp }}
- Debugging with
printf
:{{ printf "%#v" .Values | nindent 2 }}
- Quoting and Unquoting Values:
quotedValue: {{ .Values.someValue | quote }} unquotedValue: {{ .Values.quotedValue | unquote }}
- Advanced Pod Annotations:
annotations: myAnnotation: {{ .Values.annotations.myAnnotation | quote }}
- Configurable Labels:
labels: myLabel: {{ .Values.labels.myLabel | quote }}
- Conditional Pod Disruption Budget:
{{- if .Values.enablePodDisruptionBudget }} apiVersion: policy/v1beta1 kind: PodDisruptionBudget metadata: name: {{ include "mychart.fullname" . }} spec: # PDB specification {{- end }}
- Template Debugging with
required
Function:requiredValue: {{ required "Value is required" .Values.requiredValue }}
- Manipulate value with multiple pipelines:
domain: {{ .Values.domain | lower | trunc 9 | quote}}