【问题标题】:Extract LoadBalancer name from kubectl output with go-template使用 go-template 从 kubectl 输出中提取 LoadBalancer 名称
【发布时间】:2018-07-30 18:51:43
【问题描述】:

我正在尝试编写一个提取负载均衡器值的 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"
                }
            ]
        }
    }
}

【问题讨论】:

    标签: kubernetes go-templates


    【解决方案1】:

    从 JSON 中可以看出,入口元素是一个数组。您可以使用template function index 来获取此数组元素。

    试试:

    kubectl get svc <name> -o=go-template --template='{{(index .status.loadBalancer.ingress 0 ).hostname}}'
    

    这当然是假设您只配置一个负载均衡器,如果您有多个,则必须使用范围

    【讨论】:

      【解决方案2】:

      试试这个:

      kubectl get svc <name> -o go-template='{{range .items}}{{range .status.loadBalancer.ingress}}{{.hostname}}{{printf "\n"}}{{end}}{{end}}'
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-07-11
        • 1970-01-01
        • 1970-01-01
        • 2021-12-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多