Skip to content

fix: avoid reconciling ended leases#846

Open
bennyz wants to merge 1 commit into
jumpstarter-dev:mainfrom
bennyz:ended-lease-invalid
Open

fix: avoid reconciling ended leases#846
bennyz wants to merge 1 commit into
jumpstarter-dev:mainfrom
bennyz:ended-lease-invalid

Conversation

@bennyz

@bennyz bennyz commented Jun 28, 2026

Copy link
Copy Markdown
Member

Otherwise this will spam the logs and can check the server if there are invalid leases

@coderabbitai

coderabbitai Bot commented Jun 28, 2026

Copy link
Copy Markdown
Contributor

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
  • ✅ Review completed - (🔄 Check again to review again)

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: e3fad4f8-11d3-448b-aa75-fe6ba2cfa1b2

📥 Commits

Reviewing files that changed from the base of the PR and between 2e06178 and edc795e.

📒 Files selected for processing (2)
  • controller/internal/controller/lease_controller.go
  • controller/internal/controller/lease_controller_test.go
🚧 Files skipped from review as they are similar to previous changes (1)
  • controller/internal/controller/lease_controller_test.go

📝 Walkthrough

Walkthrough

The lease controller now filters out already-ended leases at event admission time and adds tests for ended-label detection, predicate behavior, and label restoration during reconciliation.

Changes

Ended Lease Skip

Layer / File(s) Summary
Event predicate for ended leases
controller/internal/controller/lease_controller.go
Imports event and predicate, adds isLeaseEnded, defines skipEndedPredicate for create/update/generic events, and registers the predicate in SetupWithManager.
Ended lease tests
controller/internal/controller/lease_controller_test.go
Adds unit tests for isLeaseEnded and skipEndedPredicate, plus scheduled-lease scenarios for no-op ended leases and restoring a missing ended label.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

🐇 The ended lease now takes a rest,
Its label check has passed the test.
New events hop by, old ones stay still,
And missing marks get backfilled with skill.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly matches the main change: skipping reconciliation for ended leases.
Description check ✅ Passed The description is directly related to the change by explaining the log spam and server-check impact of invalid leases.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
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

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 golangci-lint (2.12.2)

level=error msg="[linters_context] typechecking error: pattern ./...: directory prefix . does not contain main module or its selected dependencies"


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
Contributor

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 `@controller/internal/controller/lease_controller.go`:
- Around line 82-84: The early return in reconcile for lease status ended is
preventing the ended-label backfill from ever retrying when metadata update
fails. In the LeaseReconciler flow, move the `lease.Status.Ended` short-circuit
in `Reconcile` so it runs only after the ended label sync path completes, or
gate it on `isLeaseEnded(&lease)` being true as well. Make sure the label write
still happens for leases whose status is ended but that do not yet have
`jumpstarter.dev/lease-ended`, so downstream `MatchingActiveLeases` no longer
treats them as active.
🪄 Autofix (Beta)

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

Run ID: f80c2bd0-dbc5-4c2d-bc06-2fc80a4f30e2

📥 Commits

Reviewing files that changed from the base of the PR and between 26aee22 and c0a5e67.

📒 Files selected for processing (1)
  • controller/internal/controller/lease_controller.go

Comment thread controller/internal/controller/lease_controller.go Outdated
@bennyz bennyz force-pushed the ended-lease-invalid branch 2 times, most recently from 62c3ae1 to 35002c5 Compare June 28, 2026 10:21

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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

1712-1769: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

These specs don’t actually prove the new skip behavior.

Both cases call reconcileLease() directly, which bypasses SetupWithManager entirely, so the new skipEnded predicate is never exercised. Also, the RequeueAfter == 0 assertion does not distinguish the early-return path from any normal reconcile that already converges on an ended lease. Please make one test observe a skipped operation (for example via a spy/fake client) or add a focused predicate-level test.

🤖 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 `@controller/internal/controller/lease_controller_test.go` around lines 1712 -
1769, The current lease tests in lease_controller_test.go do not exercise the
new skipEnded behavior because they call reconcileLease directly instead of
going through the manager/predicate path. Update these specs to verify the skip
path itself, either by adding a focused test for skipEnded in SetupWithManager
or by using a spy/fake client to observe that reconciliation is skipped. Keep
the existing end-of-lease cases tied to reconcileLease only for state changes,
and add an assertion that specifically proves the predicate prevents a reconcile
from running rather than relying on RequeueAfter.
🤖 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 `@controller/internal/controller/lease_controller_test.go`:
- Around line 1712-1769: The current lease tests in lease_controller_test.go do
not exercise the new skipEnded behavior because they call reconcileLease
directly instead of going through the manager/predicate path. Update these specs
to verify the skip path itself, either by adding a focused test for skipEnded in
SetupWithManager or by using a spy/fake client to observe that reconciliation is
skipped. Keep the existing end-of-lease cases tied to reconcileLease only for
state changes, and add an assertion that specifically proves the predicate
prevents a reconcile from running rather than relying on RequeueAfter.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: ccd2b42a-ee83-466b-b2a1-7f66ddc46c26

