From d1253018de10e9ac381d0b5921feb34b0aa63ccf Mon Sep 17 00:00:00 2001 From: huangyanfeng Date: Tue, 16 Jan 2024 13:42:49 +0800 Subject: [PATCH] search add check member cluster api enable Signed-off-by: huangyanfeng --- pkg/search/controller.go | 14 ++++++++++++-- pkg/search/proxy/controller.go | 10 ++++++++++ pkg/search/proxy/controller_test.go | 19 +++++++++++++++++++ 3 files changed, 41 insertions(+), 2 deletions(-) diff --git a/pkg/search/controller.go b/pkg/search/controller.go index cdb6e15f7800..61c25c4d1c59 100644 --- a/pkg/search/controller.go +++ b/pkg/search/controller.go @@ -46,6 +46,7 @@ import ( "github.com/karmada-io/karmada/pkg/util/fedinformer" "github.com/karmada-io/karmada/pkg/util/fedinformer/genericmanager" "github.com/karmada-io/karmada/pkg/util/gclient" + "github.com/karmada-io/karmada/pkg/util/helper" "github.com/karmada-io/karmada/pkg/util/restmapper" ) @@ -401,10 +402,18 @@ func (c *Controller) doCacheCluster(cluster string) error { sci := c.InformerManager.GetSingleClusterManager(cluster) for gvr := range cr.resources { + gvk, err := c.restMapper.KindFor(gvr) + if err != nil { + klog.Errorf("Failed to get gvk: %v", err) + continue + } + if !helper.IsAPIEnabled(cls.Status.APIEnablements, gvk.GroupVersion().String(), gvk.Kind) { + klog.Warningf("Resource %s is not enabled for cluster %s", gvr.String(), cluster) + continue + } klog.Infof("Add informer for %s, %v", cluster, gvr) sci.ForResource(gvr, handler) } - klog.Infof("Start informer for %s", cluster) sci.Start() _ = sci.WaitForCacheSync() @@ -486,7 +495,8 @@ func (c *Controller) updateCluster(oldObj, curObj interface{}) { c.queue.Add(curCluster.GetName()) } - if !reflect.DeepEqual(curCluster.Spec, oldCluster.Spec) || !reflect.DeepEqual(curCluster.Labels, oldCluster.Labels) { + if !reflect.DeepEqual(curCluster.Spec, oldCluster.Spec) || !reflect.DeepEqual(curCluster.Labels, oldCluster.Labels) || + !reflect.DeepEqual(curCluster.Status.APIEnablements, oldCluster.Status.APIEnablements) { c.queue.Add(curCluster.GetName()) } } diff --git a/pkg/search/proxy/controller.go b/pkg/search/proxy/controller.go index dc4bcbc33cc6..938a9a2447c2 100644 --- a/pkg/search/proxy/controller.go +++ b/pkg/search/proxy/controller.go @@ -47,6 +47,7 @@ import ( pluginruntime "github.com/karmada-io/karmada/pkg/search/proxy/framework/runtime" "github.com/karmada-io/karmada/pkg/search/proxy/store" "github.com/karmada-io/karmada/pkg/util" + "github.com/karmada-io/karmada/pkg/util/helper" "github.com/karmada-io/karmada/pkg/util/lifted" "github.com/karmada-io/karmada/pkg/util/restmapper" ) @@ -223,6 +224,15 @@ func (ctl *Controller) reconcile(util.QueueKey) error { } for resource, multiNS := range matchedResources { + gvk, err := ctl.restMapper.KindFor(resource) + if err != nil { + klog.Errorf("Failed to get gvk: %v", err) + continue + } + if !helper.IsAPIEnabled(cluster.Status.APIEnablements, gvk.GroupVersion().String(), gvk.Kind) { + klog.Warningf("Resource %s is not enabled for cluster %s", resource.String(), cluster) + continue + } resourcesByClusters[cluster.Name][resource] = multiNS } } diff --git a/pkg/search/proxy/controller_test.go b/pkg/search/proxy/controller_test.go index 6cae6b5c0436..41eedbde96fd 100644 --- a/pkg/search/proxy/controller_test.go +++ b/pkg/search/proxy/controller_test.go @@ -510,9 +510,28 @@ func TestController_Connect_Error(t *testing.T) { func newCluster(name string) *clusterv1alpha1.Cluster { c := &clusterv1alpha1.Cluster{} + clusterEnablements := []clusterv1alpha1.APIEnablement{ + { + GroupVersion: "v1", + Resources: []clusterv1alpha1.APIResource{ + { + Kind: "Pod", + }, + }, + }, + { + GroupVersion: "v1", + Resources: []clusterv1alpha1.APIResource{ + { + Kind: "Node", + }, + }, + }, + } c.Name = name conditions := make([]metav1.Condition, 0, 1) conditions = append(conditions, util.NewCondition(clusterv1alpha1.ClusterConditionReady, "", "", metav1.ConditionTrue)) c.Status.Conditions = conditions + c.Status.APIEnablements = clusterEnablements return c }