Basic Template Functions:

  1. Access a Value from values.yaml:

      replicas: {{ .Values.replicaCount }}
      
  2. Access Release Information:

      releaseName: {{ .Release.Name }}
    releaseNamespace: {{ .Release.Namespace }}
      
  3. Access Chart Information:

      chartName: {{ .Chart.Name }}
    chartVersion: {{ .Chart.Version }}
      
  4. Port Configuration:

      containerPort: {{ .Values.containerPort }}
      

Conditional Blocks:

  1. Conditional Rendering:

      {{- if .Values.enableFeature }}
    featureEnabled: true
    {{- end }}
      
  2. Conditional Rendering with Else Block:

      {{- if .Values.enableFeature }}
    featureEnabled: true
    {{- else }}
    featureEnabled: false
    {{- end }}
      

Loops:

  1. Loop Over a List:

      {{- range .Values.ingress.hosts }}
    - host: {{ . }}
    {{- end }}
      
  2. Loop Over a Map:

      {{- range $key, $value := .Values.labels }}
    {{ $key }}: {{ $value }}
    {{- end }}
      

Accessing Values:

  1. Access Nested Values:

      username: {{ .Values.database.credentials.username }}
      
  2. Quoting a Value:

      message: {{ .Values.message | quote }}
      

Including Partial Templates:

  1. Include a Partial Template:
      {{- include "partials.common" . | indent 2 }}
      

Template Functions:

  1. Convert to YAML:

      {{- toYaml .Values.config | nindent 4 }}
      
  2. Convert to JSON:

      jsonData: {{ toJson .Values.data }}
      
  3. Using Sprig Functions:

      password: {{ randAlphaNum 10 | quote }}
      

Helm Hook Templates:

  1. Pre-install Hook:

      {{- if .Values.preInstallJob }}
    # pre-install job definition
    {{- end }}
      
  2. Post-install Hook:

      {{- if .Values.postInstallJob }}
    # post-install job definition
    {{- end }}
      

Advanced Examples:

  1. Creating a Kubernetes Secret:

      apiVersion: v1
    kind: Secret
    metadata:
      name: {{ .Release.Name }}-secret
    type: Opaque
    data:
      password: {{ .Values.secret.password | b64enc }}
      
  2. Creating a Service:

      apiVersion: v1
    kind: Service
    metadata:
      name: {{ include "mychart.fullname" . }}
      
  3. Defining a ConfigMap:

      apiVersion: v1
    kind: ConfigMap
    metadata:
      name: {{ include "mychart.fullname" . }}
    data:
      config.yaml: |
        {{ .Files.Get "config.yaml" | indent 4 }}
      
  4. Creating an Ingress:

      apiVersion: networking.k8s.io/v1
    kind: Ingress
    metadata:
      name: {{ include "mychart.fullname" . }}
    spec:
      rules:
        - host: {{ .Values.ingress.host }}
      
  5. Defining a PersistentVolumeClaim:

      apiVersion: v1
    kind: PersistentVolumeClaim
    metadata:
      name: {{ include "mychart.fullname" . }}-data
      
  6. Setting Resource Limits:

      resources:
      limits:
        cpu: {{ .Values.resources.cpu | quote }}
        memory: {{ .Values.resources.memory | quote }}
      
  7. 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 }}"]
      
  8. 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 }}"]
      
  9. Creating a StatefulSet:

      apiVersion: apps/v1
    kind: StatefulSet
    metadata:
      name: {{ .Release.Name }}-statefulset
      
  10. 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
      
  11. 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:

  1. Accessing Values from Dependencies:

      dependencyValue: {{ .Values.dependencies.subchart.value }}
      
  2. Accessing Values with Default:

      port: {{ .Values.port | default 8080 }}
      

Advanced Conditional Blocks:

  1. Conditional Block with Nested Conditions:

      {{- if and .Values.condition1 .Values.condition2 }}
    # Block content
    {{- end }}
      
  2. Conditional Block with OR Operator:

      {{- if or .Values.condition1 .Values.condition2 }}
    # Block content
    {{- end }}
      

Advanced Loops:

  1. Loop with Index:

      {{- range $index, $element := .Values.list }}
    - index: {{ $index }}, value: {{ $element }}
    {{- end }}
      
  2. Loop with Sorting:

      {{- range sort .Values.list }}
    - value: {{ . }}
    {{- end }}
      

Advanced Template Functions:

  1. Using tpl Function for Advanced Template Rendering:

      renderedTemplate: {{ tpl (.Files.Get "template.yaml") . | indent 4 }}
      
  2. Using include with Dynamic Name:

      {{- include (printf "%s-config" .Values.serviceName) . | indent 2 }}
      

External Template Files:

  1. Including External Template File:
      {{- include "external-template.tpl" . | indent 2 }}
      

Debugging:

  1. Printing Values for Debugging:
      {{- printf "%#v" .Values | nindent 2 }}
      

Naming Conventions:

  1. Using Release Name and Chart Name for Naming Resources:
      metadata:
      name: {{ .Release.Name }}-{{ .Chart.Name }}
      

Comments:

  1. Adding Comments in Templates:
      # This is a comment
      

External Resources:

  1. Mounting External Files as ConfigMaps:
      apiVersion: v1
    kind: ConfigMap
    metadata:
      name: my-configmap
    data:
      {{ .Files.Get "config-file.yaml" | indent 2 }}
      

Combining Functions:

  1. Combining Functions for Complex Operations:
      combinedValue: {{ trimPrefix "prefix-" (upper .Values.value) }}
      

Advanced StatefulSet Configuration:

  1. StatefulSet with Persistent Volume Claim:
      volumeClaimTemplates:
      - metadata:
          name: data
        spec:
          accessModes: [ "ReadWriteOnce" ]
          resources:
            requests:
              storage: 1Gi
      

External Secrets:

  1. 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:

  1. Accessing Release Timestamp:
      releaseDate: {{ .Release.Timestamp }}
      

Debugging with printf:

  1. Debugging with printf:
      {{ printf "%#v" .Values | nindent 2 }}
      

Using quote and unquote Functions:

  1. Quoting and Unquoting Values:
      quotedValue: {{ .Values.someValue | quote }}
    unquotedValue: {{ .Values.quotedValue | unquote }}
      

Advanced Resource Definitions:

  1. Advanced Pod Annotations:
      annotations:
      myAnnotation: {{ .Values.annotations.myAnnotation | quote }}
      

Configurable Labels:

  1. Configurable Labels:
      labels:
      myLabel: {{ .Values.labels.myLabel | quote }}
      

Conditional Pod Disruption Budget:

  1. Conditional Pod Disruption Budget:
      {{- if .Values.enablePodDisruptionBudget }}
    apiVersion: policy/v1beta1
    kind: PodDisruptionBudget
    metadata:
      name: {{ include "mychart.fullname" . }}
    spec:
      # PDB specification
    {{- end }}
      

Helm Template Debugging:

  1. Template Debugging with required Function:
      requiredValue: {{ required "Value is required" .Values.requiredValue }}
      

Multiple pipelines:

  1. Manipulate value with multiple pipelines:
      domain: {{ .Values.domain | lower | trunc 9 | quote}}
      

Last updated 03 Jun 2024, 13:43 +0530 . history