Skip to content

fix(mock): rediscover resource name changes - #23

Open
blackdragoon26 wants to merge 2 commits into
Project-HAMi:masterfrom
blackdragoon26:fix/dynamic-resource-discovery
Open

blackdragoon26 wants to merge 2 commits into
Project-HAMi:masterfrom
blackdragoon26:fix/dynamic-resource-discovery

Conversation

@blackdragoon26

@blackdragoon26 blackdragoon26 commented Aug 8, 2026

Copy link
Copy Markdown

What type of PR is this?

Bug fix.

What this PR does / why we need it:

This PR fixes dynamic resource discovery in MockLister.SetResource.

Previously, resource names were advertised only before the first mock plugin was created. If the resource set later changed
, for example, when enabling Ascend hami-vnpu-core added a new -core resource, and the existing plugin counts were updated, but the device-plugin manager was not notified about the new resource name. Consequently, no plugin was created for that resource.

This change:

  • tracks the last advertised resource-name set independently of existing plugins;
  • publishes the complete resource list when names are added or removed;
  • preserves in-place updates when only resource counts change;
  • sends discovery updates after releasing the lister mutex;
  • adds regression tests for resource addition, removal, count-only updates, empty sets, and initial zero counts.

Validation completed on macOS:

  • go test ./...
  • go test -race ./internal/pkg/mock
  • go vet ./...
  • git diff --check

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

Special notes for your reviewer:

The change is limited to the generic mock resource-discovery path and its unit tests. It does not require GPU hardware or a Kubernetes cluster to reproduce and verify.

The resource-name list is sorted before comparison and publication, preventing Go map iteration order from causing unnecessary discovery updates.

Does this PR introduce a user-facing change?:

Yes. Runtime additions or removals of mock resource types are now reflected through the device-plugin manager instead of being ignored after startup.

AI Disclosure:
This change was developed with Codex assistance. I reviewed and validated the implementation, tests, repository behavior, and issue reproduction, and I take responsibility for the submitted changes.

Summary by CodeRabbit

  • Bug Fixes

    • Improved resource update handling for more accurate discovery, updates, and removals.
    • Resource lists are now delivered in a consistent order.
    • Prevented premature empty or non-positive updates during initial resource availability.
    • Improved synchronization of plugin counts and resource changes.
  • Tests

    • Added coverage for resource discovery, removal, count updates, empty-resource behavior, and initial update timing.

Signed-off-by: blackdragoon26 <sankalp.jha9643@gmail.com>
@hami-robot

hami-robot Bot commented Aug 8, 2026

Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: blackdragoon26
Once this PR has been reviewed and has the lgtm label, please assign archlitchi 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 commented Aug 8, 2026

Copy link
Copy Markdown
Contributor

Welcome @blackdragoon26! It looks like this is your first PR to Project-HAMi/mock-device-plugin 🎉

@hami-robot hami-robot Bot added the size/L label Aug 8, 2026
@coderabbitai

coderabbitai Bot commented Aug 8, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

MockLister.SetResource now tracks and publishes sorted resource-name changes independently of plugin creation. It updates plugin counts in place, suppresses unchanged or initially non-positive updates, and adds tests for discovery, removal, count changes, and delayed initial publication.

Changes

Dynamic resource publication

Layer / File(s) Summary
Resource state and publication
internal/pkg/mock/lister.go
MockLister stores the last published resource list. SetResource sorts and compares resource names, updates plugin counts, suppresses insignificant updates, and sends changed lists after unlocking.
Resource publication validation
internal/pkg/mock/lister_test.go
Tests cover resource discovery, removal, count-only changes, plugin count updates, initial zero counts, and update-channel assertions.

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

Sequence Diagram(s)

sequenceDiagram
  participant ResourceSource
  participant MockLister
  participant DevicePluginManager
  ResourceSource->>MockLister: SetResource(resource counts)
  MockLister->>MockLister: Update plugin counts and compare names
  MockLister->>DevicePluginManager: Send changed sorted resource list
Loading

Suggested labels: enhancement

Poem

I’m a rabbit with resources neat,
Sorted names hop down the street.
Counts update, new names appear,
Empty starts wait until counts are clear.
The lister signals: “All is bright!”

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Linked Issues check ✅ Passed The changes track resource names, publish sorted complete lists on additions or removals, preserve count-only updates, and add matching regression tests for issue #22.
Out of Scope Changes check ✅ Passed The code and tests remain focused on MockLister resource discovery and the requirements in issue #22.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main change: rediscovering resource name changes in MockLister.
✨ 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 added the enhancement New feature or request label Aug 8, 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 `@internal/pkg/mock/lister.go`:
- Around line 101-110: The resource update path around SetResource must
serialize publication order so concurrent calls cannot send stale lists after
newer ones. Add a dedicated publication mutex or ordered publisher, acquire it
before sending on ResUpdateChan while keeping l.mutex released during the send,
and release it afterward; add a concurrent regression test verifying consumers
receive updates in the same order as the committed l.resources 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: d61db0e4-1f98-4eea-b53c-3310f4bb359a

📥 Commits

Reviewing files that changed from the base of the PR and between d3b1687 and 36db634.

📒 Files selected for processing (2)
  • internal/pkg/mock/lister.go
  • internal/pkg/mock/lister_test.go

Comment thread internal/pkg/mock/lister.go
Signed-off-by: blackdragoon26 <sankalp.jha9643@gmail.com>
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.

Runtime resource-name changes are not advertised after mock plugin startup

1 participant