Skip to content

Commit db0fa42

Browse files
committed
Introduce watch-filter flag as predicate filter + filter out paused objects
1 parent 678c8e2 commit db0fa42

11 files changed

+29
-3
lines changed

controllers/serviceaccount_controller.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import (
3636
"sigs.k8s.io/cluster-api/util/annotations"
3737
"sigs.k8s.io/cluster-api/util/conditions"
3838
"sigs.k8s.io/cluster-api/util/patch"
39+
"sigs.k8s.io/cluster-api/util/predicates"
3940
ctrl "sigs.k8s.io/controller-runtime"
4041
"sigs.k8s.io/controller-runtime/pkg/client"
4142
"sigs.k8s.io/controller-runtime/pkg/controller"
@@ -121,6 +122,7 @@ func AddServiceAccountProviderControllerToManager(ctx *context.ControllerManager
121122
return requests
122123
}),
123124
).
125+
WithEventFilter(predicates.ResourceNotPausedAndHasFilterLabel(ctrl.LoggerFrom(ctx), ctx.WatchFilterValue)).
124126
Complete(r)
125127
}
126128

controllers/servicediscovery_controller.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import (
3737
clusterutilv1 "sigs.k8s.io/cluster-api/util"
3838
"sigs.k8s.io/cluster-api/util/conditions"
3939
"sigs.k8s.io/cluster-api/util/patch"
40+
"sigs.k8s.io/cluster-api/util/predicates"
4041
ctrl "sigs.k8s.io/controller-runtime"
4142
"sigs.k8s.io/controller-runtime/pkg/cache"
4243
"sigs.k8s.io/controller-runtime/pkg/client"
@@ -120,6 +121,7 @@ func AddServiceDiscoveryControllerToManager(ctx *context.ControllerManagerContex
120121
&vmwarev1.VSphereCluster{},
121122
handler.OnlyControllerOwner(),
122123
)).
124+
WithEventFilter(predicates.ResourceNotPausedAndHasFilterLabel(ctrl.LoggerFrom(ctx), ctx.WatchFilterValue)).
123125
Complete(r)
124126
}
125127

controllers/vspherecluster_controller.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ func AddClusterControllerToManager(ctx *context.ControllerManagerContext, mgr ma
102102
&vmwarev1.VSphereMachine{},
103103
handler.EnqueueRequestsFromMapFunc(reconciler.VSphereMachineToCluster),
104104
).
105+
WithEventFilter(predicates.ResourceNotPausedAndHasFilterLabel(ctrl.LoggerFrom(ctx), ctx.WatchFilterValue)).
105106
Complete(reconciler)
106107
}
107108

