Skip to content

feat: add CAPI cleanup finalizer to NamespaceReservation#572

Merged
bsquizz merged 3 commits intomainfrom
feat/capi-cleanup-finalizer
Apr 23, 2026
Merged

feat: add CAPI cleanup finalizer to NamespaceReservation#572
bsquizz merged 3 commits intomainfrom
feat/capi-cleanup-finalizer

Conversation

@bsquizz
Copy link
Copy Markdown
Contributor

@bsquizz bsquizz commented Apr 22, 2026

Summary

  • Registers the cluster-api v1beta1 scheme so the controller can list and delete Cluster resources
  • Adds helpers.DeleteCAPIResources helper that deletes all Cluster objects in a namespace and reports whether any remain
  • Adds a capi-cleanup.cloud.redhat.com finalizer to NamespaceReservation once a namespace is assigned
  • On deletion, handleCAPICleanup blocks removal and requeues every 10s until all Cluster resources are fully gone, then releases the finalizer

Why: CAPI controllers need rosa-creds-secret to run their own finalizers when a Cluster is deleted. If the namespace is GC'd before those finalizers complete, the secret disappears and cleanup stalls/fails. This finalizer ensures CAPI gets a clean window to finish before the namespace is released.

Test plan

  • Create a reservation in a pool, deploy a CAPI Cluster into the reserved namespace
  • Delete the reservation and verify it stays in Terminating until the Cluster resource is fully removed
  • Verify reservations with no CAPI resources delete immediately without being held by the finalizer
  • Verify reservations with no assigned namespace (empty Status.Namespace) skip cleanup and delete cleanly
  • Run make pre-push to confirm formatting, vet, and tests pass

🤖 Generated with Claude Code

bsquizz and others added 3 commits April 22, 2026 17:08
Gate namespace GC on CAPI Cluster resource deletion so that CAPI
controllers have time to run their own finalizers (which require
rosa-creds-secret) before the namespace disappears.

- Register cluster-api v1beta1 scheme in run.go
- Add helpers.DeleteCAPIResources to list/delete Cluster objects
- Add capiCleanupFinalizer to reservations once a namespace is assigned
- handleCAPICleanup blocks deletion and requeues every 10s until all
  Cluster resources are gone, then removes the finalizer

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
- Handle missing CAPI CRD: treat IsNoMatchError from List as no
  resources remaining, avoiding error-loops on clusters without CAPI
- Fix finalizer ordering: add finalizer after Status().Update so
  Status.Namespace is guaranteed to be set whenever the finalizer is
  present; also add guard in the active case to catch any missed window
- Add 1-hour safety timeout in handleCAPICleanup to unblock deletion
  if a CAPI Cluster finalizer is permanently stuck
- Move const/timeout declarations above RBAC markers for clarity
- Add unit tests for DeleteCAPIResources covering: no clusters, clusters
  present, already-terminating clusters, and cross-namespace isolation

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Without clusterv1 in the manager scheme, the controller-runtime client
can't encode ClusterList, so DeleteCAPIResources errors before reaching
the API server and bypasses the IsNoMatchError guard. This caused the
expired-reservation test to time out with the finalizer stuck on.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@bsquizz bsquizz merged commit 93d8faf into main Apr 23, 2026
5 of 6 checks passed
bsquizz added a commit that referenced this pull request Apr 24, 2026
)

* fix(rbac): add CAPI cluster RBAC permissions for cleanup finalizer

Add delete/get/list/watch permissions on cluster.x-k8s.io/clusters to
support the CAPI cleanup finalizer added in #572.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* fix(test): re-fetch pool inside Eventually before retrying Update

The update loops were retrying with a stale pool object, causing 409
conflict errors on every retry until timeout because the controller
continuously bumps the ResourceVersion via status updates.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

---------

Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
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.

1 participant