Skip to content

Clean up organization memberships when users are deleted#544

Merged
scotwells merged 1 commit intomainfrom
fix/issue-536-user-policybinding-gc
Apr 2, 2026
Merged

Clean up organization memberships when users are deleted#544
scotwells merged 1 commit intomainfrom
fix/issue-536-user-policybinding-gc

Conversation

@scotwells
Copy link
Copy Markdown
Contributor

@scotwells scotwells commented Mar 27, 2026

Summary

  • Adds a finalizer to the UserController that deletes all OrganizationMemberships referencing a user before allowing the User object to be removed
  • Extends the OrganizationMembership validation webhook to allow last-owner membership deletion when the referenced User is being deleted (verified via direct API server read, not cache)
  • Fixes the Organization webhook and UserInvitation controller to set correct User ownerReferences on OrganizationMemberships
  • Adds a two-pass self-delete in the OrganizationMembership controller for existing orphaned memberships where the User no longer exists

Context

When a User was deleted, OrganizationMembership resources referencing that user were not cleaned up because they had no User ownerReference (webhook creation path) or had a malformed one (invitation path used .Group instead of .String() for APIVersion). The orphaned memberships left their owned PolicyBindings stuck in SubjectValidationFailed state.

Closes #536

Test plan

  • Webhook tests verify deletion is allowed when User has DeletionTimestamp
  • Webhook tests verify deletion is allowed when User is already gone
  • Webhook tests verify last-owner guard still blocks when User is active
  • All existing unit tests pass
  • Chainsaw e2e test validates full lifecycle: create user + org → verify membership → delete user → verify cleanup
  • e2e test passes in CI

Note

The CI workflow reports a failure due to pre-existing e2e test issues on main (note-multicluster-subject, clusternote-multicluster-subject, crm-note-contact-lifecycle). These are unrelated to this PR and are being resolved in #549.

🤖 Generated with Claude Code

@joggrbot
Copy link
Copy Markdown
Contributor

joggrbot bot commented Mar 27, 2026

📝 Documentation Analysis

All docs are up to date! 🎉


✅ Latest commit analyzed: 485ee94 | Powered by Joggr

Add a finalizer to the UserController that deletes all
OrganizationMemberships referencing a user before allowing the User
object to be removed. This prevents PolicyBindings owned by those
memberships from becoming permanently orphaned.

Also fixes:
- OrganizationMembership validation webhook now allows last-owner
  membership deletion when the referenced User is being deleted
- Organization webhook sets User ownerReference on memberships
- UserInvitation controller fixes malformed ownerReference APIVersion
- OrganizationMembership controller self-deletes after two-pass
  confirmation when the referenced User no longer exists

Closes #536

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@scotwells scotwells force-pushed the fix/issue-536-user-policybinding-gc branch from 1f29aaa to 485ee94 Compare March 27, 2026 15:39
@scotwells scotwells merged commit 33ceab3 into main Apr 2, 2026
7 of 13 checks passed
@scotwells scotwells deleted the fix/issue-536-user-policybinding-gc branch April 2, 2026 18:58
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.

Orphaned PolicyBindings for deleted Users not garbage collected

3 participants