From 9541a0e9fb673c39d01e0e168ee9767d91eb3a01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20GLON?= Date: Tue, 23 Apr 2024 16:53:56 +0200 Subject: [PATCH] feat(cluster-api): implement watch-filter arg **What type of PR is this?** /kind feature implement watch-filter cluster-api arg **What this PR does / why we need it**: **Which issue(s) this PR fixes** *(optional, in `fixes #(, fixes #, ...)` format, will close the issue(s) when PR gets merged)*: Fixes #330 **Special notes for your reviewer**: _Please confirm that if this PR changes any image versions, then that's the sole change this PR makes._ **TODOs**: - [ ] squashed commits - [ ] includes documentation - [ ] adds unit tests --- controllers/osccluster_controller.go | 5 ++++- controllers/oscmachine_controller.go | 2 ++ .../oscmachinetemplate_capacity_controller.go | 3 +++ main.go | 13 ++++++++++--- 4 files changed, 19 insertions(+), 4 deletions(-) diff --git a/controllers/osccluster_controller.go b/controllers/osccluster_controller.go index a02f49248..562b8b5a5 100644 --- a/controllers/osccluster_controller.go +++ b/controllers/osccluster_controller.go @@ -37,6 +37,7 @@ import ( "sigs.k8s.io/cluster-api/util" "sigs.k8s.io/cluster-api/util/annotations" "sigs.k8s.io/cluster-api/util/conditions" + "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/controllerutil" @@ -49,6 +50,7 @@ type OscClusterReconciler struct { client.Client Recorder record.EventRecorder ReconcileTimeout time.Duration + WatchFilterValue string } //+kubebuilder:rbac:groups=infrastructure.cluster.x-k8s.io,resources=oscclusters,verbs=get;list;watch;create;update;patch;delete @@ -500,8 +502,9 @@ func (r *OscClusterReconciler) reconcileDelete(ctx context.Context, clusterScope } // SetupWithManager sets up the controller with the Manager. -func (r *OscClusterReconciler) SetupWithManager(mgr ctrl.Manager) error { +func (r *OscClusterReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager) error { return ctrl.NewControllerManagedBy(mgr). For(&infrastructurev1beta1.OscCluster{}). + WithEventFilter(predicates.ResourceNotPausedAndHasFilterLabel(ctrl.LoggerFrom(ctx), r.WatchFilterValue)). Complete(r) } diff --git a/controllers/oscmachine_controller.go b/controllers/oscmachine_controller.go index 24fd0a723..eaaac0b29 100644 --- a/controllers/oscmachine_controller.go +++ b/controllers/oscmachine_controller.go @@ -55,6 +55,7 @@ type OscMachineReconciler struct { client.Client Recorder record.EventRecorder ReconcileTimeout time.Duration + WatchFilterValue string } //+kubebuilder:rbac:groups=infrastructure.cluster.x-k8s.io,resources=oscmachines,verbs=get;list;watch;create;update;patch;delete @@ -393,6 +394,7 @@ func (r *OscMachineReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Ma return errors.Errorf("failed to create mapper for Cluster to OscMachines: %+v", err) } err = ctrl.NewControllerManagedBy(mgr). + WithEventFilter(predicates.ResourceNotPausedAndHasFilterLabel(ctrl.LoggerFrom(ctx), r.WatchFilterValue)). For(&infrastructurev1beta1.OscMachine{}). Watches( &source.Kind{Type: &clusterv1.Machine{}}, diff --git a/controllers/oscmachinetemplate_capacity_controller.go b/controllers/oscmachinetemplate_capacity_controller.go index 7fbc6d22d..5ac2fdcb7 100644 --- a/controllers/oscmachinetemplate_capacity_controller.go +++ b/controllers/oscmachinetemplate_capacity_controller.go @@ -27,6 +27,7 @@ import ( apierrors "k8s.io/apimachinery/pkg/api/errors" "k8s.io/client-go/tools/record" clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" + "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" @@ -43,6 +44,7 @@ type OscMachineTemplateReconciler struct { client.Client Recorder record.EventRecorder ReconcileTimeout time.Duration + WatchFilterValue string } // +kubebuilder:rbac:groups=infrastructure.cluster.x-k8s.io,resources=oscmachinetemplates,verbs=get;list;watch;create;update;patch;delete @@ -147,5 +149,6 @@ func (r *OscMachineTemplateReconciler) SetupWithManager(ctx context.Context, mgr return ctrl.NewControllerManagedBy(mgr). WithOptions(options). For(&infrastructurev1beta1.OscMachineTemplate{}). + WithEventFilter(predicates.ResourceNotPausedAndHasFilterLabel(ctrl.LoggerFrom(ctx), r.WatchFilterValue)). Complete(r) } diff --git a/main.go b/main.go index e0197a920..7e3918ddc 100644 --- a/main.go +++ b/main.go @@ -18,6 +18,7 @@ package main import ( "flag" + "fmt" "os" "time" @@ -59,11 +60,14 @@ func main() { var metricsAddr string var enableLeaderElection bool var probeAddr string + var watchFilterValue string flag.StringVar(&metricsAddr, "metrics-bind-address", ":8080", "The address the metric endpoint binds to.") flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.") flag.BoolVar(&enableLeaderElection, "leader-elect", true, "Enable leader election for controller manager. "+ "Enabling this will ensure there is only one active controller manager.") + flag.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)) + opts := zap.Options{} opts.BindFlags(flag.CommandLine) flag.Parse() @@ -88,21 +92,23 @@ func main() { os.Exit(1) } + ctx := ctrl.SetupSignalHandler() + if err = (&controllers.OscClusterReconciler{ Client: mgr.GetClient(), Recorder: mgr.GetEventRecorderFor("osccluster-controller"), ReconcileTimeout: reconcileTimeout, - }).SetupWithManager(mgr); err != nil { + WatchFilterValue: watchFilterValue, + }).SetupWithManager(ctx, mgr); err != nil { setupLog.Error(err, "unable to create controller", "controller", "OscCluster") os.Exit(1) } - ctx := ctrl.SetupSignalHandler() - if err = (&controllers.OscMachineReconciler{ Client: mgr.GetClient(), Recorder: mgr.GetEventRecorderFor("oscmachine-controller"), ReconcileTimeout: reconcileTimeout, + WatchFilterValue: watchFilterValue, }).SetupWithManager(ctx, mgr, controller.Options{}); err != nil { setupLog.Error(err, "unable to create controller", "controller", "OscMachine") os.Exit(1) @@ -112,6 +118,7 @@ func main() { Client: mgr.GetClient(), Recorder: mgr.GetEventRecorderFor("oscmachinetemplate-controller"), ReconcileTimeout: reconcileTimeout, + WatchFilterValue: watchFilterValue, }).SetupWithManager(ctx, mgr, controller.Options{}); err != nil { setupLog.Error(err, "unable to create controller", "controller", "OscMachineTemplate") os.Exit(1)