Kubectl: Unable to kubectl get -o jsonpath annotation value

Created on 7 Jun 2017  路  9Comments  路  Source: kubernetes/kubectl

What keywords did you search in Kubernetes issues before filing this one? (If you have found any duplicates, you should instead reply there.): kubectl get annotation json

This feature request seems like it would help in this scenario: https://github.com/kubernetes/kubernetes/issues/19817


Is this a BUG REPORT or FEATURE REQUEST? (choose one): BUG

Kubernetes version (use kubectl version):

Output:

Client Version: version.Info{Major:"1", Minor:"6", GitVersion:"v1.6.4", GitCommit:"d6f433224538d4f9ca2f7ae19b252e6fcb66a3ae", GitTreeState:"clean", BuildDate:"2017-05-19T18:44:27Z", GoVersion:"go1.7.5", Compiler:"gc", Platform:"darwin/amd64"}
Server Version: version.Info{Major:"1", Minor:"6", GitVersion:"v1.6.4", GitCommit:"d6f433224538d4f9ca2f7ae19b252e6fcb66a3ae", GitTreeState:"clean", BuildDate:"2017-05-19T18:33:17Z", GoVersion:"go1.7.5", Compiler:"gc", Platform:"linux/amd64"}

Environment:

  • Cloud provider or hardware configuration: GKE
  • OS (e.g. from /etc/os-release):
  • Kernel (e.g. uname -a):
  • Install tools:
  • Others:

What happened:
Unable to get annotation value when there is a / in the annotation key.

What you expected to happen:
Get the annotation value (see below).

How to reproduce it (as minimally and precisely as possible):

Data:

{
    "apiVersion": "extensions/v1beta1",
    "kind": "Ingress",
    "metadata": {
        "annotations": {
            "description": "my frontend",
            "ingress.gcp.kubernetes.io/pre-shared-cert": "tony-cert-1",
            "ingress.kubernetes.io/backends": "{\"k8s-be-30237--d785be79bbf6d463\":\"HEALTHY\"}",
            "ingress.kubernetes.io/https-forwarding-rule": "k8s-fws-default-echo-app-tls-2--d785be79bbf6d463",
            "ingress.kubernetes.io/https-target-proxy": "k8s-tps-default-echo-app-tls-2--d785be79bbf6d463",
            "ingress.kubernetes.io/ssl-cert": "tony-cert-1",
            "ingress.kubernetes.io/url-map": "k8s-um-default-echo-app-tls-2--d785be79bbf6d463",
            "kubectl.kubernetes.io/last-applied-configuration": "{\"apiVersion\":\"extensions/v1beta1\",\"kind\":\"Ingress\",\"metadata\":{\"annotations\":{\"ingress.gcp.kubernetes.io/pre-shared-cert\":\"tony-cert-1\",\"kubernetes.io/ingress.allow-http\":\"false\",\"kubernetes.io/ingress.global-static-ip-name\":\"make-static\"},\"name\":\"echo-app-tls-2\",\"namespace\":\"default\"},\"spec\":{\"backend\":{\"serviceName\":\"echo-app\",\"servicePort\":88}}}\n",
            "kubernetes.io/ingress.allow-http": "false",
            "kubernetes.io/ingress.global-static-ip-name": "make-static"
        },
        "creationTimestamp": "2017-05-24T19:57:16Z",
        "generation": 1,
        "name": "echo-app-tls-2",
        "namespace": "default",
        "resourceVersion": "17247450",
        "selfLink": "/apis/extensions/v1beta1/namespaces/default/ingresses/echo-app-tls-2",
        "uid": "2fe93467-40bb-11e7-a242-42010a8000f8"
    },
    "spec": {
        "backend": {
            "serviceName": "echo-app",
            "servicePort": 88
        }
    },
    "status": {
        "loadBalancer": {
            "ingress": [
                {
                    "ip": ".............."
                }
            ]
        }
    }
}

Works:

$ kubectl get ing/echo-app-tls-2 -o jsonpath='{.metadata.annotations.description}'
my frontend

Does not work:

$ kubectl get ing/echo-app-tls-2 -o jsonpath='{.metadata.annotations."ingress.kubernetes.io/url-map"}'
<blank>

$ kubectl get ing/echo-app-tls-2 -o jsonpath='{.metadata.annotations.ingress.kubernetes.io/url-map}'
<blank>

$ kubectl get ing/echo-app-tls-2 -o jsonpath="{.metadata.annotations.'ingress.kubernetes.io/url-map'}"
<blank>

$ kubectl get ing/echo-app-tls-2 -o jsonpath="{.metadata.annotations[ingress.kubernetes.io/url-map]}"
error: error parsing jsonpath {.metadata.annotations[ingress.kubernetes.io/url-map]}, invalid array index ingress.kubernetes.io/url-map

$ kubectl get ing/echo-app-tls-2 -o jsonpath="{.metadata.annotations['ingress.kubernetes.io/url-map']}"
<blank>

$ kubectl get ing/echo-app-tls-2 -o jsonpath='{.metadata.annotations["ingress.kubernetes.io/url-map"]}'
error: error parsing jsonpath {.metadata.annotations["ingress.kubernetes.io/url-map"]}, invalid array index "ingress.kubernetes.io/url-map"

Anything else we need to know:
Tested the above data with https://jsonpath.curiousconcept.com/ and these selectors were able to get the value:

  • .metadata.annotations['ingress.kubernetes.io/url-map']
  • .metadata.annotations.['ingress.kubernetes.io/url-map']

Most helpful comment

@tonglil please take a look at the test case https://github.com/kubernetes/kubernetes/blob/master/staging/src/k8s.io/client-go/util/jsonpath/jsonpath_test.go#L280 to know what is supported now.

{.metadata.annotations.kubernetes\.io/created-by}

This should work for you.

All 9 comments

@tonglil please take a look at the test case https://github.com/kubernetes/kubernetes/blob/master/staging/src/k8s.io/client-go/util/jsonpath/jsonpath_test.go#L280 to know what is supported now.

{.metadata.annotations.kubernetes\.io/created-by}

This should work for you.

The listed annotations should work, which means we have a bug in our JSONPath implementation

Please file a bug in the main repo

Thanks. I didn't know about escaping the ..

This query allowed it to work (note for future readers, escape all .s in the key (ingress.kubernetes.io/url-map) of the map):

kubectl get ing/echo-app-tls-2 -o jsonpath='{.metadata.annotations.ingress\.kubernetes\.io/url-map}'

@tonglil Thank you for adding the last comment. You are a star :-)

Thanks @tonglil ; I was seeing the same issue, found that I was not escaping the .

@tonglil - Please document this example in kubernetes.io/docs

this not work on k8s 1.17

.metadata.annotations.ingress\.kubernetes\.io/url-map works for me in 1.17

Was this page helpful?
0 / 5 - 0 ratings