From c3f5eaa3f75f8c721f478e2640e36b8042af15f5 Mon Sep 17 00:00:00 2001 From: chaunceyjiang Date: Fri, 16 Dec 2022 18:34:33 +0800 Subject: [PATCH] fix failed to apply Label/Annotation Overriders Signed-off-by: chaunceyjiang --- .../labelannotationoverrider.go | 7 +++ .../labelannotationoverrider_test.go | 58 +++++++++++++++++-- 2 files changed, 59 insertions(+), 6 deletions(-) diff --git a/pkg/util/overridemanager/labelannotationoverrider.go b/pkg/util/overridemanager/labelannotationoverrider.go index ba1548da84bd..207ad2696462 100644 --- a/pkg/util/overridemanager/labelannotationoverrider.go +++ b/pkg/util/overridemanager/labelannotationoverrider.go @@ -39,6 +39,13 @@ func buildLabelAnnotationOverriderPatches(rawObj *unstructured.Unstructured, ove continue } case policyv1alpha1.OverriderOpAdd: + _, exist, _ := unstructured.NestedStringMap(rawObj.Object, path...) + if exist { + break + } + if err := unstructured.SetNestedStringMap(rawObj.Object, map[string]string{}, path...); err != nil { + continue + } } patches = append(patches, overrideOption{ Op: string(overrider.Operator), diff --git a/pkg/util/overridemanager/labelannotationoverrider_test.go b/pkg/util/overridemanager/labelannotationoverrider_test.go index f0d46fd33cb1..1824c6eef928 100644 --- a/pkg/util/overridemanager/labelannotationoverrider_test.go +++ b/pkg/util/overridemanager/labelannotationoverrider_test.go @@ -23,12 +23,35 @@ func Test_applyLabelsOverriders(t *testing.T) { "foo": "foo", "bar": "bar", } + deployment2 := helper.NewDeployment(metav1.NamespaceDefault, "test") + deployment2.Labels = nil tests := []struct { name string args args want map[string]string wantErr bool }{ + { + name: "test empty labels", + args: args{ + rawObj: func() *unstructured.Unstructured { + deploymentObj, _ := utilhelper.ToUnstructured(deployment2) + return deploymentObj + }(), + commandOverriders: []policyv1alpha1.LabelAnnotationOverrider{ + { + Operator: policyv1alpha1.OverriderOpAdd, + Value: map[string]string{ + "test2": "test2", + }, + }, + }, + }, + want: map[string]string{ + "test2": "test2", + }, + wantErr: false, + }, { name: "test labels replace", args: args{ @@ -122,6 +145,8 @@ func Test_applyAnnotationsOverriders(t *testing.T) { "foo": "foo", "bar": "bar", } + deployment2 := helper.NewDeployment(metav1.NamespaceDefault, "test") + deployment2.Annotations = nil tests := []struct { name string args args @@ -129,7 +154,28 @@ func Test_applyAnnotationsOverriders(t *testing.T) { wantErr bool }{ { - name: "test labels replace", + name: "test empty annotations", + args: args{ + rawObj: func() *unstructured.Unstructured { + deploymentObj, _ := utilhelper.ToUnstructured(deployment2) + return deploymentObj + }(), + commandOverriders: []policyv1alpha1.LabelAnnotationOverrider{ + { + Operator: policyv1alpha1.OverriderOpAdd, + Value: map[string]string{ + "test2": "test2", + }, + }, + }, + }, + want: map[string]string{ + "test2": "test2", + }, + wantErr: false, + }, + { + name: "test annotations replace", args: args{ rawObj: func() *unstructured.Unstructured { deploymentObj, _ := utilhelper.ToUnstructured(deployment) @@ -152,7 +198,7 @@ func Test_applyAnnotationsOverriders(t *testing.T) { wantErr: false, }, { - name: "test labels add", + name: "test annotations add", args: args{ rawObj: func() *unstructured.Unstructured { deploymentObj, _ := utilhelper.ToUnstructured(deployment) @@ -175,7 +221,7 @@ func Test_applyAnnotationsOverriders(t *testing.T) { wantErr: false, }, { - name: "test labels remove", + name: "test annotations remove", args: args{ rawObj: func() *unstructured.Unstructured { deploymentObj, _ := utilhelper.ToUnstructured(deployment) @@ -197,7 +243,7 @@ func Test_applyAnnotationsOverriders(t *testing.T) { wantErr: false, }, { - name: "test labels remain the same", + name: "test annotations remain the same", args: args{ rawObj: func() *unstructured.Unstructured { deploymentObj, _ := utilhelper.ToUnstructured(deployment) @@ -217,7 +263,7 @@ func Test_applyAnnotationsOverriders(t *testing.T) { wantErr: false, }, { - name: "test labels remain the same", + name: "test annotations remain the same", args: args{ rawObj: func() *unstructured.Unstructured { deploymentObj, _ := utilhelper.ToUnstructured(deployment) @@ -239,7 +285,7 @@ func Test_applyAnnotationsOverriders(t *testing.T) { wantErr: false, }, { - name: "test labels remain the same", + name: "test annotations remain the same", args: args{ rawObj: func() *unstructured.Unstructured { deploymentObj, _ := utilhelper.ToUnstructured(deployment)