Skip to content

feat(): Standby remote CRD mirroring for cross-cluster HA - #411

Open
sumanthd032 wants to merge 15 commits into
kubeslice:masterfrom
sumanthd032:feat/295-remote-syncer
Open

feat(): Standby remote CRD mirroring for cross-cluster HA#411
sumanthd032 wants to merge 15 commits into
kubeslice:masterfrom
sumanthd032:feat/295-remote-syncer

Conversation

@sumanthd032

Copy link
Copy Markdown

Adds RemoteSyncer, which mirrors a fixed set of hub-side CRDs from the Active cluster onto the Standby's own cluster, so the Standby has current state ready for when it's promoted (promotion itself is #297).

  • pkg/ha/mirror.go, mirror_set.go: the mirror engine — create/update/delete against the Standby, a label-based conflict guard so it never touches an object it didn't create itself, ownerReferences stripped for VpnKeyRotation (its Active-side owner UID doesn't exist on the Standby), and explicit Status().Update() since every mirrored type has a status subresource that a plain Update() doesn't touch.
  • pkg/ha/remote_syncer.go: informers on the Active hub's cache.Cache feed a rate-limited workqueue (the same primitive controller-runtime's own Controller uses); a small worker pool dequeues, re-reads from the Active cache, and mirrors — retrying with backoff on any failure. Informer setup itself also retries with backoff rather than failing once and giving up for the process's lifetime. The Namespace mirror target is scoped to project namespaces only (label-selected), not every namespace on the Active cluster.
  • pkg/ha/metrics.go: ha_sync_lag_seconds, ha_sync_errors_total.
  • main.go: wires RemoteSyncer into the Standby startup path alongside the existing lease-watch loop.
  • config/ha/: a documented, least-privilege sample ClusterRole for the identity behind --ha-active-kubeconfig (read-only on Namespace plus everything in CRDMirrorSet), applied manually on the Active cluster — deliberately not wired into this repo's own kustomize overlays.

Mirrored set: Namespace, Project, Cluster, SliceConfig,ServiceExportConfig, SliceQoSConfig, VpnKeyRotation, WorkerSliceConfig, WorkerSliceGateway, WorkerServiceImport.

Out of scope for this PR (tracked separately): a prune backstop for objects deleted while the syncer was down, SyncError Kubernetes events, and credential mirroring (Secret/ServiceAccount/Role/RoleBinding).

Fixes # 295

How Has This Been Tested?

  • go build ./..., go vet ./pkg/ha/..., gofmt — clean.
  • go test -race -count=1 ./pkg/ha/... — full suite, fake clients throughout.

Checklist:

  • The title of the PR states what changed and the related issues number (used for the release note).
  • Does this PR requires documentation updates?
  • I've updated documentation as required by this PR.
  • I have performed a self-review of my own code.
  • I have commented my code, particularly in hard-to-understand areas.
  • I have tested it for all user roles.
  • I have added all the required unit test cases.

Does this PR introduce a breaking change for other components like worker-operator?

No. WorkerSliceConfig/WorkerSliceGateway/WorkerServiceImport are this repo's own hub-side CRDs, not worker-cluster resources — nothing here touches worker connection/reconnection behavior.

Introduce pkg/ha, the foundation for cross-cluster (Active/Standby)
high availability per ADR kubeslice#293 (issue kubeslice#294).

- HAMode (active|standby|standalone) with fail-safe parsing: empty or
  unknown input maps to standalone so a misconfig never disables writes.
- Lease helpers over coordination.k8s.io/v1: acquire/renew (bumping
  leaseTransitions on takeover), get, and a leaseDuration+padding
  staleness check.
- ClusterLeaderElector: IsLeader() reads an atomic flag kept current by
  background loops, so it is cheap enough to call at the top of every
  Reconcile and always reflects live leadership. StartLeaseRenewal
  (Active) renews the local Lease and releases leadership once the renew
  deadline is exceeded (natural fencing). WatchRemoteLease (Standby)
  reads the Active's Lease and logs staleness but does not promote;
  promotion is issue kubeslice#297.

Standalone is the default and is always the leader, preserving today's
single-hub behaviour (no regression).

Unit tests are race-clean and cover leadership by mode, renew-deadline
loss, lease staleness, and the standby-never-promotes boundary.

vendor: add controller-runtime fake client + interceptor packages
(test-only) via go mod vendor.

