我正在尝试编写一个go模板来提取负载均衡器的值。使用--go-template={{status.loadBalancer.ingress}}
会返回[map[hostname:GUID.us-west-2.elb.amazonaws.com]]%
,当我向模板中添加.hostname
时,我收到一条错误消息:“无法评估类型为{}的字段主机名”。我尝试使用range
关键字,但似乎语法不正确。
{
"apiVersion": "v1",
"kind": "Service",
"metadata": {
"creationTimestamp": "2018-07-30T17:22:12Z",
"labels": {
"run": "nginx"
},
"name": "nginx-http",
"namespace": "jx",
"resourceVersion": "495789",
"selfLink": "/api/v1/namespaces/jx/services/nginx-http",
"uid": "18aea6e2-941d-11e8-9c8a-0aae2cf24842"
},
"spec": {
"clusterIP": "10.100.92.49",
"externalTrafficPolicy": "Cluster",
"ports": [
{
"nodePort": 31032,
"port": 80,
"protocol": "TCP",
"targetPort": 8080
}
],
"selector": {
"run": "nginx"
},
"sessionAffinity": "None",
"type": "LoadBalancer"
},
"status": {
"loadBalancer": {
"ingress": [
{
"hostname": "GUID.us-west-2.elb.amazonaws.com"
}
]
}
}
}
答案 0 :(得分:1)
从JSON中可以看到,入口元素是一个数组。您可以使用template function index来获取此数组元素。
尝试:
kubectl get svc <name> -o=go-template --template='{{(index .status.loadBalancer.ingress 0 ).hostname}}'
这当然是假设您仅配置一个负载均衡器,如果有多个负载均衡器,则必须使用范围
答案 1 :(得分:0)
尝试一下:
kubectl get svc <name> -o go-template='{{range .items}}{{range .status.loadBalancer.ingress}}{{.hostname}}{{printf "\n"}}{{end}}{{end}}'