@@ -159,6 +160,7 @@ func AddClusterControllerToManager(ctx *context.ControllerManagerContext, mgr ma
159160
&source.Channel{Source: ctx.GetGenericEventChannelFor(clusterControlledTypeGVK)},
160161
&handler.EnqueueRequestForObject{},
161162
).
163+
WithEventFilter(predicates.ResourceNotPausedAndHasFilterLabel(ctrl.LoggerFrom(ctx), ctx.WatchFilterValue)).
162164
WithEventFilter(predicates.ResourceIsNotExternallyManaged(reconciler.Logger)).
163165
Build(reconciler)
164166
if err != nil {

controllers/vsphereclusteridentity_controller.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
clusterutilv1 "sigs.k8s.io/cluster-api/util"
3131
"sigs.k8s.io/cluster-api/util/conditions"
3232
"sigs.k8s.io/cluster-api/util/patch"
33+
"sigs.k8s.io/cluster-api/util/predicates"
3334
ctrl "sigs.k8s.io/controller-runtime"
3435
"sigs.k8s.io/controller-runtime/pkg/client"
3536
"sigs.k8s.io/controller-runtime/pkg/controller"
@@ -71,6 +72,7 @@ func AddVsphereClusterIdentityControllerToManager(ctx *context.ControllerManager
7172
return ctrl.NewControllerManagedBy(mgr).
7273
For(identityControlledType).
7374
WithOptions(options).
75+
WithEventFilter(predicates.ResourceNotPausedAndHasFilterLabel(ctrl.LoggerFrom(ctx), ctx.WatchFilterValue)).
7476
Complete(reconciler)
7577
}
7678

controllers/vspheredeploymentzone_controller.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import (
3232
"sigs.k8s.io/cluster-api/util/collections"
3333
"sigs.k8s.io/cluster-api/util/conditions"
3434
"sigs.k8s.io/cluster-api/util/patch"
35+
"sigs.k8s.io/cluster-api/util/predicates"
3536
ctrl "sigs.k8s.io/controller-runtime"
3637
"sigs.k8s.io/controller-runtime/pkg/client"
3738
"sigs.k8s.io/controller-runtime/pkg/controller"
@@ -88,6 +89,7 @@ func AddVSphereDeploymentZoneControllerToManager(ctx *context.ControllerManagerC
8889
&source.Channel{Source: ctx.GetGenericEventChannelFor(controlledTypeGVK)},
8990
&handler.EnqueueRequestForObject{},
9091
).
92+
WithEventFilter(predicates.ResourceNotPausedAndHasFilterLabel(ctrl.LoggerFrom(ctx), ctx.WatchFilterValue)).
9193
Complete(reconciler)
9294
}
9395

controllers/vspheremachine_controller.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,8 @@ func AddMachineControllerToManager(ctx *context.ControllerManagerContext, mgr ma
122122
WatchesRawSource(
123123
&source.Channel{Source: ctx.GetGenericEventChannelFor(controlledTypeGVK)},
124124
&handler.EnqueueRequestForObject{},
125-
)
125+
).
126+
WithEventFilter(predicates.ResourceNotPausedAndHasFilterLabel(ctrl.LoggerFrom(ctx), ctx.WatchFilterValue))
126127

127128
r := &machineReconciler{
128129
ControllerContext: controllerContext,

controllers/vspherevm_controller.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import (
3636
"sigs.k8s.io/cluster-api/util/annotations"
3737
"sigs.k8s.io/cluster-api/util/conditions"
3838
"sigs.k8s.io/cluster-api/util/patch"
39+
"sigs.k8s.io/cluster-api/util/predicates"
3940
ctrl "sigs.k8s.io/controller-runtime"
4041
ctrlclient "sigs.k8s.io/controller-runtime/pkg/client"
4142
"sigs.k8s.io/controller-runtime/pkg/controller"
@@ -103,6 +104,7 @@ func AddVMControllerToManager(ctx *context.ControllerManagerContext, mgr manager
103104
&source.Channel{Source: ctx.GetGenericEventChannelFor(controlledTypeGVK)},
104105
&handler.EnqueueRequestForObject{},
105106
).
107+
WithEventFilter(predicates.ResourceNotPausedAndHasFilterLabel(ctrl.LoggerFrom(ctx), ctx.WatchFilterValue)).
106108
Build(r)
107109
if err != nil {
108110
return err

main.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import (
3535
logsv1 "k8s.io/component-base/logs/api/v1"
3636
_ "k8s.io/component-base/logs/json/register"
3737
"k8s.io/klog/v2"
38+
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
3839
"sigs.k8s.io/cluster-api/controllers/remote"
3940
"sigs.k8s.io/cluster-api/util/flags"
4041
ctrl "sigs.k8s.io/controller-runtime"
@@ -173,6 +174,9 @@ func InitFlags(fs *pflag.FlagSet) {
173174
fs.StringVar(&watchNamespace, "namespace", "",
174175
"Namespace that the controller watches to reconcile cluster-api objects. If unspecified, the controller watches for cluster-api objects across all namespaces.")
175176

177+
fs.StringVar(&managerOpts.WatchFilterValue, "watch-filter", "",
178+
fmt.Sprintf("Label value that the controller watches to reconcile cluster-api objects. Label key is always %s. If unspecified, the controller watches for all cluster-api objects.", clusterv1.WatchLabel))
179+
176180
fs.StringVar(&managerOpts.PprofBindAddress, "profiler-address", defaultProfilerAddr,
177181
"Bind address to expose the pprof profiler (e.g. localhost:6060)")
178182

pkg/context/controller_manager_context.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ type ControllerManagerContext struct {
8585
// NetworkProvider is the network provider used by Supervisor based clusters
8686
NetworkProvider string
8787

88+
// WatchFilterValue is used to filter incoming objects by label.
89+
WatchFilterValue string
90+
8891
genericEventCache sync.Map
8992
}
9093

pkg/manager/manager.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ func New(opts Options) (Manager, error) {
9898
EnableKeepAlive: opts.EnableKeepAlive,
9999
KeepAliveDuration: opts.KeepAliveDuration,
100100
NetworkProvider: opts.NetworkProvider,
101+
WatchFilterValue: opts.WatchFilterValue,
101102
}
102103

103104
// Add the requested items to the manager.

0 commit comments

Comments
 (0)