From b0cc356681b6ccb4e60f989b932b92854e1fed1e Mon Sep 17 00:00:00 2001 From: Tamal Saha Date: Fri, 8 Mar 2024 08:07:08 -0800 Subject: [PATCH] Simplify installer watchers (#180) Signed-off-by: Tamal Saha --- .../service_resolver_controller.go | 32 ++++++++-- .../operator/eventhandler/clusterset_event.go | 62 ------------------- .../proxyserviceresolver_event.go | 38 ------------ 3 files changed, 27 insertions(+), 105 deletions(-) delete mode 100644 pkg/proxyserver/operator/eventhandler/clusterset_event.go delete mode 100644 pkg/proxyserver/operator/eventhandler/proxyserviceresolver_event.go diff --git a/pkg/proxyserver/controllers/service_resolver_controller.go b/pkg/proxyserver/controllers/service_resolver_controller.go index 10356c21..6734d493 100644 --- a/pkg/proxyserver/controllers/service_resolver_controller.go +++ b/pkg/proxyserver/controllers/service_resolver_controller.go @@ -10,10 +10,10 @@ import ( utilerrors "k8s.io/apimachinery/pkg/util/errors" clusterv1beta2 "open-cluster-management.io/api/cluster/v1beta2" proxyv1alpha1 "open-cluster-management.io/cluster-proxy/pkg/apis/proxy/v1alpha1" - "open-cluster-management.io/cluster-proxy/pkg/proxyserver/operator/eventhandler" "open-cluster-management.io/cluster-proxy/pkg/util" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" + "sigs.k8s.io/controller-runtime/pkg/handler" "sigs.k8s.io/controller-runtime/pkg/reconcile" ) @@ -35,13 +35,35 @@ func (c *ServiceResolverReconciler) SetupWithManager(mgr ctrl.Manager) error { For(&proxyv1alpha1.ManagedProxyServiceResolver{}). Watches( &proxyv1alpha1.ManagedProxyServiceResolver{}, - &eventhandler.ProxyServiceResolverHandler{}, + handler.EnqueueRequestsFromMapFunc(func(ctx context.Context, object client.Object) []reconcile.Request { + return []reconcile.Request{ + {NamespacedName: types.NamespacedName{Name: object.GetName()}}, + } + }), ). Watches( &clusterv1beta2.ManagedClusterSet{}, - &eventhandler.ClustersetHandler{ - Client: mgr.GetClient(), - }, + handler.EnqueueRequestsFromMapFunc(func(ctx context.Context, object client.Object) []reconcile.Request { + var reqs []reconcile.Request + // Check whether the clusterset is related with any managedproxyserviceresolver. + mpsrList := &proxyv1alpha1.ManagedProxyServiceResolverList{} + err := mgr.GetClient().List(context.TODO(), mpsrList, &client.ListOptions{}) + if err != nil { + return reqs + } + for _, mpsr := range mpsrList.Items { + if !util.IsServiceResolverLegal(&mpsr) { + continue + } + if mpsr.Spec.ManagedClusterSelector.ManagedClusterSet.Name == object.GetName() { + req := reconcile.Request{} + req.Name = mpsr.Name + reqs = append(reqs, req) + break + } + } + return reqs + }), ). Complete(c) } diff --git a/pkg/proxyserver/operator/eventhandler/clusterset_event.go b/pkg/proxyserver/operator/eventhandler/clusterset_event.go deleted file mode 100644 index e1da74a4..00000000 --- a/pkg/proxyserver/operator/eventhandler/clusterset_event.go +++ /dev/null @@ -1,62 +0,0 @@ -package eventhandler - -import ( - "context" - - "k8s.io/client-go/util/workqueue" - clusterv1beta2 "open-cluster-management.io/api/cluster/v1beta2" - proxyv1alpha1 "open-cluster-management.io/cluster-proxy/pkg/apis/proxy/v1alpha1" - "open-cluster-management.io/cluster-proxy/pkg/util" - "sigs.k8s.io/controller-runtime/pkg/client" - "sigs.k8s.io/controller-runtime/pkg/event" - "sigs.k8s.io/controller-runtime/pkg/handler" - "sigs.k8s.io/controller-runtime/pkg/reconcile" -) - -var _ handler.EventHandler = &ClustersetHandler{} - -type ClustersetHandler struct { - client.Client -} - -func (m ClustersetHandler) Create(_ context.Context, event event.CreateEvent, limitingInterface workqueue.RateLimitingInterface) { - clusterset := event.Object.(*clusterv1beta2.ManagedClusterSet) - m.findClusterProxyAddon(clusterset, limitingInterface) -} - -func (m ClustersetHandler) Update(_ context.Context, event event.UpdateEvent, limitingInterface workqueue.RateLimitingInterface) { - clusterset := event.ObjectNew.(*clusterv1beta2.ManagedClusterSet) - m.findClusterProxyAddon(clusterset, limitingInterface) -} - -func (m ClustersetHandler) Delete(_ context.Context, event event.DeleteEvent, limitingInterface workqueue.RateLimitingInterface) { - clusterset := event.Object.(*clusterv1beta2.ManagedClusterSet) - m.findClusterProxyAddon(clusterset, limitingInterface) -} - -func (m ClustersetHandler) Generic(_ context.Context, event event.GenericEvent, limitingInterface workqueue.RateLimitingInterface) { - clusterset := event.Object.(*clusterv1beta2.ManagedClusterSet) - m.findClusterProxyAddon(clusterset, limitingInterface) -} - -// findClusterProxyAddon will triger clustermanagementaddon on all managed clusters to reconcile. -func (m *ClustersetHandler) findClusterProxyAddon(clusterset *clusterv1beta2.ManagedClusterSet, limitingInterface workqueue.RateLimitingInterface) { - var err error - // Check whether the clusterset is related with any managedproxyserviceresolver. - mpsrList := &proxyv1alpha1.ManagedProxyServiceResolverList{} - err = m.Client.List(context.TODO(), mpsrList, &client.ListOptions{}) - if err != nil { - return - } - for _, mpsr := range mpsrList.Items { - if !util.IsServiceResolverLegal(&mpsr) { - continue - } - if mpsr.Spec.ManagedClusterSelector.ManagedClusterSet.Name == clusterset.Name { - req := reconcile.Request{} - req.Name = mpsr.Name - limitingInterface.Add(req) - break - } - } -} diff --git a/pkg/proxyserver/operator/eventhandler/proxyserviceresolver_event.go b/pkg/proxyserver/operator/eventhandler/proxyserviceresolver_event.go deleted file mode 100644 index 6f61e18a..00000000 --- a/pkg/proxyserver/operator/eventhandler/proxyserviceresolver_event.go +++ /dev/null @@ -1,38 +0,0 @@ -package eventhandler - -import ( - "context" - - "k8s.io/client-go/util/workqueue" - "sigs.k8s.io/controller-runtime/pkg/event" - "sigs.k8s.io/controller-runtime/pkg/handler" - "sigs.k8s.io/controller-runtime/pkg/reconcile" -) - -var _ handler.EventHandler = &ProxyServiceResolverHandler{} - -type ProxyServiceResolverHandler struct{} - -func (m ProxyServiceResolverHandler) Create(_ context.Context, event event.CreateEvent, queue workqueue.RateLimitingInterface) { - req := reconcile.Request{} - req.Name = event.Object.GetName() - queue.Add(req) -} - -func (m ProxyServiceResolverHandler) Update(_ context.Context, event event.UpdateEvent, queue workqueue.RateLimitingInterface) { - req := reconcile.Request{} - req.Name = event.ObjectNew.GetName() - queue.Add(req) -} - -func (m ProxyServiceResolverHandler) Delete(_ context.Context, event event.DeleteEvent, queue workqueue.RateLimitingInterface) { - req := reconcile.Request{} - req.Name = event.Object.GetName() - queue.Add(req) -} - -func (m ProxyServiceResolverHandler) Generic(_ context.Context, event event.GenericEvent, queue workqueue.RateLimitingInterface) { - req := reconcile.Request{} - req.Name = event.Object.GetName() - queue.Add(req) -}