在 helm-template 中,我试图通过键检索地图的值。
我尝试使用indexgo-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
Run Code Online (Sandbox Code Playgroud)
values.yaml:
label:
- name: foo
value: foo1
- name: bar
value: bar2
Run Code Online (Sandbox Code Playgroud)
templates/test.txt
label: {{ .Values.label }}
Run Code Online (Sandbox Code Playgroud)
适用于helm template .:
---
# Source: foochart/templates/test.txt
label: [map[value:foo1 name:foo] map[name:bar value:bar2]]
Run Code Online (Sandbox Code Playgroud)
但是一旦尝试使用index:
templates/test.txt
label: {{ .Values.label }}
foolabel: {{ index .Values.label "foo" }}
Run Code Online (Sandbox Code Playgroud)
它不会工作 - 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
Run Code Online (Sandbox Code Playgroud)
Ign*_*lán 14
label 是一个数组,所以 index 函数只能处理整数,这是一个工作示例:
foolabel: {{ index .Values.label 0 }}
Run Code Online (Sandbox Code Playgroud)
0 选择数组的第一个元素。
更好的选择是避免使用数组并将其替换为地图:
label:
foo:
name: foo
value: foo1
bar:
name: bar
value: bar2
Run Code Online (Sandbox Code Playgroud)
你甚至不需要 index 函数:
foolabel: {{ .Values.label.foo }}
Run Code Online (Sandbox Code Playgroud)
小智 12
值.yaml
coins:
ether:
host: 10.11.0.50
port: 123
btc:
host: 10.11.0.10
port: 321
Run Code Online (Sandbox Code Playgroud)
template.yaml
{{- range $key, $val := .Values.coins }}
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ $key }}
- env:
- name: SSH_HOSTNAME
value: {{ $val.host | quote }}
- name: SSH_TUNNEL_HOST
value: {{ $val.port | quote }}
---
{{- end }}
Run Code Online (Sandbox Code Playgroud)
运行 $ helm 模板 ./helm
---
# Source: test/templates/ether.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: btc
- env:
- name: SSH_HOSTNAME
value: "10.11.0.10"
- name: SSH_TUNNEL_HOST
value: "321"
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: ether
- env:
- name: SSH_HOSTNAME
value: "10.11.0.50"
- name: SSH_TUNNEL_HOST
value: "123"
---
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
34834 次 |
| 最近记录: |