Skip to content

Commit

Permalink
Move index func from addon framework to ocm
Browse files Browse the repository at this point in the history
Signed-off-by: zhujian <[email protected]>
  • Loading branch information
zhujian7 committed Nov 29, 2024
1 parent ddf4c0d commit 96626c0
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 102 deletions.
6 changes: 2 additions & 4 deletions pkg/addonmanager/cloudevents/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
)
Expand All @@ -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
Expand Down
6 changes: 2 additions & 4 deletions pkg/addonmanager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
)
Expand All @@ -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
Expand Down
95 changes: 1 addition & 94 deletions pkg/index/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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"
Expand Down

0 comments on commit 96626c0

Please sign in to comment.