Allow nil label and annotaion

This fix is to allow value of lable or annoation to be nil
This commit is contained in:
Jian Qiu
2019-05-21 13:55:19 +08:00
parent 72f565d55d
commit 5e33ac4a09
2 changed files with 48 additions and 1 deletions

View File

@@ -76,7 +76,9 @@ func (o *mapTransformer) Transform(m resmap.ResMap) error {
func (o *mapTransformer) addMap(in interface{}) (interface{}, error) {
m, ok := in.(map[string]interface{})
if !ok {
if in == nil {
m = map[string]interface{}{}
} else if !ok {
return nil, fmt.Errorf("%#v is expected to be %T", in, m)
}
for k, v := range o.m {

View File

@@ -583,3 +583,48 @@ func TestAnnotationsRun(t *testing.T) {
t.Fatalf("actual doesn't match expected: %v", err)
}
}
func TestAnnotaionsRunWithNullValue(t *testing.T) {
m := resmap.ResMap{
resid.NewResId(cmap, "cm1"): rf.FromMap(
map[string]interface{}{
"apiVersion": "v1",
"kind": "ConfigMap",
"metadata": map[string]interface{}{
"name": "cm1",
"annotations": nil,
},
}),
}
expected := resmap.ResMap{
resid.NewResId(cmap, "cm1"): rf.FromMap(
map[string]interface{}{
"apiVersion": "v1",
"kind": "ConfigMap",
"metadata": map[string]interface{}{
"name": "cm1",
"annotations": map[string]interface{}{
"anno-key1": "anno-value1",
"anno-key2": "anno-value2",
},
},
}),
}
at, err := NewAnnotationsMapTransformer(
map[string]string{"anno-key1": "anno-value1", "anno-key2": "anno-value2"},
defaultTransformerConfig.CommonAnnotations)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
err = at.Transform(m)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if !reflect.DeepEqual(m, expected) {
err = expected.ErrorIfNotEqual(m)
t.Fatalf("actual doesn't match expected: %v", err)
}
}