Skip to content

feat: refactor global tenantresource reconcile speed - #2311

Merged
oliverbaehler merged 71 commits into
projectcapsule:mainfrom
oliverbaehler:feat/refactor-gtr
Nov 23, 2026
Merged

oliverbaehler merged 71 commits into
projectcapsule:mainfrom
oliverbaehler:feat/refactor-gtr

Conversation

@oliverbaehler

@oliverbaehler oliverbaehler commented Nov 21, 2026 •

Copy link
Copy Markdown
Member
Measurement Before Now
Policy visible, without health checks — median / p95 9.95 / 21.25 10.31 / 20.91
Policy visible, with health checks — median / p95 26.47 / 33.77 9.27 / 19.27
Post-burst unchanged resync, without health checks 18.90 1.22
Post-burst unchanged resync, with health checks 25.70 0.81

So:

  • Without health checks: initial policy delivery was roughly unchanged.
  • With health checks: median delivery was 2.9× faster.
  • Post-burst resyncs: 15.5–31.9× faster, with zero target PATCHes in the current implementation.

Signed-off-by: Oliver Baehler <oliver@sudo-i.net>
Signed-off-by: Oliver Baehler <oliver@sudo-i.net>
Signed-off-by: Oliver Baehler <oliver@sudo-i.net>
…paths

Signed-off-by: Oliver Baehler <oliver@sudo-i.net>
…paths

Signed-off-by: Oliver Baehler <oliver@sudo-i.net>
…paths

Signed-off-by: Oliver Baehler <oliver@sudo-i.net>
…paths

Signed-off-by: Oliver Baehler <oliver@sudo-i.net>
…paths

Signed-off-by: Oliver Baehler <oliver@sudo-i.net>
…paths

Signed-off-by: Oliver Baehler <oliver@sudo-i.net>
…paths

Signed-off-by: Oliver Baehler <oliver@sudo-i.net>
Signed-off-by: Oliver Baehler <oliver@sudo-i.net>
Signed-off-by: Oliver Baehler <oliver@sudo-i.net>
Signed-off-by: Oliver Baehler <oliver@sudo-i.net>
Signed-off-by: Oliver Baehler <oliver@sudo-i.net>
Signed-off-by: Oliver Baehler <oliver@sudo-i.net>
Signed-off-by: Oliver Baehler <oliver@sudo-i.net>
Signed-off-by: Oliver Baehler <oliver@sudo-i.net>
Signed-off-by: Oliver Baehler <oliver@sudo-i.net>
Signed-off-by: Oliver Baehler <oliver@sudo-i.net>
Copilot AI balanced review requested due to automatic review settings November 21, 2026 01:30

Copilot AI 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.

🟡 Changes recommended

A critical reconciliation race can remove the finalizer despite newly published cleanup records, leaving targets unprotected, and namespace profile label changes can delay bootstrap until periodic resync.

2 open issues
What changed in this PR

This PR aims to speed up TenantResource and GlobalTenantResource replication, particularly when new namespaces join a tenant, while retaining full reconciliation for dynamic sources and health checks.

Changes:

  • Adds tenant-partitioned namespace bootstrap scheduling, indexed source lookups, and a separate bootstrap API budget.
  • Caches parsed generators, selectors, and verified apply results to reduce repeated work.
  • Coordinates concurrent inventory publication and adds unit tests, benchmarks, and tenant-aware e2e scenarios.

Caches have bounded retention, and target reads and ownership checks remain part of replication. Performance gains and real-cluster behavior remain unverified here; tests and benchmarks were not run.

