22. November 2020 1 min read
Helm: generate comma separated list
When you want to deploy your Django application with Helm to Kubernetes cluster, you will need to produce ALLOWED_HOSTS list inside the settings.py. I use Python decouple module and then inject the values through the environment variables.
"""
Django settings for some project.
"""
ALLOWED_HOSTS = config('ALLOWED_HOSTS', default='localhost', cast=Csv())
In my Helm values.yml I have a list of hosts which looks like this:
# Source: helmchart/values.yaml
# ...
ingressroute:
hosts:
- host: example1.com
- host: example2.com
# ...
And then to generate a comma separated list of hosts in Helm, to inject into containers, you need to add to the _helpers.tpl
{{/*
Create list of allowed hosts for Django
*/}}
{{- define "allowedHosts" -}}
{{- $hosts := list -}}
{{- range .Values.ingressroute.hosts }}
{{- $hosts = .host | append $hosts -}}
{{- end -}}
{{- join "," $hosts }}
{{- end -}}
Now we can call the function to retrieve comma separated list of the hosts array with key host.