From 288d24c7b996d7b71ca5f4a16ab00b7da4080e7a Mon Sep 17 00:00:00 2001 From: Hongcai Ren Date: Sat, 6 Mar 2021 09:28:01 +0800 Subject: [PATCH] Deprecate create by label from manifests (#191) Signed-off-by: RainbowMango --- pkg/controllers/binding/binding_controller.go | 22 +++++++++----- .../execution/execution_controller.go | 2 -- .../status/workstatus_controller.go | 29 ++++++------------- pkg/util/constants.go | 6 ++++ pkg/util/worker.go | 24 ++++----------- 5 files changed, 35 insertions(+), 48 deletions(-) diff --git a/pkg/controllers/binding/binding_controller.go b/pkg/controllers/binding/binding_controller.go index 896925c1e..ada10255e 100644 --- a/pkg/controllers/binding/binding_controller.go +++ b/pkg/controllers/binding/binding_controller.go @@ -203,6 +203,18 @@ func (c *PropagationBindingController) ensureWork(workload *unstructured.Unstruc return err } + workNamespace, err := names.GenerateExecutionSpaceName(clusterName) + if err != nil { + klog.Errorf("Failed to ensure Work for cluster: %s. Error: %v.", clusterName, err) + return err + } + workName := binding.Name + + util.MergeLabel(clonedWorkload, util.ResourceBindingNamespaceLabel, binding.Namespace) + util.MergeLabel(clonedWorkload, util.ResourceBindingNameLabel, binding.Name) + util.MergeLabel(clonedWorkload, util.WorkNamespaceLabel, workNamespace) + util.MergeLabel(clonedWorkload, util.WorkNameLabel, workName) + workloadJSON, err := clonedWorkload.MarshalJSON() if err != nil { klog.Errorf("Failed to marshal workload, kind: %s, namespace: %s, name: %s. Error: %v", @@ -210,16 +222,10 @@ func (c *PropagationBindingController) ensureWork(workload *unstructured.Unstruc return err } - executionSpace, err := names.GenerateExecutionSpaceName(clusterName) - if err != nil { - klog.Errorf("Failed to ensure Work for cluster: %s. Error: %v.", clusterName, err) - return err - } - work := &v1alpha1.Work{ ObjectMeta: metav1.ObjectMeta{ - Name: binding.Name, - Namespace: executionSpace, + Name: workName, + Namespace: workNamespace, Finalizers: []string{util.ExecutionControllerFinalizer}, OwnerReferences: []metav1.OwnerReference{ *metav1.NewControllerRef(binding, controllerKind), diff --git a/pkg/controllers/execution/execution_controller.go b/pkg/controllers/execution/execution_controller.go index 4f306d2ce..3fdb83a6e 100644 --- a/pkg/controllers/execution/execution_controller.go +++ b/pkg/controllers/execution/execution_controller.go @@ -180,8 +180,6 @@ func (c *Controller) syncToClusters(cluster *v1alpha1.Cluster, work *policyv1alp return err } - util.MergeLabel(workload, util.OwnerLabel, names.GenerateOwnerLabelValue(work.GetNamespace(), work.GetName())) - applied := c.isResourceApplied(&work.Status) if applied { // todo: get clusterObj from cache diff --git a/pkg/controllers/status/workstatus_controller.go b/pkg/controllers/status/workstatus_controller.go index e3bd7fb96..f49b3296d 100644 --- a/pkg/controllers/status/workstatus_controller.go +++ b/pkg/controllers/status/workstatus_controller.go @@ -116,28 +116,22 @@ func (c *WorkStatusController) syncWorkStatus(key util.QueueKey) error { klog.V(2).Infof("Ignore the event key %s which not managed by karmada.", key) return nil } - owner := util.GetLabelValue(obj.GetLabels(), util.OwnerLabel) - if len(owner) == 0 { - // Ignore the object which not managed by karmada. - // TODO(RainbowMango): Consider to add event filter to informer event handler to skip event from enqueue. - klog.V(2).Infof("Ignore the event of %s(%s/%s) which not managed by karmada.", obj.GetKind(), obj.GetNamespace(), obj.GetName()) + + workNamespace := util.GetLabelValue(obj.GetLabels(), util.WorkNamespaceLabel) + workName := util.GetLabelValue(obj.GetLabels(), util.WorkNameLabel) + if len(workNamespace) == 0 || len(workName) == 0 { + klog.Infof("Ignore object(%s) which not managed by karmada.", keyStr) return nil } - ownerNamespace, ownerName, err := names.GetNamespaceAndName(owner) - if err != nil { - klog.Errorf("Failed to parse object(%s/%s) owner by label: %s", obj.GetNamespace(), obj.GetName(), owner) - return err - } - workObject := &v1alpha1.Work{} - if err := c.Client.Get(context.TODO(), client.ObjectKey{Namespace: ownerNamespace, Name: ownerName}, workObject); err != nil { + if err := c.Client.Get(context.TODO(), client.ObjectKey{Namespace: workNamespace, Name: workName}, workObject); err != nil { // Stop processing if resource no longer exist. if errors.IsNotFound(err) { return nil } - klog.Errorf("Failed to get Work(%s/%s) from cache: %v", ownerNamespace, ownerName, err) + klog.Errorf("Failed to get Work(%s/%s) from cache: %v", workNamespace, workName, err) return err } @@ -147,9 +141,7 @@ func (c *WorkStatusController) syncWorkStatus(key util.QueueKey) error { return err } - util.MergeLabel(desireObj, util.OwnerLabel, names.GenerateOwnerLabelValue(workObject.GetNamespace(), workObject.GetName())) - - clusterName, err := names.GetClusterName(ownerNamespace) + clusterName, err := names.GetClusterName(workNamespace) if err != nil { klog.Errorf("Failed to get member cluster name: %v", err) return err @@ -165,7 +157,7 @@ func (c *WorkStatusController) syncWorkStatus(key util.QueueKey) error { return c.ObjectWatcher.Update(clusterName, desireObj, obj) } - klog.Infof("reflecting %s(%s/%s) status of to Work(%s/%s)", obj.GetKind(), obj.GetNamespace(), obj.GetName(), ownerNamespace, ownerName) + klog.Infof("reflecting %s(%s/%s) status to Work(%s/%s)", obj.GetKind(), obj.GetNamespace(), obj.GetName(), workNamespace, workName) return c.reflectStatus(workObject, obj) } @@ -213,9 +205,6 @@ func (c *WorkStatusController) recreateResourceIfNeeded(work *v1alpha1.Work, clu if reflect.DeepEqual(desiredGVK, clusterWorkload.GVK) && manifest.GetNamespace() == clusterWorkload.Namespace && manifest.GetName() == clusterWorkload.Name { - - util.MergeLabel(manifest, util.OwnerLabel, names.GenerateOwnerLabelValue(work.GetNamespace(), work.GetName())) - klog.Infof("recreating %s/%s/%s in member cluster %s", clusterWorkload.GVK.Kind, clusterWorkload.Namespace, clusterWorkload.Name, clusterWorkload.Cluster) return c.ObjectWatcher.Create(clusterWorkload.Cluster, manifest) } diff --git a/pkg/util/constants.go b/pkg/util/constants.go index d355d7be6..1e5fbe725 100644 --- a/pkg/util/constants.go +++ b/pkg/util/constants.go @@ -19,6 +19,12 @@ const ( // ClusterResourceBindingLabel is added to objects to specify associated ClusterResourceBinding. ClusterResourceBindingLabel = "clusterresourcebinding.karmada.io/name" + // WorkNamespaceLabel is added to objects to specify associated Work's namespace. + WorkNamespaceLabel = "work.karmada.io/namespace" + + // WorkNameLabel is added to objects to specify associated Work's name. + WorkNameLabel = "work.karmada.io/name" + // OwnerLabel will set in karmada CRDs, indicates that who created it. // We can use labelSelector to find who created it quickly. // example1: set it in propagationBinding, the label value is propagationPolicy. diff --git a/pkg/util/worker.go b/pkg/util/worker.go index 1f789a749..aa2a48ca6 100644 --- a/pkg/util/worker.go +++ b/pkg/util/worker.go @@ -106,26 +106,14 @@ func GenerateKey(obj interface{}) (QueueKey, error) { // getClusterNameFromLabel gets cluster name from ownerLabel, if label not exist, means resource is not created by karmada. func getClusterNameFromLabel(resource *unstructured.Unstructured) (string, error) { - workloadLabels := resource.GetLabels() - if workloadLabels == nil { - klog.V(2).Infof("Resource %s/%s/%s is not created by karmada.", resource.GetKind(), - resource.GetNamespace(), resource.GetName()) - return "", nil + workNamespace := GetLabelValue(resource.GetLabels(), WorkNamespaceLabel) + if len(workNamespace) == 0 { + klog.Infof("Resource(%s/%s/%s) not created by karmada", resource.GetKind(), resource.GetNamespace(), resource.GetName()) } - value, exist := workloadLabels[OwnerLabel] - if !exist { - klog.V(2).Infof("Resource %s/%s/%s is not created by karmada.", resource.GetKind(), - resource.GetNamespace(), resource.GetName()) - return "", nil - } - executionNamespace, _, err := names.GetNamespaceAndName(value) + + cluster, err := names.GetClusterName(workNamespace) if err != nil { - klog.Errorf("Failed to get executionNamespace from label %s", value) - return "", err - } - cluster, err := names.GetClusterName(executionNamespace) - if err != nil { - klog.Errorf("Failed to get member cluster name by %s. Error: %v.", value, err) + klog.Errorf("Failed to get cluster name from work namespace: %s, error: %v.", workNamespace, err) return "", err } return cluster, nil