Skip to content

fix webhook to deny privileged containers - #2139

Merged
hami-robot[bot] merged 3 commits into
Project-HAMi:masterfrom
Jay2006sawant:fix/webhook-deny-privileged-containers
Jul 28, 2026
Merged

fix webhook to deny privileged containers#2139
hami-robot[bot] merged 3 commits into
Project-HAMi:masterfrom
Jay2006sawant:fix/webhook-deny-privileged-containers

Conversation

@Jay2006sawant

@Jay2006sawant Jay2006sawant commented Jul 27, 2026

Copy link
Copy Markdown
Contributor

What type of PR is this?

/kind bug

What this PR does / why we need it:

The mutating admission webhook in pkg/scheduler/webhook.go logged "Denying admission" for privileged containers but used continue instead of returning admission.Denied. When a pod had both a privileged container and a GPU-requesting container, the webhook still allowed the pod and applied scheduler/GPU patches.

This PR returns admission.Denied immediately when any container is privileged, and adds unit tests for privileged-only and mixed privileged+GPU pods.

Which issue(s) this PR fixes:
Fixes #2138

Special notes for your reviewer:

  • Change is limited to webhook.go and webhook_test.go
  • No GPU hardware required to verify
  • make test and make verify pass locally

Does this PR introduce a user-facing change?:

Yes. Pods containing any privileged container are now rejected by the HAMi mutating webhook. Previously, a pod with a privileged sidecar plus a GPU workload could be admitted.

Summary by CodeRabbit

  • Bug Fixes

    • Admission requests that include privileged containers are now denied when the workload also triggers GPU mutation, with a denial message identifying the privileged container.
    • Privileged containers are handled consistently across both init and regular containers (including privileged sidecars).
  • Tests

    • Added coverage to verify denial for privileged-only, privileged + GPU workload, and scheduler-name mismatch scenarios, and to confirm no patches are returned on denial.

Signed-off-by: Jay2006sawant <jay242902@gmail.com>
Signed-off-by: Jay2006sawant <jay242902@gmail.com>
@hami-robot hami-robot Bot added kind/bug Something isn't working dco-signoff: yes labels Jul 27, 2026
@hami-robot
hami-robot Bot requested review from DSFans2014 and wawa0210 July 27, 2026 04:49
@hami-robot

hami-robot Bot commented Jul 27, 2026

Copy link
Copy Markdown
Contributor

Welcome @Jay2006sawant! It looks like this is your first PR to Project-HAMi/HAMi 🎉

@hami-robot hami-robot Bot added the size/L label Jul 27, 2026
@coderabbitai

coderabbitai Bot commented Jul 27, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

The admission webhook detects privileged init or regular containers and denies Pods containing them when GPU resources are processed. Tests cover privileged-only, mixed GPU, init-container, and scheduler-name scenarios.

Changes

Privileged admission enforcement

Layer / File(s) Summary
Privileged container detection and denial
pkg/scheduler/webhook.go
The webhook scans init and regular containers, continues GPU mutation processing, then denies admission with the privileged container name when resources were found.
Admission behavior coverage
pkg/scheduler/webhook_test.go
Tests isolate scheduler and device state and verify allowed responses, denied responses without patches, and denial messages for privileged GPU workloads.

Estimated code review effort: 2 (Simple) | ~10 minutes

Possibly related PRs

  • Project-HAMi/HAMi#2116: Both changes update webhook container scanning involving init containers and GPU processing.

Suggested reviewers: wawa0210

Poem

A rabbit guards the GPU gate,
Privileged pods must now await.
Init or sidecar, rules align,
Clear denial marks the line.
The webhook keeps the burrow safe.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Linked Issues check ⚠️ Warning The fix still gates denial on GPU mutation, so privileged pods without GPU requests can still be admitted. Reject the pod immediately when any privileged container is detected, before any GPU or scheduler mutation, regardless of resource requests.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly states the main fix: denying privileged containers in the webhook.
Out of Scope Changes check ✅ Passed The changes stay within the webhook and its tests, matching the stated bug-fix scope.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 1

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
pkg/scheduler/webhook.go (1)

