From 6f631259043f3ec9b3f083e20c371bda8cb8d0c5 Mon Sep 17 00:00:00 2001 From: Andrea Panattoni Date: Thu, 28 Sep 2023 09:40:03 +0200 Subject: [PATCH] Refactor network controller namespace handlers Move namespace handlers to its own named function in `SriovNetworkReconciler` and `SriovIBNetworkReconciler`. These changes aim to reduce complexity and increase readability. Signed-off-by: Andrea Panattoni --- controllers/sriovibnetwork_controller.go | 38 +++++++++++++----------- controllers/sriovnetwork_controller.go | 38 +++++++++++++----------- 2 files changed, 40 insertions(+), 36 deletions(-) diff --git a/controllers/sriovibnetwork_controller.go b/controllers/sriovibnetwork_controller.go index 86caf92fe..0ffe5fba0 100644 --- a/controllers/sriovibnetwork_controller.go +++ b/controllers/sriovibnetwork_controller.go @@ -183,24 +183,7 @@ func (r *SriovIBNetworkReconciler) Reconcile(ctx context.Context, req ctrl.Reque func (r *SriovIBNetworkReconciler) SetupWithManager(mgr ctrl.Manager) error { // Reconcile when the target namespace is created after the SriovNetwork object. namespaceHandler := handler.Funcs{ - CreateFunc: func(ctx context.Context, e event.CreateEvent, q workqueue.RateLimitingInterface) { - ibNetworkList := sriovnetworkv1.SriovIBNetworkList{} - err := r.List(ctx, - &ibNetworkList, - client.MatchingFields{"spec.networkNamespace": e.Object.GetName()}, - ) - if err != nil { - log.Log.WithName("SriovIBNetworkReconciler"). - Info("Can't list SriovIBNetworkReconciler for namespace", "resource", e.Object.GetName(), "error", err) - } - - for _, network := range ibNetworkList.Items { - q.Add(reconcile.Request{NamespacedName: types.NamespacedName{ - Namespace: network.Namespace, - Name: network.Name, - }}) - } - }, + CreateFunc: r.namespaceHandlerCreate, } return ctrl.NewControllerManagedBy(mgr). For(&sriovnetworkv1.SriovIBNetwork{}). @@ -208,3 +191,22 @@ func (r *SriovIBNetworkReconciler) SetupWithManager(mgr ctrl.Manager) error { Watches(&corev1.Namespace{}, &namespaceHandler). Complete(r) } + +func (r *SriovIBNetworkReconciler) namespaceHandlerCreate(ctx context.Context, e event.CreateEvent, q workqueue.RateLimitingInterface) { + ibNetworkList := sriovnetworkv1.SriovIBNetworkList{} + err := r.List(ctx, + &ibNetworkList, + client.MatchingFields{"spec.networkNamespace": e.Object.GetName()}, + ) + if err != nil { + log.Log.WithName("SriovIBNetworkReconciler"). + Info("Can't list SriovIBNetworkReconciler for namespace", "resource", e.Object.GetName(), "error", err) + } + + for _, network := range ibNetworkList.Items { + q.Add(reconcile.Request{NamespacedName: types.NamespacedName{ + Namespace: network.Namespace, + Name: network.Name, + }}) + } +} diff --git a/controllers/sriovnetwork_controller.go b/controllers/sriovnetwork_controller.go index 72fd3077c..f89d524c1 100644 --- a/controllers/sriovnetwork_controller.go +++ b/controllers/sriovnetwork_controller.go @@ -184,24 +184,7 @@ func (r *SriovNetworkReconciler) Reconcile(ctx context.Context, req ctrl.Request func (r *SriovNetworkReconciler) SetupWithManager(mgr ctrl.Manager) error { // Reconcile when the target namespace is created after the SriovNetwork object. namespaceHandler := handler.Funcs{ - CreateFunc: func(ctx context.Context, e event.CreateEvent, q workqueue.RateLimitingInterface) { - networkList := sriovnetworkv1.SriovNetworkList{} - err := r.List(ctx, - &networkList, - client.MatchingFields{"spec.networkNamespace": e.Object.GetName()}, - ) - if err != nil { - log.Log.WithName("SriovNetworkReconciler"). - Info("Can't list SriovNetworks for namespace", "resource", e.Object.GetName(), "error", err) - } - - for _, network := range networkList.Items { - q.Add(reconcile.Request{NamespacedName: types.NamespacedName{ - Namespace: network.Namespace, - Name: network.Name, - }}) - } - }, + CreateFunc: r.namespaceHandlerCreate, } return ctrl.NewControllerManagedBy(mgr). @@ -210,3 +193,22 @@ func (r *SriovNetworkReconciler) SetupWithManager(mgr ctrl.Manager) error { Watches(&corev1.Namespace{}, &namespaceHandler). Complete(r) } + +func (r *SriovNetworkReconciler) namespaceHandlerCreate(ctx context.Context, e event.CreateEvent, q workqueue.RateLimitingInterface) { + networkList := sriovnetworkv1.SriovNetworkList{} + err := r.List(ctx, + &networkList, + client.MatchingFields{"spec.networkNamespace": e.Object.GetName()}, + ) + if err != nil { + log.Log.WithName("SriovNetworkReconciler"). + Info("Can't list SriovNetworks for namespace", "resource", e.Object.GetName(), "error", err) + } + + for _, network := range networkList.Items { + q.Add(reconcile.Request{NamespacedName: types.NamespacedName{ + Namespace: network.Namespace, + Name: network.Name, + }}) + } +}