有没有办法用 helm 压平字典?我想通过展平位于 values.yaml 中的 YAML 配置来为图表中的应用程序提供环境变量。配置看起来像。 (不是实际的)
config:
server:
port: 3333
other:
setting:
name: test
并希望提供环境变量作为
- name: CONFIG_SERVER_PORT
value: 3333
- name: CONFIG_OTHER_SETTING_NAME
value: test
我考虑过使用 Kubernetes 配置映射,但这意味着使用随机版本名称部署略有不同的应用程序实例,这样配置就不会被覆盖。
本库https://github.com/jeremywohl/flatten提供了一种使用分隔符展平 map[string]interface{}
的方法。有没有一种方法可以为使用该库的 helm 提供自定义管道或其他方法来展平配置?
最佳答案
我不知道有什么内置的东西。Sprig为 helm 模板提供了大部分有用的功能,但 dict functions只覆盖原语。
你可以定义
一个named template执行业务并递归配置字典/映射。然后在需要的地方包含
模板:
{{- define "recurseFlattenMap" -}}
{{- $map := first . -}}
{{- $label := last . -}}
{{- range $key, $val := $map -}}
{{- $sublabel := list $label $key | join "_" | upper -}}
{{- if kindOf $val | eq "map" -}}
{{- list $val $sublabel | include "recurseFlattenMap" -}}
{{- else -}}
- name: {{ $sublabel | quote }}
value: {{ $val | quote }}
{{ end -}}
{{- end -}}
{{- end -}}
通过 list
传递 config
数据在这里有点复杂,然后将其分离回 $map
和 $标签
。这是因为模板只接受一个变量 scope .
env: {{ list .Values.config "CONFIG" | include "recurseFlattenMap" | nindent 2 }}
使用示例值:
config:
server:
port: 3333
first: astr
other:
setting:
name: test
结果
$ helm template .
---
# Source: so61280873/templates/config.yaml
env:
- name: "CONFIG_FIRST"
value: "astr"
- name: "CONFIG_OTHER_SETTING_NAME"
value: "test"
- name: "CONFIG_SERVER_PORT"
value: "3333"
关于go - 用 Helm 压平字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61280873/