fix(device): guard nil maps in mthreads MutateAdmission - #2254
Conversation
MthreadsDevices.MutateAdmission writes to ctr.Resources.Limits and p.Annotations without checking they are non-nil. Two normal inputs trigger "assignment to entry in nil map" and crash the mutating webhook goroutine: - a pod that requests more than one mthreads.com/vgpu but carries no annotations (p.Annotations is nil) panics on the request-gpu-num write; - a container that specifies the vgpu count under requests only, with no limits block (ctr.Resources.Limits is nil), panics on the cores/memory write. Because the webhook runs MutateAdmission for every registered device on every pod, this takes down admission for affected mthreads pods. Initialize ctr.Resources.Limits and p.Annotations before writing, matching the nil guard already used in the metax backend. Add table cases covering both inputs (each panics before this change, passes after). Signed-off-by: imantaba <itn.taba@gmail.com>
📝 WalkthroughWalkthrough
Changesmthreads admission
Estimated code review effort: 2 (Simple) | ~10 minutes Possibly related PRs
Suggested reviewers: Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
pkg/device/mthreads/device_test.go (1)
67-83: 🎯 Functional Correctness | 🔵 Trivial | ⚡ Quick winAssert the values written by
MutateAdmission.These cases only check the return value and error state. They would pass if the function stopped writing the derived resource limits or
mthreads.com/request-gpu-num. Assert the expected core limit, memory limit, annotation value, and non-nil maps in both cases.Also applies to: 84-104
🤖 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/device/mthreads/device_test.go` around lines 67 - 83, Strengthen the test cases around MutateAdmission, including both referenced scenarios, to verify the mutation outputs rather than only its return value and error. Assert the expected core and memory resource limits, the mthreads.com/request-gpu-num annotation, and that the mutated resource and annotation maps are non-nil.
🤖 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/device/mthreads/device_test.go`:
- Around line 67-83: Strengthen the test cases around MutateAdmission, including
both referenced scenarios, to verify the mutation outputs rather than only its
return value and error. Assert the expected core and memory resource limits, the
mthreads.com/request-gpu-num annotation, and that the mutated resource and
annotation maps are non-nil.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro Plus
Run ID: 39af774c-26e7-4d70-ae8a-96907ebf7c25
📒 Files selected for processing (2)
pkg/device/mthreads/device.gopkg/device/mthreads/device_test.go
Codecov Report✅ All modified and coverable lines are covered by tests.
Flags with carried forward coverage won't be shown. Click here to find out more.
... and 1 file with indirect coverage changes 🚀 New features to boost your workflow:
|
mesutoezdil
left a comment
There was a problem hiding this comment.
pls fix the title scope, files are under pkg/device so it should be fix(device) not fix(device-plugin).
Add a table case for a container that sets the vgpu count under requests only, count=1, with no limits block. This exercises the memok-false branch, which also writes to ctr.Resources.Limits and panicked on a nil map before the guard added in the previous commit. Signed-off-by: imantaba <itn.taba@gmail.com>
Updated |
|
/lgtm |
|
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: archlitchi, imantaba The full list of commands accepted by this bot can be found here. The pull request process is described here DetailsNeeds approval from an approver in each of these files:
Approvers can indicate their approval by writing |
A container declaring only limits (no requests block) reaches the trimmed-memory write with ctr.Resources.Requests == nil and panics (assignment to entry in nil map) in the admission webhook. Initialize the map before writing, as kunlun vdevice already does for the same write and as merged for mthreads in Project-HAMi#2254. Add Test_MutateAdmission_NilRequests covering both panicking shapes: memory limit present without a requests block, and count-only without a requests block. Signed-off-by: keshav9926 <kkakani160@gmail.com>
A container declaring only limits (no requests block) reaches the trimmed-memory write with ctr.Resources.Requests == nil and panics (assignment to entry in nil map) in the admission webhook. Initialize the map before writing, as kunlun vdevice already does for the same write and as merged for mthreads in #2254. Add Test_MutateAdmission_NilRequests covering both panicking shapes: memory limit present without a requests block, and count-only without a requests block. Signed-off-by: keshav9926 <kkakani160@gmail.com>
What this PR does
MthreadsDevices.MutateAdmissionwrites toctr.Resources.Limitsandp.Annotationswithout checking they are non-nil. Writing to a nil map panicsin Go and crashes the mutating-webhook goroutine. Two ordinary inputs trigger
it:
mthreads.com/vgpubut carries noannotations (
p.Annotations == nil) panics on therequest-gpu-numwrite;requestsonly, with nolimitsblock (ctr.Resources.Limits == nil), panics on the cores/memorywrite.
Because the webhook runs
MutateAdmissionfor every registered device on everypod, this takes down admission for affected mthreads pods.
This initializes
ctr.Resources.Limitsandp.Annotationsbefore writing,matching the nil guard already used in the metax backend
(
pkg/device/metax/sdevice.go).Fixes
Fixes #2253
Testing
Added two cases to
Test_MutateAdmissioncovering both inputs. Each producespanic: assignment to entry in nil mapbefore this change and passes after.Verified red→green; full package passes under
-race; gofmt clean.AI assistance disclosure
This change was developed with AI assistance (per CONTRIBUTING.md).
Summary by CodeRabbit
Note: a requests-only extended-resource pod is eventually rejected by API validation, but mutating admission runs before validation, so the nil-
Limitspanic is still reachable.