From 5e33ac4a09cd9ba2c4201748655e44ba46e49e6c Mon Sep 17 00:00:00 2001 From: Jian Qiu Date: Tue, 21 May 2019 13:55:19 +0800 Subject: [PATCH] Allow nil label and annotaion This fix is to allow value of lable or annoation to be nil --- pkg/transformers/labelsandannotations.go | 4 +- pkg/transformers/labelsandannotations_test.go | 45 +++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/pkg/transformers/labelsandannotations.go b/pkg/transformers/labelsandannotations.go index 836abcaa4..10c515a44 100644 --- a/pkg/transformers/labelsandannotations.go +++ b/pkg/transformers/labelsandannotations.go @@ -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 { diff --git a/pkg/transformers/labelsandannotations_test.go b/pkg/transformers/labelsandannotations_test.go index 5e88be13f..ae2503b6a 100644 --- a/pkg/transformers/labelsandannotations_test.go +++ b/pkg/transformers/labelsandannotations_test.go @@ -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) + } + +}