How to get the output via jsonpath with kubectl

3/13/2020

I run kubectl get events to get the events details, now I'd like to do a fuzzy search to get the particular pods with prefix nginx-*

Suppose, I have this output as below

$ kubectl get events -o json

{
    "apiVersion": "v1",
    "items": [
        {
            "apiVersion": "v1",
            "count": 1,
            "eventTime": null,
            "firstTimestamp": "2020-03-12T06:18:58Z",
            "involvedObject": {
                "apiVersion": "v1",
                "kind": "Pod",
                "name": "nginx-6db489d4b7-99xmd",
                "namespace": "default",
                "resourceVersion": "9683",
                "uid": "64f6eeb1-c267-4ee1-b34d-14e65573d63f"
            },
            "kind": "Event",
            "lastTimestamp": "2020-03-12T06:18:58Z",
            "message": "Successfully assigned default/nginx-6db489d4b7-99xmd to kind-worker3",
            "metadata": {
                "creationTimestamp": "2020-03-12T06:18:58Z",
                "name": "nginx-6db489d4b7-99xmd.15fb7a182197a184",
                "namespace": "default",
                "resourceVersion": "9703",
                "selfLink": "/api/v1/namespaces/default/events/nginx-6db489d4b7-99xmd.15fb7a182197a184",
                "uid": "de0ff737-e4d6-4218-b441-26c68a1ee8bd"
            },
            "reason": "Scheduled",
            "reportingComponent": "",
            "reportingInstance": "",
            "source": {
                "component": "default-scheduler"
            },
            "type": "Normal"
        },
        {
            "apiVersion": "v1",
            "count": 1,
            "eventTime": null,
            "firstTimestamp": "2020-03-12T06:18:59Z",
            "involvedObject": {
                "apiVersion": "v1",
                "fieldPath": "spec.containers{nginx}",
                "kind": "Pod",
                "name": "nginx-6db489d4b7-99xmd",
                "namespace": "default",
                "resourceVersion": "9693",
                "uid": "64f6eeb1-c267-4ee1-b34d-14e65573d63f"
            },
            "kind": "Event",
            "lastTimestamp": "2020-03-12T06:18:59Z",
            "message": "Pulling image \"nginx\"",
            "metadata": {
                "creationTimestamp": "2020-03-12T06:18:59Z",
                "name": "nginx-6db489d4b7-99xmd.15fb7a18754d0bfc",
                "namespace": "default",
                "resourceVersion": "9709",
                "selfLink": "/api/v1/namespaces/default/events/nginx-6db489d4b7-99xmd.15fb7a18754d0bfc",
                "uid": "d541f134-5e9c-4b7f-b035-ae4d49a3745f"
            },
            "reason": "Pulling",
            "reportingComponent": "",
            "reportingInstance": "",
            "source": {
                "component": "kubelet",
                "host": "kind-worker3"
            },
            "type": "Normal"
        },
        {
            "apiVersion": "v1",
            "count": 1,
            "eventTime": null,
            "firstTimestamp": "2020-03-12T06:18:26Z",
            "involvedObject": {
                "apiVersion": "v1",
                "fieldPath": "spec.containers{nginx}",
                "kind": "Pod",
                "name": "nginx",
                "namespace": "default",
                "resourceVersion": "9555",
                "uid": "f9d0ae86-4d7d-4553-91c2-efc0c3f8144f"
            },
            "kind": "Event",
            "lastTimestamp": "2020-03-12T06:18:26Z",
            "message": "Pulling image \"nginx\"",
            "metadata": {
                "creationTimestamp": "2020-03-12T06:18:26Z",
                "name": "nginx.15fb7a10b4975ae0",
                "namespace": "default",
                "resourceVersion": "9565",
                "selfLink": "/api/v1/namespaces/default/events/nginx.15fb7a10b4975ae0",
                "uid": "f66cf712-1284-4f65-895a-5fbfa974e317"
            },
            "reason": "Pulling",
            "reportingComponent": "",
            "reportingInstance": "",
            "source": {
                "component": "kubelet",
                "host": "kind-worker"
            },
            "type": "Normal"
        },
        {
            "apiVersion": "v1",
            "count": 1,
            "eventTime": null,
            "firstTimestamp": "2020-03-12T06:18:38Z",
            "involvedObject": {
                "apiVersion": "v1",
                "fieldPath": "spec.containers{nginx}",
                "kind": "Pod",
                "name": "nginx",
                "namespace": "default",
                "resourceVersion": "9555",
                "uid": "f9d0ae86-4d7d-4553-91c2-efc0c3f8144f"
            },
            "kind": "Event",
            "lastTimestamp": "2020-03-12T06:18:38Z",
            "message": "Successfully pulled image \"nginx\"",
            "metadata": {
                "creationTimestamp": "2020-03-12T06:18:38Z",
                "name": "nginx.15fb7a13a4aed9fc",
                "namespace": "default",
                "resourceVersion": "9613",
                "selfLink": "/api/v1/namespaces/default/events/nginx.15fb7a13a4aed9fc",
                "uid": "55a4a512-d5c0-41da-ae9c-c1654b6bbdfe"
            },
            "reason": "Pulled",
            "reportingComponent": "",
            "reportingInstance": "",
            "source": {
                "component": "kubelet",
                "host": "kind-worker"
            },
            "type": "Normal"
        }
    ],
    "kind": "List",
    "metadata": {
        "resourceVersion": "",
        "selfLink": ""
    }
}

I'd like to get the messages from pod nginx-* only.

$ kubectl get events -o=jsonpath='{.items[*].involvedObject}'

But I am not sure how to check with name if it is nginx-* and then export its messages

            "involvedObject": {
                "apiVersion": "v1",
                "kind": "Pod",
                "name": "nginx-6db489d4b7-99xmd",
                "namespace": "default",
                "resourceVersion": "9683",
                "uid": "64f6eeb1-c267-4ee1-b34d-14e65573d63f"
            },
            "message": "Successfully assigned default/nginx-6db489d4b7-99xmd to kind-worker3",
-- Bill
jsonpath
kubectl
kubernetes

1 Answer

3/13/2020

Kubectl jsonpath implementation doesn't support regexp matching so its not possible to achieve using only this tool (Take a look at this github issue). Fortunately you can always use jq to filter events, take a look for example below.

kubectl get events -ojson | jq '.items[] | select(.involvedObject.name | test("^ngin-")) | .message'
-- HelloWorld
Source: StackOverflow