Skip to content

Commit

Permalink
Merge pull request #2105 from chrischdi/pr-capi-flags-watch-filter-value
Browse files Browse the repository at this point in the history
🌱 Introduce watch-filter flag as predicate filter + filter out paused objects
  • Loading branch information
k8s-ci-robot authored Aug 2, 2023
2 parents 678c8e2 + db0fa42 commit 7de8e8b
Show file tree
Hide file tree
Showing 11 changed files with 29 additions and 3 deletions.
2 changes: 2 additions & 0 deletions controllers/serviceaccount_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (
"sigs.k8s.io/cluster-api/util/annotations"
"sigs.k8s.io/cluster-api/util/conditions"
"sigs.k8s.io/cluster-api/util/patch"
"sigs.k8s.io/cluster-api/util/predicates"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
Expand Down Expand Up @@ -121,6 +122,7 @@ func AddServiceAccountProviderControllerToManager(ctx *context.ControllerManager
return requests
}),
).
WithEventFilter(predicates.ResourceNotPausedAndHasFilterLabel(ctrl.LoggerFrom(ctx), ctx.WatchFilterValue)).
Complete(r)
}

Expand Down
2 changes: 2 additions & 0 deletions controllers/servicediscovery_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (
clusterutilv1 "sigs.k8s.io/cluster-api/util"
"sigs.k8s.io/cluster-api/util/conditions"
"sigs.k8s.io/cluster-api/util/patch"
"sigs.k8s.io/cluster-api/util/predicates"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/cache"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -120,6 +121,7 @@ func AddServiceDiscoveryControllerToManager(ctx *context.ControllerManagerContex
&vmwarev1.VSphereCluster{},
handler.OnlyControllerOwner(),
)).
WithEventFilter(predicates.ResourceNotPausedAndHasFilterLabel(ctrl.LoggerFrom(ctx), ctx.WatchFilterValue)).
Complete(r)
}

Expand Down
2 changes: 2 additions & 0 deletions controllers/vspherecluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ func AddClusterControllerToManager(ctx *context.ControllerManagerContext, mgr ma
&vmwarev1.VSphereMachine{},
handler.EnqueueRequestsFromMapFunc(reconciler.VSphereMachineToCluster),
).
WithEventFilter(predicates.ResourceNotPausedAndHasFilterLabel(ctrl.LoggerFrom(ctx), ctx.WatchFilterValue)).
Complete(reconciler)
}

Expand Down Expand Up @@ -159,6 +160,7 @@ func AddClusterControllerToManager(ctx *context.ControllerManagerContext, mgr ma
&source.Channel{Source: ctx.GetGenericEventChannelFor(clusterControlledTypeGVK)},
&handler.EnqueueRequestForObject{},
).
WithEventFilter(predicates.ResourceNotPausedAndHasFilterLabel(ctrl.LoggerFrom(ctx), ctx.WatchFilterValue)).
WithEventFilter(predicates.ResourceIsNotExternallyManaged(reconciler.Logger)).
Build(reconciler)
if err != nil {
Expand Down
2 changes: 2 additions & 0 deletions controllers/vsphereclusteridentity_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
clusterutilv1 "sigs.k8s.io/cluster-api/util"
"sigs.k8s.io/cluster-api/util/conditions"
"sigs.k8s.io/cluster-api/util/patch"
"sigs.k8s.io/cluster-api/util/predicates"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
Expand Down Expand Up @@ -71,6 +72,7 @@ func AddVsphereClusterIdentityControllerToManager(ctx *context.ControllerManager
return ctrl.NewControllerManagedBy(mgr).
For(identityControlledType).
WithOptions(options).
WithEventFilter(predicates.ResourceNotPausedAndHasFilterLabel(ctrl.LoggerFrom(ctx), ctx.WatchFilterValue)).
Complete(reconciler)
}

Expand Down
2 changes: 2 additions & 0 deletions controllers/vspheredeploymentzone_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"sigs.k8s.io/cluster-api/util/collections"
"sigs.k8s.io/cluster-api/util/conditions"
"sigs.k8s.io/cluster-api/util/patch"
"sigs.k8s.io/cluster-api/util/predicates"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
Expand Down Expand Up @@ -88,6 +89,7 @@ func AddVSphereDeploymentZoneControllerToManager(ctx *context.ControllerManagerC
&source.Channel{Source: ctx.GetGenericEventChannelFor(controlledTypeGVK)},
&handler.EnqueueRequestForObject{},
).
WithEventFilter(predicates.ResourceNotPausedAndHasFilterLabel(ctrl.LoggerFrom(ctx), ctx.WatchFilterValue)).
Complete(reconciler)
}

