Skip to content

fix(amd): empty nouse-gputype annotation should not exclude every device - #2395

Merged
hami-robot[bot] merged 6 commits into
Project-HAMi:masterfrom
adity1raut:fix/amd-empty-nouse-gputype
Aug 7, 2026
Merged

fix(amd): empty nouse-gputype annotation should not exclude every device#2395
hami-robot[bot] merged 6 commits into
Project-HAMi:masterfrom
adity1raut:fix/amd-empty-nouse-gputype

Conversation

@adity1raut

@adity1raut adity1raut commented Aug 6, 2026

Copy link
Copy Markdown
Contributor

What this fixes

checkAMDType in pkg/device/amd/device.go decides whether an AMD card is eligible for a pod based on the amd.com/use-gputype / amd.com/nouse-gputype annotations. The in-use check is guarded against an empty annotation value:

if inuse, ok := annos[AMDInUse]; ok {

but the no-use check was not:

if noUse, ok := annos[AMDNoUse]; ok {
    noUseTypes := strings.Split(noUse, ",")
    if slices.ContainsFunc(noUseTypes, func(noUseType string) bool {
        return strings.Contains(cardType, strings.ToUpper(strings.TrimSpace(noUseType)))
    }) {
        return false
    }
}

If amd.com/nouse-gputype is present but set to an empty string, strings.Split("", ",") returns [""], and strings.Contains(cardType, "") is always true in Go (every string contains the empty string). So ContainsFunc returns true, and the function returns false for every card — an empty exclusion annotation ends up excluding all AMD devices instead of excluding none, and the pod becomes unschedulable.

The shared device.CheckType helper used by the other vendor backends (nvidia, hygon, ...) already guards against this with ok && strings.TrimSpace(value) != "". The AMD backend has its own local copy of this logic and was missing that guard on the no-use branch.

Fix

Add the same ok && strings.TrimSpace(...) != "" guard to the no-use branch, mirroring the existing guard on the in-use branch and the shared helper.

Testing

  • Added TestCheckAMDType covering empty/non-empty use and no-use annotations, including the regression case. Verified it fails against the old code and passes with the fix.
  • go test ./pkg/device/amd/... -race -count=1 passes.
  • This is scheduler-extender filtering logic (runs during Fit()), not device-plugin/container isolation code, so per the contribution guide's testing policy, unit tests are the appropriate validation here — I don't have AMD GPU hardware to validate against.

AI disclosure

This PR was written primarily by Geminy, used to investigate the codebase, identify the bug, implement the fix, and write the regression test. I reviewed and verified the change before submitting.

Summary by CodeRabbit

Summary by CodeRabbit

  • Bug Fixes

    • Improved AMD device filtering so empty or whitespace-only usage annotations no longer incorrectly exclude devices.
    • Comma-separated filters now ignore blank entries and trim surrounding whitespace for more predictable matching.
  • Tests

    • Added coverage for AMD filtering edge cases.
    • Improved scheduler cleanup and quota validation test reliability.

@hami-robot
hami-robot Bot requested review from DSFans2014 and FouoF August 6, 2026 03:02
@github-actions github-actions Bot added the kind/bug Something isn't working label Aug 6, 2026
@hami-robot hami-robot Bot added the size/S label Aug 6, 2026
@coderabbitai

coderabbitai Bot commented Aug 6, 2026

Copy link
Copy Markdown

Review Change Stack

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 006cd3bd-ca0f-423f-b021-68da9f26e846

📥 Commits

Reviewing files that changed from the base of the PR and between fe2f753 and ab4a621.

📒 Files selected for processing (4)
  • pkg/device/amd/device.go
  • pkg/device/amd/device_test.go
  • pkg/scheduler/scheduler_test.go
  • pkg/scheduler/webhook_test.go
🚧 Files skipped from review as they are similar to previous changes (3)
  • pkg/device/amd/device.go
  • pkg/device/amd/device_test.go
  • pkg/scheduler/scheduler_test.go

📝 Walkthrough

Walkthrough

The AMD type filter now ignores empty or whitespace-only annotation values and empty comma-separated tokens. Tests cover the filtering cases. Scheduler tests now close channels, remove a temporary device registration, and mark quota limits as explicitly configured.

Changes

AMD annotation filtering and scheduler test updates

Layer / File(s) Summary
AMD filter behavior and regression coverage
pkg/device/amd/device.go, pkg/device/amd/device_test.go
checkAMDType trims filter tokens and skips empty tokens for inclusion and exclusion filters. Tests cover matching, exclusion, absent annotations, blank values, and blank comma-separated members.
Scheduler test resource cleanup
pkg/scheduler/scheduler_test.go
Scheduler tests close stopCh during cleanup. The malformed-annotation test also removes its temporary TEST device registration.
Explicit quota fixture configuration
pkg/scheduler/webhook_test.go
Cambricon, Hygon, multi-MLU, and Ascend quota fixtures set LimitSet: true.

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

Possibly related PRs

Suggested reviewers: dsfans2014, wawa0210, fouof

Poem

Poem

A rabbit trims each AMD tag,
Blank tokens leave the flag.
Tests close channels tight,
Quotas mark limits right,
Clean fixtures hop along the path.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main fix: empty AMD no-use annotations must not exclude every device.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
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.
✨ 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

🧹 Nitpick comments (1)
pkg/device/amd/device_test.go (1)

475-477: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Add whitespace-only regression cases.

The implementation uses strings.TrimSpace, but the table tests only the empty string. Add " " cases for both AMDInUse and AMDNoUse to verify the whitespace-only behavior.

Proposed test additions
 		{"empty nouse annotation keeps card", map[string]string{AMDNoUse: ""}, "MI300X", true},
 		{"empty use annotation keeps card", map[string]string{AMDInUse: ""}, "MI300X", true},
+		{"whitespace-only nouse annotation keeps card", map[string]string{AMDNoUse: "   "}, "MI300X", true},
+		{"whitespace-only use annotation keeps card", map[string]string{AMDInUse: "   "}, "MI300X", true},
🤖 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/amd/device_test.go` around lines 475 - 477, Add whitespace-only
table cases alongside the existing empty annotation cases in the relevant test
table, covering both AMDInUse and AMDNoUse with a value of spaces and expecting
the MI300X card to remain included.
🤖 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/device/amd/device.go`:
- Around line 167-175: Update the AMDInUse matching logic around the useTypes
predicate to trim each comma-separated member and skip empty entries before
calling strings.Contains. Preserve the existing rejection behavior when no
non-empty member matches, and apply the same empty-member handling without
changing AMDNoUse processing.

---

Nitpick comments:
In `@pkg/device/amd/device_test.go`:
- Around line 475-477: Add whitespace-only table cases alongside the existing
empty annotation cases in the relevant test table, covering both AMDInUse and
AMDNoUse with a value of spaces and expecting the MI300X card to remain
included.
🪄 Autofix

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: 9ffefe8b-f816-4464-a52f-c49788e2f4f3

📥 Commits

Reviewing files that changed from the base of the PR and between e6d0902 and 689e7d4.

📒 Files selected for processing (2)
  • pkg/device/amd/device.go
  • pkg/device/amd/device_test.go

Comment thread pkg/device/amd/device.go
@codecov

codecov Bot commented Aug 6, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

Flag Coverage Δ
unittests 63.73% <100.00%> (+0.35%) ⬆️

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

Files with missing lines Coverage Δ
pkg/device/amd/device.go 71.49% <100.00%> (+3.09%) ⬆️

... and 3 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@coderabbitai

coderabbitai Bot commented Aug 6, 2026

Copy link
Copy Markdown

Note

GitHub couldn't provide a complete incremental comparison for this pull request, so CodeRabbit is performing a full review instead. This review may take a little longer.

@adity1raut
adity1raut marked this pull request as draft August 6, 2026 04:29
@adity1raut
adity1raut marked this pull request as ready for review August 6, 2026 07:53
@hami-robot
hami-robot Bot requested a review from wawa0210 August 6, 2026 07:53
@archlitchi

Copy link
Copy Markdown
Member

please resolve the code-rabbit comment

adity1raut added a commit to adity1raut/HAMi that referenced this pull request Aug 7, 2026
strings.Split on a trailing/leading/double comma (e.g. "MI250,") leaves
an empty member, and strings.Contains(cardType, "") is always true, so
that member matched every card. This let a nouse-gputype annotation
like "MI250," exclude all cards instead of just MI250, and let a
use-gputype annotation like "MI300," match cards it shouldn't. Trim
and skip empty members before matching, in both branches.

Also add whitespace-only and trailing/leading-comma regression cases
to TestCheckAMDType, per review feedback on PR Project-HAMi#2395.

Signed-off-by: Aditya Raut <araut7798@gmail.com>
@hami-robot hami-robot Bot added size/M and removed size/S labels Aug 7, 2026

@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

🤖 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/scheduler_test.go`:
- Line 2070: Update the test setup around device.SupportDevices to capture the
prior "TEST" mapping and whether the key was present before assigning the
temporary value. In the t.Cleanup callback, restore the saved mapping when it
previously existed; otherwise delete "TEST", preserving the original
package-level state.
🪄 Autofix

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: 009fb84f-56bb-4873-a6ac-33c059320f31

📥 Commits

Reviewing files that changed from the base of the PR and between a6e09ad and 0eeb3e9.

📒 Files selected for processing (1)
  • pkg/scheduler/scheduler_test.go

Comment thread pkg/scheduler/scheduler_test.go
checkAMDType only guarded the in-use annotation against an empty
value; the no-use annotation had no such guard. If
amd.com/nouse-gputype was present but set to an empty string,
strings.Split returned a single empty-string element, and
strings.Contains(cardType, "") is always true in Go, so every AMD
card was treated as excluded and no AMD device could be scheduled.

Apply the same "ok && non-empty" guard used for the in-use annotation
to the no-use annotation as well, matching the shared
device.CheckType helper used by other vendor backends.

Signed-off-by: Aditya Raut <araut7798@gmail.com>
…ling

Signed-off-by: Aditya Raut <araut7798@gmail.com>
strings.Split on a trailing/leading/double comma (e.g. "MI250,") leaves
an empty member, and strings.Contains(cardType, "") is always true, so
that member matched every card. This let a nouse-gputype annotation
like "MI250," exclude all cards instead of just MI250, and let a
use-gputype annotation like "MI300," match cards it shouldn't. Trim
and skip empty members before matching, in both branches.

Also add whitespace-only and trailing/leading-comma regression cases
to TestCheckAMDType, per review feedback on PR Project-HAMi#2395.

Signed-off-by: Aditya Raut <araut7798@gmail.com>
Signed-off-by: Aditya Raut <araut7798@gmail.com>
Several tests call informerFactory.Start(s.stopCh) but never close
s.stopCh, so the shared informer's event-processing goroutine keeps
running after the test finishes. Test_Filter and Test_ResourceQuota
also wire that informer's UpdateFunc to s.onUpdatePod, so a pod
update processed during the test can still be in flight on that
leaked goroutine once later tests start, racing their unsynchronized
writes to global state (e.g. device.SupportDevices in
Test_onAddPod_BadDeviceAnnotation) against DecodePodDevices' reads.

Reproduced with the race detector via the same command CI's
hack/unit-test.sh runs (go test $(go list ./pkg/... ./cmd/...)
-short --race -count=1); confirmed unrelated to the pkg/device/amd
change in this PR since it reproduces without it. Add t.Cleanup to
close each stopCh (and to remove the "TEST" entry added to
device.SupportDevices) so no test outlives its own goroutines.

Signed-off-by: Aditya Raut <araut7798@gmail.com>
Project-HAMi#2313 added a LimitSet flag that FitQuota now gates on instead of
Limit != 0, and updated TestFitResourceQuota's fixture accordingly,
but missed three sibling tests in the same file that also build
device.Quota directly: TestFitResourceQuotaNonNvidia,
TestFitResourceQuotaCountsEveryDevice, and
TestFitResourceQuotaAscendMemoryFactor. Their fixtures defaulted to
LimitSet: false, so FitQuota treated the configured limits as unset
and admitted every pod, failing the denial assertions in all three
tests on current master.

Set LimitSet: true on the five affected fixture entries to match what
AddQuota produces, mirroring the fix already applied to
TestFitResourceQuota.

Signed-off-by: Aditya Raut <araut7798@gmail.com>
@adity1raut
adity1raut force-pushed the fix/amd-empty-nouse-gputype branch from 0eeb3e9 to ab4a621 Compare August 7, 2026 04:43
@coderabbitai

coderabbitai Bot commented Aug 7, 2026

Copy link
Copy Markdown

Note

GitHub couldn't provide a complete incremental comparison for this pull request, so CodeRabbit is performing a full review instead. This review may take a little longer.

@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

@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 Aug 7, 2026

Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

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

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 merged commit 8f692ce into Project-HAMi:master Aug 7, 2026
17 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.

2 participants