diff --git a/bootstrap/kubeadm/controllers/kubeadmconfig_controller.go b/bootstrap/kubeadm/controllers/kubeadmconfig_controller.go index 00477ef96feb..e4975f8cadf9 100644 --- a/bootstrap/kubeadm/controllers/kubeadmconfig_controller.go +++ b/bootstrap/kubeadm/controllers/kubeadmconfig_controller.go @@ -71,8 +71,9 @@ type InitLocker interface { // KubeadmConfigReconciler reconciles a KubeadmConfig object. type KubeadmConfigReconciler struct { - Client client.Client - KubeadmInitLock InitLocker + Client client.Client + KubeadmInitLock InitLocker + WatchFilterValue string remoteClientGetter remote.ClusterClientGetter } @@ -97,7 +98,7 @@ func (r *KubeadmConfigReconciler) SetupWithManager(ctx context.Context, mgr ctrl b := ctrl.NewControllerManagedBy(mgr). For(&bootstrapv1.KubeadmConfig{}). WithOptions(option). - WithEventFilter(predicates.ResourceNotPaused(ctrl.LoggerFrom(ctx))). + WithEventFilter(predicates.ResourceNotPausedAndHasFilterLabel(ctrl.LoggerFrom(ctx), r.WatchFilterValue)). Watches( &source.Kind{Type: &clusterv1.Machine{}}, handler.EnqueueRequestsFromMapFunc(r.MachineToBootstrapMapFunc), @@ -107,7 +108,7 @@ func (r *KubeadmConfigReconciler) SetupWithManager(ctx context.Context, mgr ctrl b = b.Watches( &source.Kind{Type: &expv1.MachinePool{}}, handler.EnqueueRequestsFromMapFunc(r.MachinePoolToBootstrapMapFunc), - ) + ).WithEventFilter(predicates.ResourceNotPausedAndHasFilterLabel(ctrl.LoggerFrom(ctx), r.WatchFilterValue)) } c, err := b.Build(r) @@ -118,7 +119,10 @@ func (r *KubeadmConfigReconciler) SetupWithManager(ctx context.Context, mgr ctrl err = c.Watch( &source.Kind{Type: &clusterv1.Cluster{}}, handler.EnqueueRequestsFromMapFunc(r.ClusterToKubeadmConfigs), - predicates.ClusterUnpausedAndInfrastructureReady(ctrl.LoggerFrom(ctx)), + predicates.All(ctrl.LoggerFrom(ctx), + predicates.ClusterUnpausedAndInfrastructureReady(ctrl.LoggerFrom(ctx)), + predicates.ResourceHasFilterLabel(ctrl.LoggerFrom(ctx), r.WatchFilterValue), + ), ) if err != nil { return errors.Wrap(err, "failed adding Watch for Clusters to controller manager") diff --git a/bootstrap/kubeadm/main.go b/bootstrap/kubeadm/main.go index 8e2ead75094e..5c4f3da29fb9 100644 --- a/bootstrap/kubeadm/main.go +++ b/bootstrap/kubeadm/main.go @@ -19,6 +19,7 @@ package main import ( "context" "flag" + "fmt" "math/rand" "net/http" _ "net/http/pprof" @@ -69,6 +70,7 @@ var ( leaderElectionLeaseDuration time.Duration leaderElectionRenewDeadline time.Duration leaderElectionRetryPeriod time.Duration + watchFilterValue string watchNamespace string profilerAddress string kubeadmConfigConcurrency int @@ -110,6 +112,9 @@ func InitFlags(fs *pflag.FlagSet) { fs.DurationVar(&kubeadmbootstrapcontrollers.DefaultTokenTTL, "bootstrap-token-ttl", 15*time.Minute, "The amount of time the bootstrap token will be valid") + fs.StringVar(&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.IntVar(&webhookPort, "webhook-port", 9443, "Webhook Server port") @@ -193,7 +198,8 @@ func setupChecks(mgr ctrl.Manager) { func setupReconcilers(ctx context.Context, mgr ctrl.Manager) { if err := (&kubeadmbootstrapcontrollers.KubeadmConfigReconciler{ - Client: mgr.GetClient(), + Client: mgr.GetClient(), + WatchFilterValue: watchFilterValue, }).SetupWithManager(ctx, mgr, concurrency(kubeadmConfigConcurrency)); err != nil { setupLog.Error(err, "unable to create controller", "controller", "KubeadmConfig") os.Exit(1) diff --git a/controllers/remote/cluster_cache_reconciler.go b/controllers/remote/cluster_cache_reconciler.go index 970338d89e82..c0739da16394 100644 --- a/controllers/remote/cluster_cache_reconciler.go +++ b/controllers/remote/cluster_cache_reconciler.go @@ -23,6 +23,7 @@ import ( "github.com/pkg/errors" apierrors "k8s.io/apimachinery/pkg/api/errors" clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha4" + "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" @@ -32,15 +33,17 @@ import ( // ClusterCacheReconciler is responsible for stopping remote cluster caches when // the cluster for the remote cache is being deleted. type ClusterCacheReconciler struct { - Log logr.Logger - Client client.Client - Tracker *ClusterCacheTracker + Log logr.Logger + Client client.Client + Tracker *ClusterCacheTracker + WatchFilterValue string } func (r *ClusterCacheReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, options controller.Options) error { _, err := ctrl.NewControllerManagedBy(mgr). For(&clusterv1.Cluster{}). WithOptions(options). + WithEventFilter(predicates.ResourceHasFilterLabel(ctrl.LoggerFrom(ctx), r.WatchFilterValue)). Build(r) if err != nil { diff --git a/controlplane/kubeadm/controllers/controller.go b/controlplane/kubeadm/controllers/controller.go index 3a7aee87eac1..a6cb62b7ee10 100644 --- a/controlplane/kubeadm/controllers/controller.go +++ b/controlplane/kubeadm/controllers/controller.go @@ -59,10 +59,11 @@ import ( // KubeadmControlPlaneReconciler reconciles a KubeadmControlPlane object. type KubeadmControlPlaneReconciler struct { - Client client.Client - controller controller.Controller - recorder record.EventRecorder - Tracker *remote.ClusterCacheTracker + Client client.Client + controller controller.Controller + recorder record.EventRecorder + Tracker *remote.ClusterCacheTracker + WatchFilterValue string managementCluster internal.ManagementCluster managementClusterUncached internal.ManagementCluster @@ -73,7 +74,7 @@ func (r *KubeadmControlPlaneReconciler) SetupWithManager(ctx context.Context, mg For(&controlplanev1.KubeadmControlPlane{}). Owns(&clusterv1.Machine{}). WithOptions(options). - WithEventFilter(predicates.ResourceNotPaused(ctrl.LoggerFrom(ctx))). + WithEventFilter(predicates.ResourceNotPausedAndHasFilterLabel(ctrl.LoggerFrom(ctx), r.WatchFilterValue)). Build(r) if err != nil { return errors.Wrap(err, "failed setting up with a controller manager") @@ -82,7 +83,10 @@ func (r *KubeadmControlPlaneReconciler) SetupWithManager(ctx context.Context, mg err = c.Watch( &source.Kind{Type: &clusterv1.Cluster{}}, handler.EnqueueRequestsFromMapFunc(r.ClusterToKubeadmControlPlane), - predicates.ClusterUnpausedAndInfrastructureReady(ctrl.LoggerFrom(ctx)), + predicates.All(ctrl.LoggerFrom(ctx), + predicates.ResourceHasFilterLabel(ctrl.LoggerFrom(ctx), r.WatchFilterValue), + predicates.ClusterUnpausedAndInfrastructureReady(ctrl.LoggerFrom(ctx)), + ), ) if err != nil { return errors.Wrap(err, "failed adding Watch for Clusters to controller manager") diff --git a/controlplane/kubeadm/main.go b/controlplane/kubeadm/main.go index cb2625318156..01b94f80db2f 100644 --- a/controlplane/kubeadm/main.go +++ b/controlplane/kubeadm/main.go @@ -19,6 +19,7 @@ package main import ( "context" "flag" + "fmt" "math/rand" "net/http" _ "net/http/pprof" @@ -68,6 +69,7 @@ var ( leaderElectionLeaseDuration time.Duration leaderElectionRenewDeadline time.Duration leaderElectionRetryPeriod time.Duration + watchFilterValue string watchNamespace string profilerAddress string kubeadmControlPlaneConcurrency int @@ -106,6 +108,9 @@ func InitFlags(fs *pflag.FlagSet) { fs.DurationVar(&syncPeriod, "sync-period", 10*time.Minute, "The minimum interval at which watched resources are reconciled (e.g. 15m)") + fs.StringVar(&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.IntVar(&webhookPort, "webhook-port", 9443, "Webhook Server port") @@ -193,17 +198,19 @@ func setupReconcilers(ctx context.Context, mgr ctrl.Manager) { os.Exit(1) } if err := (&remote.ClusterCacheReconciler{ - Client: mgr.GetClient(), - Log: ctrl.Log.WithName("remote").WithName("ClusterCacheReconciler"), - Tracker: tracker, + Client: mgr.GetClient(), + Log: ctrl.Log.WithName("remote").WithName("ClusterCacheReconciler"), + Tracker: tracker, + WatchFilterValue: watchFilterValue, }).SetupWithManager(ctx, mgr, concurrency(kubeadmControlPlaneConcurrency)); err != nil { setupLog.Error(err, "unable to create controller", "controller", "ClusterCacheReconciler") os.Exit(1) } if err := (&kubeadmcontrolplanecontrollers.KubeadmControlPlaneReconciler{ - Client: mgr.GetClient(), - Tracker: tracker, + Client: mgr.GetClient(), + Tracker: tracker, + WatchFilterValue: watchFilterValue, }).SetupWithManager(ctx, mgr, concurrency(kubeadmControlPlaneConcurrency)); err != nil { setupLog.Error(err, "unable to create controller", "controller", "KubeadmControlPlane") os.Exit(1) diff --git a/main.go b/main.go index 2225dfe80e79..20b9d852f2e8 100644 --- a/main.go +++ b/main.go @@ -244,9 +244,10 @@ func setupReconcilers(ctx context.Context, mgr ctrl.Manager) { os.Exit(1) } if err := (&remote.ClusterCacheReconciler{ - Client: mgr.GetClient(), - Log: ctrl.Log.WithName("remote").WithName("ClusterCacheReconciler"), - Tracker: tracker, + Client: mgr.GetClient(), + Log: ctrl.Log.WithName("remote").WithName("ClusterCacheReconciler"), + Tracker: tracker, + WatchFilterValue: watchFilterValue, }).SetupWithManager(ctx, mgr, concurrency(clusterConcurrency)); err != nil { setupLog.Error(err, "unable to create controller", "controller", "ClusterCacheReconciler") os.Exit(1)