我在 _helpers.tpl 文件中有一些模板,如下所示
{{- define "clusterNodes" }}
- ip: "192.168.7.10"
hostnames:
- "m1"
- ip: "192.168.7.20"
hostnames:
- "w1"
- ip: "192.168.7.30"
hostnames:
- "w2"
{{- end }}
我想获取上面第一个元素的主机名以用于以下 yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx2
spec:
replicas: 1
selector:
matchLabels:
app: nginx2
template:
metadata:
labels:
app: nginx2
spec:
nodeName: "{{ (index (index $clusterNodes "clusterNodes") 0).hostnames.0 }}"
containers:
- name: nginx2
image: nginx:alpine
imagePullPolicy: IfNotPresent
使该条目如下所示
nodeName: m1
答案1
这应该有效:
nodeName: {{ index (index (include "clusterNodes" . | fromYamlArray ) 0).hostnames 0 }}
您的方法几乎是正确的,但是您需要解析clusterNodes
为 YAML 数组,否则它将被解释为字符串。