将YAML转换为Helm中的字符串 [英] Convert a YAML to string in Helm

查看:161
本文介绍了将YAML转换为Helm中的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一张舵图用于部署具有YAML格式的配置文件的应用程序.目前,我的头盔图表使用以下代码:

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

此代码使我可以轻松地从值中更改 databaseUser ,但是问题是,如果我想更改 databasePort 的值,则必须重写整个这样的配置:

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

这很不方便.之所以这样工作,是因为将 db_config_file.yaml 内容解释为字符串,因为我将其提供给仅接受字符串的 tpl 函数.

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.

所以我的问题是,有没有一种方法可以将YAML转换为Helm模板中的字符串并获得以下信息:

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

推荐答案

您是否也考虑将 databasePort 模板化并将值用双引号引起来?

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

这篇关于将YAML转换为Helm中的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