Skip to content

feat: refactor controller components - #2303

Merged
oliverbaehler merged 73 commits into
projectcapsule:mainfrom
oliverbaehler:refactor/replications
Nov 20, 2026
Merged

oliverbaehler merged 73 commits into
projectcapsule:mainfrom
oliverbaehler:refactor/replications

Conversation

@oliverbaehler

Copy link
Copy Markdown
Member

Refactors deployment into two dedicated deployments (one for replications and one core (admission). Upgrade should be without large interruptions.

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 19, 2026 12:48

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 generation change during apply can discard a newly created target’s inventory entry, preventing later pruning and leaving orphaned resources.

3 open issues
1 file not reviewed
  • api/v1beta2/zz_generated.deepcopy.go: Generated file
What changed in this PR

Separates Capsule’s core admission and namespace-profile controllers from replication workers, allowing independent scaling and failover. It also adds optional health checks that gate replication readiness.

Changes:

  • Introduces dedicated controller commands, shared startup configuration, and separate replication deployment and monitoring resources.
  • Adds health-check APIs, bounded observation batches, cached CEL expressions, durable progress, and health metrics.
  • Extends unit tests, benchmarks, outage/failover scenarios, and end-to-end CI triggers.

The design separates worker capacity from admission and retains impersonated target access. Validation was limited to source inspection; tests, benchmarks, and cluster upgrade checks were not run.

File Description
security/​threats.yaml Updates the RBAC evidence path.
security/​capabilities.yaml Updates the RBAC implementation reference.
pkg/​runtime/​ssa/​manager.go Reports applied target generation and content revision.
pkg/​runtime/​indexers/​tenantresource/​health.go Adds health-enabled replication indexes.
pkg/​runtime/​indexers/​tenantresource/​health_test.go Tests and benchmarks health indexes.
pkg/​runtime/​indexers/​indexer.go Separates replication-specific index registration.
pkg/​runtime/​indexers/​indexer_test.go Verifies controller-specific index sets.
pkg/​runtime/​cel/​health_condition.go Compiles object-state health expressions.
pkg/​runtime/​cel/​expression.go Adds bounded boolean health evaluation.
pkg/​api/​processor/​processor.go Adds opt-in health tracking.
pkg/​api/​processor/​processor_func.go Captures target health references across apply retries.
pkg/​api/​processor/​health_test.go Tests health tracking and retry preservation.
pkg/​api/​meta/​zz_generated.deepcopy.go Copies health references in generated helpers.
pkg/​api/​meta/​reference.go Defines applied-target health identity.
pkg/​api/​meta/​conditions.go Adds the Healthy condition type.
Makefile Updates controller commands and replication deployment settings.
internal/​webhook/​rules/​generic/​mutation/​workload_ephemeral_test.go Points the test at the relocated template.
internal/​metrics/​tenantresource_recorder.go Exports namespaced replication health metrics.
internal/​metrics/​rulestatus_recorder.go Removes a lint-suppression comment.
internal/​metrics/​replication_conditions_test.go Tests and benchmarks condition metrics and cleanup.
internal/​metrics/​global_tenantresource_recorder.go Exports global replication health metrics.
internal/​controllers/​resources/​trigger_schedule.go Extends source freshness and retry handling.
internal/​controllers/​resources/​trigger_schedule_test.go Updates retry-helper regression tests.
internal/​controllers/​resources/​scoped_replication.go Enables health tracking for eligible replications.
internal/​controllers/​resources/​namespaced.go Integrates namespaced health readiness and scheduling.
internal/​controllers/​resources/​namespace_trigger.go Routes health-enabled work through full reconciliation.
internal/​controllers/​resources/​manager.go Registers the observer and shared metric recorders.
internal/​controllers/​resources/​health.go Implements health evaluation, revisions, and readiness gating.
internal/​controllers/​resources/​health_metadata.go Separates metadata patches from health status writes.
internal/​controllers/​resources/​health_controller.go Adds batched health observation and status publication.
internal/​controllers/​resources/​global.go Integrates global health readiness and scheduling.
internal/​controllers/​cfg/​invalidator/​replications_test.go Tests and benchmarks replication-only invalidation.
internal/​controllers/​cfg/​invalidator/​manager.go Supports replication-specific cache invalidation.
internal/​controllers/​cfg/​invalidator/​clients.go Skips absent impersonation caches.
internal/​cache/​health_conditions.go Reuses bounded caching for health expressions.
internal/​cache/​health_conditions_test.go Tests cache isolation, concurrency, limits, and performance.
internal/​cache/​cel.go Dispatches health-expression compilation.
go.mod Makes the health-evaluation library a direct dependency.
e2e/​replications_watches_failover_test.go Targets dedicated replication pods and election lease.
e2e/​replications_controller_test.go Covers replication outages, isolation, and recovery.
cmd/​controller/​replications/​controller.go Starts the dedicated replication manager.
cmd/​controller/​main_test.go Tests subcommands, flags, help, and errors.
cmd/​controller/​core/​tracing.go Relocates tracing and extracts shared flag handling.
cmd/​controller/​common/​scheme.go Centralizes Kubernetes and Capsule scheme registration.
cmd/​controller/​common/​scheme_test.go Retains resource-decoding coverage for the shared scheme.
cmd/​controller/​common/​options.go Centralizes controller flags and options.
cmd/​controller/​common/​bootstrap.go Shares client, manager, TLS, and probe setup.
cmd/​controller/​common/​bootstrap_test.go Tests shared startup options and cached reads.
charts/​capsule/​values.yaml Adds replication settings and health alerts.
charts/​capsule/​values.schema.json Defines replication configuration value types.
charts/​capsule/​templates/​controllers/​serviceaccount.yaml Relocates the shared controller ServiceAccount template.
charts/​capsule/​templates/​controllers/​replications/​servicemonitor.yaml Renders replication metrics monitoring.
charts/​capsule/​templates/​controllers/​replications/​poddisruptionbudget.yaml Adds an independent replication disruption budget.
charts/​capsule/​templates/​controllers/​replications/​metrics-service.yaml Exposes replication metrics separately.
charts/​capsule/​templates/​controllers/​replications/​deployment.yaml Creates the dedicated replication deployment.
charts/​capsule/​templates/​controllers/​replications/​_helpers.tpl Builds isolated replication rendering values and selectors.
charts/​capsule/​templates/​controllers/​rbac.yaml Relocates shared controller permissions.
charts/​capsule/​templates/​controllers/​rbac-tenants.yaml Relocates tenant-facing resource permissions.
charts/​capsule/​templates/​controllers/​core/​webhook-service.yaml Relocates the core admission service.
charts/​capsule/​templates/​controllers/​core/​servicemonitor.yaml Uses the shared monitoring template.
charts/​capsule/​templates/​controllers/​core/​prioritylevelconfiguration.yaml Relocates API priority configuration.
charts/​capsule/​templates/​controllers/​core/​poddisruptionbudget.yaml Relocates the core disruption budget.
charts/​capsule/​templates/​controllers/​core/​metrics-service.yaml Relocates core metrics and health service.
charts/​capsule/​templates/​controllers/​core/​flowschema.yaml Relocates API request flow classification.
charts/​capsule/​templates/​controllers/​core/​deployment.yaml Relocates the core deployment template.
charts/​capsule/​templates/​controllers/​core/​daemonset.yaml Retains the core DaemonSet option.
charts/​capsule/​templates/​controllers/​core/​certs.yaml Relocates the webhook certificate secret template.
charts/​capsule/​templates/​controllers/​core/​certificate.yaml Relocates cert-manager webhook certificate resources.
charts/​capsule/​templates/​controllers/​_pod.tpl Selects controller commands and core-only webhook settings.
charts/​capsule/​templates/​controllers/​_monitoring.tpl Extracts a reusable ServiceMonitor template.
charts/​capsule/​crds/​capsule.clastix.io_tenantresources.yaml Adds namespaced health configuration and status schemas.
charts/​capsule/​crds/​capsule.clastix.io_globaltenantresources.yaml Adds global health configuration and status schemas.
api/​v1beta2/​zz_generated.deepcopy.go Generates deep-copy support for health API fields.
api/​v1beta2/​tenantresource_types.go Adds optional health specification and status fields.
api/​v1beta2/​tenantresource_health.go Defines health expressions, timeouts, and progress checkpoints.
api/​v1beta2/​tenantresource_health_test.go Tests opt-in semantics, serialization, and copy isolation.
api/​v1beta2/​tenantresource_health_bench_test.go Benchmarks health specification copying.
.github/​workflows/​e2e.yml Triggers end-to-end CI for command and chart changes.

Review effort: Balanced

Signed-off-by: Oliver Baehler <oliver@sudo-i.net>
Copilot AI balanced review requested due to automatic review settings November 19, 2026 16: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

Unresolved critical issues can falsely report unhealthy targets as healthy and allow old and new replication controllers to modify targets concurrently during rolling upgrades.

6 open issues
1 previously missed issue

In code that hasn't changed since last review

Medium severity Finalize health references per shared target

pkg/​api/​processor/​processor_func.go:340

When multiple origins apply disjoint spec fields to the same Deployment, their separate inventory entries can retain different generations for the same target UID. The health observer requires exact generation equality for every entry, so a healthy combined rollout can time out and block dependents until another full apply refreshes the earlier reference. Finalize health references per physical target after all contributions complete, retain per-origin lifecycle tracking, and add a shared-target regression case.

1 file not reviewed
  • api/v1beta2/zz_generated.deepcopy.go: Generated file

Review effort: Balanced

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

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

An in-flight replication race can cause premature Healthy/Ready reporting, while merged Deployment strategies can block installation or upgrades and duplicate health observations add unnecessary polling work.

No open issues 🎉

3 previously missed issues

In code that hasn't changed since last review

High severity Fence scoped status publication against health-mode changes

internal/​controllers/​resources/​namespace_trigger.go:267

A scoped replication that passes this cached health-disabled check can finish after health is enabled and overwrite health-tracked inventory with nil-Health entries, leaving the new checkpoint intact; the observer can then skip those targets and report Healthy/Ready prematurely. Fence scoped status publication against the live source UID, generation and health mode, preserving cleanup inventory while invalidating the checkpoint and requesting a full apply when an in-flight operation changes it. The same race affects the namespaced path at internal/controllers/resources/namespace_trigger.go:324–326.

Medium severity Replace worker deployment strategy instead of deep-merging it

charts/​capsule/​templates/​controllers/​replications/​_helpers.tpl:7

Deep-merging the entire manager also merges the two Deployment strategies. If core uses RollingUpdate with a rollingUpdate configuration and replications sets deploymentStrategy.type: Recreate, the worker retains core's rollingUpdate field. Kubernetes rejects that Deployment because rollingUpdate is forbidden with Recreate, so an otherwise valid pair of controller configurations cannot be installed or upgraded. Replace the worker's deploymentStrategy as a whole after this merge rather than inheriting core's strategy fields.

Medium severity Memoize repeated health observations within each batch

internal/​controllers/​resources/​health_controller.go:190

When several inventory contributions share a target and health reference, this loop repeats the same target GET and evaluation for every contribution on each poll. Memoize observations by target GVK, namespace/name and health reference within each batch, while preserving the separate ownership records and bounded cursor traversal.

6 resolved since last review
1 file not reviewed
  • api/v1beta2/zz_generated.deepcopy.go: Generated file

Review effort: Balanced

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

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

Late namespace-scoped status updates can overwrite health inventory while retaining a newer checkpoint, allowing targets to be reported Healthy/Ready without being checked.

4 open issues
1 file not reviewed
  • api/v1beta2/zz_generated.deepcopy.go: Generated file

Review effort: Balanced

Comment on lines +265 to +267
if tntResource.Spec.HealthChecks.Enabled() {
return meta.TriggerRequestReconcileAnnotation(ctx, replicationClient{Client: r.client, reader: r.reader}, capsulev1beta2.GroupVersion.WithKind("GlobalTenantResource"), client.ObjectKeyFromObject(tntResource))
}
Comment thread pkg/api/processor/health.go Resolved
Signed-off-by: Oliver Baehler <oliver@sudo-i.net>
Copilot AI balanced review requested due to automatic review settings November 20, 2026 07: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

The chart can fail to render during upgrades with reused legacy values, while the direct client ignores configured API limits and inherited monitor selectors can leave replication metrics unscraped.

5 open issues
2 resolved since last review
1 file not reviewed
  • api/v1beta2/zz_generated.deepcopy.go: Generated file

Review effort: Balanced

Comment thread cmd/controller/common/bootstrap.go Resolved
Signed-off-by: Oliver Baehler <oliver@sudo-i.net>
Copilot AI balanced review requested due to automatic review settings November 20, 2026 09:13

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

Human assessment is needed because subtle retry-state and workload-identification interactions could bypass rollout health checks or indefinitely block replication startup.

1 open issue
4 resolved since last review
1 file not reviewed
  • api/v1beta2/zz_generated.deepcopy.go: Generated file

Review effort: Balanced

@oliverbaehler
oliverbaehler merged commit fe54045 into projectcapsule:main Nov 20, 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