Skip to content

Commit

Permalink
Merge pull request #2971 from chaunceyjiang/LabelAnnotationOverrider
Browse files Browse the repository at this point in the history
fix failed to apply Label/Annotation Overriders
  • Loading branch information
karmada-bot committed Dec 17, 2022
2 parents 0d7e54b + c3f5eaa commit 617409f
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 6 deletions.
7 changes: 7 additions & 0 deletions pkg/util/overridemanager/labelannotationoverrider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
58 changes: 52 additions & 6 deletions pkg/util/overridemanager/labelannotationoverrider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -122,14 +145,37 @@ 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
want map[string]string
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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit 617409f

Please sign in to comment.