Convert a YAML to string in Helm

霸气de小男生 提交于 2020-06-29 04:13:08

问题


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 }}

回答1:


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




回答2:


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 }}"


来源:https://stackoverflow.com/questions/62432632/convert-a-yaml-to-string-in-helm

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!