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"
"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
}

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 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)