-
Notifications
You must be signed in to change notification settings - Fork 1.4k
✨ Add ClusterFilter to ClusterCache Options #12665
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -60,13 +60,22 @@ type Options struct { | |
// will never be created. | ||
WatchFilterValue string | ||
|
||
// ClusterFilter is a function that can be used to filter which clusters should be handled | ||
// by the ClusterCache. If nil, all clusters will be handled. If set, only clusters for which | ||
// the filter returns true will be handled. | ||
ClusterFilter ClusterFilter | ||
|
||
// Cache are the cache options for the caches that are created per cluster. | ||
Cache CacheOptions | ||
|
||
// Client are the client options for the clients that are created per cluster. | ||
Client ClientOptions | ||
} | ||
|
||
// ClusterFilter is a function that filters which clusters should be handled by the ClusterCache. | ||
// It returns true if the cluster should be handled, false otherwise. | ||
type ClusterFilter func(cluster *clusterv1.Cluster) bool | ||
|
||
// CacheOptions are the cache options for the caches that are created per cluster. | ||
type CacheOptions struct { | ||
// SyncPeriod is the sync period of the cache. | ||
|
@@ -357,6 +366,11 @@ type clusterCache struct { | |
|
||
// cacheCtxCancel is used during Shutdown to stop caches. | ||
cacheCtxCancel context.CancelCauseFunc | ||
|
||
// ClusterFilter is a function that can be used to filter which clusters should be handled | ||
// by the ClusterCache. If nil, all clusters will be handled. If set, only clusters for which | ||
// the filter returns true will be handled. | ||
clusterFilter ClusterFilter | ||
} | ||
|
||
// clusterSource stores the necessary information so we can enqueue reconcile.Requests for reconcilers that | ||
|
@@ -451,6 +465,15 @@ func (cc *clusterCache) Reconcile(ctx context.Context, req reconcile.Request) (r | |
return ctrl.Result{RequeueAfter: defaultRequeueAfter}, nil | ||
} | ||
|
||
// Apply cluster filter if set | ||
if cc.clusterFilter != nil && !cc.clusterFilter(cluster) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. wondering if we can avoid to create an accessor entirely if the cluster is filtered out (~like what we can achieve with predicates). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure how this would look like. The problem is that we should also handle the case where a clusterFilter starts filtering out a cluster that it didn't filter out before Creating the clusterAccessor is super cheap There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But maybe there's a better option, I assume it requires some refactoring though (and accordingly maybe some risk of introducing new issues) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe we can move |
||
log.V(6).Info("Cluster filtered out by ClusterFilter, not connecting") | ||
accessor.Disconnect(ctx) | ||
cc.deleteClusterAccessor(clusterKey) | ||
cc.cleanupClusterSourcesForCluster(clusterKey) | ||
return ctrl.Result{}, nil | ||
} | ||
|
||
// Return if infrastructure is not ready yet to avoid trying to open a connection when it cannot succeed. | ||
// Requeue is not needed as there will be a new reconcile.Request when Cluster.status.initialization.infrastructureProvisioned is set. | ||
if !ptr.Deref(cluster.Status.Initialization.InfrastructureProvisioned, false) { | ||
|
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's add some simple unit test coverage (for the reconciler logic)