From a3a80961df5aaeb46422b46e24a5b7f4ebe11534 Mon Sep 17 00:00:00 2001 From: Jian Zhu Date: Fri, 29 Nov 2024 15:32:30 +0800 Subject: [PATCH] Move index func from addon framework to ocm (#292) Signed-off-by: zhujian --- pkg/addonmanager/cloudevents/manager.go | 6 +- pkg/addonmanager/manager.go | 6 +- pkg/index/index.go | 95 +------------------------ 3 files changed, 5 insertions(+), 102 deletions(-) diff --git a/pkg/addonmanager/cloudevents/manager.go b/pkg/addonmanager/cloudevents/manager.go index 5bc2915e9..0282d9ba9 100644 --- a/pkg/addonmanager/cloudevents/manager.go +++ b/pkg/addonmanager/cloudevents/manager.go @@ -160,8 +160,7 @@ func (a *cloudeventsAddonManager) Start(ctx context.Context) error { err = addonInformers.Addon().V1alpha1().ManagedClusterAddOns().Informer().AddIndexers( cache.Indexers{ - index.ManagedClusterAddonByNamespace: index.IndexManagedClusterAddonByNamespace, // addonDeployController - index.ManagedClusterAddonByName: index.IndexManagedClusterAddonByName, // addonConfigController + index.ManagedClusterAddonByNamespace: index.IndexManagedClusterAddonByNamespace, // agentDeployController index.AddonByConfig: index.IndexAddonByConfig, // addonConfigController }, ) @@ -171,8 +170,7 @@ func (a *cloudeventsAddonManager) Start(ctx context.Context) error { err = addonInformers.Addon().V1alpha1().ClusterManagementAddOns().Informer().AddIndexers( cache.Indexers{ - index.ClusterManagementAddonByConfig: index.IndexClusterManagementAddonByConfig, // managementAddonConfigController - index.ClusterManagementAddonByPlacement: index.IndexClusterManagementAddonByPlacement, // addonConfigController + index.ClusterManagementAddonByConfig: index.IndexClusterManagementAddonByConfig, // cmaConfigController }) if err != nil { return err diff --git a/pkg/addonmanager/manager.go b/pkg/addonmanager/manager.go index f98a12310..8c0d3dd30 100644 --- a/pkg/addonmanager/manager.go +++ b/pkg/addonmanager/manager.go @@ -106,8 +106,7 @@ func (a *addonManager) Start(ctx context.Context) error { err = addonInformers.Addon().V1alpha1().ManagedClusterAddOns().Informer().AddIndexers( cache.Indexers{ - index.ManagedClusterAddonByNamespace: index.IndexManagedClusterAddonByNamespace, // addonDeployController - index.ManagedClusterAddonByName: index.IndexManagedClusterAddonByName, // addonConfigController + index.ManagedClusterAddonByNamespace: index.IndexManagedClusterAddonByNamespace, // agentDeployController index.AddonByConfig: index.IndexAddonByConfig, // addonConfigController }, ) @@ -117,8 +116,7 @@ func (a *addonManager) Start(ctx context.Context) error { err = addonInformers.Addon().V1alpha1().ClusterManagementAddOns().Informer().AddIndexers( cache.Indexers{ - index.ClusterManagementAddonByConfig: index.IndexClusterManagementAddonByConfig, // managementAddonConfigController - index.ClusterManagementAddonByPlacement: index.IndexClusterManagementAddonByPlacement, // addonConfigController + index.ClusterManagementAddonByConfig: index.IndexClusterManagementAddonByConfig, // cmaConfigController }) if err != nil { return err diff --git a/pkg/index/index.go b/pkg/index/index.go index 68f761362..dfb90624d 100644 --- a/pkg/index/index.go +++ b/pkg/index/index.go @@ -4,56 +4,17 @@ import ( "fmt" "strings" - "k8s.io/apimachinery/pkg/api/meta" - "k8s.io/apimachinery/pkg/runtime" - utilruntime "k8s.io/apimachinery/pkg/util/runtime" "k8s.io/apimachinery/pkg/util/sets" - "k8s.io/client-go/tools/cache" - "k8s.io/klog/v2" addonv1alpha1 "open-cluster-management.io/api/addon/v1alpha1" - addoninformerv1alpha1 "open-cluster-management.io/api/client/addon/informers/externalversions/addon/v1alpha1" - clusterv1beta1 "open-cluster-management.io/api/cluster/v1beta1" workapiv1 "open-cluster-management.io/api/work/v1" "open-cluster-management.io/addon-framework/pkg/addonmanager/constants" ) const ( - ClusterManagementAddonByPlacement = "clusterManagementAddonByPlacement" - ManagedClusterAddonByName = "managedClusterAddonByName" - ManagedClusterAddonByNamespace = "managedClusterAddonByNamespace" + ManagedClusterAddonByNamespace = "managedClusterAddonByNamespace" ) -func IndexClusterManagementAddonByPlacement(obj interface{}) ([]string, error) { - cma, ok := obj.(*addonv1alpha1.ClusterManagementAddOn) - - if !ok { - return []string{}, fmt.Errorf("obj %T is not a ClusterManagementAddon", obj) - } - - var keys []string - if cma.Spec.InstallStrategy.Type == "" || cma.Spec.InstallStrategy.Type == addonv1alpha1.AddonInstallStrategyManual { - return keys, nil - } - - for _, placement := range cma.Spec.InstallStrategy.Placements { - key := fmt.Sprintf("%s/%s", placement.PlacementRef.Namespace, placement.PlacementRef.Name) - keys = append(keys, key) - } - - return keys, nil -} - -func IndexManagedClusterAddonByName(obj interface{}) ([]string, error) { - mca, ok := obj.(*addonv1alpha1.ManagedClusterAddOn) - - if !ok { - return []string{}, fmt.Errorf("obj %T is not a ManagedClusterAddon", obj) - } - - return []string{mca.Name}, nil -} - func IndexManagedClusterAddonByNamespace(obj interface{}) ([]string, error) { mca, ok := obj.(*addonv1alpha1.ManagedClusterAddOn) @@ -64,60 +25,6 @@ func IndexManagedClusterAddonByNamespace(obj interface{}) ([]string, error) { return []string{mca.Namespace}, nil } -func ClusterManagementAddonByPlacementQueueKey( - cmai addoninformerv1alpha1.ClusterManagementAddOnInformer) func(obj runtime.Object) []string { - return func(obj runtime.Object) []string { - key, err := cache.DeletionHandlingMetaNamespaceKeyFunc(obj) - if err != nil { - utilruntime.HandleError(err) - return []string{} - } - - objs, err := cmai.Informer().GetIndexer().ByIndex(ClusterManagementAddonByPlacement, key) - if err != nil { - utilruntime.HandleError(err) - return []string{} - } - - var keys []string - for _, o := range objs { - cma := o.(*addonv1alpha1.ClusterManagementAddOn) - klog.V(4).Infof("enqueue ClusterManagementAddon %s, because of placement %s", cma.Name, key) - keys = append(keys, cma.Name) - } - - return keys - } -} - -func ClusterManagementAddonByPlacementDecisionQueueKey( - cmai addoninformerv1alpha1.ClusterManagementAddOnInformer) func(obj runtime.Object) []string { - return func(obj runtime.Object) []string { - accessor, _ := meta.Accessor(obj) - placementName, ok := accessor.GetLabels()[clusterv1beta1.PlacementLabel] - if !ok { - return []string{} - } - - objs, err := cmai.Informer().GetIndexer().ByIndex(ClusterManagementAddonByPlacement, - fmt.Sprintf("%s/%s", accessor.GetNamespace(), placementName)) - if err != nil { - utilruntime.HandleError(err) - return []string{} - } - - var keys []string - for _, o := range objs { - cma := o.(*addonv1alpha1.ClusterManagementAddOn) - klog.V(4).Infof("enqueue ClusterManagementAddon %s, because of placementDecision %s/%s", - cma.Name, accessor.GetNamespace(), accessor.GetName()) - keys = append(keys, cma.Name) - } - - return keys - } -} - const ( ManifestWorkByAddon = "manifestWorkByAddon" ManifestWorkByHostedAddon = "manifestWorkByHostedAddon"