Deprecate owner label(karmada.io/created-by) set to Work object (#184)

Signed-off-by: RainbowMango <renhongcai@huawei.com>
This commit is contained in:
Hongcai Ren 2021-03-04 11:20:26 +08:00 committed by GitHub
parent abaf2b2bee
commit 3e31afdf4f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 13 deletions

View File

@ -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{

View File

@ -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