Skip to content

fix: process init containers in admission mutation and quota checks - #2168

Closed
Gaurav-205 wants to merge 1 commit into
Project-HAMi:masterfrom
Gaurav-205:fix/webhook-init-container-admission
Closed

fix: process init containers in admission mutation and quota checks#2168
Gaurav-205 wants to merge 1 commit into
Project-HAMi:masterfrom
Gaurav-205:fix/webhook-init-container-admission

Conversation

@Gaurav-205

@Gaurav-205 Gaurav-205 commented Jul 28, 2026

Copy link
Copy Markdown

Summary

While pkg/device/devices.go accounts for both InitContainers and Containers during scheduling and resource allocation fitting, the mutating admission webhook (pkg/scheduler/webhook.go) and resource quota checks (fitResourceQuota) previously only iterated pod.Spec.Containers.

When a Pod requested accelerator resources (e.g., nvidia.com/gpu or hami.io/gpu) exclusively inside an init container:

  1. The webhook failed to mutate admission for the init container.
  2. The HAMi custom scheduler name (config.SchedulerName) was not assigned to the Pod.
  3. fitResourceQuota() skipped init container resource limits/requests.

This PR updates pkg/scheduler/webhook.go to iterate over pod.Spec.InitContainers in both MutateAdmission and fitResourceQuota(), ensuring consistent resource mutation, scheduler injection, and quota enforcement.

Changes

  • Updated Handle() in pkg/scheduler/webhook.go to process pod.Spec.InitContainers.
  • Updated fitResourceQuota() in pkg/scheduler/webhook.go to calculate init container resource requests.
  • Added unit test TestInitContainerOnlyGPUAdmission in pkg/scheduler/webhook_test.go.

Fixes #2167

Summary by CodeRabbit

  • New Features

    • GPU and device resource requests in init containers are now recognized during pod admission.
    • Scheduler assignment, quota checks, and resource mutations now account for init-container requirements.
  • Bug Fixes

    • Pods requesting GPU resources exclusively through init containers are correctly admitted and assigned to the configured scheduler.

Signed-off-by: Gaurav-205 <gauravkhandelwal205@gmail.com>
@hami-robot

hami-robot Bot commented Jul 28, 2026

Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: Gaurav-205
Once this PR has been reviewed and has the lgtm label, please assign fouof for approval. For more information see the Kubernetes Code Review Process.

The full list of commands accepted by this bot can be found 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 requested review from chaunceyjiang and wawa0210 July 28, 2026 09:42
@hami-robot hami-robot Bot added the size/L label Jul 28, 2026
@coderabbitai

coderabbitai Bot commented Jul 28, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

The scheduler webhook now processes init-container device resources during admission and quota evaluation. A regression test verifies admission and scheduler-name patching for a pod whose GPU request exists only in an init container.

Changes

Init-container GPU accounting

Layer / File(s) Summary
Admission mutation, quota evaluation, and regression coverage
pkg/scheduler/webhook.go, pkg/scheduler/webhook_test.go
Admission mutations and quota calculations now include init containers, with a test covering init-container-only GPU requests and scheduler assignment.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Sequence Diagram(s)

sequenceDiagram
  participant AdmissionRequest
  participant webhook.Handle
  participant device.GetDevices
  participant fitResourceQuota
  AdmissionRequest->>webhook.Handle: submit Pod admission request
  webhook.Handle->>device.GetDevices: mutate init-container resources
  webhook.Handle->>fitResourceQuota: evaluate init-container quota requests
  webhook.Handle-->>AdmissionRequest: return admission result and schedulerName patch
Loading

Possibly related issues

  • Project-HAMi/HAMi#2167 — Directly addresses init-container resource processing in admission mutation and quota checks.

Possibly related PRs

  • Project-HAMi/HAMi#2064 — Directly concerns init-container GPU resource accounting in admission and quota calculations.
  • Project-HAMi/HAMi#2116 — Also updates webhook admission and quota handling for init-container GPU resources.
  • Project-HAMi/HAMi#2139 — Shares changes to webhook container inspection and admission decisions involving init containers.

Suggested reviewers: dsfans2014, jay2006sawant

Poem

A bunny found GPUs tucked out of sight,
In init pods waiting through the night.
The webhook now counts each tiny request,
And patches the scheduler as expected.
Hop, test, pass—what a delightful flight!

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly matches the main change: processing init containers in admission mutation and quota checks.
✨ 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.

🧹 Nitpick comments (1)
pkg/scheduler/webhook_test.go (1)

853-856: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Add coverage for init-container quota enforcement.

This fixture specifies only hami.io/gpu; therefore the new memory/core accumulation at pkg/scheduler/webhook.go Lines 170-175 remains zero and the test still passes if that quota loop is removed. Add an init-container case with GPU memory/core values and a constrained quota that verifies the expected admission result.

Also applies to: 898-913

🤖 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 853 - 856, Add a test case in the
existing webhook quota tests around the resource fixture and the related
assertions to cover init-container GPU memory/core accumulation. Configure
init-container resources with nonzero GPU memory and core values, apply a quota
constrained below their accumulated usage, and assert the expected admission
result so removing the init-container quota loop causes the test to fail.
🤖 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.

Nitpick comments:
In `@pkg/scheduler/webhook_test.go`:
- Around line 853-856: Add a test case in the existing webhook quota tests
around the resource fixture and the related assertions to cover init-container
GPU memory/core accumulation. Configure init-container resources with nonzero
GPU memory and core values, apply a quota constrained below their accumulated
usage, and assert the expected admission result so removing the init-container
quota loop causes the test to fail.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: c9410f69-10af-4a39-a447-11bb1035df91

📥 Commits

Reviewing files that changed from the base of the PR and between 37730dd and 41de062.

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

@github-actions github-actions Bot added the kind/bug Something isn't working label Jul 28, 2026
@maishivamhoo123

Copy link
Copy Markdown
Member

Thank you for your contribution @Gaurav-205 please check
https://github.com/Project-HAMi/HAMi/blob/master/CONTRIBUTING.md#code-of-conduct before raising a PR.

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: Init-container-only accelerator requests bypass admission mutation and quota checks

3 participants