Skip to content

Commit f8d6be5

Browse files
Deprecate key labels of rb/crb
Signed-off-by: whitewindmills <[email protected]>
1 parent f369c55 commit f8d6be5

File tree

11 files changed

+56
-182
lines changed

11 files changed

+56
-182
lines changed

Diff for: pkg/apis/work/v1alpha1/well_known_constants.go

-9
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,6 @@ limitations under the License.
1717
package v1alpha1
1818

1919
const (
20-
// ResourceBindingNamespaceLabel is added to objects to specify associated ResourceBinding's namespace.
21-
ResourceBindingNamespaceLabel = "resourcebinding.karmada.io/namespace"
22-
23-
// ResourceBindingNameLabel is added to objects to specify associated ResourceBinding's name.
24-
ResourceBindingNameLabel = "resourcebinding.karmada.io/name"
25-
26-
// ClusterResourceBindingLabel is added to objects to specify associated ClusterResourceBinding.
27-
ClusterResourceBindingLabel = "clusterresourcebinding.karmada.io/name"
28-
2920
// WorkNamespaceLabel is added to objects to specify associated Work's namespace.
3021
WorkNamespaceLabel = "work.karmada.io/namespace"
3122

Diff for: pkg/apis/work/v1alpha2/well_known_constants.go

-10
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,6 @@ const (
4040
// WorkNameAnnotation is added to objects to specify associated Work's name.
4141
WorkNameAnnotation = "work.karmada.io/name"
4242

43-
// ResourceBindingReferenceKey is the key of ResourceBinding object.
44-
// It is usually a unique hash value of ResourceBinding object's namespace and name, intended to be added to the Work object.
45-
// It will be used to retrieve all Works objects that derived from a specific ResourceBinding object.
46-
ResourceBindingReferenceKey = "resourcebinding.karmada.io/key"
47-
48-
// ClusterResourceBindingReferenceKey is the key of ClusterResourceBinding object.
49-
// It is usually a unique hash value of ClusterResourceBinding object's namespace and name, intended to be added to the Work object.
50-
// It will be used to retrieve all Works objects that derived by a specific ClusterResourceBinding object.
51-
ClusterResourceBindingReferenceKey = "clusterresourcebinding.karmada.io/key"
52-
5343
// ResourceBindingNamespaceAnnotationKey is added to object to describe the associated ResourceBinding's namespace.
5444
// It is added to:
5545
// - Work object: describes the namespace of ResourceBinding which the Work derived from.

Diff for: pkg/controllers/binding/binding_controller.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ func (c *ResourceBindingController) Reconcile(ctx context.Context, req controlle
8282

8383
if !binding.DeletionTimestamp.IsZero() {
8484
klog.V(4).Infof("Begin to delete works owned by binding(%s).", req.NamespacedName.String())
85-
if err := helper.DeleteWorkByRBNamespaceAndName(c.Client, req.Namespace, req.Name); err != nil {
85+
if err := helper.DeleteWorks(c.Client, req.Namespace, req.Name, binding.Labels[workv1alpha2.ResourceBindingPermanentIDLabel]); err != nil {
8686
klog.Errorf("Failed to delete works related to %s/%s: %v", binding.GetNamespace(), binding.GetName(), err)
8787
return controllerruntime.Result{}, err
8888
}
@@ -143,7 +143,8 @@ func (c *ResourceBindingController) syncBinding(binding *workv1alpha2.ResourceBi
143143
}
144144

145145
func (c *ResourceBindingController) removeOrphanWorks(binding *workv1alpha2.ResourceBinding) error {
146-
works, err := helper.FindOrphanWorks(c.Client, binding.Namespace, binding.Name, helper.ObtainBindingSpecExistingClusters(binding.Spec))
146+
works, err := helper.FindOrphanWorks(c.Client, binding.Namespace, binding.Name,
147+
binding.Labels[workv1alpha2.ResourceBindingPermanentIDLabel], helper.ObtainBindingSpecExistingClusters(binding.Spec))
147148
if err != nil {
148149
klog.Errorf("Failed to find orphan works by resourceBinding(%s/%s). Error: %v.",
149150
binding.GetNamespace(), binding.GetName(), err)

Diff for: pkg/controllers/binding/cluster_resource_binding_controller.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ func (c *ClusterResourceBindingController) Reconcile(ctx context.Context, req co
8282

8383
if !clusterResourceBinding.DeletionTimestamp.IsZero() {
8484
klog.V(4).Infof("Begin to delete works owned by binding(%s).", req.NamespacedName.String())
85-
if err := helper.DeleteWorkByCRBName(c.Client, req.Name); err != nil {
85+
if err := helper.DeleteWorks(c.Client, "", req.Name, clusterResourceBinding.Labels[workv1alpha2.ClusterResourceBindingPermanentIDLabel]); err != nil {
8686
klog.Errorf("Failed to delete works related to %s: %v", clusterResourceBinding.GetName(), err)
8787
return controllerruntime.Result{}, err
8888
}
@@ -142,7 +142,7 @@ func (c *ClusterResourceBindingController) syncBinding(binding *workv1alpha2.Clu
142142
}
143143

144144
func (c *ClusterResourceBindingController) removeOrphanWorks(binding *workv1alpha2.ClusterResourceBinding) error {
145-
works, err := helper.FindOrphanWorks(c.Client, "", binding.Name, helper.ObtainBindingSpecExistingClusters(binding.Spec))
145+
works, err := helper.FindOrphanWorks(c.Client, "", binding.Name, binding.Labels[workv1alpha2.ClusterResourceBindingPermanentIDLabel], helper.ObtainBindingSpecExistingClusters(binding.Spec))
146146
if err != nil {
147147
klog.Errorf("Failed to find orphan works by ClusterResourceBinding(%s). Error: %v.", binding.GetName(), err)
148148
c.EventRecorder.Event(binding, corev1.EventTypeWarning, events.EventReasonCleanupWorkFailed, err.Error())

Diff for: pkg/controllers/binding/common.go

+1-7
Original file line numberDiff line numberDiff line change
@@ -157,17 +157,11 @@ func mergeLabel(workload *unstructured.Unstructured, binding metav1.Object, scop
157157
util.MergeLabel(workload, util.ManagedByKarmadaLabel, util.ManagedByKarmadaLabelValue)
158158
if scope == apiextensionsv1.NamespaceScoped {
159159
bindingID := util.GetLabelValue(binding.GetLabels(), workv1alpha2.ResourceBindingPermanentIDLabel)
160-
util.MergeLabel(workload, workv1alpha2.ResourceBindingReferenceKey, names.GenerateBindingReferenceKey(binding.GetNamespace(), binding.GetName()))
161160
util.MergeLabel(workload, workv1alpha2.ResourceBindingPermanentIDLabel, bindingID)
162-
163-
workLabel[workv1alpha2.ResourceBindingReferenceKey] = names.GenerateBindingReferenceKey(binding.GetNamespace(), binding.GetName())
164161
workLabel[workv1alpha2.ResourceBindingPermanentIDLabel] = bindingID
165162
} else {
166163
bindingID := util.GetLabelValue(binding.GetLabels(), workv1alpha2.ClusterResourceBindingPermanentIDLabel)
167-
util.MergeLabel(workload, workv1alpha2.ClusterResourceBindingReferenceKey, names.GenerateBindingReferenceKey("", binding.GetName()))
168164
util.MergeLabel(workload, workv1alpha2.ClusterResourceBindingPermanentIDLabel, bindingID)
169-
170-
workLabel[workv1alpha2.ClusterResourceBindingReferenceKey] = names.GenerateBindingReferenceKey("", binding.GetName())
171165
workLabel[workv1alpha2.ClusterResourceBindingPermanentIDLabel] = bindingID
172166
}
173167
return workLabel
@@ -234,7 +228,7 @@ func mergeConflictResolution(workload *unstructured.Unstructured, conflictResolu
234228
return annotations
235229
} else if conflictResolutionInRT != "" {
236230
// ignore its value and add logs if conflictResolutionInRT is neither abort nor overwrite.
237-
klog.Warningf("ignore the invalid conflict-resolution annotation in ResourceTemplate %s/%s/%s: %s",
231+
klog.Warningf("Ignore the invalid conflict-resolution annotation in ResourceTemplate %s/%s/%s: %s",
238232
workload.GetKind(), workload.GetNamespace(), workload.GetName(), conflictResolutionInRT)
239233
}
240234

Diff for: pkg/controllers/binding/common_test.go

+10-4
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import (
2626

2727
policyv1alpha1 "github.com/karmada-io/karmada/pkg/apis/policy/v1alpha1"
2828
workv1alpha2 "github.com/karmada-io/karmada/pkg/apis/work/v1alpha2"
29-
"github.com/karmada-io/karmada/pkg/util/names"
3029
)
3130

3231
func Test_mergeTargetClusters(t *testing.T) {
@@ -136,7 +135,6 @@ func Test_mergeLabel(t *testing.T) {
136135
scope: v1.NamespaceScoped,
137136
want: map[string]string{
138137
workv1alpha2.ResourceBindingPermanentIDLabel: rbID,
139-
workv1alpha2.ResourceBindingReferenceKey: names.GenerateBindingReferenceKey(namespace, bindingName),
140138
},
141139
},
142140
{
@@ -161,13 +159,21 @@ func Test_mergeLabel(t *testing.T) {
161159
scope: v1.ClusterScoped,
162160
want: map[string]string{
163161
workv1alpha2.ClusterResourceBindingPermanentIDLabel: rbID,
164-
workv1alpha2.ClusterResourceBindingReferenceKey: names.GenerateBindingReferenceKey("", bindingName),
165162
},
166163
},
167164
}
165+
166+
checker := func(got, want map[string]string) bool {
167+
for key, val := range want {
168+
if got[key] != val {
169+
return false
170+
}
171+
}
172+
return true
173+
}
168174
for _, tt := range tests {
169175
t.Run(tt.name, func(t *testing.T) {
170-
if got := mergeLabel(tt.workload, tt.binding, tt.scope); !reflect.DeepEqual(got, tt.want) {
176+
if got := mergeLabel(tt.workload, tt.binding, tt.scope); !checker(got, tt.want) {
171177
t.Errorf("mergeLabel() = %v, want %v", got, tt.want)
172178
}
173179
})

Diff for: pkg/dependenciesdistributor/dependencies_distributor.go

-4
Original file line numberDiff line numberDiff line change
@@ -522,10 +522,6 @@ func (d *DependenciesDistributor) createOrUpdateAttachedBinding(attachedBinding
522522
key := client.ObjectKeyFromObject(attachedBinding)
523523
err := d.Client.Get(context.TODO(), key, existBinding)
524524
if err == nil {
525-
if util.GetLabelValue(existBinding.Labels, workv1alpha2.ResourceBindingPermanentIDLabel) == "" {
526-
existBinding.Labels = util.DedupeAndMergeLabels(existBinding.Labels,
527-
map[string]string{workv1alpha2.ResourceBindingPermanentIDLabel: uuid.New().String()})
528-
}
529525
existBinding.Spec.RequiredBy = mergeBindingSnapshot(existBinding.Spec.RequiredBy, attachedBinding.Spec.RequiredBy)
530526
existBinding.Labels = util.DedupeAndMergeLabels(existBinding.Labels, attachedBinding.Labels)
531527
existBinding.Spec.Resource = attachedBinding.Spec.Resource

Diff for: pkg/util/helper/binding.go

+5-15
Original file line numberDiff line numberDiff line change
@@ -194,9 +194,9 @@ func ObtainBindingSpecExistingClusters(bindingSpec workv1alpha2.ResourceBindingS
194194

195195
// FindOrphanWorks retrieves all works that labeled with current binding(ResourceBinding or ClusterResourceBinding) objects,
196196
// then pick the works that not meet current binding declaration.
197-
func FindOrphanWorks(c client.Client, bindingNamespace, bindingName string, expectClusters sets.Set[string]) ([]workv1alpha1.Work, error) {
197+
func FindOrphanWorks(c client.Client, bindingNamespace, bindingName, bindingID string, expectClusters sets.Set[string]) ([]workv1alpha1.Work, error) {
198198
var needJudgeWorks []workv1alpha1.Work
199-
workList, err := GetWorksByBindingNamespaceName(c, bindingNamespace, bindingName)
199+
workList, err := GetWorksByBindingID(c, bindingID, bindingNamespace != "")
200200
if err != nil {
201201
klog.Errorf("Failed to get works by binding object (%s/%s): %v", bindingNamespace, bindingName, err)
202202
return nil, err
@@ -344,21 +344,11 @@ func GetResourceBindings(c client.Client, ls labels.Set) (*workv1alpha2.Resource
344344
return bindings, c.List(context.TODO(), bindings, listOpt)
345345
}
346346

347-
// DeleteWorkByRBNamespaceAndName will delete all Work objects by ResourceBinding namespace and name.
348-
func DeleteWorkByRBNamespaceAndName(c client.Client, namespace, name string) error {
349-
return DeleteWorks(c, namespace, name)
350-
}
351-
352-
// DeleteWorkByCRBName will delete all Work objects by ClusterResourceBinding name.
353-
func DeleteWorkByCRBName(c client.Client, name string) error {
354-
return DeleteWorks(c, "", name)
355-
}
356-
357347
// DeleteWorks will delete all Work objects by labels.
358-
func DeleteWorks(c client.Client, namespace, name string) error {
359-
workList, err := GetWorksByBindingNamespaceName(c, namespace, name)
348+
func DeleteWorks(c client.Client, namespace, name, bindingID string) error {
349+
workList, err := GetWorksByBindingID(c, bindingID, namespace != "")
360350
if err != nil {
361-
klog.Errorf("Failed to get works by ResourceBinding(%s/%s) : %v", namespace, name, err)
351+
klog.Errorf("Failed to get works by (Cluster)ResourceBinding(%s/%s) : %v", namespace, name, err)
362352
return err
363353
}
364354

0 commit comments

Comments
 (0)