From d4170797ae239d1dc9f205071686550b2fec31ae Mon Sep 17 00:00:00 2001 From: ryane Date: Tue, 1 Jan 2019 10:40:56 -0500 Subject: [PATCH 1/2] configmap binarydata support --- .../configmapandsecret/configmapfactory.go | 20 ++++++++++++++++--- .../configmapfactory_test.go | 10 ++++++++-- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/k8sdeps/configmapandsecret/configmapfactory.go b/k8sdeps/configmapandsecret/configmapfactory.go index 10be41a71..c853476f2 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" @@ -48,6 +49,7 @@ func (f *ConfigMapFactory) makeFreshConfigMap( cm.Name = args.Name cm.Namespace = args.Namespace cm.Data = map[string]string{} + cm.BinaryData = map[string][]byte{} return cm } @@ -139,10 +141,22 @@ 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 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 + + // binary data + 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..461ad0021 100644 --- a/k8sdeps/configmapandsecret/configmapfactory_test.go +++ b/k8sdeps/configmapandsecret/configmapfactory_test.go @@ -5,7 +5,7 @@ Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at - http://www.apache.org/licenses/LICENSE-2.0 + http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, @@ -40,6 +40,7 @@ func makeEnvConfigMap(name string) *corev1.ConfigMap { "DB_USERNAME": "admin", "DB_PASSWORD": "somepw", }, + BinaryData: map[string][]byte{}, } } @@ -57,6 +58,9 @@ func makeFileConfigMap(name string) *corev1.ConfigMap { BAR=baz `, }, + BinaryData: map[string][]byte{ + "app.bin": {0xff, 0xfd}, + }, } } @@ -75,6 +79,7 @@ func makeLiteralConfigMap(name string) *corev1.ConfigMap { "c": "Hello World", "d": "true", }, + BinaryData: map[string][]byte{}, } cm.SetLabels(map[string]string{"foo": "bar"}) return cm @@ -105,7 +110,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 +136,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) From 87411590c5fde9fb288afb6f9825b8e242cd4dfb Mon Sep 17 00:00:00 2001 From: ryane Date: Wed, 16 Jan 2019 14:12:31 -0500 Subject: [PATCH 2/2] configmap binarydata comments and small tweaks the BinaryData map is nil until the generator finds a file with contents that needs injected into the BinaryData field of the configmap --- k8sdeps/configmapandsecret/configmapfactory.go | 8 ++++++-- k8sdeps/configmapandsecret/configmapfactory_test.go | 4 +--- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/k8sdeps/configmapandsecret/configmapfactory.go b/k8sdeps/configmapandsecret/configmapfactory.go index c853476f2..f0d8f4962 100644 --- a/k8sdeps/configmapandsecret/configmapfactory.go +++ b/k8sdeps/configmapandsecret/configmapfactory.go @@ -49,7 +49,6 @@ func (f *ConfigMapFactory) makeFreshConfigMap( cm.Name = args.Name cm.Namespace = args.Namespace cm.Data = map[string]string{} - cm.BinaryData = map[string][]byte{} return cm } @@ -144,6 +143,8 @@ func addKvToConfigMap(configMap *v1.ConfigMap, keyName, data string) error { 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) @@ -152,7 +153,10 @@ func addKvToConfigMap(configMap *v1.ConfigMap, keyName, data string) error { return nil } - // binary 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) } diff --git a/k8sdeps/configmapandsecret/configmapfactory_test.go b/k8sdeps/configmapandsecret/configmapfactory_test.go index 461ad0021..4e00aca55 100644 --- a/k8sdeps/configmapandsecret/configmapfactory_test.go +++ b/k8sdeps/configmapandsecret/configmapfactory_test.go @@ -5,7 +5,7 @@ Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at - http://www.apache.org/licenses/LICENSE-2.0 + http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, @@ -40,7 +40,6 @@ func makeEnvConfigMap(name string) *corev1.ConfigMap { "DB_USERNAME": "admin", "DB_PASSWORD": "somepw", }, - BinaryData: map[string][]byte{}, } } @@ -79,7 +78,6 @@ func makeLiteralConfigMap(name string) *corev1.ConfigMap { "c": "Hello World", "d": "true", }, - BinaryData: map[string][]byte{}, } cm.SetLabels(map[string]string{"foo": "bar"}) return cm