Skip to content

Expose pod details to the webhook template - #1289

Merged
Lu-David merged 2 commits into
aws:mainfrom
zhan9san:webhook-pod-details
Nov 12, 2026
Merged

Lu-David merged 2 commits into
aws:mainfrom
zhan9san:webhook-pod-details

Conversation

@zhan9san

Copy link
Copy Markdown
Contributor

Issue #, if available: Fixes #1288

Description of changes:

What

Adds a PodDetails field to the webhook template data, with each pod's namespace, phase, controller kind, labels and annotations.

Why

The webhook fires while the affected pods can still be inspected, but Pods only carries names. Without namespace, phase, owner, labels or annotations, a receiver can't tell which workloads an interruption actually hit, or route the notification to the team that owns them. Details are in #1288.

Changes

  • pkg/node: add a PodDetail type, FetchPodDetails and a PodNames helper. FetchPodDetails uses the same fetchAllPods List call as FetchPodNameList, so no extra API request is made.
  • pkg/interruptionevent/draincordon: the handler fetches pod details once and sets both Pods (unchanged) and PodDetails from that result.
  • pkg/monitor: add PodDetails []node.PodDetail to InterruptionEvent, tagged json:"-". The event is logged with Interface("event", ...) when handling fails, so this keeps pod annotations out of the logs. Templates still see the field.
  • OwnerKind is the kind of the pod's controller (metav1.GetControllerOf), or "" for a bare pod. Expose pod details (namespace, phase, owner, labels, annotations) to the webhook template #1288 proposed the first owner reference; the controller reference is more precise when a pod has non-controller owners.
  • Pods, LogPods and the default template are unchanged, so existing templates render the same as before.

Example: running workload pods only.

{{ range .PodDetails }}
{{- if and (eq .Phase "Running") (ne .OwnerKind "DaemonSet") }}
{{ .Namespace }}/{{ .Name }} {{ index .Annotations "example.com/owner" }}
{{ end }}{{ end }}

How you tested your changes:

  • New unit tests:
    • TestFetchPodDetails covers field mapping, a controller owner and a non-controller owner.
    • TestPodNames.
    • TestHandleEvent_PopulatesPodDetails checks that the handler sets Pods and PodDetails.
    • TestPostPodDetails filters by phase and owner and reads labels and annotations in a template.
    • TestInterruptionEventJSONOmitsPodDetails.
  • go test ./... passes.
  • e2e: test/e2e/webhook-test now ranges over .PodDetails in its template. If the field couldn't be rendered, template execution would fail and the test's Webhook Success assertion would not pass. I checked the escaped --set value with helm template, but I didn't run the kind-based e2e suite locally.

Environment (Linux / Windows): macOS (darwin/arm64), unit tests only
Kubernetes Version: client-go fake clientset (k8s.io/client-go v0.30.4)

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

Add PodDetails to the webhook template data: namespace, phase, controller
kind, labels and annotations for each pod on the node. It is filled from
the same pod List call as Pods, so no extra API request is made. Pods and
the default template are unchanged.

PodDetails is excluded from JSON so pod annotations do not end up in the
event logs.
@zhan9san
zhan9san requested a review from a team as a code owner October 29, 2026 06:03
@zhan9san

Copy link
Copy Markdown
Contributor Author

@LikithaVemulapalli

Could you plz help review this PR?

@LikithaVemulapalli LikithaVemulapalli left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall looks good, added some minor comments, will enable the PR tests to see if the change runs successfully on all the k8s versions that we have in our e2e tests. Thanks for owning the fix :)

Comment thread pkg/node/node.go Outdated
Comment on lines +99 to +100
Labels map[string]string
Annotations map[string]string

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exposing full label/annotation maps is fine since the template opts in and the default template is unchanged, but annotations can hold sensitive data (e.g. last-applied-configuration). Add a docs caveat to select specific keys rather than dumping whole maps. The json:"-" keeping them out of logs is good.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a "Webhook Template" section to the chart README listing the .PodDetails fields, with a caveat to select specific keys via index .Annotations "key" rather than rendering whole maps, since annotations like last-applied-configuration can carry sensitive data. Also noted it on the Annotations field.

Comment thread pkg/node/node.go Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FetchPodNameList now has no in-repo caller, remove it or reimplement as PodNames(FetchPodDetails(...)).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed it along with its test; the handler derives names via PodNames(FetchPodDetails(...)), and TestFetchPodDetails/TestPodNames cover that path.

Add a Webhook Template section to the chart README listing the
.PodDetails fields, with a caveat to select specific annotation keys
rather than rendering whole maps, since annotations such as
last-applied-configuration can hold sensitive data.

Remove FetchPodNameList, which no longer has a caller; the handler
derives pod names from FetchPodDetails.
@zhan9san

Copy link
Copy Markdown
Contributor Author

@LikithaVemulapalli Thanks for the review! Both comments are addressed in 7bb07c5. Could you approve the workflow run for the new commit, and add the stalebot-ignore label so the PR is not closed by the stale bot while it waits for review? Thanks!

@LikithaVemulapalli LikithaVemulapalli left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/lgtm

@Lu-David
Lu-David merged commit ae82987 into aws:main Nov 12, 2026
10 checks passed
@zhan9san
zhan9san deleted the webhook-pod-details branch November 13, 2026 01:42
@zhan9san

Copy link
Copy Markdown
Contributor Author

@LikithaVemulapalli thanks for your help

May I ask when we can have a new release for this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Expose pod details (namespace, phase, owner, labels, annotations) to the webhook template

3 participants