将YAML字符串转换为Helm模板中的dict [英] Convert YAML string to dict in Helm template

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

问题描述

我正在为一个项目创建一个图表,该项目的二进制文件在执行时会生成YAML格式的配置文件,如下所示:

I am creating a chart for a project that has a binary that when executed generates a configuration file in YAML format that looks like this:

---
PARAM_1: value1
PARAM_2: value2

我的图表需要读取此文件并将其所有值加载到容器中的环境变量中,因此我在我的values.yaml文件中创建了一个变量config,并且在安装该图表时,我正在传递文件内容使用--set-file:

My chart needs to read this file and and load all of its values into environment variables in a container, so I created a variable config in my values.yaml file and when the chart is installed I am passing the file content using --set-file:

helm install <CHART> --set-file config=/path/to/yaml/config/file

接下来,我创建一个值为.Values.configConfigMap:

Next I create a ConfigMap withe the value of .Values.config:

apiVersion: v1
kind: ConfigMap
metadata:
  ...
data:
  {{ .Values.config }}

我遇到的问题是我需要对值config做两件事:

The problem I am having is that I need to do two things with values of config:

  • 为所有键加上预定义的值(因此,在上面的示例中,我将MY_APP_PARAM_1作为键)
  • 确保所有值均为字符串,否则ConfigMap将失败
  • prefix all keys with a predefined value (so in the example above I would MY_APP_PARAM_1 as key)
  • make sure the values are all string, otherwise the ConfigMap will fail

如何在模板中将.Values.config的值解析为dict,以便可以使用range循环来进行这些更改?

How can I parse the value of .Values.config in my template as a dict so that I can use a range loop do these changes?

推荐答案

最后,我能够执行以下操作:

In the end I was able to do something like this:

{{ $lines := splitList "\n" .Values.config -}}
{{- range $lines }}
{{- if not (. | trim | empty) -}}
{{- $kv := . | splitn ":" 2 -}}
{{ printf "MY_APP_%s: %s" $kv._0 ($kv._1 | trim | quote) | indent 2 }}
{{ end -}}
{{- end -}}

我很难弄清{{- vs {{,而helm install --debug --dry-run .在这部分中有很大帮助.

I had a hard time getting the {{- vs {{ right, and helm install --debug --dry-run . help a lot in this part.

有点乱,所以我很想看看是否有人有更好的解决方案.

It's kind of messy, so I would be very interested in seeing if anyone has a better solution.

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

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