Signed-off-by: Sumanth D <sumanthd032@gmail.com>
Wire pkg/ha into the controller so only the Active hub writes (issue kubeslice#294).

- Add a LeaderElector field to all nine reconcilers and a per-call guard
  at the top of every Reconcile: a Standby logs "standby mode, skipping
  reconcile" and returns without writing. The guard is nil-safe, so a
  reconciler built without an elector keeps today's behaviour.
- main.go: add --ha-mode, --ha-identity, --ha-active-kubeconfig,
  --ha-lease-duration, --ha-renew-deadline, --ha-retry-period and
  --ha-padding-seconds; construct the elector (building a remote client
  from the mounted Active kubeconfig in standby mode); start
  StartLeaseRenewal (active) or WatchRemoteLease (standby) and pass the
  shared signal-handler context to the manager.
- Add coordination.k8s.io/leases RBAC for the Lease.

--ha-mode=standalone is the default and is always the leader, so existing
single-hub deployments are unaffected (no regression). The existing
--leader-elect (in-cluster pod election) is left untouched.

A controller test asserts the Standby skips and logs on every call.

vendor: add go.uber.org/zap/zaptest/observer (test-only).
Signed-off-by: Sumanth D <sumanthd032@gmail.com>
The HA leader-election Lease lives in the controller's own namespace and
is already covered by the existing leader-election Role's leases grant
(config/rbac/leader_election_role.yaml). The kubebuilder marker added a
redundant cluster-wide grant and left the generated manifests out of sync
(make manifests was not run). Replace it with a note pointing at the role
that provides the permission, keeping markers and manifests consistent.

Signed-off-by: Sumanth D <sumanthd032@gmail.com>
…hutdown

- acquireOrRenewLease rounds LeaseDurationSeconds up to whole seconds and
  clamps to a minimum of 1, so a sub-second --ha-lease-duration is not
  truncated to 0 (invalid, and skews staleness checks).
- StartLeaseRenewal and WatchRemoteLease return nil instead of ctx.Err() on
  context cancellation, so a graceful shutdown is not logged as an error.

Signed-off-by: Sumanth D <sumanthd032@gmail.com>
The Lease namespace defaulted to a hard-coded constant, but the controller
runs in a namespace injected at runtime via KUBESLICE_CONTROLLER_MANAGER_NAMESPACE
(downward API), and the leader-election Role that grants leases is namespaced to
that deploy namespace. Deploying into any other namespace would create the Lease
where the controller has no leases permission, so the Active could not renew it
and would fence itself permanently.

Add --ha-lease-namespace defaulting to KUBESLICE_CONTROLLER_MANAGER_NAMESPACE so
the Lease lands in the controller's own namespace. Empty (local runs) falls back
to the pkg/ha default.

Signed-off-by: Sumanth D <sumanthd032@gmail.com>
NewClusterLeaderElector fell back to the hard-coded DefaultLeaseNamespace
whenever LeaseNamespace was empty, independent of main.go's flag default.
Since the controller already exposes KUBESLICE_CONTROLLER_MANAGER_NAMESPACE
to represent its actual runtime namespace, check that env var first so the
package resolves correctly on its own, not only by accident of how main.go
wires the --ha-lease-namespace flag default. Only falls back to
DefaultLeaseNamespace when the env var is unset too (e.g. running outside
a pod). Regression tests included.

Signed-off-by: Sumanth D <sumanthd032@gmail.com>
StartLeaseRenewal/WatchRemoteLease returning nil (not ctx.Err()) on
context cancellation was fixed in 0da6d82 but never actually got a
regression test. Also add coverage for: the mode-guard no-op branches,
WatchRemoteLease failing fast without a remote client, getLease/
checkRemoteLeaseOnce propagating a missing-Lease error instead of
reporting fresh, renewOnce keeping leadership on a transient failure
within renewDeadline, and setLeader logging Acquired/Lost exactly once
per transition (F4 in 294-evaluation.md).

No production code changes.

Signed-off-by: Sumanth D <sumanthd032@gmail.com>
…uard, ownerRef strip, status mirroring)

Part of kubeslice#295 — the write path RemoteSyncer's workqueue will drive
(next commit) to mirror KubeSlice CRDs from the Active hub onto the
Standby.

- MirroredResource + CRDMirrorSet (pkg/ha/mirror_set.go): the hub-side
  resource table (Project, Cluster, SliceConfig, ServiceExportConfig,
  SliceQoSConfig, VpnKeyRotation, WorkerSliceConfig, WorkerSliceGateway,
  WorkerServiceImport, plus core Namespace). Deliberately does not match
  issue kubeslice#295's own CRD table, which names Slice/SliceGateway/
  ServiceExport — none of those types exist in this repo; they are
  worker-cluster data-plane CRDs owned by the separate worker-operator
  repo, irrelevant to hub-to-hub mirroring.

- mirrorCreateOrUpdate/mirrorDelete (pkg/ha/mirror.go): strip
  resourceVersion/uid/managedFields/finalizers (+ ownerReferences only
  for VpnKeyRotation, the one mirrored type that carries one) before
  writing, label mirrored objects ha.kubeslice.io/synced-from=active,
  and only ever overwrite or delete a target object that already
  carries that label — the conflict guard that keeps the syncer off
  anything the Standby's own reconcilers or an operator created
  directly, including pre-existing namespaces like kube-system/default
  now that Namespace is an ordinary mirrored type rather than a
  special-cased cold-start step.

