diff --git a/k8sdeps/configmapandsecret/configmapfactory.go b/k8sdeps/configmapandsecret/configmapfactory.go index 10be41a71..f0d8f4962 100644 --- a/k8sdeps/configmapandsecret/configmapfactory.go +++ b/k8sdeps/configmapandsecret/configmapfactory.go @@ -21,6 +21,7 @@ import ( "fmt" "path" "strings" + "unicode/utf8" "github.com/pkg/errors" "k8s.io/api/core/v1" @@ -139,10 +140,27 @@ func addKvToConfigMap(configMap *v1.ConfigMap, keyName, data string) error { if errs := validation.IsConfigMapKey(keyName); len(errs) != 0 { return fmt.Errorf("%q is not a valid key name for a ConfigMap: %s", keyName, strings.Join(errs, ";")) } - if _, entryExists := configMap.Data[keyName]; entryExists { - return fmt.Errorf("cannot add key %s, another key by that name already exists: %v", keyName, configMap.Data) + + keyExistsErrorMsg := "cannot add key %s, another key by that name already exists: %v" + + // If the configmap data contains byte sequences that are all in the UTF-8 + // range, we will write it to .Data + if utf8.Valid([]byte(data)) { + if _, entryExists := configMap.Data[keyName]; entryExists { + return fmt.Errorf(keyExistsErrorMsg, keyName, configMap.Data) + } + configMap.Data[keyName] = data + return nil } - configMap.Data[keyName] = data + + // otherwise, it's BinaryData + if configMap.BinaryData == nil { + configMap.BinaryData = map[string][]byte{} + } + if _, entryExists := configMap.BinaryData[keyName]; entryExists { + return fmt.Errorf(keyExistsErrorMsg, keyName, configMap.BinaryData) + } + configMap.BinaryData[keyName] = []byte(data) return nil } diff --git a/k8sdeps/configmapandsecret/configmapfactory_test.go b/k8sdeps/configmapandsecret/configmapfactory_test.go index d34a190ab..4e00aca55 100644 --- a/k8sdeps/configmapandsecret/configmapfactory_test.go +++ b/k8sdeps/configmapandsecret/configmapfactory_test.go @@ -57,6 +57,9 @@ func makeFileConfigMap(name string) *corev1.ConfigMap { BAR=baz `, }, + BinaryData: map[string][]byte{ + "app.bin": {0xff, 0xfd}, + }, } } @@ -105,7 +108,7 @@ func TestConstructConfigMap(t *testing.T) { input: types.ConfigMapArgs{ GeneratorArgs: types.GeneratorArgs{Name: "fileConfigMap"}, DataSources: types.DataSources{ - FileSources: []string{"configmap/app-init.ini"}, + FileSources: []string{"configmap/app-init.ini", "configmap/app.bin"}, }, }, options: nil, @@ -131,6 +134,7 @@ func TestConstructConfigMap(t *testing.T) { fSys := fs.MakeFakeFS() fSys.WriteFile("/configmap/app.env", []byte("DB_USERNAME=admin\nDB_PASSWORD=somepw\n")) fSys.WriteFile("/configmap/app-init.ini", []byte("FOO=bar\nBAR=baz\n")) + fSys.WriteFile("/configmap/app.bin", []byte{0xff, 0xfd}) f := NewConfigMapFactory(loader.NewFileLoaderAtRoot(fSys)) for _, tc := range testCases { cm, err := f.MakeConfigMap(&tc.input, tc.options)