Skip to content

Commit 2e015ff

Browse files
committed
Add ClusterFilter to ClusterCache Options
This allows filtering the Clusters that are handled by the cache. It can be used for example by providers that only want to cache Clusters of the relevant type to them. Signed-off-by: Lennart Jern <[email protected]>
1 parent e4fd578 commit 2e015ff

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

controllers/clustercache/cluster_accessor.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,11 @@ type clusterAccessorConfig struct {
9090

9191
// HealthProbe is the configuration for the health probe.
9292
HealthProbe *clusterAccessorHealthProbeConfig
93+
94+
// ClusterFilter is a function that can be used to filter which clusters should be handled
95+
// by the ClusterCache. If nil, all clusters will be handled. If set, only clusters for which
96+
// the filter returns true will be handled.
97+
ClusterFilter ClusterFilter
9398
}
9499

95100
// clusterAccessorCacheConfig is the config used for the cache that the clusterAccessor creates.

controllers/clustercache/cluster_cache.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,22 @@ type Options struct {
6060
// will never be created.
6161
WatchFilterValue string
6262

63+
// ClusterFilter is a function that can be used to filter which clusters should be handled
64+
// by the ClusterCache. If nil, all clusters will be handled. If set, only clusters for which
65+
// the filter returns true will be handled.
66+
ClusterFilter ClusterFilter
67+
6368
// Cache are the cache options for the caches that are created per cluster.
6469
Cache CacheOptions
6570

6671
// Client are the client options for the clients that are created per cluster.
6772
Client ClientOptions
6873
}
6974

75+
// ClusterFilter is a function that filters which clusters should be handled by the ClusterCache.
76+
// It returns true if the cluster should be handled, false otherwise.
77+
type ClusterFilter func(cluster *clusterv1.Cluster) bool
78+
7079
// CacheOptions are the cache options for the caches that are created per cluster.
7180
type CacheOptions struct {
7281
// SyncPeriod is the sync period of the cache.
@@ -451,6 +460,15 @@ func (cc *clusterCache) Reconcile(ctx context.Context, req reconcile.Request) (r
451460
return ctrl.Result{RequeueAfter: defaultRequeueAfter}, nil
452461
}
453462

463+
// Apply cluster filter if set
464+
if cc.clusterAccessorConfig.ClusterFilter != nil && !cc.clusterAccessorConfig.ClusterFilter(cluster) {
465+
log.V(6).Info("Cluster filtered out by ClusterFilter, not connecting")
466+
accessor.Disconnect(ctx)
467+
cc.deleteClusterAccessor(clusterKey)
468+
cc.cleanupClusterSourcesForCluster(clusterKey)
469+
return ctrl.Result{}, nil
470+
}
471+
454472
// Return if infrastructure is not ready yet to avoid trying to open a connection when it cannot succeed.
455473
// Requeue is not needed as there will be a new reconcile.Request when Cluster.status.initialization.infrastructureProvisioned is set.
456474
if !ptr.Deref(cluster.Status.Initialization.InfrastructureProvisioned, false) {
@@ -735,5 +753,6 @@ func buildClusterAccessorConfig(scheme *runtime.Scheme, options Options, control
735753
Interval: 10 * time.Second,
736754
FailureThreshold: 5,
737755
},
756+
ClusterFilter: options.ClusterFilter,
738757
}
739758
}

0 commit comments

Comments
 (0)