gpt4 book ai didi

kubernetes-helm - helm-template 通过key获取map的值

转载 作者:行者123 更新时间:2023-12-02 11:26:15 27 4
gpt4 key购买 nike

在 helm-template 中,我试图通过键检索 map 的值。

我试过使用 index来自 go-templates,如此处所建议的:
Access a map value using a variable key in a Go template

但是它对我不起作用(见后面的测试)。关于替代解决方案的任何想法?
Chart.yaml :

apiVersion: v1
appVersion: "1.0"
description: A Helm chart for Kubernetes
name: foochart
version: 0.1.0
values.yaml :
label:
- name: foo
value: foo1
- name: bar
value: bar2
templates/test.txt
label: {{ .Values.label }}

工作正常 helm template . :
---
# Source: foochart/templates/test.txt
label: [map[value:foo1 name:foo] map[name:bar value:bar2]]

但是一旦尝试使用 index :
templates/test.txt
label: {{ .Values.label }}
foolabel: {{ index .Values.label "foo" }}

它不起作用 - helm template . :
Error: render error in "foochart/templates/test.txt": template: foochart/templates/test.txt:2:13: executing "foochart/templates/test.txt" at <index .Values.label ...>: error calling index: cannot index slice/array with type string

最佳答案

label 是一个数组,所以 index 函数只能处理整数,这是一个工作示例:

foolabel: {{ index .Values.label 0 }}

0 选择数组的第一个元素。

更好的选择是避免使用数组并将其替换为 map :
label:
foo:
name: foo
value: foo1
bar:
name: bar
value: bar2

你甚至不需要 index 函数:
foolabel: {{ .Values.label.foo }}

关于kubernetes-helm - helm-template 通过key获取map的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52792090/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com