Skip to content

Commit

Permalink
fix confiMap transformer to include string pointer's empty value
Browse files Browse the repository at this point in the history
Signed-off-by: Jeeva Kandasamy <[email protected]>
  • Loading branch information
jkandasa authored and tekton-robot committed May 31, 2024
1 parent 2fdf7d3 commit 13c5ba5
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
12 changes: 10 additions & 2 deletions pkg/reconciler/common/transformers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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()
}
Expand Down
27 changes: 27 additions & 0 deletions pkg/reconciler/common/transformers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 13c5ba5

Please sign in to comment.