fix failed to apply Label/Annotation Overriders
Signed-off-by: chaunceyjiang <chaunceyjiang@gmail.com>
This commit is contained in:
parent
9b626c0f01
commit
c3f5eaa3f7
|
@ -39,6 +39,13 @@ func buildLabelAnnotationOverriderPatches(rawObj *unstructured.Unstructured, ove
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
case policyv1alpha1.OverriderOpAdd:
|
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{
|
patches = append(patches, overrideOption{
|
||||||
Op: string(overrider.Operator),
|
Op: string(overrider.Operator),
|
||||||
|
|
|
@ -23,12 +23,35 @@ func Test_applyLabelsOverriders(t *testing.T) {
|
||||||
"foo": "foo",
|
"foo": "foo",
|
||||||
"bar": "bar",
|
"bar": "bar",
|
||||||
}
|
}
|
||||||
|
deployment2 := helper.NewDeployment(metav1.NamespaceDefault, "test")
|
||||||
|
deployment2.Labels = nil
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
args args
|
args args
|
||||||
want map[string]string
|
want map[string]string
|
||||||
wantErr bool
|
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",
|
name: "test labels replace",
|
||||||
args: args{
|
args: args{
|
||||||
|
@ -122,6 +145,8 @@ func Test_applyAnnotationsOverriders(t *testing.T) {
|
||||||
"foo": "foo",
|
"foo": "foo",
|
||||||
"bar": "bar",
|
"bar": "bar",
|
||||||
}
|
}
|
||||||
|
deployment2 := helper.NewDeployment(metav1.NamespaceDefault, "test")
|
||||||
|
deployment2.Annotations = nil
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
args args
|
args args
|
||||||
|
@ -129,7 +154,28 @@ func Test_applyAnnotationsOverriders(t *testing.T) {
|
||||||
wantErr bool
|
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{
|
args: args{
|
||||||
rawObj: func() *unstructured.Unstructured {
|
rawObj: func() *unstructured.Unstructured {
|
||||||
deploymentObj, _ := utilhelper.ToUnstructured(deployment)
|
deploymentObj, _ := utilhelper.ToUnstructured(deployment)
|
||||||
|
@ -152,7 +198,7 @@ func Test_applyAnnotationsOverriders(t *testing.T) {
|
||||||
wantErr: false,
|
wantErr: false,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "test labels add",
|
name: "test annotations add",
|
||||||
args: args{
|
args: args{
|
||||||
rawObj: func() *unstructured.Unstructured {
|
rawObj: func() *unstructured.Unstructured {
|
||||||
deploymentObj, _ := utilhelper.ToUnstructured(deployment)
|
deploymentObj, _ := utilhelper.ToUnstructured(deployment)
|
||||||
|
@ -175,7 +221,7 @@ func Test_applyAnnotationsOverriders(t *testing.T) {
|
||||||
wantErr: false,
|
wantErr: false,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "test labels remove",
|
name: "test annotations remove",
|
||||||
args: args{
|
args: args{
|
||||||
rawObj: func() *unstructured.Unstructured {
|
rawObj: func() *unstructured.Unstructured {
|
||||||
deploymentObj, _ := utilhelper.ToUnstructured(deployment)
|
deploymentObj, _ := utilhelper.ToUnstructured(deployment)
|
||||||
|
@ -197,7 +243,7 @@ func Test_applyAnnotationsOverriders(t *testing.T) {
|
||||||
wantErr: false,
|
wantErr: false,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "test labels remain the same",
|
name: "test annotations remain the same",
|
||||||
args: args{
|
args: args{
|
||||||
rawObj: func() *unstructured.Unstructured {
|
rawObj: func() *unstructured.Unstructured {
|
||||||
deploymentObj, _ := utilhelper.ToUnstructured(deployment)
|
deploymentObj, _ := utilhelper.ToUnstructured(deployment)
|
||||||
|
@ -217,7 +263,7 @@ func Test_applyAnnotationsOverriders(t *testing.T) {
|
||||||
wantErr: false,
|
wantErr: false,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "test labels remain the same",
|
name: "test annotations remain the same",
|
||||||
args: args{
|
args: args{
|
||||||
rawObj: func() *unstructured.Unstructured {
|
rawObj: func() *unstructured.Unstructured {
|
||||||
deploymentObj, _ := utilhelper.ToUnstructured(deployment)
|
deploymentObj, _ := utilhelper.ToUnstructured(deployment)
|
||||||
|
@ -239,7 +285,7 @@ func Test_applyAnnotationsOverriders(t *testing.T) {
|
||||||
wantErr: false,
|
wantErr: false,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "test labels remain the same",
|
name: "test annotations remain the same",
|
||||||
args: args{
|
args: args{
|
||||||
rawObj: func() *unstructured.Unstructured {
|
rawObj: func() *unstructured.Unstructured {
|
||||||
deploymentObj, _ := utilhelper.ToUnstructured(deployment)
|
deploymentObj, _ := utilhelper.ToUnstructured(deployment)
|
||||||
|
|
Loading…
Reference in New Issue