configmap binarydata support

This commit is contained in:
ryane
2019-01-01 10:40:56 -05:00
parent 4e2c4b94e3
commit d4170797ae
2 changed files with 25 additions and 5 deletions

View File

@@ -21,6 +21,7 @@ import (
"fmt" "fmt"
"path" "path"
"strings" "strings"
"unicode/utf8"
"github.com/pkg/errors" "github.com/pkg/errors"
"k8s.io/api/core/v1" "k8s.io/api/core/v1"
@@ -48,6 +49,7 @@ func (f *ConfigMapFactory) makeFreshConfigMap(
cm.Name = args.Name cm.Name = args.Name
cm.Namespace = args.Namespace cm.Namespace = args.Namespace
cm.Data = map[string]string{} cm.Data = map[string]string{}
cm.BinaryData = map[string][]byte{}
return cm return cm
} }
@@ -139,10 +141,22 @@ func addKvToConfigMap(configMap *v1.ConfigMap, keyName, data string) error {
if errs := validation.IsConfigMapKey(keyName); len(errs) != 0 { 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, ";")) 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 return nil
} }

View File

@@ -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 not use this file except in compliance with the License.
You may obtain a copy of the License at 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 Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS, 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_USERNAME": "admin",
"DB_PASSWORD": "somepw", "DB_PASSWORD": "somepw",
}, },
BinaryData: map[string][]byte{},
} }
} }
@@ -57,6 +58,9 @@ func makeFileConfigMap(name string) *corev1.ConfigMap {
BAR=baz BAR=baz
`, `,
}, },
BinaryData: map[string][]byte{
"app.bin": {0xff, 0xfd},
},
} }
} }
@@ -75,6 +79,7 @@ func makeLiteralConfigMap(name string) *corev1.ConfigMap {
"c": "Hello World", "c": "Hello World",
"d": "true", "d": "true",
}, },
BinaryData: map[string][]byte{},
} }
cm.SetLabels(map[string]string{"foo": "bar"}) cm.SetLabels(map[string]string{"foo": "bar"})
return cm return cm
@@ -105,7 +110,7 @@ func TestConstructConfigMap(t *testing.T) {
input: types.ConfigMapArgs{ input: types.ConfigMapArgs{
GeneratorArgs: types.GeneratorArgs{Name: "fileConfigMap"}, GeneratorArgs: types.GeneratorArgs{Name: "fileConfigMap"},
DataSources: types.DataSources{ DataSources: types.DataSources{
FileSources: []string{"configmap/app-init.ini"}, FileSources: []string{"configmap/app-init.ini", "configmap/app.bin"},
}, },
}, },
options: nil, options: nil,
@@ -131,6 +136,7 @@ func TestConstructConfigMap(t *testing.T) {
fSys := fs.MakeFakeFS() fSys := fs.MakeFakeFS()
fSys.WriteFile("/configmap/app.env", []byte("DB_USERNAME=admin\nDB_PASSWORD=somepw\n")) 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-init.ini", []byte("FOO=bar\nBAR=baz\n"))
fSys.WriteFile("/configmap/app.bin", []byte{0xff, 0xfd})
f := NewConfigMapFactory(loader.NewFileLoaderAtRoot(fSys)) f := NewConfigMapFactory(loader.NewFileLoaderAtRoot(fSys))
for _, tc := range testCases { for _, tc := range testCases {
cm, err := f.MakeConfigMap(&tc.input, tc.options) cm, err := f.MakeConfigMap(&tc.input, tc.options)