bash 命名路径的 kubectl jsonpath 表达式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41527367/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 15:35:28  来源:igfitidea点击:

kubectl jsonpath expression for named path

bashkuberneteskubectl

提问by Lev Kuznetsov

I have kube service running with 2 named ports like this:

我有 kube 服务运行 2 个命名端口,如下所示:

$ kubectl get service elasticsearch --output json
{
    "apiVersion": "v1",
    "kind": "Service",
    "metadata": {
        ... stuff that really has nothing to do with my question ...
    },
    "spec": {
        "clusterIP": "10.0.0.174",
        "ports": [
             {
                "name": "http",
                "nodePort": 31041,
                "port": 9200,
                "protocol": "TCP",
                "targetPort": 9200
            },
            {
                "name": "transport",
                "nodePort": 31987,
                "port": 9300,
                "protocol": "TCP",
                "targetPort": 9300
            }
        ],
        "selector": {
            "component": "elasticsearch"
        },
        "sessionAffinity": "None",
        "type": "NodePort"
    },
    "status": {
        "loadBalancer": {}
    }
}

I'm trying to get output containing just the 'http' port:

我正在尝试获取仅包含“http”端口的输出:

$ kubectl get service elasticsearch --output jsonpath={.spec.ports[*].nodePort}
31041 31987

Except when I add the test expression as hinted in the cheatsheet here http://kubernetes.io/docs/user-guide/kubectl-cheatsheet/for the name I get an error

除了当我在此处的备忘单中添加测试表达式时http://kubernetes.io/docs/user-guide/kubectl-cheatsheet/名称我收到错误

$ kubectl get service elasticsearch --output jsonpath={.spec.ports[?(@.name=="http")].nodePort}
-bash: syntax error near unexpected token `('

回答by Amit Kumar Gupta

(and )mean something in bash (see subshell), so your shell interpreter is doing that first and getting confused. Wrap the argument to jsonpathin single quotes, that will fix it:

()在 bash 中表示某些内容(请参阅subshel​​l),因此您的 shell 解释器首先执行此操作并感到困惑。将参数jsonpath用单引号括起来,这将修复它:

$ kubectl get service elasticsearch --output jsonpath='{.spec.ports[?(@.name=="http")].nodePort}'

For example:

例如:

# This won't work:
$ kubectl get service kubernetes --output jsonpath={.spec.ports[?(@.name=="https")].targetPort}
-bash: syntax error near unexpected token `('

# ... but this will:
$ kubectl get service kubernetes --output jsonpath='{.spec.ports[?(@.name=="https")].targetPort}'
443

回答by AC81

I had this issues on Windows in Powershell:

我在 Powershell 的 Windows 上遇到了这个问题:

Error executing template: unrecognized identifier http2

执行模板时出错:无法识别的标识符 http2

When specifying a string value wrapped in double quotes in the jsonpath - to solve the error there are 2 ways:

在 jsonpath 中指定用双引号括起来的字符串值时 - 要解决错误,有两种方法:

Swap the single and double quotes:

交换单引号和双引号:

kubectl -n istio-system get service istio-ingressgateway -o jsonpath="{.spec.ports[?(@.name=='http2')].port}"

Or escape the single quote encapsulated in the double quotes:

或者转义双引号中封装的单引号:

kubectl -n istio-system get service istio-ingressgateway -o jsonpath='{.spec.ports[?(@.name==\"http2\")].port}'

回答by Utkarsh Yeolekar

For me, it was giving the error on windows machine :

对我来说,它在 Windows 机器上给出了错误:

kubectl --namespace=app-test get svc proxy --output jsonpath='{.spec.ports[?(@.name=="web")].nodePort}'

> executing jsonpath "'{.spec.ports[?(@.name==web)].nodePort}'":
> unrecognized identifier web

Even though my json contains the name field in the ports array. Online it was working fine.

即使我的 json 包含端口数组中的名称字段。在线它工作正常。

Instead of using the name field, then i have tried with the port field, which was of type integer and it works.

然后我没有使用 name 字段,而是尝试使用 port 字段,它是整数类型并且可以工作。

So, if any one is facing the same issue and if port field is predefined, then they can go with it.

因此,如果有人面临同样的问题,并且端口字段是预定义的,那么他们可以继续使用。

kubectl --namespace=app-test get svc proxy --output jsonpath='{.spec.ports[?(@.port==9000)].nodePort}'