From 93845c01d95eedb00959832f37b093f874018768 Mon Sep 17 00:00:00 2001 From: Stefan Catargiu Date: Wed, 7 Feb 2024 18:02:12 +0100 Subject: [PATCH] Only reconcile ip objects with specific label --- controllers/controllers_test.go | 2 +- controllers/ip_controller.go | 35 ++++++++++++++++++++++++++------- main.go | 8 +++++++- 3 files changed, 36 insertions(+), 9 deletions(-) diff --git a/controllers/controllers_test.go b/controllers/controllers_test.go index 5bf902c..ed96a50 100644 --- a/controllers/controllers_test.go +++ b/controllers/controllers_test.go @@ -84,7 +84,7 @@ var _ = BeforeSuite(func() { Expect(err).NotTo(HaveOccurred()) Expect(mgr).NotTo(BeNil()) - ipReconciler, err = NewIPReconciler("") + ipReconciler, err = NewIPReconciler("", "", "") Expect(err).NotTo(HaveOccurred()) Expect(ipReconciler.SetupWithManager(mgr)).To(Succeed()) diff --git a/controllers/ip_controller.go b/controllers/ip_controller.go index 7de97a0..1851127 100644 --- a/controllers/ip_controller.go +++ b/controllers/ip_controller.go @@ -39,19 +39,23 @@ import ( //+kubebuilder:rbac:groups=ipam.onmetal.de,resources=ips,verbs=get;list;watch //+kubebuilder:rbac:groups=ipam.onmetal.de,resources=ips/status,verbs=get -func NewIPReconciler(namespace string) (*IPReconciler, error) { +func NewIPReconciler(namespace string, subnetLabelName string, subnetLabelValue string) (*IPReconciler, error) { return &IPReconciler{ - namespace: namespace, + namespace: namespace, + subnetLabelName: subnetLabelName, + subnetLabelValue: subnetLabelValue, }, nil } // IPReconciler reconciles a IP object. type IPReconciler struct { client.Client - namespace string - disabled bool - disabledMtx sync.RWMutex - macRegex *regexp.Regexp + namespace string + subnetLabelName string + subnetLabelValue string + disabled bool + disabledMtx sync.RWMutex + macRegex *regexp.Regexp } func (r *IPReconciler) enable() { @@ -268,5 +272,22 @@ func (r *IPReconciler) SetupWithManager(mgr ctrl.Manager) error { }, } - return ctrl.NewControllerManagedBy(mgr).For(&ipamv1alpha1.IP{}).WithEventFilter(predicate.And(predicate.GenerationChangedPredicate{}, inCorrectNamespacePredicate, notBeingDeletedPredicate, validPredicate)).WithOptions(controller.Options{MaxConcurrentReconciles: 10}).Complete(r) + hasValidLabelPredicate := predicate.Funcs{ + CreateFunc: func(e event.CreateEvent) bool { + if r.subnetLabelName != "" { + l, ok := e.Object.GetLabels()[r.subnetLabelName] + return ok && l == r.subnetLabelValue + } + return true + }, + UpdateFunc: func(e event.UpdateEvent) bool { + if r.subnetLabelName != "" { + l, ok := e.ObjectNew.GetLabels()[r.subnetLabelName] + return ok && l == r.subnetLabelValue + } + return true + }, + } + + return ctrl.NewControllerManagedBy(mgr).For(&ipamv1alpha1.IP{}).WithEventFilter(predicate.And(predicate.GenerationChangedPredicate{}, inCorrectNamespacePredicate, notBeingDeletedPredicate, validPredicate, hasValidLabelPredicate)).WithOptions(controller.Options{MaxConcurrentReconciles: 10}).Complete(r) } diff --git a/main.go b/main.go index f2dbeb1..6e3ea1f 100644 --- a/main.go +++ b/main.go @@ -73,6 +73,8 @@ type params struct { shutdownTimeout time.Duration consoleServerCert string consoleServerKey string + subnetLabelName string + subnetLabelValue string } func parseCmdLine() params { @@ -91,6 +93,8 @@ func parseCmdLine() params { pflag.Duration("shutdown-timeout", 5*time.Minute, "Wait this long before issuing an immediate shutdown, if graceful shutdown has not succeeded. See https://golang.org/pkg/time/#ParseDuration") pflag.String("console-server-cert", "", "Use a TLS certificate for the console server. If not set, do not start the console server.") pflag.String("console-server-key", "", "Use a TLS key for the console server. If not set, do not start the console server.") + pflag.String("subnet-label-name", "", "Label name used to filter ip.ipam objects. Not specifying it will reconcile ALL ip.ipam objects.") + pflag.String("subnet-label-value", "", "Label value used to filter ip.ipam objects.") var help bool pflag.BoolVarP(&help, "help", "h", false, "Show this help message.") @@ -120,6 +124,8 @@ func parseCmdLine() params { shutdownTimeout: viper.GetDuration("shutdown-timeout"), consoleServerCert: viper.GetString("console-server-cert"), consoleServerKey: viper.GetString("console-server-key"), + subnetLabelName: viper.GetString("subnet-label-name"), + subnetLabelValue: viper.GetString("subnet-label-value"), } } @@ -205,7 +211,7 @@ func main() { } var ipReconciler *controllers.IPReconciler - ipReconciler, err = controllers.NewIPReconciler(p.namespace) + ipReconciler, err = controllers.NewIPReconciler(p.namespace, p.subnetLabelName, p.subnetLabelValue) if err != nil { log.Error(ctx, fmt.Errorf("cannot create controller: %w", err), "controller", "IP") exitCode = 1