Expand Down
3 changes: 2 additions & 1 deletion controllers/vspheremachine_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@ func AddMachineControllerToManager(ctx *context.ControllerManagerContext, mgr ma
WatchesRawSource(
&source.Channel{Source: ctx.GetGenericEventChannelFor(controlledTypeGVK)},
&handler.EnqueueRequestForObject{},
)
).
WithEventFilter(predicates.ResourceNotPausedAndHasFilterLabel(ctrl.LoggerFrom(ctx), ctx.WatchFilterValue))

r := &machineReconciler{
ControllerContext: controllerContext,
Expand Down
2 changes: 2 additions & 0 deletions controllers/vspherevm_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (
"sigs.k8s.io/cluster-api/util/annotations"
"sigs.k8s.io/cluster-api/util/conditions"
"sigs.k8s.io/cluster-api/util/patch"
"sigs.k8s.io/cluster-api/util/predicates"
ctrl "sigs.k8s.io/controller-runtime"
ctrlclient "sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
Expand Down Expand Up @@ -103,6 +104,7 @@ func AddVMControllerToManager(ctx *context.ControllerManagerContext, mgr manager
&source.Channel{Source: ctx.GetGenericEventChannelFor(controlledTypeGVK)},
&handler.EnqueueRequestForObject{},
).
WithEventFilter(predicates.ResourceNotPausedAndHasFilterLabel(ctrl.LoggerFrom(ctx), ctx.WatchFilterValue)).
Build(r)
if err != nil {
return err
Expand Down
4 changes: 4 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
logsv1 "k8s.io/component-base/logs/api/v1"
_ "k8s.io/component-base/logs/json/register"
"k8s.io/klog/v2"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
"sigs.k8s.io/cluster-api/controllers/remote"
"sigs.k8s.io/cluster-api/util/flags"
ctrl "sigs.k8s.io/controller-runtime"
Expand Down Expand Up @@ -173,6 +174,9 @@ func InitFlags(fs *pflag.FlagSet) {
fs.StringVar(&watchNamespace, "namespace", "",
"Namespace that the controller watches to reconcile cluster-api objects. If unspecified, the controller watches for cluster-api objects across all namespaces.")

fs.StringVar(&managerOpts.WatchFilterValue, "watch-filter", "",
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))

fs.StringVar(&managerOpts.PprofBindAddress, "profiler-address", defaultProfilerAddr,
"Bind address to expose the pprof profiler (e.g. localhost:6060)")

Expand Down
3 changes: 3 additions & 0 deletions pkg/context/controller_manager_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ type ControllerManagerContext struct {
// NetworkProvider is the network provider used by Supervisor based clusters
NetworkProvider string

// WatchFilterValue is used to filter incoming objects by label.
WatchFilterValue string

genericEventCache sync.Map
}

Expand Down
1 change: 1 addition & 0 deletions pkg/manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ func New(opts Options) (Manager, error) {
EnableKeepAlive: opts.EnableKeepAlive,
KeepAliveDuration: opts.KeepAliveDuration,
NetworkProvider: opts.NetworkProvider,
WatchFilterValue: opts.WatchFilterValue,
}

// Add the requested items to the manager.
Expand Down
9 changes: 7 additions & 2 deletions pkg/manager/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ type Options struct {
// for better load management on vSphere api server
EnableKeepAlive bool

// LeaderElectionNamespace is the namespace in which the pod running the
// controller maintains a leader election lock
// PodNamespace is the namespace in which the pod running the controller
// maintains a leader election lock.
//
// Defaults to the eponymous constant in this package.
PodNamespace string
Expand Down Expand Up @@ -81,6 +81,11 @@ type Options struct {
// If not set, it will default to a DummyNetworkProvider which is intended for testing purposes.
// VIM based clusters and managers will not need to set this flag.
NetworkProvider string

// WatchFilterValue is used to filter incoming objects by label.
//
// Defaults to the empty string and by that not filter anything.
WatchFilterValue string
}

func (o *Options) defaults() {
Expand Down

0 comments on commit 7de8e8b

Please sign in to comment.