地图通过关键的掌舵模板GET值

问题描述 投票:1回答:1

在掌舵模板我试图检索地图的键的值。

我试图使用从转到模板的index,如下建议: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
kubernetes-helm
1个回答
4
投票

标签是一个数组,所以索引函数将只与整数的工作,这是一个工作示例:

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

该0选择该阵列的第一个元素。

一个更好的选择是避免使用的阵列,并且与地图代替它:

label:
  foo:
    name: foo
    value: foo1
  bar:
    name: bar
    value: bar2

而且你甚至需要的指数函数:

foolabel: {{ .Values.label.foo }}
© www.soinside.com 2019 - 2024. All rights reserved.