- Every mirrored type has a status subresource. A plain Update() never
  touches .status once one is registered, so mirrorCreateOrUpdate
  always follows up with an explicit Status().Update() when the source
  object has a non-empty status — matching this repo's own
  UpdateStatus/CleanupUpdateStatus convention. Regression-tested.

Fake-client unit tests cover create, update-of-existing, the conflict
guard on both update and delete, delete-idempotent-on-NotFound,
StripOwnerRefs true/false, and status mirroring.

Signed-off-by: Sumanth D <sumanthd032@gmail.com>
Part of kubeslice#295. Registered via prometheus.NewHistogramVec/NewCounterVec
+ prometheus.MustRegister in pkg/ha's own init(), the same
self-registration idiom metrics/prometheus.go already uses for
KubeSliceEventsCounter — not routed through
metrics.StartMetricsCollector, whose default labels are slice-specific
and don't apply to a cross-cluster mirror.

- ha_sync_lag_seconds{kind,operation}: time.Now() minus
  CreationTimestamp for creates; minus first-enqueue time for
  update/delete (RemoteSyncer's workqueue coalesces repeated events for
  the same object, so there's no single "delivery time" once a retry
  has backed off a few times — first-enqueue is the more useful number
  to alert on, since it reflects total time since the triggering
  change).
- ha_sync_errors_total{kind,operation}: counts mirror failures. The
  syncer keeps running and retries via its workqueue on every
  increment; this metric never indicates a crash.

vendor: add prometheus/client_golang/prometheus/testutil (test-only)
via go mod vendor, used to assert metric samples/labels directly.

Signed-off-by: Sumanth D <sumanthd032@gmail.com>
Part of kubeslice#295. RemoteSyncer runs only in standby mode: builds a
controller-runtime cache.Cache against the Active hub's rest.Config,
registers one informer per CRDMirrorSet entry, and starts a small
worker pool draining a rate-limited workqueue.TypedRateLimitingInterface
[syncKey] — the same primitive controller-runtime's own Controller
uses internally (internal/controller.Controller).

Informer callbacks (handlersFor) only enqueue a syncKey; no mirror
logic runs on the informer's own goroutine. A burst of Update events
for the same object coalesces into one queued item, and a worker
determines the real action at dequeue time by re-reading the Active
cache (found -> mirrorCreateOrUpdate, NotFound -> mirrorDelete) — the
same way a Reconcile call would.

