diff --git a/pkg/reconciler/common/transformers.go b/pkg/reconciler/common/transformers.go index 0c798fa4f0..7e7d000c75 100644 --- a/pkg/reconciler/common/transformers.go +++ b/pkg/reconciler/common/transformers.go @@ -527,8 +527,8 @@ func replaceNamespaceInContainerArg(container *corev1.Container, targetNamespace } } -// AddConfigMapValues will loop on the interface passed and add the fields in configmap -// with key as json tag of the struct field +// AddConfigMapValues will loop on the interface (should be a struct) and add the fields in to configMap +// the key will be the json tag of the struct field func AddConfigMapValues(configMapName string, prop interface{}) mf.Transformer { return func(u *unstructured.Unstructured) error { if u.GetKind() != "ConfigMap" || u.GetName() != configMapName || prop == nil { @@ -565,6 +565,14 @@ func AddConfigMapValues(configMapName string, prop interface{}) mf.Transformer { if element.IsNil() { continue } + // empty string value will not be included in the following switch statement + // however, *string pointer can have empty("") string + // so copying the actual string value to the configMap, it can be a empty string too + if value, ok := element.Interface().(*string); ok { + if value != nil { + cm.Data[key] = *value + } + } // extract the actual element from the pointer element = values.Field(index).Elem() } diff --git a/pkg/reconciler/common/transformers_test.go b/pkg/reconciler/common/transformers_test.go index 714a894fa4..4dfc8fb35c 100644 --- a/pkg/reconciler/common/transformers_test.go +++ b/pkg/reconciler/common/transformers_test.go @@ -658,6 +658,33 @@ func TestAddConfigMapValues(t *testing.T) { expectedData: map[string]string{"foo": "bar"}, doesConfigMapExists: true, }, + { + name: "verify-with-empty-string-pointer", + targetConfigMapName: configMapName, + props: struct { + StringPtr *string `json:"stringPtr"` + NilStringPtr *string `json:"nilStringPtr"` + EmptyStringPtr *string `json:"emptyStringPtr"` + String string `json:"string"` + EmptyString string `json:"emptyString"` + }{ + StringPtr: ptr.String("hi"), + NilStringPtr: nil, + EmptyStringPtr: ptr.String(""), + String: "hello", + EmptyString: "", + }, + expectedData: map[string]string{ + "stringPtr": "hi", + "emptyStringPtr": "", + "string": "hello", + }, + keysShouldNotBeIn: []string{ + "nilStringPtr", + "emptyString", + }, + doesConfigMapExists: true, + }, } for _, test := range tests {