From e4792e4b83ca5911f0eb01514f4ed9d76fa935d1 Mon Sep 17 00:00:00 2001 From: Jian Qiu Date: Wed, 28 Jun 2023 21:59:19 +0800 Subject: [PATCH] Refactor to use common queue/filter funcs (#197) Signed-off-by: Jian Qiu --- .../addonconfiguration/controller.go | 29 ++-- .../controllers/addonmanagement/controller.go | 8 +- .../controllers/addonowner/controller.go | 13 +- .../addonprogressing/controller.go | 6 +- .../controllers/addontemplate/controller.go | 7 +- .../controller.go | 8 +- pkg/common/queue/queuekey.go | 70 ++++++++++ pkg/common/queue/queuekey_test.go | 128 ++++++++++++++++++ .../certrotation_controller.go | 8 +- .../clustermanager_controller.go | 15 +- .../crd_status_controller.go | 7 +- .../migration_controller.go | 6 +- .../clustermanager_status_controller.go | 7 +- .../addonsecretcontroller/controller.go | 15 +- .../klusterlet_cleanup_controller.go | 7 +- .../klusterlet_controller.go | 7 +- .../klusterlet_ssar_controller.go | 7 +- .../klusterlet_status_controller.go | 7 +- .../scheduling/cluster_event_handler_test.go | 36 +++-- .../controllers/scheduling/enqueue_test.go | 6 +- .../controllers/scheduling/schedule_test.go | 62 +++++---- .../scheduling/scheduling_controller.go | 37 ++--- .../scheduling/scheduling_controller_test.go | 39 +++--- .../hub/addon/discovery_controller.go | 16 +-- .../hub/addon/healthcheck_controller.go | 7 +- .../hub/clusterrole/controller.go | 10 +- pkg/registration/hub/csr/controller.go | 8 +- pkg/registration/hub/lease/controller.go | 30 +--- .../hub/managedcluster/controller.go | 26 +--- .../hub/managedclusterset/controller.go | 7 +- .../default_managedclusterset_controller.go | 20 +-- .../global_managedclusterset_controller.go | 20 +-- .../managedclustersetbinding/controller.go | 7 +- .../hub/rbacfinalizerdeletion/controller.go | 7 +- pkg/registration/hub/taint/controller.go | 7 +- .../spoke/addon/registration_controller.go | 9 +- .../spoke/registration/secret_controller.go | 10 +- .../manifestworkreplicaset_controller.go | 23 +--- .../manifestworkreplicaset_index.go | 7 +- .../manifestworkreplicaset_index_test.go | 2 +- .../appliedmanifestwork_controller.go | 8 +- .../add_finalizer_controller.go | 8 +- ...appliedmanifestwork_finalize_controller.go | 9 +- .../manifestwork_finalize_controller.go | 8 +- ...nmanaged_appliedmanifestwork_controller.go | 9 +- .../manifestwork_controller.go | 6 +- .../availablestatus_controller.go | 7 +- 47 files changed, 411 insertions(+), 395 deletions(-) create mode 100644 pkg/common/queue/queuekey.go create mode 100644 pkg/common/queue/queuekey_test.go diff --git a/pkg/addon/controllers/addonconfiguration/controller.go b/pkg/addon/controllers/addonconfiguration/controller.go index f0ac5f3f5..94a5e513d 100644 --- a/pkg/addon/controllers/addonconfiguration/controller.go +++ b/pkg/addon/controllers/addonconfiguration/controller.go @@ -7,7 +7,6 @@ import ( "github.com/openshift/library-go/pkg/operator/events" "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/labels" - "k8s.io/apimachinery/pkg/runtime" utilerrors "k8s.io/apimachinery/pkg/util/errors" "k8s.io/client-go/tools/cache" "k8s.io/klog/v2" @@ -22,6 +21,7 @@ import ( clusterv1beta1 "open-cluster-management.io/api/cluster/v1beta1" "open-cluster-management.io/ocm/pkg/common/patcher" + "open-cluster-management.io/ocm/pkg/common/queue" ) // addonConfigurationController is a controller to update configuration of mca with the following order @@ -64,6 +64,8 @@ func NewAddonConfigurationController( addonClient: addonClient, clusterManagementAddonLister: clusterManagementAddonInformers.Lister(), managedClusterAddonIndexer: addonInformers.Informer().GetIndexer(), + placementLister: placementInformer.Lister(), + placementDecisionLister: placementDecisionInformer.Lister(), addonFilterFunc: addonFilterFunc, } @@ -79,27 +81,14 @@ func NewAddonConfigurationController( } controllerFactory := factory.New().WithFilteredEventsInformersQueueKeysFunc( - func(obj runtime.Object) []string { - key, _ := cache.DeletionHandlingMetaNamespaceKeyFunc(obj) - return []string{key} - }, + queue.QueueKeyByMetaNamespaceName, c.addonFilterFunc, - clusterManagementAddonInformers.Informer()).WithInformersQueueKeysFunc( - func(obj runtime.Object) []string { - key, _ := cache.DeletionHandlingMetaNamespaceKeyFunc(obj) - return []string{key} - }, - addonInformers.Informer()) - - // This is to handle the case the self managed addon-manager does not have placementInformer/placementDecisionInformer. - // we will not consider installStrategy related placement for self managed addon-manager. - if placementInformer != nil && placementDecisionInformer != nil { - controllerFactory = controllerFactory.WithInformersQueueKeysFunc( + clusterManagementAddonInformers.Informer()). + WithInformersQueueKeysFunc(queue.QueueKeyByMetaNamespaceName, addonInformers.Informer()). + WithInformersQueueKeysFunc( index.ClusterManagementAddonByPlacementDecisionQueueKey(clusterManagementAddonInformers), placementDecisionInformer.Informer()). - WithInformersQueueKeysFunc(index.ClusterManagementAddonByPlacementQueueKey(clusterManagementAddonInformers), placementInformer.Informer()) - c.placementLister = placementInformer.Lister() - c.placementDecisionLister = placementDecisionInformer.Lister() - } + WithInformersQueueKeysFunc( + index.ClusterManagementAddonByPlacementQueueKey(clusterManagementAddonInformers), placementInformer.Informer()) return controllerFactory.WithSync(c.sync).ToController("addon-configuration-controller", recorder) } diff --git a/pkg/addon/controllers/addonmanagement/controller.go b/pkg/addon/controllers/addonmanagement/controller.go index 516e36895..551c58689 100644 --- a/pkg/addon/controllers/addonmanagement/controller.go +++ b/pkg/addon/controllers/addonmanagement/controller.go @@ -6,7 +6,6 @@ import ( "github.com/openshift/library-go/pkg/controller/factory" "github.com/openshift/library-go/pkg/operator/events" "k8s.io/apimachinery/pkg/api/errors" - "k8s.io/apimachinery/pkg/runtime" utilerrors "k8s.io/apimachinery/pkg/util/errors" "k8s.io/client-go/tools/cache" "k8s.io/klog/v2" @@ -17,6 +16,8 @@ import ( addoninformerv1alpha1 "open-cluster-management.io/api/client/addon/informers/externalversions/addon/v1alpha1" addonlisterv1alpha1 "open-cluster-management.io/api/client/addon/listers/addon/v1alpha1" clusterinformersv1beta1 "open-cluster-management.io/api/client/cluster/informers/externalversions/cluster/v1beta1" + + "open-cluster-management.io/ocm/pkg/common/queue" ) type addonManagementController struct { @@ -65,10 +66,7 @@ func NewAddonManagementController( } return factory.New().WithInformersQueueKeysFunc( - func(obj runtime.Object) []string { - key, _ := cache.DeletionHandlingMetaNamespaceKeyFunc(obj) - return []string{key} - }, + queue.QueueKeyByMetaNamespaceName, addonInformers.Informer(), clusterManagementAddonInformers.Informer()). WithInformersQueueKeysFunc( index.ClusterManagementAddonByPlacementDecisionQueueKey( diff --git a/pkg/addon/controllers/addonowner/controller.go b/pkg/addon/controllers/addonowner/controller.go index 36d69d847..148749f3a 100644 --- a/pkg/addon/controllers/addonowner/controller.go +++ b/pkg/addon/controllers/addonowner/controller.go @@ -7,7 +7,6 @@ import ( "github.com/openshift/library-go/pkg/operator/events" "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/runtime" "k8s.io/client-go/tools/cache" "k8s.io/klog/v2" @@ -16,6 +15,8 @@ import ( addonv1alpha1client "open-cluster-management.io/api/client/addon/clientset/versioned" addoninformerv1alpha1 "open-cluster-management.io/api/client/addon/informers/externalversions/addon/v1alpha1" addonlisterv1alpha1 "open-cluster-management.io/api/client/addon/listers/addon/v1alpha1" + + "open-cluster-management.io/ocm/pkg/common/queue" ) const UnsupportedConfigurationType = "UnsupportedConfiguration" @@ -45,16 +46,10 @@ func NewAddonOwnerController( return factory.New(). WithFilteredEventsInformersQueueKeysFunc( - func(obj runtime.Object) []string { - key, _ := cache.DeletionHandlingMetaNamespaceKeyFunc(obj) - return []string{key} - }, + queue.QueueKeyByMetaNamespaceName, c.addonFilterFunc, clusterManagementAddonInformers.Informer()). WithInformersQueueKeysFunc( - func(obj runtime.Object) []string { - key, _ := cache.DeletionHandlingMetaNamespaceKeyFunc(obj) - return []string{key} - }, + queue.QueueKeyByMetaNamespaceName, addonInformers.Informer()). WithSync(c.sync). ToController("addon-owner-controller", recorder) diff --git a/pkg/addon/controllers/addonprogressing/controller.go b/pkg/addon/controllers/addonprogressing/controller.go index 285ac8a23..53e22c4fe 100644 --- a/pkg/addon/controllers/addonprogressing/controller.go +++ b/pkg/addon/controllers/addonprogressing/controller.go @@ -29,6 +29,7 @@ import ( workapiv1 "open-cluster-management.io/api/work/v1" "open-cluster-management.io/ocm/pkg/common/patcher" + "open-cluster-management.io/ocm/pkg/common/queue" ) const ( @@ -64,10 +65,7 @@ func NewAddonProgressingController( } return factory.New().WithInformersQueueKeysFunc( - func(obj runtime.Object) []string { - key, _ := cache.DeletionHandlingMetaNamespaceKeyFunc(obj) - return []string{key} - }, + queue.QueueKeyByMetaNamespaceName, addonInformers.Informer(), clusterManagementAddonInformers.Informer()). // TODO: consider hosted manifestwork WithInformersQueueKeysFunc( diff --git a/pkg/addon/controllers/addontemplate/controller.go b/pkg/addon/controllers/addontemplate/controller.go index e0b3b0caa..0ebcc4350 100644 --- a/pkg/addon/controllers/addontemplate/controller.go +++ b/pkg/addon/controllers/addontemplate/controller.go @@ -8,7 +8,6 @@ import ( "github.com/openshift/library-go/pkg/operator/events" "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/runtime" utilrand "k8s.io/apimachinery/pkg/util/rand" utilruntime "k8s.io/apimachinery/pkg/util/runtime" "k8s.io/client-go/dynamic/dynamicinformer" @@ -28,6 +27,7 @@ import ( workv1informers "open-cluster-management.io/api/client/work/informers/externalversions" "open-cluster-management.io/ocm/pkg/addon/templateagent" + "open-cluster-management.io/ocm/pkg/common/queue" ) // addonTemplateController monitors ManagedClusterAddOns on hub to get all the in-used addon templates, @@ -81,10 +81,7 @@ func NewAddonTemplateController( c.runControllerFunc = c.runController } return factory.New().WithInformersQueueKeysFunc( - func(obj runtime.Object) []string { - key, _ := cache.DeletionHandlingMetaNamespaceKeyFunc(obj) - return []string{key} - }, + queue.QueueKeyByMetaNamespaceName, addonInformers.Addon().V1alpha1().ClusterManagementAddOns().Informer()). WithSync(c.sync). ToController("addon-template-controller", recorder) diff --git a/pkg/addon/controllers/managementaddoninstallprogression/controller.go b/pkg/addon/controllers/managementaddoninstallprogression/controller.go index 3334f99ec..d8a6b5176 100644 --- a/pkg/addon/controllers/managementaddoninstallprogression/controller.go +++ b/pkg/addon/controllers/managementaddoninstallprogression/controller.go @@ -6,8 +6,6 @@ import ( "github.com/openshift/library-go/pkg/controller/factory" "github.com/openshift/library-go/pkg/operator/events" "k8s.io/apimachinery/pkg/api/errors" - "k8s.io/apimachinery/pkg/api/meta" - "k8s.io/apimachinery/pkg/runtime" "k8s.io/klog/v2" addonv1alpha1 "open-cluster-management.io/api/addon/v1alpha1" @@ -16,6 +14,7 @@ import ( addonlisterv1alpha1 "open-cluster-management.io/api/client/addon/listers/addon/v1alpha1" "open-cluster-management.io/ocm/pkg/common/patcher" + "open-cluster-management.io/ocm/pkg/common/queue" ) // managementAddonInstallProgressionController reconciles instances of clustermanagementaddon the hub @@ -45,10 +44,7 @@ func NewManagementAddonInstallProgressionController( } return factory.New().WithInformersQueueKeysFunc( - func(obj runtime.Object) []string { - accessor, _ := meta.Accessor(obj) - return []string{accessor.GetName()} - }, + queue.QueueKeyByMetaName, addonInformers.Informer(), clusterManagementAddonInformers.Informer()). WithSync(c.sync).ToController("management-addon-status-controller", recorder) diff --git a/pkg/common/queue/queuekey.go b/pkg/common/queue/queuekey.go new file mode 100644 index 000000000..6fe0f948f --- /dev/null +++ b/pkg/common/queue/queuekey.go @@ -0,0 +1,70 @@ +package queue + +import ( + "github.com/openshift/library-go/pkg/controller/factory" + "k8s.io/apimachinery/pkg/api/meta" + "k8s.io/apimachinery/pkg/runtime" + "k8s.io/client-go/tools/cache" +) + +func FileterByLabel(key string) factory.EventFilterFunc { + return func(obj interface{}) bool { + accessor, _ := meta.Accessor(obj) + return len(accessor.GetLabels()) > 0 && len(accessor.GetLabels()[key]) > 0 + } +} + +func FileterByLabelKeyValue(key, value string) factory.EventFilterFunc { + return func(obj interface{}) bool { + accessor, _ := meta.Accessor(obj) + return len(accessor.GetLabels()) > 0 && accessor.GetLabels()[key] == value + } +} + +func FilterByNames(names ...string) factory.EventFilterFunc { + return func(obj interface{}) bool { + accessor, _ := meta.Accessor(obj) + for _, name := range names { + if accessor.GetName() == name { + return true + } + } + return false + } +} + +func UnionFilter(filters ...factory.EventFilterFunc) factory.EventFilterFunc { + return func(obj interface{}) bool { + for _, filter := range filters { + if !filter(obj) { + return false + } + } + return true + } +} + +func QueueKeyByLabel(key string) factory.ObjectQueueKeysFunc { + return func(obj runtime.Object) []string { + accessor, _ := meta.Accessor(obj) + if len(accessor.GetLabels()) == 0 || len(accessor.GetLabels()[key]) == 0 { + return []string{} + } + return []string{accessor.GetLabels()[key]} + } +} + +func QueueKeyByMetaName(obj runtime.Object) []string { + accessor, _ := meta.Accessor(obj) + return []string{accessor.GetName()} +} + +func QueueKeyByMetaNamespace(obj runtime.Object) []string { + accessor, _ := meta.Accessor(obj) + return []string{accessor.GetNamespace()} +} + +func QueueKeyByMetaNamespaceName(obj runtime.Object) []string { + key, _ := cache.MetaNamespaceKeyFunc(obj) + return []string{key} +} diff --git a/pkg/common/queue/queuekey_test.go b/pkg/common/queue/queuekey_test.go new file mode 100644 index 000000000..63295a2e5 --- /dev/null +++ b/pkg/common/queue/queuekey_test.go @@ -0,0 +1,128 @@ +package queue + +import ( + "reflect" + "testing" + + "github.com/openshift/library-go/pkg/controller/factory" + corev1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/runtime" +) + +func TestFileter(t *testing.T) { + tc := []struct { + name string + filter factory.EventFilterFunc + object runtime.Object + filtered bool + }{ + { + name: "filter by label with no label", + filter: FileterByLabel("test"), + object: &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: "test", Labels: map[string]string{"test1": "value1"}}}, + filtered: false, + }, + { + name: "filter by label with label", + filter: FileterByLabel("test"), + object: &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: "test", Labels: map[string]string{"test": "value1"}}}, + filtered: true, + }, + { + name: "filter by label with incorrect labelkeyvalue", + filter: FileterByLabelKeyValue("test", "value"), + object: &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: "test", Labels: map[string]string{"test": "value1"}}}, + filtered: false, + }, + { + name: "filter by label with incorrect labelkeyvalue", + filter: FileterByLabelKeyValue("test", "value1"), + object: &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: "test", Labels: map[string]string{"test": "value1"}}}, + filtered: true, + }, + { + name: "filter by unmatched name", + filter: FilterByNames("test"), + object: &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: "test1"}}, + filtered: false, + }, + { + name: "filter by matched name", + filter: FilterByNames("test", "test1"), + object: &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: "test1"}}, + filtered: true, + }, + { + name: "uniion filter by unmatched", + filter: UnionFilter(FilterByNames("test"), FileterByLabel("test")), + object: &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: "test"}}, + filtered: false, + }, + { + name: "uniion filter by matched", + filter: UnionFilter(FilterByNames("test"), FileterByLabel("test")), + object: &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: "test", Labels: map[string]string{"test": "value"}}}, + filtered: true, + }, + } + + for _, c := range tc { + t.Run(c.name, func(t *testing.T) { + actual := c.filter(c.object) + if c.filtered != actual { + t.Errorf("expect filter %v, but got %v", c.filtered, actual) + } + }) + } +} + +func TestQueueKey(t *testing.T) { + tc := []struct { + name string + queueKeyFunc factory.ObjectQueueKeysFunc + object runtime.Object + expecteKey []string + }{ + { + name: "by name", + queueKeyFunc: QueueKeyByMetaName, + object: &corev1.Secret{ObjectMeta: metav1.ObjectMeta{Name: "test", Namespace: "testns"}}, + expecteKey: []string{"test"}, + }, + { + name: "by namespace", + queueKeyFunc: QueueKeyByMetaNamespace, + object: &corev1.Secret{ObjectMeta: metav1.ObjectMeta{Name: "test", Namespace: "testns"}}, + expecteKey: []string{"testns"}, + }, + { + name: "by namespace name", + queueKeyFunc: QueueKeyByMetaNamespaceName, + object: &corev1.Secret{ObjectMeta: metav1.ObjectMeta{Name: "test", Namespace: "testns"}}, + expecteKey: []string{"testns/test"}, + }, + { + name: "by emptey label", + queueKeyFunc: QueueKeyByLabel("testlabel"), + object: &corev1.Secret{ObjectMeta: metav1.ObjectMeta{Name: "test", Namespace: "testns"}}, + expecteKey: []string{}, + }, + { + name: "by matched label", + queueKeyFunc: QueueKeyByLabel("testlabel"), + object: &corev1.Secret{ObjectMeta: metav1.ObjectMeta{ + Name: "test", Namespace: "testns", Labels: map[string]string{"testlabel": "value"}}}, + expecteKey: []string{"value"}, + }, + } + + for _, c := range tc { + t.Run(c.name, func(t *testing.T) { + actual := c.queueKeyFunc(c.object) + if !reflect.DeepEqual(c.expecteKey, actual) { + t.Errorf("expect key %v, but got %v", c.expecteKey, actual) + } + }) + } +} diff --git a/pkg/operator/operators/clustermanager/controllers/certrotationcontroller/certrotation_controller.go b/pkg/operator/operators/clustermanager/controllers/certrotationcontroller/certrotation_controller.go index d579d8d5f..f65f1ebfd 100644 --- a/pkg/operator/operators/clustermanager/controllers/certrotationcontroller/certrotation_controller.go +++ b/pkg/operator/operators/clustermanager/controllers/certrotationcontroller/certrotation_controller.go @@ -10,10 +10,8 @@ import ( errorhelpers "github.com/openshift/library-go/pkg/operator/v1helpers" operatorhelpers "github.com/openshift/library-go/pkg/operator/v1helpers" "k8s.io/apimachinery/pkg/api/errors" - "k8s.io/apimachinery/pkg/api/meta" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/labels" - "k8s.io/apimachinery/pkg/runtime" corev1informers "k8s.io/client-go/informers/core/v1" "k8s.io/client-go/kubernetes" "k8s.io/klog/v2" @@ -22,6 +20,7 @@ import ( operatorlister "open-cluster-management.io/api/client/operator/listers/operator/v1" operatorv1 "open-cluster-management.io/api/operator/v1" + "open-cluster-management.io/ocm/pkg/common/queue" "open-cluster-management.io/ocm/pkg/operator/certrotation" "open-cluster-management.io/ocm/pkg/operator/helpers" ) @@ -79,10 +78,7 @@ func NewCertRotationController( return factory.New(). ResyncEvery(ResyncInterval). WithSync(c.sync). - WithInformersQueueKeyFunc(func(obj runtime.Object) string { - accessor, _ := meta.Accessor(obj) - return accessor.GetName() - }, clusterManagerInformer.Informer()). + WithInformersQueueKeysFunc(queue.QueueKeyByMetaName, clusterManagerInformer.Informer()). WithInformersQueueKeyFunc(helpers.ClusterManagerQueueKeyFunc(c.clusterManagerLister), configMapInformer.Informer(), secretInformers[helpers.SignerSecret].Informer(), diff --git a/pkg/operator/operators/clustermanager/controllers/clustermanagercontroller/clustermanager_controller.go b/pkg/operator/operators/clustermanager/controllers/clustermanagercontroller/clustermanager_controller.go index 90324349e..0fdb3fb91 100644 --- a/pkg/operator/operators/clustermanager/controllers/clustermanagercontroller/clustermanager_controller.go +++ b/pkg/operator/operators/clustermanager/controllers/clustermanagercontroller/clustermanager_controller.go @@ -15,7 +15,6 @@ import ( "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/api/meta" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/runtime" utilerrors "k8s.io/apimachinery/pkg/util/errors" appsinformer "k8s.io/client-go/informers/apps/v1" corev1informers "k8s.io/client-go/informers/core/v1" @@ -33,6 +32,7 @@ import ( "open-cluster-management.io/ocm/manifests" "open-cluster-management.io/ocm/pkg/common/patcher" + "open-cluster-management.io/ocm/pkg/common/queue" "open-cluster-management.io/ocm/pkg/operator/helpers" ) @@ -104,18 +104,9 @@ func NewClusterManagerController( WithInformersQueueKeyFunc(helpers.ClusterManagerDeploymentQueueKeyFunc(controller.clusterManagerLister), deploymentInformer.Informer()). WithFilteredEventsInformersQueueKeyFunc( helpers.ClusterManagerConfigmapQueueKeyFunc(controller.clusterManagerLister), - func(obj interface{}) bool { - accessor, _ := meta.Accessor(obj) - if name := accessor.GetName(); name != helpers.CaBundleConfigmap { - return false - } - return true - }, + queue.FilterByNames(helpers.CaBundleConfigmap), configMapInformer.Informer()). - WithInformersQueueKeyFunc(func(obj runtime.Object) string { - accessor, _ := meta.Accessor(obj) - return accessor.GetName() - }, clusterManagerInformer.Informer()). + WithInformersQueueKeysFunc(queue.QueueKeyByMetaName, clusterManagerInformer.Informer()). ToController("ClusterManagerController", recorder) } diff --git a/pkg/operator/operators/clustermanager/controllers/crdstatuccontroller/crd_status_controller.go b/pkg/operator/operators/clustermanager/controllers/crdstatuccontroller/crd_status_controller.go index 88ae84ae0..1d934ccc1 100644 --- a/pkg/operator/operators/clustermanager/controllers/crdstatuccontroller/crd_status_controller.go +++ b/pkg/operator/operators/clustermanager/controllers/crdstatuccontroller/crd_status_controller.go @@ -14,7 +14,6 @@ import ( "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/api/meta" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/runtime" "k8s.io/client-go/kubernetes" "k8s.io/client-go/rest" "k8s.io/klog/v2" @@ -22,6 +21,7 @@ import ( operatorinformer "open-cluster-management.io/api/client/operator/informers/externalversions/operator/v1" operatorlister "open-cluster-management.io/api/client/operator/listers/operator/v1" + "open-cluster-management.io/ocm/pkg/common/queue" "open-cluster-management.io/ocm/pkg/operator/helpers" "open-cluster-management.io/ocm/pkg/operator/operators/clustermanager/controllers/migrationcontroller" ) @@ -56,10 +56,7 @@ func NewCRDStatusController( } return factory.New().WithSync(controller.sync). - WithInformersQueueKeyFunc(func(obj runtime.Object) string { - accessor, _ := meta.Accessor(obj) - return accessor.GetName() - }, clusterManagerInformer.Informer()). + WithInformersQueueKeysFunc(queue.QueueKeyByMetaName, clusterManagerInformer.Informer()). ToController("CRDStatusController", recorder) } diff --git a/pkg/operator/operators/clustermanager/controllers/migrationcontroller/migration_controller.go b/pkg/operator/operators/clustermanager/controllers/migrationcontroller/migration_controller.go index 895969fc0..09795f661 100644 --- a/pkg/operator/operators/clustermanager/controllers/migrationcontroller/migration_controller.go +++ b/pkg/operator/operators/clustermanager/controllers/migrationcontroller/migration_controller.go @@ -32,6 +32,7 @@ import ( "open-cluster-management.io/ocm/manifests" "open-cluster-management.io/ocm/pkg/common/patcher" + "open-cluster-management.io/ocm/pkg/common/queue" "open-cluster-management.io/ocm/pkg/operator/helpers" ) @@ -88,10 +89,7 @@ func NewCRDMigrationController( } return factory.New().WithSync(controller.sync). - WithInformersQueueKeyFunc(func(obj runtime.Object) string { - accessor, _ := meta.Accessor(obj) - return accessor.GetName() - }, clusterManagerInformer.Informer()). + WithInformersQueueKeysFunc(queue.QueueKeyByMetaName, clusterManagerInformer.Informer()). ToController("CRDMigrationController", recorder) } diff --git a/pkg/operator/operators/clustermanager/controllers/statuscontroller/clustermanager_status_controller.go b/pkg/operator/operators/clustermanager/controllers/statuscontroller/clustermanager_status_controller.go index 70455ceee..36c3d9487 100644 --- a/pkg/operator/operators/clustermanager/controllers/statuscontroller/clustermanager_status_controller.go +++ b/pkg/operator/operators/clustermanager/controllers/statuscontroller/clustermanager_status_controller.go @@ -9,7 +9,6 @@ import ( "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/api/meta" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/runtime" appsinformer "k8s.io/client-go/informers/apps/v1" appslister "k8s.io/client-go/listers/apps/v1" "k8s.io/klog/v2" @@ -20,6 +19,7 @@ import ( operatorapiv1 "open-cluster-management.io/api/operator/v1" "open-cluster-management.io/ocm/pkg/common/patcher" + "open-cluster-management.io/ocm/pkg/common/queue" "open-cluster-management.io/ocm/pkg/operator/helpers" ) @@ -52,10 +52,7 @@ func NewClusterManagerStatusController( return factory.New().WithSync(controller.sync). WithInformersQueueKeyFunc( helpers.ClusterManagerDeploymentQueueKeyFunc(controller.clusterManagerLister), deploymentInformer.Informer()). - WithInformersQueueKeyFunc(func(obj runtime.Object) string { - accessor, _ := meta.Accessor(obj) - return accessor.GetName() - }, clusterManagerInformer.Informer()). + WithInformersQueueKeysFunc(queue.QueueKeyByMetaName, clusterManagerInformer.Informer()). ToController("ClusterManagerStatusController", recorder) } diff --git a/pkg/operator/operators/klusterlet/controllers/addonsecretcontroller/controller.go b/pkg/operator/operators/klusterlet/controllers/addonsecretcontroller/controller.go index 7a30de352..15eb6e7b6 100644 --- a/pkg/operator/operators/klusterlet/controllers/addonsecretcontroller/controller.go +++ b/pkg/operator/operators/klusterlet/controllers/addonsecretcontroller/controller.go @@ -5,13 +5,12 @@ import ( "github.com/openshift/library-go/pkg/controller/factory" "github.com/openshift/library-go/pkg/operator/events" - corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/runtime" coreinformer "k8s.io/client-go/informers/core/v1" "k8s.io/client-go/kubernetes" + "open-cluster-management.io/ocm/pkg/common/queue" "open-cluster-management.io/ocm/pkg/operator/helpers" ) @@ -40,14 +39,10 @@ func NewAddonPullImageSecretController(kubeClient kubernetes.Interface, operator kubeClient: kubeClient, recorder: recorder, } - return factory.New().WithFilteredEventsInformersQueueKeyFunc(func(o runtime.Object) string { - namespace := o.(*corev1.Namespace) - return namespace.GetName() - }, func(obj interface{}) bool { - // if obj has the label, return true - namespace := obj.(*corev1.Namespace) - return namespace.Labels[addonInstallNamespaceLabelKey] == "true" - }, namespaceInformer.Informer()).WithSync(ac.sync).ToController("AddonPullImageSecretController", recorder) + return factory.New().WithFilteredEventsInformersQueueKeysFunc( + queue.QueueKeyByMetaName, + queue.FileterByLabelKeyValue(addonInstallNamespaceLabelKey, "true"), + namespaceInformer.Informer()).WithSync(ac.sync).ToController("AddonPullImageSecretController", recorder) } func (c *addonPullImageSecretController) sync(ctx context.Context, controllerContext factory.SyncContext) error { diff --git a/pkg/operator/operators/klusterlet/controllers/klusterletcontroller/klusterlet_cleanup_controller.go b/pkg/operator/operators/klusterlet/controllers/klusterletcontroller/klusterlet_cleanup_controller.go index 13ee424f2..e7b615940 100644 --- a/pkg/operator/operators/klusterlet/controllers/klusterletcontroller/klusterlet_cleanup_controller.go +++ b/pkg/operator/operators/klusterlet/controllers/klusterletcontroller/klusterlet_cleanup_controller.go @@ -12,7 +12,6 @@ import ( "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/api/meta" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/runtime" utilerrors "k8s.io/apimachinery/pkg/util/errors" "k8s.io/apimachinery/pkg/util/version" appsinformer "k8s.io/client-go/informers/apps/v1" @@ -28,6 +27,7 @@ import ( "open-cluster-management.io/ocm/manifests" "open-cluster-management.io/ocm/pkg/common/patcher" + "open-cluster-management.io/ocm/pkg/common/queue" "open-cluster-management.io/ocm/pkg/operator/helpers" ) @@ -68,10 +68,7 @@ func NewKlusterletCleanupController( secretInformers[helpers.BootstrapHubKubeConfig].Informer(), secretInformers[helpers.ExternalManagedKubeConfig].Informer()). WithInformersQueueKeyFunc(helpers.KlusterletDeploymentQueueKeyFunc(controller.klusterletLister), deploymentInformer.Informer()). - WithInformersQueueKeyFunc(func(obj runtime.Object) string { - accessor, _ := meta.Accessor(obj) - return accessor.GetName() - }, klusterletInformer.Informer()). + WithInformersQueueKeysFunc(queue.QueueKeyByMetaName, klusterletInformer.Informer()). ToController("KlusterletCleanupController", recorder) } diff --git a/pkg/operator/operators/klusterlet/controllers/klusterletcontroller/klusterlet_controller.go b/pkg/operator/operators/klusterlet/controllers/klusterletcontroller/klusterlet_controller.go index 0927aee66..94ee98455 100644 --- a/pkg/operator/operators/klusterlet/controllers/klusterletcontroller/klusterlet_controller.go +++ b/pkg/operator/operators/klusterlet/controllers/klusterletcontroller/klusterlet_controller.go @@ -13,7 +13,6 @@ import ( "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/api/meta" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/runtime" utilerrors "k8s.io/apimachinery/pkg/util/errors" "k8s.io/apimachinery/pkg/util/version" appsinformer "k8s.io/client-go/informers/apps/v1" @@ -30,6 +29,7 @@ import ( operatorapiv1 "open-cluster-management.io/api/operator/v1" "open-cluster-management.io/ocm/pkg/common/patcher" + "open-cluster-management.io/ocm/pkg/common/queue" "open-cluster-management.io/ocm/pkg/operator/helpers" ) @@ -101,10 +101,7 @@ func NewKlusterletController( secretInformers[helpers.BootstrapHubKubeConfig].Informer(), secretInformers[helpers.ExternalManagedKubeConfig].Informer()). WithInformersQueueKeyFunc(helpers.KlusterletDeploymentQueueKeyFunc(controller.klusterletLister), deploymentInformer.Informer()). - WithInformersQueueKeyFunc(func(obj runtime.Object) string { - accessor, _ := meta.Accessor(obj) - return accessor.GetName() - }, klusterletInformer.Informer()). + WithInformersQueueKeysFunc(queue.QueueKeyByMetaName, klusterletInformer.Informer()). ToController("KlusterletController", recorder) } diff --git a/pkg/operator/operators/klusterlet/controllers/ssarcontroller/klusterlet_ssar_controller.go b/pkg/operator/operators/klusterlet/controllers/ssarcontroller/klusterlet_ssar_controller.go index 390f48e37..db4d7bf04 100644 --- a/pkg/operator/operators/klusterlet/controllers/ssarcontroller/klusterlet_ssar_controller.go +++ b/pkg/operator/operators/klusterlet/controllers/ssarcontroller/klusterlet_ssar_controller.go @@ -13,7 +13,6 @@ import ( "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/api/meta" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/runtime" coreinformer "k8s.io/client-go/informers/core/v1" "k8s.io/client-go/kubernetes" "k8s.io/klog/v2" @@ -24,6 +23,7 @@ import ( operatorapiv1 "open-cluster-management.io/api/operator/v1" "open-cluster-management.io/ocm/pkg/common/patcher" + "open-cluster-management.io/ocm/pkg/common/queue" "open-cluster-management.io/ocm/pkg/operator/helpers" ) @@ -70,10 +70,7 @@ func NewKlusterletSSARController( secretInformers[helpers.HubKubeConfig].Informer(), secretInformers[helpers.BootstrapHubKubeConfig].Informer(), secretInformers[helpers.ExternalManagedKubeConfig].Informer()). - WithInformersQueueKeyFunc(func(obj runtime.Object) string { - accessor, _ := meta.Accessor(obj) - return accessor.GetName() - }, klusterletInformer.Informer()). + WithInformersQueueKeysFunc(queue.QueueKeyByMetaName, klusterletInformer.Informer()). ToController("KlusterletSSARController", recorder) } diff --git a/pkg/operator/operators/klusterlet/controllers/statuscontroller/klusterlet_status_controller.go b/pkg/operator/operators/klusterlet/controllers/statuscontroller/klusterlet_status_controller.go index 32c272f15..646a34259 100644 --- a/pkg/operator/operators/klusterlet/controllers/statuscontroller/klusterlet_status_controller.go +++ b/pkg/operator/operators/klusterlet/controllers/statuscontroller/klusterlet_status_controller.go @@ -10,7 +10,6 @@ import ( "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/api/meta" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/runtime" appsinformer "k8s.io/client-go/informers/apps/v1" "k8s.io/client-go/kubernetes" appslister "k8s.io/client-go/listers/apps/v1" @@ -22,6 +21,7 @@ import ( operatorapiv1 "open-cluster-management.io/api/operator/v1" "open-cluster-management.io/ocm/pkg/common/patcher" + "open-cluster-management.io/ocm/pkg/common/queue" "open-cluster-management.io/ocm/pkg/operator/helpers" ) @@ -55,10 +55,7 @@ func NewKlusterletStatusController( } return factory.New().WithSync(controller.sync). WithInformersQueueKeyFunc(helpers.KlusterletDeploymentQueueKeyFunc(controller.klusterletLister), deploymentInformer.Informer()). - WithInformersQueueKeyFunc(func(obj runtime.Object) string { - accessor, _ := meta.Accessor(obj) - return accessor.GetName() - }, klusterletInformer.Informer()). + WithInformersQueueKeysFunc(queue.QueueKeyByMetaName, klusterletInformer.Informer()). ToController("KlusterletStatusController", recorder) } diff --git a/pkg/placement/controllers/scheduling/cluster_event_handler_test.go b/pkg/placement/controllers/scheduling/cluster_event_handler_test.go index 2b6b8212c..a226e6ca3 100644 --- a/pkg/placement/controllers/scheduling/cluster_event_handler_test.go +++ b/pkg/placement/controllers/scheduling/cluster_event_handler_test.go @@ -34,7 +34,8 @@ func TestOnClusterChange(t *testing.T) { }, { name: "clusterset does not exist", - obj: testinghelpers.NewManagedCluster("cluster1").WithLabel(clusterSetLabel, "clusterset1").Build(), + obj: testinghelpers.NewManagedCluster("cluster1").WithLabel( + clusterapiv1beta2.ClusterSetLabel, "clusterset1").Build(), initObjs: []runtime.Object{ testinghelpers.NewClusterSetBinding("ns1", "clusterset1"), testinghelpers.NewPlacement("ns1", "placement1").Build(), @@ -43,7 +44,8 @@ func TestOnClusterChange(t *testing.T) { }, { name: "clusterset exists", - obj: testinghelpers.NewManagedCluster("cluster1").WithLabel(clusterSetLabel, "clusterset1").Build(), + obj: testinghelpers.NewManagedCluster("cluster1").WithLabel( + clusterapiv1beta2.ClusterSetLabel, "clusterset1").Build(), initObjs: []runtime.Object{ testinghelpers.NewClusterSet("clusterset1").Build(), testinghelpers.NewClusterSet("clusterset2").Build(), @@ -61,7 +63,8 @@ func TestOnClusterChange(t *testing.T) { }, { name: "cluster blongs to multiple clusterset", - obj: testinghelpers.NewManagedCluster("cluster1").WithLabel("cloud", "Amazon").WithLabel(clusterSetLabel, "clusterset2").Build(), + obj: testinghelpers.NewManagedCluster("cluster1").WithLabel("cloud", "Amazon").WithLabel( + clusterapiv1beta2.ClusterSetLabel, "clusterset2").Build(), initObjs: []runtime.Object{ testinghelpers.NewClusterSet("global").WithClusterSelector(clusterapiv1beta2.ManagedClusterSelector{ SelectorType: clusterapiv1beta2.LabelSelector, @@ -139,9 +142,11 @@ func TestOnClusterUpdate(t *testing.T) { }, }, { - name: "cluster blongs to multiple clusterset", - newObj: testinghelpers.NewManagedCluster("cluster1").WithLabel(clusterSetLabel, "clusterset2").WithLabel("cloud", "Google").Build(), - oldObj: testinghelpers.NewManagedCluster("cluster1").WithLabel(clusterSetLabel, "clusterset2").WithLabel("cloud", "Amazon").Build(), + name: "cluster blongs to multiple clusterset", + newObj: testinghelpers.NewManagedCluster("cluster1").WithLabel( + clusterapiv1beta2.ClusterSetLabel, "clusterset2").WithLabel("cloud", "Google").Build(), + oldObj: testinghelpers.NewManagedCluster("cluster1").WithLabel( + clusterapiv1beta2.ClusterSetLabel, "clusterset2").WithLabel("cloud", "Amazon").Build(), initObjs: []runtime.Object{ testinghelpers.NewClusterSet("global").WithClusterSelector(clusterapiv1beta2.ManagedClusterSelector{ SelectorType: clusterapiv1beta2.LabelSelector, @@ -172,7 +177,7 @@ func TestOnClusterUpdate(t *testing.T) { { name: "assign a cluster to a clusterset", newObj: testinghelpers.NewManagedCluster("cluster1"). - WithLabel(clusterSetLabel, "clusterset1").WithLabel("cloud", "Amazon").Build(), + WithLabel(clusterapiv1beta2.ClusterSetLabel, "clusterset1").WithLabel("cloud", "Amazon").Build(), oldObj: testinghelpers.NewManagedCluster("cluster1").WithLabel("cloud", "Amazon").Build(), initObjs: []runtime.Object{ testinghelpers.NewClusterSet("clusterset1").Build(), @@ -186,7 +191,8 @@ func TestOnClusterUpdate(t *testing.T) { { name: "remove cluster from a clusterset", newObj: testinghelpers.NewManagedCluster("cluster1").Build(), - oldObj: testinghelpers.NewManagedCluster("cluster1").WithLabel(clusterSetLabel, "clusterset2").WithLabel("cloud", "Amazon").Build(), + oldObj: testinghelpers.NewManagedCluster("cluster1").WithLabel( + clusterapiv1beta2.ClusterSetLabel, "clusterset2").WithLabel("cloud", "Amazon").Build(), initObjs: []runtime.Object{ testinghelpers.NewClusterSet("global").WithClusterSelector(clusterapiv1beta2.ManagedClusterSelector{ SelectorType: clusterapiv1beta2.LabelSelector, @@ -217,9 +223,9 @@ func TestOnClusterUpdate(t *testing.T) { { name: "label change only", newObj: testinghelpers.NewManagedCluster("cluster1"). - WithLabel(clusterSetLabel, "clusterset1").WithLabel("cloud", "Amazon").Build(), + WithLabel(clusterapiv1beta2.ClusterSetLabel, "clusterset1").WithLabel("cloud", "Amazon").Build(), oldObj: testinghelpers.NewManagedCluster("cluster1"). - WithLabel(clusterSetLabel, "clusterset1").WithLabel("cloud", "google").Build(), + WithLabel(clusterapiv1beta2.ClusterSetLabel, "clusterset1").WithLabel("cloud", "google").Build(), initObjs: []runtime.Object{ testinghelpers.NewClusterSet("clusterset1").Build(), testinghelpers.NewClusterSetBinding("ns1", "clusterset1"), @@ -232,9 +238,9 @@ func TestOnClusterUpdate(t *testing.T) { { name: "move cluster from one clusterset to another", newObj: testinghelpers.NewManagedCluster("cluster1"). - WithLabel(clusterSetLabel, "clusterset2").WithLabel("cloud", "Amazon").Build(), + WithLabel(clusterapiv1beta2.ClusterSetLabel, "clusterset2").WithLabel("cloud", "Amazon").Build(), oldObj: testinghelpers.NewManagedCluster("cluster1"). - WithLabel(clusterSetLabel, "clusterset1").WithLabel("cloud", "Amazon").Build(), + WithLabel(clusterapiv1beta2.ClusterSetLabel, "clusterset1").WithLabel("cloud", "Amazon").Build(), initObjs: []runtime.Object{ testinghelpers.NewClusterSet("clusterset1").Build(), testinghelpers.NewClusterSet("clusterset2").Build(), @@ -295,7 +301,8 @@ func TestOnClusterDelete(t *testing.T) { }, { name: "cluster", - obj: testinghelpers.NewManagedCluster("cluster1").WithLabel(clusterSetLabel, "clusterset2").WithLabel("cloud", "Amazon").Build(), + obj: testinghelpers.NewManagedCluster("cluster1").WithLabel( + clusterapiv1beta2.ClusterSetLabel, "clusterset2").WithLabel("cloud", "Amazon").Build(), initObjs: []runtime.Object{ testinghelpers.NewClusterSet("global").WithClusterSelector(clusterapiv1beta2.ManagedClusterSelector{ SelectorType: clusterapiv1beta2.LabelSelector, @@ -326,7 +333,8 @@ func TestOnClusterDelete(t *testing.T) { { name: "tombstone", obj: cache.DeletedFinalStateUnknown{ - Obj: testinghelpers.NewManagedCluster("cluster1").WithLabel(clusterSetLabel, "clusterset1").Build(), + Obj: testinghelpers.NewManagedCluster("cluster1").WithLabel( + clusterapiv1beta2.ClusterSetLabel, "clusterset1").Build(), }, initObjs: []runtime.Object{ testinghelpers.NewClusterSet("clusterset1").Build(), diff --git a/pkg/placement/controllers/scheduling/enqueue_test.go b/pkg/placement/controllers/scheduling/enqueue_test.go index f2b4efe83..08a85e3cf 100644 --- a/pkg/placement/controllers/scheduling/enqueue_test.go +++ b/pkg/placement/controllers/scheduling/enqueue_test.go @@ -323,7 +323,7 @@ func TestEnqueuePlacementsByScore(t *testing.T) { testinghelpers.NewPlacement("ns1", "placement1").WithScoreCoordinateAddOn("score1", "cpu", 1).Build(), testinghelpers.NewPlacement("ns2", "placement2").WithScoreCoordinateAddOn("score2", "cpu", 1).Build(), testinghelpers.NewPlacement("ns3", "placement3").WithScoreCoordinateAddOn("score1", "cpu", 1).Build(), - testinghelpers.NewManagedCluster("cluster1").WithLabel(clusterSetLabel, "clusterset1").Build(), + testinghelpers.NewManagedCluster("cluster1").WithLabel(clusterapiv1beta2.ClusterSetLabel, "clusterset1").Build(), testinghelpers.NewClusterSet("clusterset1").Build(), testinghelpers.NewClusterSetBinding("ns1", "clusterset1"), testinghelpers.NewClusterSetBinding("ns3", "clusterset1"), @@ -340,7 +340,7 @@ func TestEnqueuePlacementsByScore(t *testing.T) { testinghelpers.NewPlacement("ns1", "placement1").WithScoreCoordinateAddOn("score1", "cpu", 1).Build(), testinghelpers.NewPlacement("ns2", "placement2").WithScoreCoordinateAddOn("score2", "cpu", 1).Build(), testinghelpers.NewPlacement("ns3", "placement3").WithScoreCoordinateAddOn("score1", "cpu", 1).Build(), - testinghelpers.NewManagedCluster("cluster1").WithLabel(clusterSetLabel, "clusterset1").Build(), + testinghelpers.NewManagedCluster("cluster1").WithLabel(clusterapiv1beta2.ClusterSetLabel, "clusterset1").Build(), testinghelpers.NewClusterSet("clusterset1").Build(), testinghelpers.NewClusterSetBinding("ns1", "clusterset2"), testinghelpers.NewClusterSetBinding("ns3", "clusterset1"), @@ -357,7 +357,7 @@ func TestEnqueuePlacementsByScore(t *testing.T) { }, initObjs: []runtime.Object{ testinghelpers.NewPlacement("ns1", "placement1").WithScoreCoordinateAddOn("score1", "cpu", 1).Build(), - testinghelpers.NewManagedCluster("cluster1").WithLabel(clusterSetLabel, "clusterset1").Build(), + testinghelpers.NewManagedCluster("cluster1").WithLabel(clusterapiv1beta2.ClusterSetLabel, "clusterset1").Build(), testinghelpers.NewClusterSet("clusterset1").Build(), testinghelpers.NewClusterSetBinding("ns1", "clusterset1"), }, diff --git a/pkg/placement/controllers/scheduling/schedule_test.go b/pkg/placement/controllers/scheduling/schedule_test.go index 69872ec80..76202d15a 100644 --- a/pkg/placement/controllers/scheduling/schedule_test.go +++ b/pkg/placement/controllers/scheduling/schedule_test.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "fmt" + clusterapiv1beta2 "open-cluster-management.io/api/cluster/v1beta2" "reflect" "sort" "testing" @@ -71,7 +72,8 @@ func TestSchedule(t *testing.T) { }, }, clusters: []*clusterapiv1.ManagedCluster{ - testinghelpers.NewManagedCluster("cluster1").WithLabel(clusterSetLabel, clusterSetName).Build(), + testinghelpers.NewManagedCluster("cluster1").WithLabel( + clusterapiv1beta2.ClusterSetLabel, clusterSetName).Build(), }, expectedUnScheduled: 0, expectedStatus: *framework.NewStatus("", framework.Success, ""), @@ -85,7 +87,7 @@ func TestSchedule(t *testing.T) { }, decisions: []runtime.Object{}, clusters: []*clusterapiv1.ManagedCluster{ - testinghelpers.NewManagedCluster("cluster1").WithLabel(clusterSetLabel, clusterSetName).Build(), + testinghelpers.NewManagedCluster("cluster1").WithLabel(clusterapiv1beta2.ClusterSetLabel, clusterSetName).Build(), }, expectedDecisions: []clusterapiv1beta1.ClusterDecision{ {ClusterName: "cluster1"}, @@ -128,7 +130,7 @@ func TestSchedule(t *testing.T) { }, decisions: []runtime.Object{}, clusters: []*clusterapiv1.ManagedCluster{ - testinghelpers.NewManagedCluster("cluster1").WithLabel(clusterSetLabel, clusterSetName).Build(), + testinghelpers.NewManagedCluster("cluster1").WithLabel(clusterapiv1beta2.ClusterSetLabel, clusterSetName).Build(), }, expectedDecisions: []clusterapiv1beta1.ClusterDecision{}, expectedFilterResult: []FilterResult{ @@ -152,18 +154,18 @@ func TestSchedule(t *testing.T) { testinghelpers.NewClusterSet(clusterSetName).Build(), testinghelpers.NewClusterSetBinding(placementNamespace, clusterSetName), testinghelpers.NewPlacementDecision(placementNamespace, placementDecisionName(placementName, 1)). - WithLabel(placementLabel, placementName). + WithLabel(clusterapiv1beta1.PlacementLabel, placementName). WithDecisions("cluster1", "cluster2").Build(), }, decisions: []runtime.Object{ testinghelpers.NewPlacementDecision(placementNamespace, placementDecisionName(placementName, 1)). - WithLabel(placementLabel, placementName). + WithLabel(clusterapiv1beta1.PlacementLabel, placementName). WithDecisions("cluster1", "cluster2").Build(), }, clusters: []*clusterapiv1.ManagedCluster{ - testinghelpers.NewManagedCluster("cluster1").WithLabel(clusterSetLabel, clusterSetName).Build(), - testinghelpers.NewManagedCluster("cluster2").WithLabel(clusterSetLabel, clusterSetName).Build(), - testinghelpers.NewManagedCluster("cluster3").WithLabel(clusterSetLabel, clusterSetName).Build(), + testinghelpers.NewManagedCluster("cluster1").WithLabel(clusterapiv1beta2.ClusterSetLabel, clusterSetName).Build(), + testinghelpers.NewManagedCluster("cluster2").WithLabel(clusterapiv1beta2.ClusterSetLabel, clusterSetName).Build(), + testinghelpers.NewManagedCluster("cluster3").WithLabel(clusterapiv1beta2.ClusterSetLabel, clusterSetName).Build(), }, expectedDecisions: []clusterapiv1beta1.ClusterDecision{ {ClusterName: "cluster1"}, @@ -228,7 +230,7 @@ func TestSchedule(t *testing.T) { }, }, clusters: []*clusterapiv1.ManagedCluster{ - testinghelpers.NewManagedCluster("cluster1").WithLabel(clusterSetLabel, clusterSetName).Build(), + testinghelpers.NewManagedCluster("cluster1").WithLabel(clusterapiv1beta2.ClusterSetLabel, clusterSetName).Build(), }, expectedUnScheduled: 0, expectedStatus: *framework.NewStatus("", framework.Success, ""), @@ -249,21 +251,21 @@ func TestSchedule(t *testing.T) { testinghelpers.NewAddOnPlacementScore("cluster3", "demo").WithScore("demo", 50).Build(), }, clusters: []*clusterapiv1.ManagedCluster{ - testinghelpers.NewManagedCluster("cluster1").WithLabel(clusterSetLabel, clusterSetName).WithTaint( + testinghelpers.NewManagedCluster("cluster1").WithLabel(clusterapiv1beta2.ClusterSetLabel, clusterSetName).WithTaint( &clusterapiv1.Taint{ Key: "key1", Value: "value1", Effect: clusterapiv1.TaintEffectNoSelect, TimeAdded: metav1.Time{}, }).Build(), - testinghelpers.NewManagedCluster("cluster2").WithLabel(clusterSetLabel, clusterSetName).WithTaint( + testinghelpers.NewManagedCluster("cluster2").WithLabel(clusterapiv1beta2.ClusterSetLabel, clusterSetName).WithTaint( &clusterapiv1.Taint{ Key: "key2", Value: "value2", Effect: clusterapiv1.TaintEffectNoSelect, TimeAdded: metav1.Time{}, }).Build(), - testinghelpers.NewManagedCluster("cluster3").WithLabel(clusterSetLabel, clusterSetName).Build(), + testinghelpers.NewManagedCluster("cluster3").WithLabel(clusterapiv1beta2.ClusterSetLabel, clusterSetName).Build(), }, decisions: []runtime.Object{}, expectedDecisions: []clusterapiv1beta1.ClusterDecision{ @@ -306,9 +308,9 @@ func TestSchedule(t *testing.T) { testinghelpers.NewAddOnPlacementScore("cluster3", "demo").WithScore("demo", 50).Build(), }, clusters: []*clusterapiv1.ManagedCluster{ - testinghelpers.NewManagedCluster("cluster1").WithLabel(clusterSetLabel, clusterSetName).WithResource(clusterapiv1.ResourceMemory, "100", "100").Build(), - testinghelpers.NewManagedCluster("cluster2").WithLabel(clusterSetLabel, clusterSetName).WithResource(clusterapiv1.ResourceMemory, "50", "100").Build(), - testinghelpers.NewManagedCluster("cluster3").WithLabel(clusterSetLabel, clusterSetName).WithResource(clusterapiv1.ResourceMemory, "0", "100").Build(), + testinghelpers.NewManagedCluster("cluster1").WithLabel(clusterapiv1beta2.ClusterSetLabel, clusterSetName).WithResource(clusterapiv1.ResourceMemory, "100", "100").Build(), + testinghelpers.NewManagedCluster("cluster2").WithLabel(clusterapiv1beta2.ClusterSetLabel, clusterSetName).WithResource(clusterapiv1.ResourceMemory, "50", "100").Build(), + testinghelpers.NewManagedCluster("cluster3").WithLabel(clusterapiv1beta2.ClusterSetLabel, clusterSetName).WithResource(clusterapiv1.ResourceMemory, "0", "100").Build(), }, decisions: []runtime.Object{}, expectedDecisions: []clusterapiv1beta1.ClusterDecision{ @@ -358,9 +360,9 @@ func TestSchedule(t *testing.T) { testinghelpers.NewClusterSetBinding(placementNamespace, clusterSetName), }, clusters: []*clusterapiv1.ManagedCluster{ - testinghelpers.NewManagedCluster("cluster1").WithLabel(clusterSetLabel, clusterSetName).WithResource(clusterapiv1.ResourceMemory, "100", "100").Build(), - testinghelpers.NewManagedCluster("cluster2").WithLabel(clusterSetLabel, clusterSetName).WithResource(clusterapiv1.ResourceMemory, "50", "100").Build(), - testinghelpers.NewManagedCluster("cluster3").WithLabel(clusterSetLabel, clusterSetName).WithResource(clusterapiv1.ResourceMemory, "0", "100").Build(), + testinghelpers.NewManagedCluster("cluster1").WithLabel(clusterapiv1beta2.ClusterSetLabel, clusterSetName).WithResource(clusterapiv1.ResourceMemory, "100", "100").Build(), + testinghelpers.NewManagedCluster("cluster2").WithLabel(clusterapiv1beta2.ClusterSetLabel, clusterSetName).WithResource(clusterapiv1.ResourceMemory, "50", "100").Build(), + testinghelpers.NewManagedCluster("cluster3").WithLabel(clusterapiv1beta2.ClusterSetLabel, clusterSetName).WithResource(clusterapiv1.ResourceMemory, "0", "100").Build(), }, decisions: []runtime.Object{}, expectedDecisions: []clusterapiv1beta1.ClusterDecision{ @@ -399,16 +401,16 @@ func TestSchedule(t *testing.T) { testinghelpers.NewClusterSet(clusterSetName).Build(), testinghelpers.NewClusterSetBinding(placementNamespace, clusterSetName), testinghelpers.NewPlacementDecision(placementNamespace, placementDecisionName(placementName, 1)). - WithLabel(placementLabel, placementName). + WithLabel(clusterapiv1beta1.PlacementLabel, placementName). WithDecisions("cluster1").Build(), }, clusters: []*clusterapiv1.ManagedCluster{ - testinghelpers.NewManagedCluster("cluster1").WithLabel(clusterSetLabel, clusterSetName).Build(), - testinghelpers.NewManagedCluster("cluster2").WithLabel(clusterSetLabel, clusterSetName).Build(), + testinghelpers.NewManagedCluster("cluster1").WithLabel(clusterapiv1beta2.ClusterSetLabel, clusterSetName).Build(), + testinghelpers.NewManagedCluster("cluster2").WithLabel(clusterapiv1beta2.ClusterSetLabel, clusterSetName).Build(), }, decisions: []runtime.Object{ testinghelpers.NewPlacementDecision(placementNamespace, placementDecisionName(placementName, 1)). - WithLabel(placementLabel, placementName). + WithLabel(clusterapiv1beta1.PlacementLabel, placementName). WithDecisions("cluster1").Build(), }, expectedDecisions: []clusterapiv1beta1.ClusterDecision{ @@ -454,9 +456,9 @@ func TestSchedule(t *testing.T) { WithDecisions("cluster1", "cluster2").Build(), }, clusters: []*clusterapiv1.ManagedCluster{ - testinghelpers.NewManagedCluster("cluster1").WithLabel(clusterSetLabel, clusterSetName).Build(), - testinghelpers.NewManagedCluster("cluster2").WithLabel(clusterSetLabel, clusterSetName).Build(), - testinghelpers.NewManagedCluster("cluster3").WithLabel(clusterSetLabel, clusterSetName).Build(), + testinghelpers.NewManagedCluster("cluster1").WithLabel(clusterapiv1beta2.ClusterSetLabel, clusterSetName).Build(), + testinghelpers.NewManagedCluster("cluster2").WithLabel(clusterapiv1beta2.ClusterSetLabel, clusterSetName).Build(), + testinghelpers.NewManagedCluster("cluster3").WithLabel(clusterapiv1beta2.ClusterSetLabel, clusterSetName).Build(), }, expectedDecisions: []clusterapiv1beta1.ClusterDecision{ {ClusterName: "cluster3"}, @@ -497,7 +499,7 @@ func TestSchedule(t *testing.T) { testinghelpers.NewPlacementDecision(placementNamespace, placementDecisionName("others", 2)). WithDecisions("cluster1", "cluster2").Build(), testinghelpers.NewPlacementDecision(placementNamespace, placementDecisionName(placementName, 1)). - WithLabel(placementLabel, placementName). + WithLabel(clusterapiv1beta1.PlacementLabel, placementName). WithDecisions("cluster3").Build(), }, decisions: []runtime.Object{ @@ -506,13 +508,13 @@ func TestSchedule(t *testing.T) { testinghelpers.NewPlacementDecision(placementNamespace, placementDecisionName("others", 2)). WithDecisions("cluster1", "cluster2").Build(), testinghelpers.NewPlacementDecision(placementNamespace, placementDecisionName(placementName, 1)). - WithLabel(placementLabel, placementName). + WithLabel(clusterapiv1beta1.PlacementLabel, placementName). WithDecisions("cluster3").Build(), }, clusters: []*clusterapiv1.ManagedCluster{ - testinghelpers.NewManagedCluster("cluster1").WithLabel(clusterSetLabel, clusterSetName).Build(), - testinghelpers.NewManagedCluster("cluster2").WithLabel(clusterSetLabel, clusterSetName).Build(), - testinghelpers.NewManagedCluster("cluster3").WithLabel(clusterSetLabel, clusterSetName).Build(), + testinghelpers.NewManagedCluster("cluster1").WithLabel(clusterapiv1beta2.ClusterSetLabel, clusterSetName).Build(), + testinghelpers.NewManagedCluster("cluster2").WithLabel(clusterapiv1beta2.ClusterSetLabel, clusterSetName).Build(), + testinghelpers.NewManagedCluster("cluster3").WithLabel(clusterapiv1beta2.ClusterSetLabel, clusterSetName).Build(), }, expectedDecisions: []clusterapiv1beta1.ClusterDecision{ {ClusterName: "cluster3"}, diff --git a/pkg/placement/controllers/scheduling/scheduling_controller.go b/pkg/placement/controllers/scheduling/scheduling_controller.go index 8fde71cf9..86a013da7 100644 --- a/pkg/placement/controllers/scheduling/scheduling_controller.go +++ b/pkg/placement/controllers/scheduling/scheduling_controller.go @@ -37,16 +37,14 @@ import ( clusterapiv1beta1 "open-cluster-management.io/api/cluster/v1beta1" clusterapiv1beta2 "open-cluster-management.io/api/cluster/v1beta2" + "open-cluster-management.io/ocm/pkg/common/queue" "open-cluster-management.io/ocm/pkg/placement/controllers/framework" ) const ( - clusterSetLabel = "cluster.open-cluster-management.io/clusterset" - placementLabel = "cluster.open-cluster-management.io/placement" - schedulingControllerName = "SchedulingController" - schedulingControllerResyncName = "SchedulingControllerResync" - maxNumOfClusterDecisions = 100 - maxEventMessageLength = 1000 //the event message can have at most 1024 characters, use 1000 as limitation here to keep some buffer + schedulingControllerName = "SchedulingController" + maxNumOfClusterDecisions = 100 + maxEventMessageLength = 1000 //the event message can have at most 1024 characters, use 1000 as limitation here to keep some buffer ) var ResyncInterval = time.Minute * 5 @@ -152,26 +150,17 @@ func NewSchedulingController( return factory.New(). WithSyncContext(syncCtx). - WithInformersQueueKeyFunc(func(obj runtime.Object) string { - key, _ := cache.MetaNamespaceKeyFunc(obj) - return key - }, placementInformer.Informer()). + WithInformersQueueKeysFunc( + queue.QueueKeyByMetaNamespaceName, + placementInformer.Informer()). WithFilteredEventsInformersQueueKeyFunc(func(obj runtime.Object) string { accessor, _ := meta.Accessor(obj) labels := accessor.GetLabels() - placementName := labels[placementLabel] + placementName := labels[clusterapiv1beta1.PlacementLabel] return fmt.Sprintf("%s/%s", accessor.GetNamespace(), placementName) - }, func(obj interface{}) bool { - accessor, err := meta.Accessor(obj) - if err != nil { - return false - } - labels := accessor.GetLabels() - if _, ok := labels[placementLabel]; ok { - return true - } - return false - }, placementDecisionInformer.Informer()). + }, + queue.FileterByLabel(clusterapiv1beta1.PlacementLabel), + placementDecisionInformer.Informer()). WithBareInformers(clusterInformer.Informer(), clusterSetInformer.Informer(), clusterSetBindingInformer.Informer(), placementScoreInformer.Informer()). WithSync(c.sync). ToController(schedulingControllerName, recorder) @@ -493,7 +482,7 @@ func (c *schedulingController) bind( } // query all placementdecisions of the placement - requirement, err := labels.NewRequirement(placementLabel, selection.Equals, []string{placement.Name}) + requirement, err := labels.NewRequirement(clusterapiv1beta1.PlacementLabel, selection.Equals, []string{placement.Name}) if err != nil { return err } @@ -549,7 +538,7 @@ func (c *schedulingController) createOrUpdatePlacementDecision( Name: placementDecisionName, Namespace: placement.Namespace, Labels: map[string]string{ - placementLabel: placement.Name, + clusterapiv1beta1.PlacementLabel: placement.Name, }, OwnerReferences: []metav1.OwnerReference{*owner}, }, diff --git a/pkg/placement/controllers/scheduling/scheduling_controller_test.go b/pkg/placement/controllers/scheduling/scheduling_controller_test.go index aa79ca336..904eee269 100644 --- a/pkg/placement/controllers/scheduling/scheduling_controller_test.go +++ b/pkg/placement/controllers/scheduling/scheduling_controller_test.go @@ -90,7 +90,8 @@ func TestSchedulingController_sync(t *testing.T) { initObjs: []runtime.Object{ testinghelpers.NewClusterSet("clusterset1").Build(), testinghelpers.NewClusterSetBinding(placementNamespace, "clusterset1"), - testinghelpers.NewManagedCluster("cluster1").WithLabel(clusterSetLabel, "clusterset1").Build(), + testinghelpers.NewManagedCluster("cluster1").WithLabel( + clusterapiv1beta2.ClusterSetLabel, "clusterset1").Build(), }, scheduleResult: &scheduleResult{ feasibleClusters: []*clusterapiv1.ManagedCluster{ @@ -214,7 +215,7 @@ func TestSchedulingController_sync(t *testing.T) { initObjs: []runtime.Object{ testinghelpers.NewClusterSet("clusterset1").Build(), testinghelpers.NewClusterSetBinding(placementNamespace, "clusterset1"), - testinghelpers.NewManagedCluster("cluster1").WithLabel(clusterSetLabel, "clusterset1").Build(), + testinghelpers.NewManagedCluster("cluster1").WithLabel(clusterapiv1beta2.ClusterSetLabel, "clusterset1").Build(), }, scheduleResult: &scheduleResult{ feasibleClusters: []*clusterapiv1.ManagedCluster{ @@ -261,9 +262,9 @@ func TestSchedulingController_sync(t *testing.T) { initObjs: []runtime.Object{ testinghelpers.NewClusterSet("clusterset1").Build(), testinghelpers.NewClusterSetBinding(placementNamespace, "clusterset1"), - testinghelpers.NewManagedCluster("cluster1").WithLabel(clusterSetLabel, "clusterset1").Build(), + testinghelpers.NewManagedCluster("cluster1").WithLabel(clusterapiv1beta2.ClusterSetLabel, "clusterset1").Build(), testinghelpers.NewPlacementDecision(placementNamespace, placementDecisionName(placementName, 1)). - WithLabel(placementLabel, placementName). + WithLabel(clusterapiv1beta1.PlacementLabel, placementName). WithDecisions("cluster1", "cluster2", "cluster3").Build(), }, scheduleResult: &scheduleResult{ @@ -475,8 +476,8 @@ func TestGetAvailableClusters(t *testing.T) { initObjs: []runtime.Object{ testinghelpers.NewClusterSet("clusterset1").Build(), testinghelpers.NewClusterSet("clusterset2").Build(), - testinghelpers.NewManagedCluster("cluster1").WithLabel(clusterSetLabel, "clusterset1").Build(), - testinghelpers.NewManagedCluster("cluster2").WithLabel(clusterSetLabel, "clusterset2").Build(), + testinghelpers.NewManagedCluster("cluster1").WithLabel(clusterapiv1beta2.ClusterSetLabel, "clusterset1").Build(), + testinghelpers.NewManagedCluster("cluster2").WithLabel(clusterapiv1beta2.ClusterSetLabel, "clusterset2").Build(), }, expectedClusterNames: []string{"cluster1", "cluster2"}, }, @@ -485,7 +486,7 @@ func TestGetAvailableClusters(t *testing.T) { clusterSetNames: []string{"clusterset1"}, initObjs: []runtime.Object{ testinghelpers.NewClusterSet("clusterset1").Build(), - testinghelpers.NewManagedCluster("cluster1").WithLabel(clusterSetLabel, "clusterset1").Build(), + testinghelpers.NewManagedCluster("cluster1").WithLabel(clusterapiv1beta2.ClusterSetLabel, "clusterset1").Build(), }, expectedClusterNames: []string{"cluster1"}, }, @@ -494,7 +495,7 @@ func TestGetAvailableClusters(t *testing.T) { clusterSetNames: []string{"clusterset1"}, initObjs: []runtime.Object{ testinghelpers.NewClusterSet("clusterset1").WithClusterSelector(clusterapiv1beta2.ManagedClusterSelector{}).Build(), - testinghelpers.NewManagedCluster("cluster1").WithLabel(clusterSetLabel, "clusterset1").Build(), + testinghelpers.NewManagedCluster("cluster1").WithLabel(clusterapiv1beta2.ClusterSetLabel, "clusterset1").Build(), }, expectedClusterNames: []string{"cluster1"}, }, @@ -505,7 +506,7 @@ func TestGetAvailableClusters(t *testing.T) { testinghelpers.NewClusterSet("clusterset1").WithClusterSelector(clusterapiv1beta2.ManagedClusterSelector{ SelectorType: clusterapiv1beta2.ExclusiveClusterSetLabel, }).Build(), - testinghelpers.NewManagedCluster("cluster1").WithLabel(clusterSetLabel, "clusterset1").Build(), + testinghelpers.NewManagedCluster("cluster1").WithLabel(clusterapiv1beta2.ClusterSetLabel, "clusterset1").Build(), }, expectedClusterNames: []string{"cluster1"}, }, @@ -521,7 +522,7 @@ func TestGetAvailableClusters(t *testing.T) { }, }, }).Build(), - testinghelpers.NewManagedCluster("cluster1").WithLabel(clusterSetLabel, "clusterset1").Build(), + testinghelpers.NewManagedCluster("cluster1").WithLabel(clusterapiv1beta2.ClusterSetLabel, "clusterset1").Build(), testinghelpers.NewManagedCluster("cluster2").WithLabel("vendor", "openShift").Build(), }, expectedClusterNames: []string{"cluster2"}, @@ -534,7 +535,7 @@ func TestGetAvailableClusters(t *testing.T) { SelectorType: clusterapiv1beta2.LabelSelector, LabelSelector: &metav1.LabelSelector{}, }).Build(), - testinghelpers.NewManagedCluster("cluster1").WithLabel(clusterSetLabel, "clusterset1").Build(), + testinghelpers.NewManagedCluster("cluster1").WithLabel(clusterapiv1beta2.ClusterSetLabel, "clusterset1").Build(), testinghelpers.NewManagedCluster("cluster2").Build(), }, expectedClusterNames: []string{"cluster1", "cluster2"}, @@ -548,7 +549,7 @@ func TestGetAvailableClusters(t *testing.T) { SelectorType: "errorType", }, ).Build(), - testinghelpers.NewManagedCluster("cluster1").WithLabel(clusterSetLabel, "clusterset1").Build(), + testinghelpers.NewManagedCluster("cluster1").WithLabel(clusterapiv1beta2.ClusterSetLabel, "clusterset1").Build(), }, expectedClusterNames: []string{}, }, @@ -790,10 +791,10 @@ func TestBind(t *testing.T) { clusterDecisions: newClusterDecisions(128), initObjs: []runtime.Object{ testinghelpers.NewPlacementDecision(placementNamespace, placementDecisionName(placementName, 1)). - WithLabel(placementLabel, placementName). + WithLabel(clusterapiv1beta1.PlacementLabel, placementName). WithDecisions(newSelectedClusters(128)[:100]...).Build(), testinghelpers.NewPlacementDecision(placementNamespace, placementDecisionName(placementName, 2)). - WithLabel(placementLabel, placementName). + WithLabel(clusterapiv1beta1.PlacementLabel, placementName). WithDecisions(newSelectedClusters(128)[100:]...).Build(), }, validateActions: testingcommon.AssertNoActions, @@ -803,7 +804,7 @@ func TestBind(t *testing.T) { clusterDecisions: newClusterDecisions(128), initObjs: []runtime.Object{ testinghelpers.NewPlacementDecision(placementNamespace, placementDecisionName(placementName, 1)). - WithLabel(placementLabel, placementName). + WithLabel(clusterapiv1beta1.PlacementLabel, placementName). WithDecisions(newSelectedClusters(128)[:100]...).Build(), }, validateActions: func(t *testing.T, actions []clienttesting.Action) { @@ -822,10 +823,10 @@ func TestBind(t *testing.T) { clusterDecisions: newClusterDecisions(10), initObjs: []runtime.Object{ testinghelpers.NewPlacementDecision(placementNamespace, placementDecisionName(placementName, 1)). - WithLabel(placementLabel, placementName). + WithLabel(clusterapiv1beta1.PlacementLabel, placementName). WithDecisions(newSelectedClusters(128)[:100]...).Build(), testinghelpers.NewPlacementDecision(placementNamespace, placementDecisionName(placementName, 2)). - WithLabel(placementLabel, placementName). + WithLabel(clusterapiv1beta1.PlacementLabel, placementName). WithDecisions(newSelectedClusters(128)[100:]...).Build(), }, validateActions: func(t *testing.T, actions []clienttesting.Action) { @@ -843,10 +844,10 @@ func TestBind(t *testing.T) { clusterDecisions: newClusterDecisions(0), initObjs: []runtime.Object{ testinghelpers.NewPlacementDecision(placementNamespace, placementDecisionName(placementName, 1)). - WithLabel(placementLabel, placementName). + WithLabel(clusterapiv1beta1.PlacementLabel, placementName). WithDecisions(newSelectedClusters(128)[:100]...).Build(), testinghelpers.NewPlacementDecision(placementNamespace, placementDecisionName(placementName, 2)). - WithLabel(placementLabel, placementName). + WithLabel(clusterapiv1beta1.PlacementLabel, placementName). WithDecisions(newSelectedClusters(128)[100:]...).Build(), }, validateActions: func(t *testing.T, actions []clienttesting.Action) { diff --git a/pkg/registration/hub/addon/discovery_controller.go b/pkg/registration/hub/addon/discovery_controller.go index cec6d038f..24d43317f 100644 --- a/pkg/registration/hub/addon/discovery_controller.go +++ b/pkg/registration/hub/addon/discovery_controller.go @@ -12,7 +12,6 @@ import ( "k8s.io/apimachinery/pkg/api/meta" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/labels" - "k8s.io/apimachinery/pkg/runtime" addonv1alpha1 "open-cluster-management.io/api/addon/v1alpha1" addoninformerv1alpha1 "open-cluster-management.io/api/client/addon/informers/externalversions/addon/v1alpha1" @@ -23,6 +22,7 @@ import ( clusterv1 "open-cluster-management.io/api/cluster/v1" "open-cluster-management.io/ocm/pkg/common/patcher" + "open-cluster-management.io/ocm/pkg/common/queue" ) const ( @@ -58,17 +58,11 @@ func NewAddOnFeatureDiscoveryController( } return factory.New(). - WithInformersQueueKeyFunc( - func(obj runtime.Object) string { - accessor, _ := meta.Accessor(obj) - return accessor.GetName() - }, + WithInformersQueueKeysFunc( + queue.QueueKeyByMetaName, clusterInformer.Informer()). - WithInformersQueueKeyFunc( - func(obj runtime.Object) string { - accessor, _ := meta.Accessor(obj) - return accessor.GetNamespace() - }, + WithInformersQueueKeysFunc( + queue.QueueKeyByMetaNamespace, addOnInformers.Informer()). WithSync(c.sync). ToController("AddOnFeatureDiscoveryController", recorder) diff --git a/pkg/registration/hub/addon/healthcheck_controller.go b/pkg/registration/hub/addon/healthcheck_controller.go index cf78efc97..af3aba1d1 100644 --- a/pkg/registration/hub/addon/healthcheck_controller.go +++ b/pkg/registration/hub/addon/healthcheck_controller.go @@ -10,7 +10,6 @@ import ( "k8s.io/apimachinery/pkg/api/meta" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/labels" - "k8s.io/apimachinery/pkg/runtime" addonv1alpha1 "open-cluster-management.io/api/addon/v1alpha1" addonclient "open-cluster-management.io/api/client/addon/clientset/versioned" @@ -21,6 +20,7 @@ import ( clusterv1 "open-cluster-management.io/api/cluster/v1" patcher "open-cluster-management.io/ocm/pkg/common/patcher" + "open-cluster-management.io/ocm/pkg/common/queue" ) // managedClusterAddonHealthCheckController udpates managed cluster addons status through watching the managed cluster status on @@ -43,10 +43,7 @@ func NewManagedClusterAddOnHealthCheckController(addOnClient addonclient.Interfa } return factory.New(). - WithInformersQueueKeyFunc(func(obj runtime.Object) string { - accessor, _ := meta.Accessor(obj) - return accessor.GetName() - }, clusterInformer.Informer()). + WithInformersQueueKeysFunc(queue.QueueKeyByMetaName, clusterInformer.Informer()). WithSync(c.sync). ToController("ManagedClusterAddonHealthCheckController", recorder) } diff --git a/pkg/registration/hub/clusterrole/controller.go b/pkg/registration/hub/clusterrole/controller.go index 553ae42b2..19d522d2e 100644 --- a/pkg/registration/hub/clusterrole/controller.go +++ b/pkg/registration/hub/clusterrole/controller.go @@ -9,9 +9,7 @@ import ( "github.com/openshift/library-go/pkg/operator/events" "github.com/openshift/library-go/pkg/operator/resource/resourceapply" operatorhelpers "github.com/openshift/library-go/pkg/operator/v1helpers" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/labels" - "k8s.io/apimachinery/pkg/util/sets" rbacv1informers "k8s.io/client-go/informers/rbac/v1" "k8s.io/client-go/kubernetes" @@ -19,6 +17,7 @@ import ( clusterv1listers "open-cluster-management.io/api/client/cluster/listers/cluster/v1" "open-cluster-management.io/ocm/pkg/common/apply" + "open-cluster-management.io/ocm/pkg/common/queue" ) const ( @@ -64,11 +63,8 @@ func NewManagedClusterClusterroleController( } return factory.New(). WithFilteredEventsInformers( - func(obj interface{}) bool { - clusterRoles := sets.NewString(registrationClusterRole, workClusterRole) - metaObj := obj.(metav1.Object) - return clusterRoles.Has(metaObj.GetName()) - }, clusterRoleInformer.Informer()). + queue.FilterByNames(registrationClusterRole, workClusterRole), + clusterRoleInformer.Informer()). WithInformers(clusterInformer.Informer()). WithSync(c.sync). ToController("ManagedClusterClusterRoleController", recorder) diff --git a/pkg/registration/hub/csr/controller.go b/pkg/registration/hub/csr/controller.go index 6115917aa..e09b523d0 100644 --- a/pkg/registration/hub/csr/controller.go +++ b/pkg/registration/hub/csr/controller.go @@ -9,13 +9,12 @@ import ( certificatesv1beta1 "k8s.io/api/certificates/v1beta1" corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/errors" - "k8s.io/apimachinery/pkg/api/meta" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/runtime" "k8s.io/client-go/kubernetes" "k8s.io/client-go/tools/cache" "k8s.io/klog/v2" + "open-cluster-management.io/ocm/pkg/common/queue" "open-cluster-management.io/ocm/pkg/registration/helpers" ) @@ -53,10 +52,7 @@ func NewCSRApprovingController[T CSR]( } return factory.New(). - WithInformersQueueKeyFunc(func(obj runtime.Object) string { - accessor, _ := meta.Accessor(obj) - return accessor.GetName() - }, csrInformer). + WithInformersQueueKeysFunc(queue.QueueKeyByMetaName, csrInformer). WithSync(c.sync). ToController("CSRApprovingController", recorder) } diff --git a/pkg/registration/hub/lease/controller.go b/pkg/registration/hub/lease/controller.go index 55a0d5312..f5944f59f 100644 --- a/pkg/registration/hub/lease/controller.go +++ b/pkg/registration/hub/lease/controller.go @@ -10,7 +10,6 @@ import ( "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/api/meta" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/runtime" coordinformers "k8s.io/client-go/informers/coordination/v1" "k8s.io/client-go/kubernetes" coordlisters "k8s.io/client-go/listers/coordination/v1" @@ -22,6 +21,7 @@ import ( clusterv1 "open-cluster-management.io/api/cluster/v1" "open-cluster-management.io/ocm/pkg/common/patcher" + "open-cluster-management.io/ocm/pkg/common/queue" ) const leaseDurationTimes = 5 @@ -58,32 +58,12 @@ func NewClusterLeaseController( eventRecorder: recorder.WithComponentSuffix("managed-cluster-lease-controller"), } return factory.New(). - WithFilteredEventsInformersQueueKeyFunc( - func(obj runtime.Object) string { - accessor, _ := meta.Accessor(obj) - return accessor.GetLabels()[clusterv1.ClusterNameLabelKey] - }, - func(obj interface{}) bool { - metaObj, ok := obj.(metav1.ObjectMetaAccessor) - if !ok { - return false - } - - // only handle the managed cluster lease - // TODO instead of this by adding label filter in the SharedInformerFactory - // see https://github.com/open-cluster-management-io/registration/issues/225 - if _, ok := metaObj.GetObjectMeta().GetLabels()[clusterv1.ClusterNameLabelKey]; !ok { - return false - } - - return metaObj.GetObjectMeta().GetName() == leaseName - }, + WithFilteredEventsInformersQueueKeysFunc( + queue.QueueKeyByLabel(clusterv1.ClusterNameLabelKey), + queue.UnionFilter(queue.FileterByLabel(clusterv1.ClusterNameLabelKey), queue.FilterByNames(leaseName)), leaseInformer.Informer(), ). - WithInformersQueueKeyFunc(func(obj runtime.Object) string { - accessor, _ := meta.Accessor(obj) - return accessor.GetName() - }, clusterInformer.Informer()). + WithInformersQueueKeysFunc(queue.QueueKeyByMetaName, clusterInformer.Informer()). WithSync(c.sync). ToController("ManagedClusterLeaseController", recorder) } diff --git a/pkg/registration/hub/managedcluster/controller.go b/pkg/registration/hub/managedcluster/controller.go index 3ec7994bd..c791ed365 100644 --- a/pkg/registration/hub/managedcluster/controller.go +++ b/pkg/registration/hub/managedcluster/controller.go @@ -13,7 +13,6 @@ import ( "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/api/meta" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/runtime" rbacv1informers "k8s.io/client-go/informers/rbac/v1" "k8s.io/client-go/kubernetes" "k8s.io/klog/v2" @@ -25,6 +24,7 @@ import ( "open-cluster-management.io/ocm/pkg/common/apply" "open-cluster-management.io/ocm/pkg/common/patcher" + "open-cluster-management.io/ocm/pkg/common/queue" "open-cluster-management.io/ocm/pkg/registration/helpers" ) @@ -77,13 +77,10 @@ func NewManagedClusterController( eventRecorder: recorder.WithComponentSuffix("managed-cluster-controller"), } return factory.New(). - WithInformersQueueKeyFunc(func(obj runtime.Object) string { - accessor, _ := meta.Accessor(obj) - return accessor.GetName() - }, clusterInformer.Informer()). - WithFilteredEventsInformersQueueKeyFunc( - permissionQueueKey, - permissionFilterKey, + WithInformersQueueKeysFunc(queue.QueueKeyByMetaName, clusterInformer.Informer()). + WithFilteredEventsInformersQueueKeysFunc( + queue.QueueKeyByLabel(v1.ClusterNameLabelKey), + queue.FileterByLabel(v1.ClusterNameLabelKey), roleInformer.Informer(), rolebindingInformer.Informer(), clusterRoleInformer.Informer(), @@ -92,19 +89,6 @@ func NewManagedClusterController( ToController("ManagedClusterController", recorder) } -func permissionFilterKey(obj interface{}) bool { - accessor, _ := meta.Accessor(obj) - return len(accessor.GetLabels()) > 0 && len(accessor.GetLabels()[v1.ClusterNameLabelKey]) > 0 -} - -func permissionQueueKey(obj runtime.Object) string { - accessor, _ := meta.Accessor(obj) - if len(accessor.GetLabels()) == 0 { - return "" - } - return accessor.GetLabels()[v1.ClusterNameLabelKey] -} - func (c *managedClusterController) sync(ctx context.Context, syncCtx factory.SyncContext) error { managedClusterName := syncCtx.QueueKey() klog.V(4).Infof("Reconciling ManagedCluster %s", managedClusterName) diff --git a/pkg/registration/hub/managedclusterset/controller.go b/pkg/registration/hub/managedclusterset/controller.go index 380f290db..0b109bcf4 100644 --- a/pkg/registration/hub/managedclusterset/controller.go +++ b/pkg/registration/hub/managedclusterset/controller.go @@ -10,7 +10,6 @@ import ( "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/api/meta" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/runtime" utilruntime "k8s.io/apimachinery/pkg/util/runtime" "k8s.io/apimachinery/pkg/util/sets" "k8s.io/client-go/tools/cache" @@ -26,6 +25,7 @@ import ( clusterv1beta2 "open-cluster-management.io/api/cluster/v1beta2" "open-cluster-management.io/ocm/pkg/common/patcher" + "open-cluster-management.io/ocm/pkg/common/queue" ) // managedClusterSetController reconciles instances of ManagedClusterSet on the hub. @@ -110,10 +110,7 @@ func NewManagedClusterSetController( return factory.New(). WithSyncContext(syncCtx). - WithInformersQueueKeyFunc(func(obj runtime.Object) string { - accessor, _ := meta.Accessor(obj) - return accessor.GetName() - }, clusterSetInformer.Informer()). + WithInformersQueueKeysFunc(queue.QueueKeyByMetaName, clusterSetInformer.Informer()). WithBareInformers(clusterInformer.Informer()). WithSync(c.sync). ToController("ManagedClusterSetController", recorder) diff --git a/pkg/registration/hub/managedclusterset/default_managedclusterset_controller.go b/pkg/registration/hub/managedclusterset/default_managedclusterset_controller.go index e9fb842fb..c3cf745d5 100644 --- a/pkg/registration/hub/managedclusterset/default_managedclusterset_controller.go +++ b/pkg/registration/hub/managedclusterset/default_managedclusterset_controller.go @@ -9,15 +9,15 @@ import ( "github.com/openshift/library-go/pkg/operator/events" "k8s.io/apimachinery/pkg/api/equality" "k8s.io/apimachinery/pkg/api/errors" - "k8s.io/apimachinery/pkg/api/meta" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/runtime" "k8s.io/klog/v2" clustersetv1beta2 "open-cluster-management.io/api/client/cluster/clientset/versioned/typed/cluster/v1beta2" clusterinformerv1beta2 "open-cluster-management.io/api/client/cluster/informers/externalversions/cluster/v1beta2" clusterlisterv1beta2 "open-cluster-management.io/api/client/cluster/listers/cluster/v1beta2" clusterv1beta2 "open-cluster-management.io/api/cluster/v1beta2" + + "open-cluster-management.io/ocm/pkg/common/queue" ) const ( @@ -54,19 +54,9 @@ func NewDefaultManagedClusterSetController( } return factory.New(). - WithFilteredEventsInformersQueueKeyFunc( - func(obj runtime.Object) string { - accessor, _ := meta.Accessor(obj) - return accessor.GetName() - }, - func(obj interface{}) bool { - metaObj, ok := obj.(metav1.ObjectMetaAccessor) - if !ok { - return false - } - // filter clustersets except defaultManagedClusterSet. - return DefaultManagedClusterSetName != metaObj.GetObjectMeta().GetName() - }, + WithFilteredEventsInformersQueueKeysFunc( + queue.QueueKeyByMetaName, + queue.FilterByNames(DefaultManagedClusterSetName), clusterSetInformer.Informer(), ). WithSync(c.sync). diff --git a/pkg/registration/hub/managedclusterset/global_managedclusterset_controller.go b/pkg/registration/hub/managedclusterset/global_managedclusterset_controller.go index a698df99d..3f08b2049 100644 --- a/pkg/registration/hub/managedclusterset/global_managedclusterset_controller.go +++ b/pkg/registration/hub/managedclusterset/global_managedclusterset_controller.go @@ -9,15 +9,15 @@ import ( "github.com/openshift/library-go/pkg/operator/events" "k8s.io/apimachinery/pkg/api/equality" "k8s.io/apimachinery/pkg/api/errors" - "k8s.io/apimachinery/pkg/api/meta" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/runtime" "k8s.io/klog/v2" clustersetv1beta2 "open-cluster-management.io/api/client/cluster/clientset/versioned/typed/cluster/v1beta2" clusterinformerv1beta2 "open-cluster-management.io/api/client/cluster/informers/externalversions/cluster/v1beta2" clusterlisterv1beta2 "open-cluster-management.io/api/client/cluster/listers/cluster/v1beta2" clusterv1beta2 "open-cluster-management.io/api/cluster/v1beta2" + + "open-cluster-management.io/ocm/pkg/common/queue" ) const ( @@ -54,19 +54,9 @@ func NewGlobalManagedClusterSetController( } return factory.New(). - WithFilteredEventsInformersQueueKeyFunc( - func(obj runtime.Object) string { - accessor, _ := meta.Accessor(obj) - return accessor.GetName() - }, - func(obj interface{}) bool { - metaObj, ok := obj.(metav1.ObjectMetaAccessor) - if !ok { - return false - } - // filter clustersets except globalManagedClusterSet. - return GlobalManagedClusterSetName != metaObj.GetObjectMeta().GetName() - }, + WithFilteredEventsInformersQueueKeysFunc( + queue.QueueKeyByMetaName, + queue.FilterByNames(GlobalManagedClusterSetName), clusterSetInformer.Informer(), ). WithSync(c.sync). diff --git a/pkg/registration/hub/managedclustersetbinding/controller.go b/pkg/registration/hub/managedclustersetbinding/controller.go index d7911e7cc..660afba74 100644 --- a/pkg/registration/hub/managedclustersetbinding/controller.go +++ b/pkg/registration/hub/managedclustersetbinding/controller.go @@ -9,7 +9,6 @@ import ( "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/api/meta" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/runtime" utilruntime "k8s.io/apimachinery/pkg/util/runtime" "k8s.io/client-go/tools/cache" "k8s.io/client-go/util/workqueue" @@ -21,6 +20,7 @@ import ( clusterv1beta2 "open-cluster-management.io/api/cluster/v1beta2" "open-cluster-management.io/ocm/pkg/common/patcher" + "open-cluster-management.io/ocm/pkg/common/queue" ) const ( @@ -78,10 +78,7 @@ func NewManagedClusterSetBindingController( return factory.New(). WithSyncContext(syncCtx). - WithInformersQueueKeyFunc(func(obj runtime.Object) string { - key, _ := cache.MetaNamespaceKeyFunc(obj) - return key - }, clusterSetBindingInformer.Informer()). + WithInformersQueueKeysFunc(queue.QueueKeyByMetaNamespaceName, clusterSetBindingInformer.Informer()). WithBareInformers(clusterSetInformer.Informer()). WithSync(c.sync). ToController("ManagedClusterSetController", recorder) diff --git a/pkg/registration/hub/rbacfinalizerdeletion/controller.go b/pkg/registration/hub/rbacfinalizerdeletion/controller.go index f2fe96e50..c8ee49f52 100644 --- a/pkg/registration/hub/rbacfinalizerdeletion/controller.go +++ b/pkg/registration/hub/rbacfinalizerdeletion/controller.go @@ -24,6 +24,8 @@ import ( clusterv1listers "open-cluster-management.io/api/client/cluster/listers/cluster/v1" worklister "open-cluster-management.io/api/client/work/listers/work/v1" clusterv1 "open-cluster-management.io/api/cluster/v1" + + "open-cluster-management.io/ocm/pkg/common/queue" ) const ( @@ -63,10 +65,7 @@ func NewFinalizeController( } return factory.New(). - WithInformersQueueKeyFunc(func(obj runtime.Object) string { - key, _ := cache.MetaNamespaceKeyFunc(obj) - return key - }, roleInformer.Informer(), roleBindingInformer.Informer()). + WithInformersQueueKeysFunc(queue.QueueKeyByMetaNamespaceName, roleInformer.Informer(), roleBindingInformer.Informer()). WithSync(controller.sync).ToController("FinalizeController", eventRecorder) } diff --git a/pkg/registration/hub/taint/controller.go b/pkg/registration/hub/taint/controller.go index 26caa34f9..23b9b05e1 100644 --- a/pkg/registration/hub/taint/controller.go +++ b/pkg/registration/hub/taint/controller.go @@ -8,7 +8,6 @@ import ( "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/api/meta" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/runtime" "k8s.io/klog/v2" clientset "open-cluster-management.io/api/client/cluster/clientset/versioned" @@ -17,6 +16,7 @@ import ( v1 "open-cluster-management.io/api/cluster/v1" "open-cluster-management.io/ocm/pkg/common/patcher" + "open-cluster-management.io/ocm/pkg/common/queue" "open-cluster-management.io/ocm/pkg/registration/helpers" ) @@ -52,10 +52,7 @@ func NewTaintController( eventRecorder: recorder.WithComponentSuffix("taint-controller"), } return factory.New(). - WithInformersQueueKeyFunc(func(obj runtime.Object) string { - accessor, _ := meta.Accessor(obj) - return accessor.GetName() - }, clusterInformer.Informer()). + WithInformersQueueKeysFunc(queue.QueueKeyByMetaName, clusterInformer.Informer()). WithSync(c.sync). ToController("taintController", recorder) } diff --git a/pkg/registration/spoke/addon/registration_controller.go b/pkg/registration/spoke/addon/registration_controller.go index 42143ac3c..20e7142b8 100644 --- a/pkg/registration/spoke/addon/registration_controller.go +++ b/pkg/registration/spoke/addon/registration_controller.go @@ -12,7 +12,6 @@ import ( "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/api/meta" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/runtime" utilruntime "k8s.io/apimachinery/pkg/util/runtime" "k8s.io/client-go/informers" "k8s.io/client-go/kubernetes" @@ -26,6 +25,7 @@ import ( clusterv1 "open-cluster-management.io/api/cluster/v1" "open-cluster-management.io/ocm/pkg/common/patcher" + "open-cluster-management.io/ocm/pkg/common/queue" "open-cluster-management.io/ocm/pkg/registration/clientcert" ) @@ -98,11 +98,8 @@ func NewAddOnRegistrationController( c.startRegistrationFunc = c.startRegistration return factory.New(). - WithInformersQueueKeyFunc( - func(obj runtime.Object) string { - accessor, _ := meta.Accessor(obj) - return accessor.GetName() - }, + WithInformersQueueKeysFunc( + queue.QueueKeyByMetaName, hubAddOnInformers.Informer()). WithSync(c.sync). ResyncEvery(10*time.Minute). diff --git a/pkg/registration/spoke/registration/secret_controller.go b/pkg/registration/spoke/registration/secret_controller.go index 658f338aa..2acb7a832 100644 --- a/pkg/registration/spoke/registration/secret_controller.go +++ b/pkg/registration/spoke/registration/secret_controller.go @@ -15,10 +15,11 @@ import ( "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/api/meta" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/runtime" corev1informers "k8s.io/client-go/informers/core/v1" corev1client "k8s.io/client-go/kubernetes/typed/core/v1" "k8s.io/klog/v2" + + "open-cluster-management.io/ocm/pkg/common/queue" ) // hubKubeconfigSecretController watches the HubKubeconfig secret, if the secret is changed, this controller creates/updates the @@ -44,11 +45,8 @@ func NewHubKubeconfigSecretController( } return factory.New(). - WithFilteredEventsInformersQueueKeyFunc( - func(obj runtime.Object) string { - accessor, _ := meta.Accessor(obj) - return accessor.GetName() - }, + WithFilteredEventsInformersQueueKeysFunc( + queue.QueueKeyByMetaName, func(obj interface{}) bool { accessor, err := meta.Accessor(obj) if err != nil { diff --git a/pkg/work/hub/controllers/manifestworkreplicasetcontroller/manifestworkreplicaset_controller.go b/pkg/work/hub/controllers/manifestworkreplicasetcontroller/manifestworkreplicaset_controller.go index 1dc7b4a8a..7b2cfd1e6 100644 --- a/pkg/work/hub/controllers/manifestworkreplicasetcontroller/manifestworkreplicaset_controller.go +++ b/pkg/work/hub/controllers/manifestworkreplicasetcontroller/manifestworkreplicaset_controller.go @@ -28,6 +28,7 @@ import ( workapiv1alpha1 "open-cluster-management.io/api/work/v1alpha1" "open-cluster-management.io/ocm/pkg/common/patcher" + "open-cluster-management.io/ocm/pkg/common/queue" ) const ( @@ -82,14 +83,7 @@ func NewManifestWorkReplicaSetController( } return factory.New(). - WithInformersQueueKeyFunc(func(obj runtime.Object) string { - key, err := cache.DeletionHandlingMetaNamespaceKeyFunc(obj) - if err != nil { - utilruntime.HandleError(err) - return "" - } - return key - }, manifestWorkReplicaSetInformer.Informer()). + WithInformersQueueKeysFunc(queue.QueueKeyByMetaNamespaceName, manifestWorkReplicaSetInformer.Informer()). WithFilteredEventsInformersQueueKeyFunc(func(obj runtime.Object) string { accessor, _ := meta.Accessor(obj) labelValue, ok := accessor.GetLabels()[ManifestWorkReplicaSetControllerNameLabelKey] @@ -101,16 +95,9 @@ func NewManifestWorkReplicaSetController( return "" } return fmt.Sprintf("%s/%s", keys[0], keys[1]) - }, func(obj interface{}) bool { - accessor, err := meta.Accessor(obj) - if err != nil { - return false - } - if _, ok := accessor.GetLabels()[ManifestWorkReplicaSetControllerNameLabelKey]; ok { - return true - } - return false - }, manifestWorkInformer.Informer()). + }, + queue.FileterByLabel(ManifestWorkReplicaSetControllerNameLabelKey), + manifestWorkInformer.Informer()). WithInformersQueueKeysFunc(controller.placementDecisionQueueKeysFunc, placeDecisionInformer.Informer()). WithInformersQueueKeysFunc(controller.placementQueueKeysFunc, placementInformer.Informer()). WithSync(controller.sync).ToController("ManifestWorkReplicaSetController", recorder) diff --git a/pkg/work/hub/controllers/manifestworkreplicasetcontroller/manifestworkreplicaset_index.go b/pkg/work/hub/controllers/manifestworkreplicasetcontroller/manifestworkreplicaset_index.go index ff3635691..fb0b47663 100644 --- a/pkg/work/hub/controllers/manifestworkreplicasetcontroller/manifestworkreplicaset_index.go +++ b/pkg/work/hub/controllers/manifestworkreplicasetcontroller/manifestworkreplicaset_index.go @@ -2,6 +2,7 @@ package manifestworkreplicasetcontroller import ( "fmt" + "strings" "k8s.io/apimachinery/pkg/api/meta" "k8s.io/apimachinery/pkg/runtime" @@ -71,7 +72,11 @@ func (m *ManifestWorkReplicaSetController) manifestWorkQueueKeyFunc(obj runtime. if !ok { return "" } - return key + keys := strings.Split(key, ".") + if len(keys) != 2 { + return "" + } + return fmt.Sprintf("%s/%s", keys[0], keys[1]) } func indexManifestWorkReplicaSetByPlacement(obj interface{}) ([]string, error) { diff --git a/pkg/work/hub/controllers/manifestworkreplicasetcontroller/manifestworkreplicaset_index_test.go b/pkg/work/hub/controllers/manifestworkreplicasetcontroller/manifestworkreplicaset_index_test.go index 07c36b5de..b811d0422 100644 --- a/pkg/work/hub/controllers/manifestworkreplicasetcontroller/manifestworkreplicaset_index_test.go +++ b/pkg/work/hub/controllers/manifestworkreplicasetcontroller/manifestworkreplicaset_index_test.go @@ -111,7 +111,7 @@ func TestPlaceMWControllerIndex(t *testing.T) { // Check manifestWork Queue Keys key := pmwController.manifestWorkQueueKeyFunc(mw) - if key != mwrSetTest.Namespace+"."+mwrSetTest.Name { + if key != mwrSetTest.Namespace+"/"+mwrSetTest.Name { t.Fatal("Expected manifestwork key not match", key, " - ", mwrSetTest.Name) } // Check manifestWork Queue Keys not exist diff --git a/pkg/work/spoke/controllers/appliedmanifestcontroller/appliedmanifestwork_controller.go b/pkg/work/spoke/controllers/appliedmanifestcontroller/appliedmanifestwork_controller.go index c0d2cdd3e..bcac940c5 100644 --- a/pkg/work/spoke/controllers/appliedmanifestcontroller/appliedmanifestwork_controller.go +++ b/pkg/work/spoke/controllers/appliedmanifestcontroller/appliedmanifestwork_controller.go @@ -10,9 +10,7 @@ import ( "github.com/openshift/library-go/pkg/controller/factory" "github.com/openshift/library-go/pkg/operator/events" "k8s.io/apimachinery/pkg/api/errors" - "k8s.io/apimachinery/pkg/api/meta" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/runtime/schema" utilerrors "k8s.io/apimachinery/pkg/util/errors" "k8s.io/client-go/dynamic" @@ -25,6 +23,7 @@ import ( workapiv1 "open-cluster-management.io/api/work/v1" "open-cluster-management.io/ocm/pkg/common/patcher" + "open-cluster-management.io/ocm/pkg/common/queue" "open-cluster-management.io/ocm/pkg/work/helper" ) @@ -61,10 +60,7 @@ func NewAppliedManifestWorkController( } return factory.New(). - WithInformersQueueKeyFunc(func(obj runtime.Object) string { - accessor, _ := meta.Accessor(obj) - return accessor.GetName() - }, manifestWorkInformer.Informer()). + WithInformersQueueKeysFunc(queue.QueueKeyByMetaName, manifestWorkInformer.Informer()). WithFilteredEventsInformersQueueKeyFunc( helper.AppliedManifestworkQueueKeyFunc(hubHash), helper.AppliedManifestworkHubHashFilter(hubHash), diff --git a/pkg/work/spoke/controllers/finalizercontroller/add_finalizer_controller.go b/pkg/work/spoke/controllers/finalizercontroller/add_finalizer_controller.go index bdcaef3e5..8693cf0b1 100644 --- a/pkg/work/spoke/controllers/finalizercontroller/add_finalizer_controller.go +++ b/pkg/work/spoke/controllers/finalizercontroller/add_finalizer_controller.go @@ -6,8 +6,6 @@ import ( "github.com/openshift/library-go/pkg/controller/factory" "github.com/openshift/library-go/pkg/operator/events" "k8s.io/apimachinery/pkg/api/errors" - "k8s.io/apimachinery/pkg/api/meta" - "k8s.io/apimachinery/pkg/runtime" "k8s.io/klog/v2" workv1client "open-cluster-management.io/api/client/work/clientset/versioned/typed/work/v1" @@ -16,6 +14,7 @@ import ( workapiv1 "open-cluster-management.io/api/work/v1" "open-cluster-management.io/ocm/pkg/common/patcher" + "open-cluster-management.io/ocm/pkg/common/queue" "open-cluster-management.io/ocm/pkg/work/spoke/controllers" ) @@ -41,10 +40,7 @@ func NewAddFinalizerController( } return factory.New(). - WithInformersQueueKeyFunc(func(obj runtime.Object) string { - accessor, _ := meta.Accessor(obj) - return accessor.GetName() - }, manifestWorkInformer.Informer()). + WithInformersQueueKeysFunc(queue.QueueKeyByMetaName, manifestWorkInformer.Informer()). WithSync(controller.sync).ToController("ManifestWorkAddFinalizerController", recorder) } diff --git a/pkg/work/spoke/controllers/finalizercontroller/appliedmanifestwork_finalize_controller.go b/pkg/work/spoke/controllers/finalizercontroller/appliedmanifestwork_finalize_controller.go index 1c325c91f..b804a6cd4 100644 --- a/pkg/work/spoke/controllers/finalizercontroller/appliedmanifestwork_finalize_controller.go +++ b/pkg/work/spoke/controllers/finalizercontroller/appliedmanifestwork_finalize_controller.go @@ -8,8 +8,6 @@ import ( "github.com/openshift/library-go/pkg/controller/factory" "github.com/openshift/library-go/pkg/operator/events" "k8s.io/apimachinery/pkg/api/errors" - "k8s.io/apimachinery/pkg/api/meta" - "k8s.io/apimachinery/pkg/runtime" utilerrors "k8s.io/apimachinery/pkg/util/errors" "k8s.io/client-go/dynamic" "k8s.io/client-go/util/workqueue" @@ -21,6 +19,7 @@ import ( workapiv1 "open-cluster-management.io/api/work/v1" "open-cluster-management.io/ocm/pkg/common/patcher" + "open-cluster-management.io/ocm/pkg/common/queue" "open-cluster-management.io/ocm/pkg/work/helper" "open-cluster-management.io/ocm/pkg/work/spoke/controllers" ) @@ -52,10 +51,8 @@ func NewAppliedManifestWorkFinalizeController( } return factory.New(). - WithFilteredEventsInformersQueueKeyFunc(func(obj runtime.Object) string { - accessor, _ := meta.Accessor(obj) - return accessor.GetName() - }, helper.AppliedManifestworkAgentIDFilter(agentID), appliedManifestWorkInformer.Informer()). + WithFilteredEventsInformersQueueKeysFunc(queue.QueueKeyByMetaName, + helper.AppliedManifestworkAgentIDFilter(agentID), appliedManifestWorkInformer.Informer()). WithSync(controller.sync).ToController("AppliedManifestWorkFinalizer", recorder) } diff --git a/pkg/work/spoke/controllers/finalizercontroller/manifestwork_finalize_controller.go b/pkg/work/spoke/controllers/finalizercontroller/manifestwork_finalize_controller.go index 12cd24c4a..88f91fbff 100644 --- a/pkg/work/spoke/controllers/finalizercontroller/manifestwork_finalize_controller.go +++ b/pkg/work/spoke/controllers/finalizercontroller/manifestwork_finalize_controller.go @@ -8,9 +8,7 @@ import ( "github.com/openshift/library-go/pkg/controller/factory" "github.com/openshift/library-go/pkg/operator/events" "k8s.io/apimachinery/pkg/api/errors" - "k8s.io/apimachinery/pkg/api/meta" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/runtime" "k8s.io/client-go/util/workqueue" "k8s.io/klog/v2" @@ -20,6 +18,7 @@ import ( workapiv1 "open-cluster-management.io/api/work/v1" "open-cluster-management.io/ocm/pkg/common/patcher" + "open-cluster-management.io/ocm/pkg/common/queue" "open-cluster-management.io/ocm/pkg/work/helper" "open-cluster-management.io/ocm/pkg/work/spoke/controllers" ) @@ -56,10 +55,7 @@ func NewManifestWorkFinalizeController( } return factory.New(). - WithInformersQueueKeyFunc(func(obj runtime.Object) string { - accessor, _ := meta.Accessor(obj) - return accessor.GetName() - }, manifestWorkInformer.Informer()). + WithInformersQueueKeysFunc(queue.QueueKeyByMetaName, manifestWorkInformer.Informer()). WithFilteredEventsInformersQueueKeyFunc( helper.AppliedManifestworkQueueKeyFunc(hubHash), helper.AppliedManifestworkHubHashFilter(hubHash), diff --git a/pkg/work/spoke/controllers/finalizercontroller/unmanaged_appliedmanifestwork_controller.go b/pkg/work/spoke/controllers/finalizercontroller/unmanaged_appliedmanifestwork_controller.go index 3c7a5a230..6e00d6252 100644 --- a/pkg/work/spoke/controllers/finalizercontroller/unmanaged_appliedmanifestwork_controller.go +++ b/pkg/work/spoke/controllers/finalizercontroller/unmanaged_appliedmanifestwork_controller.go @@ -21,6 +21,7 @@ import ( workapiv1 "open-cluster-management.io/api/work/v1" "open-cluster-management.io/ocm/pkg/common/patcher" + "open-cluster-management.io/ocm/pkg/common/queue" "open-cluster-management.io/ocm/pkg/work/helper" ) @@ -71,11 +72,9 @@ func NewUnManagedAppliedWorkController( accessor, _ := meta.Accessor(obj) return fmt.Sprintf("%s-%s", hubHash, accessor.GetName()) }, manifestWorkInformer.Informer()). - WithFilteredEventsInformersQueueKeyFunc( - func(obj runtime.Object) string { - accessor, _ := meta.Accessor(obj) - return accessor.GetName() - }, helper.AppliedManifestworkAgentIDFilter(agentID), appliedManifestWorkInformer.Informer()). + WithFilteredEventsInformersQueueKeysFunc( + queue.QueueKeyByMetaName, + helper.AppliedManifestworkAgentIDFilter(agentID), appliedManifestWorkInformer.Informer()). WithSync(controller.sync).ToController("UnManagedAppliedManifestWork", recorder) } diff --git a/pkg/work/spoke/controllers/manifestcontroller/manifestwork_controller.go b/pkg/work/spoke/controllers/manifestcontroller/manifestwork_controller.go index 5df84e5de..518a88ba5 100644 --- a/pkg/work/spoke/controllers/manifestcontroller/manifestwork_controller.go +++ b/pkg/work/spoke/controllers/manifestcontroller/manifestwork_controller.go @@ -27,6 +27,7 @@ import ( workapiv1 "open-cluster-management.io/api/work/v1" "open-cluster-management.io/ocm/pkg/common/patcher" + "open-cluster-management.io/ocm/pkg/common/queue" "open-cluster-management.io/ocm/pkg/work/helper" "open-cluster-management.io/ocm/pkg/work/spoke/apply" "open-cluster-management.io/ocm/pkg/work/spoke/auth" @@ -96,10 +97,7 @@ func NewManifestWorkController( } return factory.New(). - WithInformersQueueKeyFunc(func(obj runtime.Object) string { - accessor, _ := meta.Accessor(obj) - return accessor.GetName() - }, manifestWorkInformer.Informer()). + WithInformersQueueKeysFunc(queue.QueueKeyByMetaName, manifestWorkInformer.Informer()). WithFilteredEventsInformersQueueKeyFunc( helper.AppliedManifestworkQueueKeyFunc(hubHash), helper.AppliedManifestworkHubHashFilter(hubHash), diff --git a/pkg/work/spoke/controllers/statuscontroller/availablestatus_controller.go b/pkg/work/spoke/controllers/statuscontroller/availablestatus_controller.go index 188fbd413..c3d27c441 100644 --- a/pkg/work/spoke/controllers/statuscontroller/availablestatus_controller.go +++ b/pkg/work/spoke/controllers/statuscontroller/availablestatus_controller.go @@ -13,7 +13,6 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "k8s.io/apimachinery/pkg/labels" - "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/runtime/schema" utilerrors "k8s.io/apimachinery/pkg/util/errors" "k8s.io/client-go/dynamic" @@ -25,6 +24,7 @@ import ( workapiv1 "open-cluster-management.io/api/work/v1" "open-cluster-management.io/ocm/pkg/common/patcher" + "open-cluster-management.io/ocm/pkg/common/queue" "open-cluster-management.io/ocm/pkg/work/helper" "open-cluster-management.io/ocm/pkg/work/spoke/controllers" "open-cluster-management.io/ocm/pkg/work/spoke/statusfeedback" @@ -62,10 +62,7 @@ func NewAvailableStatusController( } return factory.New(). - WithInformersQueueKeyFunc(func(obj runtime.Object) string { - accessor, _ := meta.Accessor(obj) - return accessor.GetName() - }, manifestWorkInformer.Informer()). + WithInformersQueueKeysFunc(queue.QueueKeyByMetaName, manifestWorkInformer.Informer()). WithSync(controller.sync).ResyncEvery(syncInterval).ToController("AvailableStatusController", recorder) }