doushenyu2537 2019-03-25 19:46
浏览 599
已采纳

将YAML字符串转换为Helm模板中的dict

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

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

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

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

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

  • 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

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?

  • 写回答

1条回答 默认 最新

  • dtpxi88884 2019-03-26 04:34
    关注

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

    {{ $lines := splitList "
    " .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 -}}
    

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