Cannot get Kubernetes Events filtered by labels

2/25/2021

I’m trying to filter some events by their labels. When I’m doing this with pods it’s working, but not with events.

kubectl get pods -l env=development (GIVES ME RESULT)

kubectl get events -l env=development (GIVES ME: No resources found in default namespace)

Can you help me figure out the difference?

Cluster information:

Kubernetes version: minikube version: v1.17.1

-- Josue Lopes
kubernetes

1 Answer

2/26/2021

I'm afraid you are not able to filter $ kubectl get event output by labels/selectors.

If you would use command $ kubectl get events -o json you would get output similar to:

        {
            "apiVersion": "v1",
            "count": 1,
            "eventTime": null,
            "firstTimestamp": "2021-02-26T10:34:26Z",
            "involvedObject": {
                "apiVersion": "v1",
                "fieldPath": "spec.containers{nginx}",
                "kind": "Pod",
                "name": "nginx",
                "namespace": "default",
                "resourceVersion": "37393",
                "uid": "fb62eac8-b6ad-4687-ac8c-1944e426d888"
                ...

Below that output you will find more information about particular event.

You can filter events only by the key: value which are in the output. For example based on the name (from involvedObject)

$ kubectl get event --field-selector involvedObject.name=nginx
LAST SEEN   TYPE     REASON      OBJECT      MESSAGE
6m26s       Normal   Scheduled   pod/nginx   Successfully assigned default/nginx to gke-cluster-1-default-pool-9a99afef-vdkv
6m25s       Normal   Pulling     pod/nginx   Pulling image "nginx"
6m25s       Normal   Pulled      pod/nginx   Successfully pulled image "nginx" in 253.411606ms
6m25s       Normal   Created     pod/nginx   Created container nginx
6m25s       Normal   Started     pod/nginx   Started container nginx

As you can see on below output, $ kubectl get event don't have key like selector nor label

$ kubectl get po --show-labels
NAME    READY   STATUS    RESTARTS   AGE   LABELS
nginx   1/1     Running   0          10m   evn=test,run=nginx
$ kubectl get events -o json | grep env
$ kubectl get events -o json | grep test
$ kubectl get events -o json | grep label
$ kubectl get events -o json | grep selector
$

kubectl get events is described in Understanding Kubernetes cluster events blog as:

Kubernetes events are objects that show you what is happening inside a cluster, such as what decisions were made by the scheduler or why some pods were evicted from the node. All core components and extensions (operators) may create events through the API Server.

In short, their don't have own labels. They more like log messages.

-- PjoterS
Source: StackOverflow