Reputation: 9071
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 }}
Upvotes: 3
Views: 25484
Reputation: 61
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
Upvotes: 6
Reputation: 6334
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 }}"
Upvotes: 3
Reputation: 191
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
Upvotes: 2