Skip to content

feat: implemented status resource routing for remote cluster mode#4579

Open
abhisheksheth28 wants to merge 1 commit into
open-policy-agent:masterfrom
abhisheksheth28:status-resource-routing
Open

feat: implemented status resource routing for remote cluster mode#4579
abhisheksheth28 wants to merge 1 commit into
open-policy-agent:masterfrom
abhisheksheth28:status-resource-routing

Conversation

@abhisheksheth28
Copy link
Copy Markdown
Contributor

What this PR does / why we need it:
Moves Status resources to the management cluster in remote cluster mode.

Which issue(s) this PR fixes (optional, using fixes #<issue number>(, fixes #<issue_number>, ...) format, will close the issue(s) when the PR gets merged):
Fixes #4381

Special notes for your reviewer:

  • pkg/routing/ : Routing layer (RoutingCache, RoutingClient) that routes status.gatekeeper.sh resources to the management cluster. All other resources go to the target cluster.
  • main.go : Wires routing into the manager via NewCache/NewClient factory functions. Returns nil in non-remote mode (zero behavior change).
  • apis/status/ : OwnerReferences are now always set on PodStatus resources (removed the remote-mode skip since PodStatus and the pod now live on the same cluster).
  • pkg/util/remote_cluster.go : Deleted (replaced by routing package).
  • docs/design/remote-cluster-routing.md : Design proposal.

Copilot AI review requested due to automatic review settings May 18, 2026 21:48
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Implements remote-cluster status-resource routing so that status.gatekeeper.sh PodStatus resources live on the management cluster (alongside the Gatekeeper pod) while all other resources continue to be reconciled on the target cluster. This enables OwnerReference-based garbage collection of PodStatus on pod restart and removes the previous orphan-cleanup workaround.

Changes:

  • Adds a new pkg/routing package with RoutingCache and RoutingClient that dispatch operations to either the management or target cluster based on GVK; wires them into the manager via NewCache/NewClient in main.go.
  • Removes the util.SetSkipPodOwnerRef/ShouldSkipPodOwnerRef mechanism and always sets the pod OwnerReference on every PodStatus type (and deletes the corresponding skip-tests).
  • Updates the remote-cluster E2E workflow, docs, and design doc to reflect status resources living on the management cluster.

Reviewed changes

Copilot reviewed 26 out of 26 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
pkg/routing/routing.go New helpers: IsManagementResource (group check) and ResolveGVK.
pkg/routing/cache.go RoutingCache wrapping two cache.Cache instances.
pkg/routing/client.go RoutingClient routing reads/writes by GVK; Status()/SubResource() not overridden.
pkg/routing/*_test.go Unit tests with fake cache/client and typed/unstructured GVK cases.
main.go Builds mgmtConfig from in-cluster config and provides NewCache/NewClient factories.
pkg/controller/controller.go Adds RemoteClusterEnabled() helper and drops the SetSkipPodOwnerRef(true) call.
pkg/util/remote_cluster*.go Deleted (replaced by routing package).
apis/status/v1alpha1, v1beta1/*_types.go Always set pod OwnerReference; removed conditional skip.
apis/status/v1*/.../*_test.go Removed _SkipsOwnerRefInRemoteClusterMode tests.
.github/workflows/remote-cluster-e2e.yaml Splits CRD deployment per cluster; asserts PodStatuses on management cluster only, with OwnerReferences.
docs/design/remote-cluster-routing.md New design document describing the routing layer.
website/docs/customize-startup.md Updates RBAC guidance and adds upgrade/migration instructions.

Comment thread pkg/routing/cache.go
Comment on lines +86 to +91
go func() {
if err := r.management.Start(ctx); err != nil {
log.Error(err, "management cache failed to start")
}
}()
return r.target.Start(ctx)
Comment thread pkg/routing/cache.go Outdated
return r.target
}

// cacheForGVK returns the corect cache for GVK
Comment thread pkg/routing/cache_test.go Outdated
Comment on lines +194 to +197
// Typed object not registered in scheme, no GVK set, should fall back to target.
type unknownObj struct {
client.Object
}
Comment thread pkg/routing/client.go
Comment on lines +19 to +39
type RoutingClient struct {
// Client is the target cluster client
client.Client
management client.Client
scheme *runtime.Scheme
}

// Compile-time interface check.
var _ client.Client = &RoutingClient{}

// NewRoutingClient creates a RoutingClient. In non-remote mode same client for both target and management
func NewRoutingClient(target, management client.Client, scheme *runtime.Scheme) *RoutingClient {
if target != management {
log.Info("routing enabled: status.gatekeeper.sh resources route to management cluster client")
}
return &RoutingClient{
Client: target,
management: management,
scheme: scheme,
}
}
Comment thread main.go
Comment on lines +252 to +261
// create management cluster config once if remote cluster mode is enabled, shared by RoutingCache (reads) and RoutingClient (writes)
var mgmtConfig *rest.Config
if controller.RemoteClusterEnabled() {
var err error
mgmtConfig, err = rest.InClusterConfig()
if err != nil {
setupLog.Error(err, "management cluster in-cluster config required for --enable-remote-cluster")
return 1
}
}
@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented May 18, 2026

Codecov Report

❌ Patch coverage is 63.36634% with 37 lines in your changes missing coverage. Please review.
✅ Project coverage is 44.57%. Comparing base (3350319) to head (3c76916).
⚠️ Report is 712 commits behind head on master.

Files with missing lines Patch % Lines
pkg/routing/cache.go 64.28% 15 Missing ⚠️
pkg/routing/client.go 87.09% 4 Missing ⚠️
apis/status/v1alpha1/connectionpodstatus_types.go 0.00% 1 Missing and 1 partial ⚠️
apis/status/v1beta1/configpodstatus_types.go 0.00% 1 Missing and 1 partial ⚠️
apis/status/v1beta1/constraintpodstatus_types.go 0.00% 1 Missing and 1 partial ⚠️
...tatus/v1beta1/constrainttemplatepodstatus_types.go 0.00% 1 Missing and 1 partial ⚠️
...status/v1beta1/expansiontemplatepodstatus_types.go 0.00% 2 Missing ⚠️
apis/status/v1beta1/mutatorpodstatus_types.go 0.00% 1 Missing and 1 partial ⚠️
apis/status/v1beta1/providerpodstatus_types.go 0.00% 1 Missing and 1 partial ⚠️
pkg/controller/controller.go 0.00% 2 Missing ⚠️
... and 1 more

❗ There is a different number of reports uploaded between BASE (3350319) and HEAD (3c76916). Click for more details.

HEAD has 1 upload less than BASE
Flag BASE (3350319) HEAD (3c76916)
unittests 2 1
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #4579      +/-   ##
==========================================
- Coverage   54.49%   44.57%   -9.93%     
==========================================
  Files         134      284     +150     
  Lines       12329    20788    +8459     
==========================================
+ Hits         6719     9266    +2547     
- Misses       5116    10728    +5612     
- Partials      494      794     +300     
Flag Coverage Δ
unittests 44.57% <63.36%> (-9.93%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 26 out of 26 changed files in this pull request and generated 5 comments.

Comment thread pkg/routing/client.go
Comment on lines +10 to +39
// RoutingClient implements client.Client by embedding the target client and
// overriding read and write methods to route PodStatus resources
// (status.gatekeeper.sh) to the management cluster.
//
// Get, List, and all write methods route by GVK so that PodStatus operations
// always target the management cluster regardless of how controller-runtime
// configures cache-backed reads.
//
// In non-remote mode, target and management are the same client.
type RoutingClient struct {
// Client is the target cluster client
client.Client
management client.Client
scheme *runtime.Scheme
}

// Compile-time interface check.
var _ client.Client = &RoutingClient{}

// NewRoutingClient creates a RoutingClient. In non-remote mode same client for both target and management.
func NewRoutingClient(target, management client.Client, scheme *runtime.Scheme) *RoutingClient {
if target != management {
log.Info("routing enabled: status.gatekeeper.sh resources route to management cluster client")
}
return &RoutingClient{
Client: target,
management: management,
scheme: scheme,
}
}
Comment thread pkg/routing/cache.go
Comment on lines +86 to +91
go func() {
if err := r.management.Start(ctx); err != nil {
log.Error(err, "management cache failed to start")
}
}()
return r.target.Start(ctx)
Comment thread main.go
Comment on lines +689 to +700
return func(config *rest.Config, opts client.Options) (client.Client, error) {
targetClient, err := client.New(config, opts)
if err != nil {
return nil, fmt.Errorf("creating target client: %w", err)
}

mgmtClient, err := client.New(mgmtConfig, client.Options{Scheme: scheme})
if err != nil {
return nil, fmt.Errorf("creating management client: %w", err)
}
return routing.NewRoutingClient(targetClient, mgmtClient, scheme), nil
}
Comment thread pkg/routing/cache.go
return r.target
}

// cacheForGVK returns the corect cache for GVK.
Comment thread main.go
Comment on lines +252 to +261
// create management cluster config once if remote cluster mode is enabled, shared by RoutingCache (reads) and RoutingClient (writes)
var mgmtConfig *rest.Config
if controller.RemoteClusterEnabled() {
var err error
mgmtConfig, err = rest.InClusterConfig()
if err != nil {
setupLog.Error(err, "management cluster in-cluster config required for --enable-remote-cluster")
return 1
}
}
@abhisheksheth28 abhisheksheth28 force-pushed the status-resource-routing branch from 0563d31 to 3c76916 Compare May 19, 2026 20:53
@abhisheksheth28 abhisheksheth28 marked this pull request as ready for review May 20, 2026 16:49
@abhisheksheth28 abhisheksheth28 requested a review from a team as a code owner May 20, 2026 16:49
Copilot AI review requested due to automatic review settings May 20, 2026 16:49
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 26 out of 26 changed files in this pull request and generated 4 comments.

Comment thread pkg/routing/cache.go
Comment on lines +80 to +92
// Start starts both caches.
func (r *RoutingCache) Start(ctx context.Context) error {
// If both caches are the same (non-remote mode), just start once
if r.target == r.management {
return r.target.Start(ctx)
}
go func() {
if err := r.management.Start(ctx); err != nil {
log.Error(err, "management cache failed to start")
}
}()
return r.target.Start(ctx)
}
Comment thread pkg/routing/cache.go
return r.target
}

// cacheForGVK returns the corect cache for GVK.
Comment thread main.go
Comment on lines +670 to +675
mgmtCache, err := cache.New(mgmtConfig, cache.Options{
Scheme: opts.Scheme,
DefaultNamespaces: map[string]cache.Config{
util.GetNamespace(): {},
},
})
Comment thread main.go
Comment on lines +689 to +699
return func(config *rest.Config, opts client.Options) (client.Client, error) {
targetClient, err := client.New(config, opts)
if err != nil {
return nil, fmt.Errorf("creating target client: %w", err)
}

mgmtClient, err := client.New(mgmtConfig, client.Options{Scheme: scheme})
if err != nil {
return nil, fmt.Errorf("creating management client: %w", err)
}
return routing.NewRoutingClient(targetClient, mgmtClient, scheme), nil
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.

Create status resources in management cluster

3 participants