73-79: 🔒 Security & Privacy | 🟠 Major | ⚡ Quick win

Evaluate privileged containers before allowing scheduler bypass.

pkg/scheduler/webhook.go allows pods with a different scheduler before any privilege check, and only inspects pod.Spec.Containers, so privileged init containers and privileged PODs assigned to another scheduler can reach the workload. Add privilege checks for both initial/sidecar containers before the scheduler-name early return and add regression cases for the missing paths.

Relevant production code
if pod.Spec.SchedulerName != "" &&
	(pod.Spec.SchedulerName != corev1.DefaultSchedulerName || !config.ForceOverwriteDefaultScheduler) &&
	(len(config.SchedulerName) == 0 || pod.Spec.SchedulerName != config.SchedulerName) {
	klog.V(3).Infof(template+" - Pod already has different scheduler assigned", req.Namespace, req.Name, req.UID)
	return admission.Allowed("pod already has different scheduler assigned")
}
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@pkg/scheduler/webhook.go` around lines 73 - 79, Move privilege validation
ahead of the scheduler-name early return in the webhook admission flow, and
apply the same validation to both pod.Spec.InitContainers and
pod.Spec.Containers before allowing any scheduler bypass. Update the regression
cases in pkg/scheduler/webhook_test.go covering privileged init containers and
privileged pods assigned to another scheduler.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@pkg/scheduler/webhook_test.go`:
- Around line 647-664: Update the test setup around InitDevicesWithConfig to
snapshot the existing global device map and handler list before initialization,
then restore both in t.Cleanup alongside config.SchedulerName. Preserve the
test’s current device initialization while preventing global registry state from
leaking into subsequent tests.

---

Outside diff comments:
In `@pkg/scheduler/webhook.go`:
- Around line 73-79: Move privilege validation ahead of the scheduler-name early
return in the webhook admission flow, and apply the same validation to both
pod.Spec.InitContainers and pod.Spec.Containers before allowing any scheduler
bypass. Update the regression cases in pkg/scheduler/webhook_test.go covering
privileged init containers and privileged pods assigned to another scheduler.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: cf53636a-14a6-43b1-8b3f-dff1e2fa2933

📥 Commits

Reviewing files that changed from the base of the PR and between c343242 and 9f4313d.

📒 Files selected for processing (2)
  • pkg/scheduler/webhook.go
  • pkg/scheduler/webhook_test.go

Comment thread pkg/scheduler/webhook_test.go

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
pkg/scheduler/webhook_test.go (1)

789-795: 🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

Assert the denial is privilege-specific and patch-free.

A different denial with a non-empty message would pass, and a denied response carrying scheduler/GPU patches is not rejected by this test. Assert that the message identifies privilege and len(resp.Patches) == 0.