This is the commit that satisfies issue kubeslice#295's own acceptance
criterion that the syncer "retries without crashing": on any mirror
failure, processOnce calls queue.AddRateLimited instead of dropping
the key. The concrete failure mode this fixes: a namespaced object
(e.g. a new Project's Cluster) created on Active after the Standby's
initial sync has already completed previously had no path to retry —
nothing fires again for an object that didn't change on Active once
its first mirror attempt failed on "namespace not found". Now it just
gets retried a few seconds later once the Namespace mirror (an
ordinary CRDMirrorSet row, no special-casing needed) has landed.

Start(ctx) delegates its blocking wait directly to
remoteCache.Start(ctx), which itself blocks on <-ctx.Done() and
returns nil — giving the "return nil, not ctx.Err(), on graceful
shutdown" contract StartLeaseRenewal/WatchRemoteLease already use, for
free.

remoteGetFunc is a small seam (defaulted to a real cache.Cache.Get in
the constructor) so the retry engine's tests exercise real workqueue
backoff/redelivery behaviour without a *rest.Config or live cluster.

Signed-off-by: Sumanth D <sumanthd032@gmail.com>
Part of kubeslice#295. Constructs ha.RemoteSyncer alongside the existing
ClusterLeaderElector, reusing the same remote *rest.Config and local
client the elector already builds rather than loading the Active
kubeconfig twice — hoisted the standby block's remoteCfg to an outer
remoteHACfg variable so both consumers can see it.

Starts remoteSyncer.Start(ctx) in its own goroutine alongside the
existing leaderElector.WatchRemoteLease(ctx) in the ha.ModeStandby
switch case; a no-op in any other mode, matching RemoteSyncer's own
mode check.

New flag: --ha-sync-workers (default ha.DefaultSyncWorkers), matching
the existing --ha-* flag style. --ha-sync-interval is intentionally
not added yet — it belongs to the periodic prune backstop (pkg/ha/
prune.go), which is out of scope for this PR and would otherwise be a
flag with no consumer.

Signed-off-by: Sumanth D <sumanthd032@gmail.com>
…mote read identity

Part of kubeslice#295. --ha-active-kubeconfig's RBAC scope on the Active cluster
has been undefined since kubeslice#294 introduced the flag — the dev demo uses
a full-admin kubeconfig, and config/rbac/leader_election_role.yaml
only ever granted configmaps/leases, nothing for the CRD/Namespace
reads RemoteSyncer now needs. The ADR (kubeslice#293) itself names this as an
unaddressed deployment-level boundary without resolving it.

config/ha/active-cluster-clusterrole.yaml: a read-only (get/list/watch)
ClusterRole covering Namespace plus every pkg/ha.CRDMirrorSet type,
plus a ClusterRoleBinding template (subject left as a placeholder —
it's deployment-specific, either a ServiceAccount for in-cluster
dialing or a client-cert User for a flattened kubeconfig, and can't be
hardcoded). config/ha/README.md explains this must be applied on the
Active cluster by whoever provisions it, not through this repo's own
deploy/kustomize flow — confirmed neither config/rbac/kustomization.yaml
nor config/default/kustomization.yaml reference this directory, so it
can never be accidentally auto-applied to the Standby's own cluster,
where it would be meaningless.

Also documents, ahead of time, that a later credential-mirroring PR
appending Secrets to this grant exposes every Secret in the project
namespaces on Active, not just the ones actually mirrored — RBAC can't
scope Secrets by .type — a real tradeoff to weigh when that lands.

Signed-off-by: Sumanth D <sumanthd032@gmail.com>
Start gave up permanently on the first GetInformer/AddEventHandler
error instead of retrying, unlike StartLeaseRenewal/WatchRemoteLease.
Split setup into registerInformers (retries with backoff) wrapping
registerInformersOnce (one attempt).

A naive retry would double-register handlers on resources that
already succeeded, since AddEventHandlerWithResyncPeriod isn't
idempotent. Added a handlerRegistered map so retries skip resources
already done.

CRDMirrorSet's Namespace entry had no filter, so it mirrored every
namespace on the Active hub, not just kubeslice ones — and
mirrorDelete's label-only guard meant an unrelated Active-side delete
could cascade-delete one of those on the Standby. Scoped the
Namespace informer to util.LabelsKubeSliceController via
cache.Options.ByObject.

Signed-off-by: Sumanth D <sumanthd032@gmail.com>
An Active-side object mid-Terminating (deletionTimestamp set, contents
still being garbage-collected) is delivered by the informer as an
ordinary Update, not a Delete yet. mirrorCreateOrUpdate's payload
carried deletionTimestamp straight through, and updating the Standby's
non-terminating mirror with it set fails real API server immutable-
field validation. Stripping it surfaced a second, related failure:
the status-mirror block was still copying a Terminating status onto
that now-non-terminating payload, which a real server also rejects
(status.Phase may only be Terminating if deletionTimestamp is set).

Both are stripped now: deletionTimestamp/deletionGracePeriodSeconds
unconditionally alongside the other identity fields already cleared
there, and status explicitly via delete(payload.Object, "status")
rather than relying on a real API server silently ignoring .status on
the main resource endpoint for subresource-registered types — a fake
client does not replicate that behavior, so the code's correctness
would otherwise depend on which client it's running against. The
Standby still converges correctly once Active reports NotFound and
mirrorDelete takes over; there's nothing useful to reflect about the
in-between Terminating state.

New tests: TestMirrorCreateOrUpdate_StripsDeletionTimestampFromTerminatingSource,
TestMirrorCreateOrUpdate_SkipsStatusMirrorWhenSourceIsTerminating.

Signed-off-by: Sumanth D <sumanthd032@gmail.com>
Dockerfile never copied pkg/ into the build context, so no image has
been buildable since pkg/ha was introduced — go build works directly,
docker build did not. Added COPY pkg/ pkg/ alongside the other source
directories.

config/rbac/role.yaml never granted namespaces/status: every other
CRDMirrorSet type already has an explicit <kind>/status rule, but
Namespace never needed one before it became a mirrored type with
explicit status mirroring. Under real RBAC this permanently fails the
Namespace status write. Added the +kubebuilder:rbac marker in main.go
next to the existing namespaces rule and regenerated via make
manifests.

config/ha/active-cluster-clusterrole.yaml never granted
coordination.k8s.io/leases: it was scoped only to what RemoteSyncer
itself reads (Namespace + CRDMirrorSet), but the same
--ha-active-kubeconfig identity is also used by WatchRemoteLease to
read the Active's own Lease directly. Under a least-privilege identity
using exactly this sample, the Standby could mirror correctly but
never observe Lease staleness at all. Added the leases rule and
updated the README to describe both consumers of this grant.

Signed-off-by: Sumanth D <sumanthd032@gmail.com>
@sumanthd032
sumanthd032 marked this pull request as ready for review July 29, 2026 11:25
Copilot AI review requested due to automatic review settings July 29, 2026 11:25

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

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