From 61a946aff7de76ca4b137452ad7da8403f1719bd Mon Sep 17 00:00:00 2001 From: iawia002 Date: Fri, 24 Sep 2021 10:52:42 +0800 Subject: [PATCH] Move GenerateKey function out of the worker file Signed-off-by: iawia002 --- .../status/workstatus_controller.go | 33 ++++++++++++++++- pkg/util/worker.go | 37 ------------------- 2 files changed, 32 insertions(+), 38 deletions(-) diff --git a/pkg/controllers/status/workstatus_controller.go b/pkg/controllers/status/workstatus_controller.go index dcaf35256..e29e4a3e7 100644 --- a/pkg/controllers/status/workstatus_controller.go +++ b/pkg/controllers/status/workstatus_controller.go @@ -108,10 +108,41 @@ func (c *WorkStatusController) getEventHandler() cache.ResourceEventHandler { // RunWorkQueue initializes worker and run it, worker will process resource asynchronously. func (c *WorkStatusController) RunWorkQueue() { - c.worker = util.NewAsyncWorker("work-status", time.Second, util.GenerateKey, c.syncWorkStatus) + c.worker = util.NewAsyncWorker("work-status", time.Second, generateKey, c.syncWorkStatus) c.worker.Run(c.WorkerNumber, c.StopChan) } +// generateKey generates a key from obj, the key contains cluster, GVK, namespace and name. +func generateKey(obj interface{}) (util.QueueKey, error) { + resource := obj.(*unstructured.Unstructured) + cluster, err := getClusterNameFromLabel(resource) + if err != nil { + return "", err + } + // it happens when the obj not managed by Karmada. + if cluster == "" { + return "", nil + } + + return keys.FederatedKeyFunc(cluster, obj) +} + +// getClusterNameFromLabel gets cluster name from ownerLabel, if label not exist, means resource is not created by karmada. +func getClusterNameFromLabel(resource *unstructured.Unstructured) (string, error) { + workNamespace := util.GetLabelValue(resource.GetLabels(), workv1alpha1.WorkNamespaceLabel) + if len(workNamespace) == 0 { + klog.V(4).Infof("Ignore resource(%s/%s/%s) which not managed by karmada", resource.GetKind(), resource.GetNamespace(), resource.GetName()) + return "", nil + } + + cluster, err := names.GetClusterName(workNamespace) + if err != nil { + klog.Errorf("Failed to get cluster name from work namespace: %s, error: %v.", workNamespace, err) + return "", err + } + return cluster, nil +} + // syncWorkStatus will collect status of object referencing by key and update to work which holds the object. func (c *WorkStatusController) syncWorkStatus(key util.QueueKey) error { fedKey, ok := key.(keys.FederatedKey) diff --git a/pkg/util/worker.go b/pkg/util/worker.go index 53c986fc4..237cc5d7e 100644 --- a/pkg/util/worker.go +++ b/pkg/util/worker.go @@ -3,15 +3,10 @@ package util import ( "time" - "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/util/wait" "k8s.io/client-go/util/workqueue" "k8s.io/klog/v2" - - workv1alpha1 "github.com/karmada-io/karmada/pkg/apis/work/v1alpha1" - "github.com/karmada-io/karmada/pkg/util/informermanager/keys" - "github.com/karmada-io/karmada/pkg/util/names" ) const ( @@ -67,38 +62,6 @@ func NewAsyncWorker(name string, interval time.Duration, keyFunc KeyFunc, reconc } } -// GenerateKey generates a key from obj, the key contains cluster, GVK, namespace and name. -// TODO(RainbowMango): Move this function out of this file, to it's user. -func GenerateKey(obj interface{}) (QueueKey, error) { - resource := obj.(*unstructured.Unstructured) - cluster, err := getClusterNameFromLabel(resource) - if err != nil { - return "", err - } - // it happens when the obj not managed by Karmada. - if cluster == "" { - return nil, nil - } - - return keys.FederatedKeyFunc(cluster, obj) -} - -// getClusterNameFromLabel gets cluster name from ownerLabel, if label not exist, means resource is not created by karmada. -func getClusterNameFromLabel(resource *unstructured.Unstructured) (string, error) { - workNamespace := GetLabelValue(resource.GetLabels(), workv1alpha1.WorkNamespaceLabel) - if len(workNamespace) == 0 { - klog.V(4).Infof("Ignore resource(%s/%s/%s) which not managed by karmada", resource.GetKind(), resource.GetNamespace(), resource.GetName()) - return "", nil - } - - cluster, err := names.GetClusterName(workNamespace) - if err != nil { - klog.Errorf("Failed to get cluster name from work namespace: %s, error: %v.", workNamespace, err) - return "", err - } - return cluster, nil -} - func (w *asyncWorker) EnqueueRateLimited(obj runtime.Object) { key, err := w.keyFunc(obj) if err != nil {