📥 Commits

Reviewing files that changed from the base of the PR and between 62c3ae1 and 35002c5.

📒 Files selected for processing (2)
  • controller/internal/controller/lease_controller.go
  • controller/internal/controller/lease_controller_test.go
🚧 Files skipped from review as they are similar to previous changes (1)
  • controller/internal/controller/lease_controller.go

@bennyz bennyz force-pushed the ended-lease-invalid branch from 35002c5 to 2e06178 Compare June 28, 2026 10:32

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

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)
controller/internal/controller/lease_controller_test.go (1)

1808-1831: 🎯 Functional Correctness | 🔵 Trivial | ⚡ Quick win

Strengthen the short-circuit assertion.

RequeueAfter == 0 doesn't prove the second reconcile skipped the expensive path; a full reconcile can still return zero. Since this PR is specifically about avoiding work for ended leases, it would be better to assert that the second reconcile performs no writes (for example, unchanged resourceVersion/generation) or use a client spy to verify Status().Update/Update are not reached.

🤖 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 `@controller/internal/controller/lease_controller_test.go` around lines 1808 -
1831, The current short-circuit check in reconcileLease only asserts
RequeueAfter is zero, which does not prove the ended-lease path skipped
reconciliation work. Strengthen the test by verifying no writes happen on the
second call to reconcileLease, such as asserting the lease’s
resourceVersion/generation stays unchanged after the second reconcile or by
using a spy/mock to confirm Update and Status().Update are not invoked; use the
existing reconcileLease, getLease, and k8sClient setup to locate and adjust the
test.
🤖 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 `@controller/internal/controller/lease_controller_test.go`:
- Around line 1059-1124: The skipEnded test is duplicating the production
predicate instead of validating the real filter used by SetupWithManager, so it
can diverge unnoticed. Extract the predicate logic into a shared helper (or
reuse the actual predicate from SetupWithManager) and update the tests to call
that shared symbol rather than rebuilding predicate.Funcs locally. Also replace
the current “transition into ended” case with the important unlabeled-ended
update path, using a Lease whose Status.Ended is true but whose ended label is
still absent, so the filter behavior matches the reconciler backfill flow.

---

Nitpick comments:
In `@controller/internal/controller/lease_controller_test.go`:
- Around line 1808-1831: The current short-circuit check in reconcileLease only
asserts RequeueAfter is zero, which does not prove the ended-lease path skipped
reconciliation work. Strengthen the test by verifying no writes happen on the
second call to reconcileLease, such as asserting the lease’s
resourceVersion/generation stays unchanged after the second reconcile or by
using a spy/mock to confirm Update and Status().Update are not invoked; use the
existing reconcileLease, getLease, and k8sClient setup to locate and adjust the
test.
🪄 Autofix (Beta)

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

Run ID: dd51b178-e689-4c9d-b006-7780a39df465

📥 Commits

Reviewing files that changed from the base of the PR and between 35002c5 and 2e06178.

📒 Files selected for processing (2)
  • controller/internal/controller/lease_controller.go
  • controller/internal/controller/lease_controller_test.go
🚧 Files skipped from review as they are similar to previous changes (1)
  • controller/internal/controller/lease_controller.go

Comment thread controller/internal/controller/lease_controller_test.go Outdated
@bennyz bennyz force-pushed the ended-lease-invalid branch from 2e06178 to c73f1c9 Compare June 28, 2026 10:44
@bennyz bennyz requested review from bkhizgiy and evakhoni June 29, 2026 07:11
Comment thread controller/internal/controller/lease_controller.go Outdated
Comment thread controller/internal/controller/lease_controller.go
Signed-off-by: Benny Zlotnik <bzlotnik@redhat.com>
Assited-by: claude-opus-4.6
@bennyz bennyz force-pushed the ended-lease-invalid branch from c73f1c9 to edc795e Compare June 29, 2026 11:05

@bkhizgiy bkhizgiy 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.

looks good from my side

@evakhoni

Copy link
Copy Markdown
Member

looks good from my side as well.

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.

4 participants