Conditional statement in helm template

 Condition statements in helm template

In the helm template, conditional statements such as if-else can  be used to make the control flow as per the user’s requirements

 

Below is a step-by-step demonstration of the if-else 

  • create helm chart
  • update existing values.yaml and add the condition
  • create a confimap based on the condition

 

Create Helm Charts

use helm commands to create sample helm charts using helm commands

helm create test

Update existing values.yaml

create a section with a key-value pair which can be used further as condition

ifstatement:
enabled: true

Create a sample config map

Add the configmap in test/template/conditional.yaml

{{- if .Values.ifstatement.enabled }}
apiVersion: v1
kind: ConfigMap
metadata:
  name: ifcondition-configmap
  data:
    var: "true"
{{- else }}
apiVersion: v1
kind: ConfigMap
metadata:
  name: elsecondition-configmap
  data:
    var: "true"
{{- end }}

Deploy and test

helm install test test --debug

As we can see in below Images our deployment will be successful and configmaps is created

 

 

For other helm templates refer to this helm template cheat sheet:  Helm template cheat sheet