Skip to content

Commit

Permalink
labels cannot be deleted via Karmada propagation.
Browse files Browse the repository at this point in the history
Signed-off-by: whitewindmills <[email protected]>
  • Loading branch information
whitewindmills committed Apr 3, 2024
1 parent 35e288d commit 9dc6e15
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 53 deletions.
2 changes: 1 addition & 1 deletion pkg/util/helper/work.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ func CreateOrUpdateWork(client client.Client, workMeta metav1.ObjectMeta, resour
}
util.ReplaceAnnotation(workload, workv1alpha2.ResourceTemplateUIDAnnotation, string(workload.GetUID()))
util.RecordManagedAnnotations(workload)
util.RecordManagedLabels(workload)
workloadJSON, err := workload.MarshalJSON()
if err != nil {
klog.Errorf("Failed to marshal workload(%s/%s), Error: %v", workload.GetNamespace(), workload.GetName(), err)
Expand Down Expand Up @@ -84,7 +85,6 @@ func CreateOrUpdateWork(client client.Client, workMeta metav1.ObjectMeta, resour
if util.GetLabelValue(runtimeObject.Labels, workv1alpha2.WorkPermanentIDLabel) == "" {
runtimeObject.Labels = util.DedupeAndMergeLabels(runtimeObject.Labels, map[string]string{workv1alpha2.WorkPermanentIDLabel: uuid.New().String()})
}
util.RecordManagedLabels(runtimeObject)
return nil
})
if err != nil {
Expand Down
9 changes: 4 additions & 5 deletions pkg/util/label.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/util/sets"

workv1alpha1 "github.com/karmada-io/karmada/pkg/apis/work/v1alpha1"
workv1alpha2 "github.com/karmada-io/karmada/pkg/apis/work/v1alpha2"
)

Expand Down Expand Up @@ -111,18 +110,18 @@ func getDeletedLabelKeys(desired, observed *unstructured.Unstructured) sets.Set[

// RecordManagedLabels sets or updates the annotation(resourcetemplate.karmada.io/managed-labels)
// to record the label keys.
func RecordManagedLabels(w *workv1alpha1.Work) {
annotations := w.GetAnnotations()
func RecordManagedLabels(object *unstructured.Unstructured) {
annotations := object.GetAnnotations()
if annotations == nil {
annotations = make(map[string]string, 1)
}
var managedKeys []string
// record labels.
labels := w.GetLabels()
labels := object.GetLabels()
for key := range labels {
managedKeys = append(managedKeys, key)
}
sort.Strings(managedKeys)
annotations[workv1alpha2.ManagedLabels] = strings.Join(managedKeys, ",")
w.SetAnnotations(annotations)
object.SetAnnotations(annotations)
}
98 changes: 51 additions & 47 deletions pkg/util/label_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,8 @@ import (
"reflect"
"testing"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"

workv1alpha1 "github.com/karmada-io/karmada/pkg/apis/work/v1alpha1"
workv1alpha2 "github.com/karmada-io/karmada/pkg/apis/work/v1alpha2"
)

Expand Down Expand Up @@ -528,65 +526,71 @@ func TestRetainLabels(t *testing.T) {
func TestRecordManagedLabels(t *testing.T) {
tests := []struct {
name string
object *workv1alpha1.Work
expected *workv1alpha1.Work
object *unstructured.Unstructured
expected *unstructured.Unstructured
}{
{
name: "nil label",
object: &workv1alpha1.Work{
TypeMeta: metav1.TypeMeta{
APIVersion: "work.karmada.io/v1alpha1",
Kind: "Work",
},
ObjectMeta: metav1.ObjectMeta{
Name: "demo-work-1",
Namespace: "cluster1-ns",
Labels: nil,
object: &unstructured.Unstructured{
Object: map[string]interface{}{
"apiVersion": "apps/v1",
"kind": "Deployment",
"metadata": map[string]interface{}{
"name": "demo-deployment-1",
},
"spec": map[string]interface{}{
"replicas": 2,
},
},
},
expected: &workv1alpha1.Work{
TypeMeta: metav1.TypeMeta{
APIVersion: "work.karmada.io/v1alpha1",
Kind: "Work",
},
ObjectMeta: metav1.ObjectMeta{
Name: "demo-work-1",
Namespace: "cluster1-ns",
Annotations: map[string]string{
workv1alpha2.ManagedLabels: "",
expected: &unstructured.Unstructured{
Object: map[string]interface{}{
"apiVersion": "apps/v1",
"kind": "Deployment",
"metadata": map[string]interface{}{
"name": "demo-deployment-1",
"annotations": map[string]interface{}{
workv1alpha2.ManagedLabels: "",
},
},
"spec": map[string]interface{}{
"replicas": 2,
},
Labels: nil,
},
},
},
{
name: "object has has labels",
object: &workv1alpha1.Work{
TypeMeta: metav1.TypeMeta{
APIVersion: "work.karmada.io/v1alpha1",
Kind: "Work",
},
ObjectMeta: metav1.ObjectMeta{
Name: "demo-work-1",
Namespace: "cluster1-ns",
Labels: map[string]string{
"foo": "bar",
object: &unstructured.Unstructured{
Object: map[string]interface{}{
"apiVersion": "apps/v1",
"kind": "Deployment",
"metadata": map[string]interface{}{
"name": "demo-deployment-1",
"labels": map[string]interface{}{
"foo": "foo",
},
},
"spec": map[string]interface{}{
"replicas": 2,
},
},
},
expected: &workv1alpha1.Work{
TypeMeta: metav1.TypeMeta{
APIVersion: "work.karmada.io/v1alpha1",
Kind: "Work",
},
ObjectMeta: metav1.ObjectMeta{
Name: "demo-work-1",
Namespace: "cluster1-ns",
Annotations: map[string]string{
workv1alpha2.ManagedLabels: "foo",
},
Labels: map[string]string{
"foo": "bar",
expected: &unstructured.Unstructured{
Object: map[string]interface{}{
"apiVersion": "apps/v1",
"kind": "Deployment",
"metadata": map[string]interface{}{
"name": "demo-deployment-1",
"annotations": map[string]interface{}{
workv1alpha2.ManagedLabels: "foo",
},
"labels": map[string]interface{}{
"foo": "foo",
},
},
"spec": map[string]interface{}{
"replicas": 2,
},
},
},
Expand Down

0 comments on commit 9dc6e15

Please sign in to comment.