-
Notifications
You must be signed in to change notification settings - Fork 1
controller/orgunit: reconciler for hold-period expiry and hard-delete (#182) #188
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Prabhjot-Sethi
merged 2 commits into
go-core-stack:main
from
dev-arya23:dev-arya/182-reconciler-hard-delete
Jun 27, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| // Copyright © 2025-2026 Prabhjot Singh Sethi, All Rights reserved | ||
| // Author: Prabhjot Singh Sethi <prabhjot.sethi@gmail.com> | ||
|
|
||
| package orgunit | ||
|
|
||
| import ( | ||
| "context" | ||
| "log" | ||
| "time" | ||
|
|
||
| "github.com/go-core-stack/auth-gateway/pkg/config" | ||
| "github.com/go-core-stack/auth-gateway/pkg/table" | ||
| "github.com/go-core-stack/core/errors" | ||
| "github.com/go-core-stack/core/reconciler" | ||
| ) | ||
|
|
||
| // OrgUnitCleanupController manages the lifecycle of soft-deleted org-units, | ||
| // hard-deleting them after the configured hold period expires. | ||
| type OrgUnitCleanupController struct { | ||
| tbl *table.OrgUnitTable | ||
| holdDuration int | ||
| } | ||
|
|
||
| type orgUnitReconciler struct { | ||
| reconciler.Controller | ||
| ctrl *OrgUnitCleanupController | ||
| } | ||
|
|
||
| func (r *orgUnitReconciler) Reconcile(k any) (*reconciler.Result, error) { | ||
| ctx := context.Background() | ||
| key := k.(*table.OrgUnitKey) | ||
|
|
||
| entry, err := r.ctrl.tbl.Find(ctx, key) | ||
| if err != nil { | ||
| if !errors.IsNotFound(err) { | ||
| // transient error — requeue with backoff | ||
| return &reconciler.Result{RequeueAfter: 5 * time.Second}, nil | ||
| } | ||
| // entry already gone — nothing to do | ||
| return &reconciler.Result{}, nil | ||
| } | ||
|
|
||
| // only process soft-deleted entries | ||
| if entry.Deleted == 0 { | ||
| return &reconciler.Result{}, nil | ||
| } | ||
|
|
||
| holdExpiry := entry.Deleted + int64(r.ctrl.holdDuration) | ||
| now := time.Now().Unix() | ||
|
|
||
| if holdExpiry > now { | ||
| // hold period has not expired — requeue after remaining time (+1s buffer) | ||
| remaining := holdExpiry - now | ||
| return &reconciler.Result{RequeueAfter: time.Duration(remaining+1) * time.Second}, nil | ||
| } | ||
|
|
||
| // hold period expired — hard-delete the entry | ||
| err = r.ctrl.tbl.DeleteKey(ctx, key) | ||
| if err != nil && !errors.IsNotFound(err) { | ||
| // transient error — requeue with backoff | ||
| log.Printf("orgunit reconciler: failed to hard-delete org-unit %s: %s", key.ID, err) | ||
| return &reconciler.Result{RequeueAfter: 5 * time.Second}, nil | ||
| } | ||
|
|
||
| log.Printf("orgunit reconciler: hard-deleted org-unit %s after hold period", key.ID) | ||
| return &reconciler.Result{}, nil | ||
| } | ||
|
|
||
| // NewOrgUnitCleanupController creates and registers the org-unit reconciler. | ||
| // It should only be called when experimental.allow_ou_delete is enabled. | ||
| func NewOrgUnitCleanupController(experimental config.ExperimentalConfig) (*OrgUnitCleanupController, error) { | ||
| tbl, err := table.GetOrgUnitTable() | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| ctrl := &OrgUnitCleanupController{ | ||
| tbl: tbl, | ||
| holdDuration: experimental.HoldDeletedOU, | ||
| } | ||
|
|
||
| r := &orgUnitReconciler{ | ||
| ctrl: ctrl, | ||
| } | ||
|
|
||
| err = tbl.Register("OrgUnitCleanupController", r) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| return ctrl, nil | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.