File Description
pkg/​template/​unstructured.go Caches parsed generator templates.
pkg/​template/​generator_cache_test.go Tests and benchmarks generator caching.
pkg/​runtime/​ssa/​manager.go Integrates verified apply-cache shortcuts.
pkg/​runtime/​ssa/​condition.go Preserves replication field-manager tracking.
pkg/​runtime/​ssa/​apply_cache.go Defines apply-cache identity and freshness checks.
pkg/​runtime/​ssa/​apply_cache_test.go Tests cache invalidation, drift repair, and apply costs.
pkg/​runtime/​cache/​impersonation_identity_test.go Tests credential and account identity separation.
pkg/​runtime/​cache/​impersonation_clients.go Shares credential identity across API budgets.
pkg/​api/​processor/​processor.go Adds the processor's apply-cache dependency.
pkg/​api/​processor/​processor_func.go Passes the cache to the apply manager.
pkg/​api/​processor/​metadata_policy_test.go Updates lifecycle-write failure fixtures.
pkg/​api/​processor/​health_test.go Preserves lifecycle-conflict test coverage.
pkg/​api/​processor/​conflict_test.go Updates owner-reference conflict fixtures.
internal/​controllers/​resources/​trigger_schedule.go Supports prompt retries for pending apply work.
internal/​controllers/​resources/​trigger_schedule_test.go Checks retries with long periodic resyncs.
internal/​controllers/​resources/​source_index.go Reuses the informer namespace index.
internal/​controllers/​resources/​source_index_test.go Tests lookup isolation, lifecycle, and scaling.
internal/​controllers/​resources/​scoped_replication.go Merges concurrent inventory and manages finalizers.
internal/​controllers/​resources/​publication_test.go Adapts status-publication tests.
internal/​controllers/​resources/​namespaced.go Integrates indexed lookups, caching, and inventory checkpoints.
internal/​controllers/​resources/​namespace_trigger.go Implements scoped bootstrap execution and fallback.
internal/​controllers/​resources/​namespace_events.go Routes simple namespace additions to bootstrap.
internal/​controllers/​resources/​namespace_events_test.go Updates event-dispatch expectations.
internal/​controllers/​resources/​namespace_bootstrap.go Adds tenant-partitioned queues and dependency gates.
internal/​controllers/​resources/​namespace_bootstrap_test.go Tests bootstrap scheduling, validation, and dependencies.
internal/​controllers/​resources/​manager.go Wires shared caches and bootstrap dependencies.
internal/​controllers/​resources/​inventory_checkpoint_test.go Tests concurrent inventory preservation.
internal/​controllers/​resources/​impersonation.go Exposes apply-cache identities through clients.
internal/​controllers/​resources/​global.go Integrates selective scheduling and inventory checkpoints.
internal/​controllers/​resources/​controller_bench_test.go Updates replication benchmarks for cached execution.
internal/​controllers/​resources/​collect.go Reuses compiled namespace selectors.
internal/​controllers/​resources/​api_budget.go Reserves bootstrap API capacity.
internal/​controllers/​resources/​api_budget_test.go Tests API-budget separation and defaults.
internal/​controllers/​cfg/​invalidator/​replications_test.go Tests additional client-cache invalidation.
internal/​controllers/​cfg/​invalidator/​manager.go Registers additional impersonation caches.
internal/​controllers/​cfg/​invalidator/​clients.go Invalidates bootstrap clients alongside primary clients.
internal/​cache/​impersonation_clients.go Exposes the shared identity helper.
internal/​cache/​applied_resources.go Adds a bounded, expiring apply-result cache.
internal/​cache/​applied_resources_test.go Tests cache isolation, expiry, and concurrency.
e2e/​replications_conflict_test.go Updates real-API lifecycle-conflict fixtures.
e2e/​replications_bootstrap_test.go Covers bootstrap profiles, isolation, dependencies, and drift.
cmd/​controller/​replications/​controller.go Wires separate bootstrap clients and API budgets.

Review effort: Balanced