Proposed test assertions
+           if len(resp.Patches) != 0 {
+               t.Fatalf("Expected no patches for privileged pod, got %d", len(resp.Patches))
+           }
-           if resp.Result == nil || resp.Result.Message == "" {
+           if resp.Result == nil || !strings.Contains(resp.Result.Message, "is privileged") {
                t.Fatalf("Expected denial message, got: %+v", resp.Result)
            }
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@pkg/scheduler/webhook_test.go` around lines 789 - 795, Strengthen the
denied-response assertions in the webhook test around wh.Handle by requiring
resp.Result.Message to identify the privilege-related denial, while preserving
the existing non-empty check, and assert that len(resp.Patches) equals zero so
no scheduler or GPU patches accompany the denial.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Outside diff comments:
In `@pkg/scheduler/webhook_test.go`:
- Around line 789-795: Strengthen the denied-response assertions in the webhook
test around wh.Handle by requiring resp.Result.Message to identify the
privilege-related denial, while preserving the existing non-empty check, and
assert that len(resp.Patches) equals zero so no scheduler or GPU patches
accompany the denial.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 4b034784-ea8d-4c98-bf0a-3beb55ae1f3c

📥 Commits

Reviewing files that changed from the base of the PR and between 9f4313d and db52c72.

📒 Files selected for processing (2)
  • pkg/scheduler/webhook.go
  • pkg/scheduler/webhook_test.go

Comment thread pkg/scheduler/webhook.go Outdated
Signed-off-by: Jay2006sawant <jay242902@gmail.com>
@Jay2006sawant
Jay2006sawant force-pushed the fix/webhook-deny-privileged-containers branch from b9c203e to d9bb61e Compare July 27, 2026 08:37

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@pkg/scheduler/webhook_test.go`:
- Line 764: Add an hami.io/gpu resource request to the privileged container in
the different-scheduler bypass fixture in webhook_test.go, while keeping
allowed: true, so the test validates scheduler-gate behavior for GPU-mutating
Pods.

In `@pkg/scheduler/webhook.go`:
- Around line 72-75: Update the admission handler around privilegedContainerName
and MutateAdmission to immediately return admission.Denied whenever
hasPrivileged is true, before any mutation or scheduler-gate checks. Remove the
hasResource requirement so privileged-only and unrecognized-GPU pods are denied,
and update TestPrivilegedContainerDenied expectations accordingly.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 9e4bd097-2359-45bd-900f-efb9f0e31d6e

📥 Commits

Reviewing files that changed from the base of the PR and between db52c72 and d9bb61e.

📒 Files selected for processing (2)
  • pkg/scheduler/webhook.go
  • pkg/scheduler/webhook_test.go

Comment thread pkg/scheduler/webhook_test.go
Comment thread pkg/scheduler/webhook.go

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

♻️ Duplicate comments (1)
pkg/scheduler/webhook.go (1)

72-75: ⚠️ Potential issue | 🟠 Major

Return admission.Denied immediately for privileged containers.

The privileged check still occurs after the scheduler gate and all MutateAdmission calls, and denial is incorrectly gated by hasResource. This admits privileged-only pods, privileged pods with unrecognized GPU requests, and privileged pods using another scheduler; mutation errors can also replace the required denial. Move the check before the scheduler gate/mutation and remove the hasResource condition. Align the related test expectations as well.

Also applies to: 85-88

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@pkg/scheduler/webhook.go` around lines 72 - 75, Update the admission flow
around privilegedContainerName and MutateAdmission so any privileged container
returns admission.Denied immediately, before the scheduler gate or mutation
calls, regardless of GPU resource detection or scheduler selection. Remove the
hasResource requirement from this denial path, ensure mutation errors cannot
supersede it, and update related test expectations.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Duplicate comments:
In `@pkg/scheduler/webhook.go`:
- Around line 72-75: Update the admission flow around privilegedContainerName
and MutateAdmission so any privileged container returns admission.Denied
immediately, before the scheduler gate or mutation calls, regardless of GPU
resource detection or scheduler selection. Remove the hasResource requirement
from this denial path, ensure mutation errors cannot supersede it, and update
related test expectations.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 20edd6e8-bd99-48d4-9c7b-711cfe74be43

📥 Commits

Reviewing files that changed from the base of the PR and between c343242 and d9bb61e.

📒 Files selected for processing (2)
  • pkg/scheduler/webhook.go
  • pkg/scheduler/webhook_test.go
🚧 Files skipped from review as they are similar to previous changes (1)
  • pkg/scheduler/webhook_test.go

@codecov

codecov Bot commented Jul 28, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

Flag Coverage Δ
unittests 62.07% <100.00%> (+0.07%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
pkg/scheduler/webhook.go 82.29% <100.00%> (+6.10%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@archlitchi archlitchi left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

/lgtm

@hami-robot

hami-robot Bot commented Jul 28, 2026

Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: archlitchi, Jay2006sawant

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@hami-robot hami-robot Bot added the approved label Jul 28, 2026
@hami-robot
hami-robot Bot merged commit 7db31bf into Project-HAMi:master Jul 28, 2026
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] Mutating webhook allows pods with privileged containers when other containers request GPU resources

3 participants