Skip to content

EvictionController: Add bounds checks for instance slice operations#283

Merged
fwiesel merged 1 commit intomainfrom
fix/eviction-controller-bounds-check
Apr 9, 2026
Merged

EvictionController: Add bounds checks for instance slice operations#283
fwiesel merged 1 commit intomainfrom
fix/eviction-controller-bounds-check

Conversation

@notandy
Copy link
Copy Markdown
Contributor

@notandy notandy commented Apr 8, 2026

Extract slice operations into helper functions (peekInstance, popInstance,
moveToBack) with proper empty slice handling to prevent potential panics
when accessing OutstandingInstances. Add unit tests for the helpers.

Summary by CodeRabbit

  • Refactor

    • Improved robustness of the eviction system by adding helper functions and better edge-case handling for instance list operations.
  • Tests

    • Added comprehensive test coverage for eviction helper functions to ensure correct behavior across edge cases.

Extract slice operations into helper functions (peekInstance, popInstance,
moveToBack) with proper empty slice handling to prevent potential panics
when accessing OutstandingInstances. Add unit tests for the helpers.
@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Apr 8, 2026

📝 Walkthrough

Walkthrough

The pull request introduces three helper functions (popInstance, peekInstance, moveToBack) to encapsulate slice operations on eviction.Status.OutstandingInstances. Control flow in handleNotFound and evictNext is refactored to use these helpers instead of inline slice manipulation, with early returns guarding against empty instance lists.

Changes

Cohort / File(s) Summary
Eviction Controller Implementation
internal/controller/eviction_controller.go
Added three helper functions to manage instance slice operations; refactored handleNotFound and evictNext to use peekInstance for non-mutating selection, popInstance for removal, and moveToBack for reordering; added early returns for empty slices; updated status update logic to short-circuit with nil when instance list is empty.
Eviction Controller Tests
internal/controller/eviction_controller_test.go
Added comprehensive test suite for peekInstance, popInstance, and moveToBack helper functions, including tests for nil vs empty slices, single/multi-element slices, immutability verification, and correct element removal and return values.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐰 Three helpers hop to organize the way,
We peek and pop instances throughout the day,
Moving slices back with careful grace,
Each function finds its proper place! 🎯

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: adding bounds checks for instance slice operations through helper functions, which is the primary focus of the changes in eviction_controller.go.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/eviction-controller-bounds-check

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 and usage tips.

@github-actions
Copy link
Copy Markdown

github-actions bot commented Apr 8, 2026

Merging this branch will increase overall coverage

Impacted Packages Coverage Δ 🤖
github.com/cobaltcore-dev/openstack-hypervisor-operator/internal/controller 63.73% (+0.47%) 👍

Coverage by file

Changed files (no unit tests)

Changed File Coverage Δ Total Covered Missed 🤖
github.com/cobaltcore-dev/openstack-hypervisor-operator/internal/controller/eviction_controller.go 35.90% (+5.34%) 156 (+12) 56 (+12) 100 👍

Please note that the "Total", "Covered", and "Missed" counts above refer to code statements instead of lines of code. The value in brackets refers to the test coverage of that file in the old version of the code.

Changed unit test files

  • github.com/cobaltcore-dev/openstack-hypervisor-operator/internal/controller/eviction_controller_test.go

Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
internal/controller/eviction_controller.go (1)

56-84: Clarify that OutstandingInstances is processed from the tail.

These helpers all treat the last element as “next”, and moveToBack then rotates that element to index 0. A short comment here—or on the OutstandingInstances field—would make that ordering contract much harder to misuse in future call sites.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@internal/controller/eviction_controller.go` around lines 56 - 84, The helper
functions popInstance, peekInstance, and moveToBack treat the slice's last
element as the "next" instance (tail-first processing) and moveToBack rotates
that tail element to index 0 to deprioritize it; add a concise comment on the
OutstandingInstances field (and/or above these functions) stating this ordering
contract (i.e., "OutstandingInstances is processed from the tail: last element
is next; moveToBack moves that element to the front to deprioritize") so future
callers won't misuse the slice ordering.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@internal/controller/eviction_controller.go`:
- Around line 56-84: The helper functions popInstance, peekInstance, and
moveToBack treat the slice's last element as the "next" instance (tail-first
processing) and moveToBack rotates that tail element to index 0 to deprioritize
it; add a concise comment on the OutstandingInstances field (and/or above these
functions) stating this ordering contract (i.e., "OutstandingInstances is
processed from the tail: last element is next; moveToBack moves that element to
the front to deprioritize") so future callers won't misuse the slice ordering.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: ff7b853e-b5eb-42c2-90ba-ef9e09aaf82d

📥 Commits

Reviewing files that changed from the base of the PR and between 499dcef and f43a424.

📒 Files selected for processing (2)
  • internal/controller/eviction_controller.go
  • internal/controller/eviction_controller_test.go

@fwiesel fwiesel merged commit 865b456 into main Apr 9, 2026
7 checks passed
@fwiesel fwiesel deleted the fix/eviction-controller-bounds-check branch April 9, 2026 07:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants