Helm | Kubernetes
Learn Conditionals in Helm faster than a two minute meal
Your one stop guide to using conditional statements in Helm
One of the best qualities of Helm is easily the ability to use conditional statements.
In this free step by step guide I explain how to best use conditional statements in helm templates to make your deployments easier.
Lets get started and if you find value in this article make sure to spend 2 extra seconds clapping for it as it helps.
The first part is how to use and the last part is an example
Some examples of using conditionals
The equal (eq) operator
{{- if eq.Values.app.env "prod" }}
env: "Production environment"
{{- else }}
env: "Non-production environment"
{{- end }}
If equal to prod then production environment else non prod.
The not equal (ne) operator
{{- if ne .Values.app.env "prod" }}
env: "Non-production environment"
{{- else }}
env: "Production environment"
{{- end }}
Conversely if not equal to prod then non prod
The and operator
{{- if and (eq .Values.app.env "prod") (gt .Values.replicaCount 2) }}
- name: sidecar-logger
image: prod.registry.com/logger:latest
{{- end }}
Deploy sidecar container only if prod and more than 2 replicas
The or operator
image: {{- if or (eq .Values.app.env "dev") (eq .Values.app.env "staging") }}
test.registry.com/{{ .Values.app.image }}:{{ .Values.app.tag }}
{{- else }}
{{ .Values.app.registry }}/{{ .Values.app.image }}:{{ .Values.app.tag }}
{{- end }}
If dev or staging use the test registry
The greater than operator (gt)
replicas: {{- if gt .Values.replicaCount 3 }}
{{ .Values.replicaCount }}
{{- else }}
3
{{- end }}
Minimum replicas must be 3 or more for prod
The less than operator (lt)
{{- if lt .Values.replicaCount 2 }}
debug: "true"
{{- else }}
debug: "false"
{{- end }}
Enable debug mode if replicas less than 2
The hasKey operator
Enable a feature if a key exists
{{- if hasKey .Values.features "logging" }}
logging: enabled
{{- else }}
logging: disabled
{{- end }}
The Example Tutorial
I want to setup my cluster in multiple environments and use helm to manage them. However since each environment uses their own separate image repository I don’t want to change it every time.
To tackle this issue we are going to use helm.
Create Helm Directory
Run the following commands to create helm directory
helm create ziggo
Create the values file for dev and prod environment.
values-dev.yaml
app:
env: dev
registry: dev.registry.com
the file for prod environment
values-prod.yaml
app:
env: prod
registry: prod.registry.com
the default values file has the custom configuration which is used if the template does not find any of the values in the above files
app:
env: "dev"
registry: "docker.io"
name: "ziggo"
image: "ziggoi"
tag: "v5"
replicaCount: 3
Running the deployment
Structure the deployment to use the conditionals.
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ .Values.app.name }}
spec:
replicas: {{ .Values.replicaCount }}
selector:
matchLabels:
app: {{ .Values.app.name }}
template:
metadata:
labels:
app: {{ .Values.app.name }}
spec:
containers:
- name: {{ .Values.app.name }}
{{- if eq .Values.app.env "prod" }}
image: {{ .Values.app.registry }}/{{ .Values.app.image }}:stable
{{- else }}
image: {{ .Values.app.registry }}/{{ .Values.app.image }}:{{ .Values.app.tag }}
{{- end }}
ports:
- containerPort: 80
This is the conditional part , if env is prod or dev
{{- if eq .Values.app.env "prod" }}
image: {{ .Values.app.registry }}/{{ .Values.app.image }}:stable
{{- else }}
image: {{ .Values.app.registry }}/{{ .Values.app.image }}:{{ .Values.app.tag }}
{{- end }}
And now run with the dev file
helm template ziggo/ -f ziggo/values-dev.yaml
Run with the prod file
helm template ziggo/ -f ziggo/values-prod.yaml
As you can see based on the different conditionals it chooses different images.
Conclusion
This is how we use conditional statements in helm to seperate concerns. If you found it useful make sure to subscribe and like the article.