From 3e31afdf4fb74bd9f9a97717dce5eaa0d17311fe Mon Sep 17 00:00:00 2001 From: Hongcai Ren Date: Thu, 4 Mar 2021 11:20:26 +0800 Subject: [PATCH] Deprecate owner label(karmada.io/created-by) set to Work object (#184) Signed-off-by: RainbowMango --- pkg/controllers/binding/binding_controller.go | 22 +++++++++---------- pkg/util/constants.go | 12 ++++++++-- 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/pkg/controllers/binding/binding_controller.go b/pkg/controllers/binding/binding_controller.go index 69cf2c769..896925c1e 100644 --- a/pkg/controllers/binding/binding_controller.go +++ b/pkg/controllers/binding/binding_controller.go @@ -10,7 +10,6 @@ import ( "k8s.io/apimachinery/pkg/labels" "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/runtime/schema" - "k8s.io/apimachinery/pkg/selection" "k8s.io/apimachinery/pkg/util/sets" "k8s.io/client-go/dynamic" "k8s.io/client-go/tools/record" @@ -79,8 +78,7 @@ func (c *PropagationBindingController) isBindingReady(binding *v1alpha1.Propagat // syncBinding will sync propagationBinding to Works. func (c *PropagationBindingController) syncBinding(binding *v1alpha1.PropagationBinding) (controllerruntime.Result, error) { clusterNames := c.getBindingClusterNames(binding) - ownerLabel := names.GenerateOwnerLabelValue(binding.GetNamespace(), binding.GetName()) - works, err := c.findOrphanWorks(ownerLabel, clusterNames) + works, err := c.findOrphanWorks(binding.Namespace, binding.Name, clusterNames) if err != nil { klog.Errorf("Failed to find orphan works by propagationBinding %s/%s. Error: %v.", binding.GetNamespace(), binding.GetName(), err) @@ -116,13 +114,12 @@ func (c *PropagationBindingController) removeOrphanWorks(works []v1alpha1.Work) } // findOrphanWorks will find orphan works that don't match current propagationBinding clusters. -func (c *PropagationBindingController) findOrphanWorks(ownerLabel string, clusterNames []string) ([]v1alpha1.Work, error) { - labelRequirement, err := labels.NewRequirement(util.OwnerLabel, selection.Equals, []string{ownerLabel}) - if err != nil { - klog.Errorf("Failed to new a requirement. Error: %v", err) - return nil, err - } - selector := labels.NewSelector().Add(*labelRequirement) +func (c *PropagationBindingController) findOrphanWorks(bindingNamespace string, bindingName string, clusterNames []string) ([]v1alpha1.Work, error) { + selector := labels.SelectorFromSet(labels.Set{ + util.ResourceBindingNamespaceLabel: bindingNamespace, + util.ResourceBindingNameLabel: bindingName, + }) + workList := &v1alpha1.WorkList{} if err := c.Client.List(context.TODO(), workList, &client.ListOptions{LabelSelector: selector}); err != nil { return nil, err @@ -227,7 +224,10 @@ func (c *PropagationBindingController) ensureWork(workload *unstructured.Unstruc OwnerReferences: []metav1.OwnerReference{ *metav1.NewControllerRef(binding, controllerKind), }, - Labels: map[string]string{util.OwnerLabel: names.GenerateOwnerLabelValue(binding.GetNamespace(), binding.GetName())}, + Labels: map[string]string{ + util.ResourceBindingNamespaceLabel: binding.Namespace, + util.ResourceBindingNameLabel: binding.Name, + }, }, Spec: v1alpha1.WorkSpec{ Workload: v1alpha1.WorkloadTemplate{ diff --git a/pkg/util/constants.go b/pkg/util/constants.go index 442b3e343..fdc27bb8d 100644 --- a/pkg/util/constants.go +++ b/pkg/util/constants.go @@ -10,11 +10,19 @@ const ( // ClusterPropagationPolicyLabel is added to objects to specify associated ClusterPropagationPolicy. ClusterPropagationPolicyLabel = "clusterpropagationpolicy.karmada.io/name" + // ResourceBindingNamespaceLabel is added to objects to specify associated ResourceBinding's namespace. + ResourceBindingNamespaceLabel = "resourcebinding.karmada.io/namespace" + + // ResourceBindingNameLabel is added to objects to specify associated ResourceBinding's name. + ResourceBindingNameLabel = "resourcebinding.karmada.io/name" + + // ClusterResourceBindingLabel is added to objects to specify associated ClusterResourceBinding. + ClusterResourceBindingLabel = "clusterresourcebinding.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. - // example2: set it in Work, the label value is propagationBinding. - // example3: set it in Work, the label value is HPA. + // example2: set it in Work, the label value is HPA. OwnerLabel = "karmada.io/created-by" // OverrideClaimKey will set in Work resource, indicates that // the resource is overridden by override policies