JonM
JonM

Reputation: 510

How do I get Events associated with a Pod via the API?

When I do a kubectl describe <pod>, the bottom section has an "Events" section, displaying Events related to that pod. For example, an event with Reason "failedScheduling", with the message "Failed for reason PodFitsResources and possibly others"

How can I query the API to return that list of events?

If I call /api/v1/namespaces/<ns>/pods/<pod_name>, it doesn't return any Events. If I try the /api/v1/events endpoint, I can specify a labelSelector parameter, but the name of the pod isn't a label of the Event, though it is in the object.involvedObject.name field.

I could request the entire Event stream and filter out the few Events that interest me client-side, but that seems like overkill. kubectl is able to do it, so I figure there must be some way that I'm missing.

Thanks.

Upvotes: 7

Views: 6837

Answers (2)

Jibin Mathews
Jibin Mathews

Reputation: 604

If you are still wondering how kubectl gets the events along with the describe command, then have a look at the following:

https://github.com/kubernetes/kubernetes/blob/b6a0718858876bbf8cedaeeb47e6de7e650a6c5b/pkg/kubectl/describe/versioned/describe.go#L242

If you see what's happening is that they first get the details of the resource requested (see https://github.com/kubernetes/kubernetes/blob/b6a0718858876bbf8cedaeeb47e6de7e650a6c5b/pkg/kubectl/describe/versioned/describe.go#L235 ) and then they get all the events from that namespace and filter out the events for the requested resource. See Line 242 in the same link.

So they are not using some other undocumented API or other ways, What you thought as overkill is what they are doing.

Upvotes: 1

Jordan Liggitt
Jordan Liggitt

Reputation: 18161

I think events support a fieldSelector for the involved object kind and name

You can also turn the verbosity level on kubectl up to 8 to see network traces to see what it is doing

Upvotes: 8

Related Questions