Comment on lines +122 to +124
if !controllerutil.ContainsFinalizer(latest, meta.ControllerFinalizer) {
original := append([]string(nil), latest.GetFinalizers()...)
controllerutil.AddFinalizer(latest, meta.ControllerFinalizer)
Signed-off-by: Oliver Baehler <oliver@sudo-i.net>
Copilot AI balanced review requested due to automatic review settings November 21, 2026 02:26

Copilot AI 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.

🟡 Changes recommended

A lagging informer snapshot can cause one tenant’s replication manifests to be written into another tenant’s namespace because the target’s current ownership is not validated.

2 open issues
2 previously missed issues

In code that hasn't changed since last review

Medium severity Keep Ready false while merged inventory needs reconciliation

internal/​controllers/​resources/​global.go:668

If a scoped apply publishes failed or partial inventory after the full snapshot, needsReconcile is set but a source without health checks can still publish Ready=True, allowing dependents to proceed before the follow-up succeeds. Keep Ready non-true while the merged inventory needs reconciliation, regardless of health-check configuration. Apply the same change in internal/controllers/resources/namespaced.go:674–676.

Medium severity Reconcile source when namespace changes deselect resources

internal/​controllers/​resources/​namespace_bootstrap.go:151

When a namespace label change deselects resources from a non-health-enabled GlobalTenantResource, scoped bootstrap does not prune them, so replicas can remain until the periodic resync. Queue an authoritative source reconcile when a profile change removes matches, including when only some resource blocks stop matching. Add a select→deselect regression test without a manual reconcile annotation.

1 resolved since last review

Review effort: Balanced

Signed-off-by: Oliver Baehler <oliver@sudo-i.net>
Copilot AI balanced review requested due to automatic review settings November 21, 2026 06:15

Copilot AI 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.

🔵 Needs a closer look

Unresolved reconciliation defects can starve namespaces, retain tenant-wide fan-out, leave stale generated replicas, and delay recovery from transient errors until a 24-hour resync.

1 open issue
4 previously missed issues

In code that hasn't changed since last review

Medium severity Renewed work can starve later namespaces

internal/​controllers/​resources/​namespace_bootstrap.go:118

If a namespace receives another label or ownership notification during each successful reconciliation, its updated sequence keeps it at the front of the shard and later namespaces can starve. Move renewed work to the tail while preserving its notification, so the next namespace gets a turn. Update the fairness test to assert this rotation rather than immediate reselection.

Medium severity Profile changes can leave stale generated replicas

internal/​controllers/​resources/​namespace_events.go:87

When a namespace label changes a generator's output name but the resource block remains selected, bootstrap creates the new replica without pruning the old one, potentially leaving both until a 24-hour resync. Schedule full gather/prune for namespace-dependent dynamic sources on profile changes, not just selector deselections. Include dynamic sources with nil namespace selectors in the candidate index and add a generated-name regression test.

Medium severity Tenant watch still fans out raw-only sources

internal/​controllers/​resources/​namespace_events.go:149

Filtering Namespace CREATE events does not prevent tenant-wide work: the Tenant watch in namespaced.go:122-125 still lists every tenant namespace and queues every TenantResource when namespace membership changes. Raw-only sources therefore still perform a full gather alongside bootstrap, retaining the fan-out this change aims to avoid. Apply additions-only filtering to that Tenant watch and reuse the source index, while preserving full reconciliation for removals and dynamic or health-enabled sources.

Medium severity Pre-apply client errors can delay retries for 24 hours

internal/​controllers/​resources/​trigger_schedule.go:136

After bootstrap hands a failed source to the full controller, a transient client-loading error occurs before checkpoint.start, so an apply-only source can still wait for its 24-hour resync after the manual request is cleared. Retry these pre-apply errors promptly as well; the validation-error check below already excludes permanent validation failures. This affects both callers in namespaced.go:174 and global.go:175.

1 resolved since last review

Review effort: Balanced

@oliverbaehler
oliverbaehler merged commit 6dfc67e into projectcapsule:main Nov 23, 2026
27 of 29 checks passed
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