Convert a YAML to string in Helm
Asked Answered
M

3

3

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 }}
Mae answered 17/6, 2020 at 15:26 Comment(0)
L
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

Liggins answered 18/3, 2021 at 11:16 Comment(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 }}Guelders
P
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 }}"
Poock answered 22/6, 2020 at 9:28 Comment(2)
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)Mae
@Mae 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 commandPoock
A
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

Acarology answered 17/6, 2020 at 21:36 Comment(1)
I'm sorry but that works when declaring variables, not afterwards to convert a variable to another typeMae

© 2022 - 2024 — McMap. All rights reserved.