kubectl jsonpath expression for named path

1/7/2017

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

$ 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:

$ 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

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

3 Answers

1/7/2017

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

$ 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
-- Amit Kumar Gupta
Source: StackOverflow

1/24/2020

I had this issues on Windows in Powershell:

Error executing template: unrecognized identifier http2

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

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}'
-- AC81
Source: StackOverflow

11/20/2017

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

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.

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

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}'
-- Utkarsh Yeolekar
Source: StackOverflow