Move GenerateKey function out of the worker file
Signed-off-by: iawia002 <z2d@jifangcheng.com>
This commit is contained in:
parent
41f1aed2bd
commit
61a946aff7
|
@ -108,10 +108,41 @@ func (c *WorkStatusController) getEventHandler() cache.ResourceEventHandler {
|
||||||
|
|
||||||
// RunWorkQueue initializes worker and run it, worker will process resource asynchronously.
|
// RunWorkQueue initializes worker and run it, worker will process resource asynchronously.
|
||||||
func (c *WorkStatusController) RunWorkQueue() {
|
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)
|
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.
|
// 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 {
|
func (c *WorkStatusController) syncWorkStatus(key util.QueueKey) error {
|
||||||
fedKey, ok := key.(keys.FederatedKey)
|
fedKey, ok := key.(keys.FederatedKey)
|
||||||
|
|
|
@ -3,15 +3,10 @@ package util
|
||||||
import (
|
import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
|
||||||
"k8s.io/apimachinery/pkg/runtime"
|
"k8s.io/apimachinery/pkg/runtime"
|
||||||
"k8s.io/apimachinery/pkg/util/wait"
|
"k8s.io/apimachinery/pkg/util/wait"
|
||||||
"k8s.io/client-go/util/workqueue"
|
"k8s.io/client-go/util/workqueue"
|
||||||
"k8s.io/klog/v2"
|
"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 (
|
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) {
|
func (w *asyncWorker) EnqueueRateLimited(obj runtime.Object) {
|
||||||
key, err := w.keyFunc(obj)
|
key, err := w.keyFunc(obj)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
Loading…
Reference in New Issue