embeding conf files into helm chart

Nick Gleed picture Nick Gleed · Aug 14, 2018 · Viewed 7.4k times · Source

Im new at helm. Im building a splunk helm chart with numerous conf files. I currently use something like this in a configmap ..

    apiVersion: v1
kind: ConfigMap
metadata:
  name: splunk-master-configmap
data:
  indexes.conf: |
    # global settings
    # Inheritable by all indexes: no hot/warm bucket can exceed 1 TB.
    # Individual indexes can override this setting.
    homePath.maxDataSizeMB = 1000000

but I would prefer to have the conf files in a seperate folder e.g. configs/helloworld.conf and have come accross "tpl" but am struggling to understand how to implement it. - can anyone advise best practices. On a side note splunk has orders of presidences >> so there may be many indexes.conf files used in various locations. does anyone have any thoughts on how best to implement this?!??!

Cheers.

Answer

Ryan Dawson picture Ryan Dawson · Aug 14, 2018

If the content of the files is static then you could create a files directory in your chart at the same level as the templates directory (not inside it) and reference them like:

kind: ConfigMap
metadata:
  name: splunk-master-configmap
data:
  {{ (.Files.Glob "files/indexes.conf").AsConfig | indent 2 }}
  {{ (.Files.Glob "files/otherfile.conf").AsConfig | indent 2 }}
# ... and so on

Where this would break down is if you want to be able to reference the values of variables inside the files so that the content is controlled from the values.yaml. If you want to expose each value individually then there's an example in the helm documentation using range. But I think a good fit or your case is what the stable/mysql chart does. It has a ConfigMap that takes values as strings:

{{- if .Values.configurationFiles }}
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ template "mysql.fullname" . }}-configuration
data:
{{- range $key, $val := .Values.configurationFiles }}
  {{ $key }}: |-
{{ $val | indent 4}}
{{- end }}
{{- end -}}

And the values.yaml allows both the files and their content to be set and overridden by the user of the chart:

# Custom mysql configuration files used to override default mysql settings
configurationFiles:
#  mysql.cnf: |-
#    [mysqld]
#    skip-name-resolve
#    ssl-ca=/ssl/ca.pem
#    ssl-cert=/ssl/server-cert.pem
#    ssl-key=/ssl/server-key.pem

It comments out that content and leaves it to the user of the chart to set but you could have defaults in the values.yaml.

You would only need tpl if you needed further flexibility. The stable/keycloak chart lets the user of the chart create their own configmap and point it into the keycloak deployment via tpl. But I think your case is probably closest to the mysql one.

Edit: the tpl function can also be used to take the content of files loaded with Files.Get and effectively make that content part of the template - see How do I load multiple templated config files into a helm chart? if you're interested in this