2

I have an helm chart used to deploy an application that have configuration file in YAML format. Currently, my helm chart use the following code:

values.yaml

databaseUser: "dbuser"

configFiles:
  db_config_file.yaml: |-
    databaseUser: {{ .Values.databaseUser }}
    databasePort: 1234
    [...]

[...]

templates/configmap.yaml

data:
  {{- range $name, $config := .Values.configFiles }}
  {{ $name }}: |-
{{ tpl $config $ | indent 4 }}
  {{- end }}

This code allow me to change easily the databaseUser from values, but the problem is that if I want to change the value of databasePort, I have to rewrite the entire configuration like that:

configFiles:
  db_config_file.yaml: |-
    databaseUser: {{ .Values.databaseUser }}
    databasePort: 9876

which is inconvenient. It works like that because the db_config_file.yaml content is interpreted as string because I give it to the tpl function which only accept strings.

So my question is, is there a way to convert the YAML to string in a Helm template and get the following things:

databaseUser: "dbuser"

configFiles:
  db_config_file.yaml: # Content is not a string block
    databaseUser: {{ .Values.databaseUser }}
    databasePort: 1234
    [...]

[...]
data:
  {{- range $name, $config := .Values.configFiles }}
  {{ $name }}: |-
{{ tpl (<a toString function> $config) $ | indent 4 }}
  {{- end }}
Guillaume
  • 8,741
  • 11
  • 49
  • 62

3 Answers3

6

I was able to solve a similar issue using the code below

values.yaml

databaseUser: "dbuser"

configFiles: # Content is not a string block
  db_config_file_yaml: 
    databaseUser: {{ .Values.databaseUser }}
    databasePort: 1234
[...]

templates/configmap.yaml

data:
   db_config_file.yaml: |
{{ .Values.configFiles.db_config_file_yaml | toYaml | indent 4 }}

Reference: https://helm.sh/docs/chart_template_guide/yaml_techniques/#strings-in-yaml

Mursil Sayed
  • 61
  • 1
  • 1
  • This was so close to the answer for me. The one thing missing was to pass the values through `tpl` so the final solution was `{{ tpl (toYaml .Values.configFiles.db_config_file_yaml ) . | indent 4 }}` – ZachP Feb 03 '22 at 13:20
3

Did you consider templating databasePort as well and wrapping your values in double quotes?

values.yaml

databaseUser: "dbuser"
databasePort: 1234

configFiles:
  db_config_file.yaml: |-
    databaseUser: "{{ .Values.databaseUser }}"
    databasePort: "{{ .Values.databasePort }}"
edbighead
  • 5,607
  • 5
  • 29
  • 35
  • Yes I consider that, but the problem is that I don't know by advance every property that will be use (it is use to configure endpoints to apis and we don't know how many) – Guillaume Jun 25 '20 at 14:52
  • @Guillaume you can export your properties into environment variables, use placeholders like `$DATABASE_PORT` in `values.yaml` and use `envsubst` to update them in-place in file or use `--set databasePort=$DATABASE_PORT` in your command – edbighead Jun 26 '20 at 07:28
2

as your question helped me to solve mine, maybe I can help you with my little knowledge. The official helm documentation describes a way to enforce type inference:

coffee: "yes, please"
age: !!str 21
port: !!int "80"

HTH, Martin

Martin Kirchner
  • 171
  • 1
  • 1
  • 7
  • I'm sorry but that works when declaring variables, not afterwards to convert a variable to another type – Guillaume Jun 18 '20 at 07:22