舵模板通过键获取地图的价值 [英] helm-template get value of the map by key

查看:66
本文介绍了舵模板通过键获取地图的价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在掌舵模板中,我试图通过键检索地图的值.

In the helm-template I'm trying to retrieve a value of the map by key.

我已经尝试过使用go-templates中的index,如下所示: 使用变量键访问地图值在Go模板中

I've tried to use the index from the go-templates, as suggested here: Access a map value using a variable key in a Go template

但是,它对我不起作用(请参阅后面的测试).对替代解决方案有什么想法吗?

However it doesn't work for me (see later test). Any idea for the alternative solution?

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函数仅适用于整数,这是一个有效的示例:

label is an array, so the index function will only work with integers, this is a working example:

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

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

The 0 selects the first element of the array.

一个更好的选择是避免使用数组并将其替换为map:

A better option is to avoid using an array and replace it with a map:

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

您甚至不需要索引功能:

And you dont even need the index function:

foolabel: {{ .Values.label.foo }}

这篇关于舵模板通过键获取地图的价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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