Put the two sets of configmap make codes sidebyside

This commit is contained in:
Jeffrey Regan
2018-07-19 18:11:13 -07:00
parent ff4a1c0b4f
commit 9576a81787
6 changed files with 127 additions and 124 deletions

View File

@@ -27,6 +27,7 @@ import (
"github.com/kubernetes-sigs/kustomize/pkg/fs"
"github.com/kubernetes-sigs/kustomize/pkg/hash"
"github.com/kubernetes-sigs/kustomize/pkg/loader"
"github.com/kubernetes-sigs/kustomize/pkg/types"
"k8s.io/api/core/v1"
corev1 "k8s.io/api/core/v1"
@@ -39,16 +40,20 @@ import (
type ConfigMapFactory struct {
args *types.ConfigMapArgs
fSys fs.FileSystem
ldr loader.Loader
}
// NewConfigMapFactory returns a new ConfigMapFactory.
func NewConfigMapFactory(args *types.ConfigMapArgs, fSys fs.FileSystem) *ConfigMapFactory {
return &ConfigMapFactory{args: args, fSys: fSys}
func NewConfigMapFactory(
args *types.ConfigMapArgs,
fSys fs.FileSystem,
l loader.Loader) *ConfigMapFactory {
return &ConfigMapFactory{args: args, fSys: fSys, ldr: l}
}
// MakeUnstructAndGenerateName returns an configmap and the name appended with a hash.
func (f *ConfigMapFactory) MakeUnstructAndGenerateName() (*unstructured.Unstructured, string, error) {
cm, err := f.MakeConfigMap()
cm, err := f.MakeConfigMap1()
if err != nil {
return nil, "", err
}
@@ -71,13 +76,18 @@ func objectToUnstructured(in runtime.Object) (*unstructured.Unstructured, error)
return &out, err
}
// MakeConfigMap returns a new ConfigMap, or nil and an error.
func (f *ConfigMapFactory) MakeConfigMap() (*corev1.ConfigMap, error) {
func (f *ConfigMapFactory) makeFreshConfigMap() *corev1.ConfigMap {
cm := &corev1.ConfigMap{}
cm.APIVersion = "v1"
cm.Kind = "ConfigMap"
cm.Name = f.args.Name
cm.Data = map[string]string{}
return cm
}
// MakeConfigMap1 returns a new ConfigMap, or nil and an error.
func (f *ConfigMapFactory) MakeConfigMap1() (*corev1.ConfigMap, error) {
cm := f.makeFreshConfigMap()
if f.args.EnvSource != "" {
if err := f.handleConfigMapFromEnvFileSource(cm); err != nil {
@@ -94,13 +104,66 @@ func (f *ConfigMapFactory) MakeConfigMap() (*corev1.ConfigMap, error) {
return nil, err
}
}
return cm, nil
}
// MakeConfigMap2 returns a new ConfigMap, or nil and an error.
// TODO: Get rid of the nearly duplicated code in MakeConfigMap1 vs MakeConfigMap2
func (f *ConfigMapFactory) MakeConfigMap2() (*corev1.ConfigMap, error) {
var envPairs, literalPairs, filePairs []kvPair
var err error
cm := f.makeFreshConfigMap()
if f.args.EnvSource != "" {
envPairs, err = keyValuesFromEnvFile(f.ldr, f.args.EnvSource)
if err != nil {
return nil, fmt.Errorf(
"error reading keys from env source file: %s %v",
f.args.EnvSource, err)
}
}
literalPairs, err = keyValuesFromLiteralSources(f.args.LiteralSources)
if err != nil {
return nil, fmt.Errorf(
"error reading key values from literal sources: %v", err)
}
filePairs, err = keyValuesFromFileSources(f.ldr, f.args.FileSources)
if err != nil {
return nil, fmt.Errorf(
"error reading key values from file sources: %v", err)
}
allPairs := append(append(envPairs, literalPairs...), filePairs...)
// merge key value pairs from all the sources
for _, kv := range allPairs {
err = addKV(cm.Data, kv)
if err != nil {
return nil, fmt.Errorf("error adding key in configmap: %v", err)
}
}
return cm, nil
}
func keyValuesFromLiteralSources(sources []string) ([]kvPair, error) {
var kvs []kvPair
for _, s := range sources {
k, v, err := ParseLiteralSource(s)
if err != nil {
return nil, err
}
kvs = append(kvs, kvPair{key: k, value: v})
}
return kvs, nil
}
// handleConfigMapFromLiteralSources adds the specified literal source
// information into the provided configMap.
func (f *ConfigMapFactory) handleConfigMapFromLiteralSources(configMap *v1.ConfigMap) error {
func (f *ConfigMapFactory) handleConfigMapFromLiteralSources(
configMap *v1.ConfigMap) error {
for _, literalSource := range f.args.LiteralSources {
keyName, value, err := ParseLiteralSource(literalSource)
if err != nil {
@@ -114,6 +177,22 @@ func (f *ConfigMapFactory) handleConfigMapFromLiteralSources(configMap *v1.Confi
return nil
}
func keyValuesFromFileSources(ldr loader.Loader, sources []string) ([]kvPair, error) {
var kvs []kvPair
for _, s := range sources {
k, path, err := ParseFileSource(s)
if err != nil {
return nil, err
}
content, err := ldr.Load(path)
if err != nil {
return nil, err
}
kvs = append(kvs, kvPair{key: k, value: string(content)})
}
return kvs, nil
}
// handleConfigMapFromFileSources adds the specified file source information
// into the provided configMap
func (f *ConfigMapFactory) handleConfigMapFromFileSources(configMap *v1.ConfigMap) error {
@@ -152,6 +231,14 @@ func (f *ConfigMapFactory) handleConfigMapFromFileSources(configMap *v1.ConfigMa
return nil
}
func keyValuesFromEnvFile(l loader.Loader, path string) ([]kvPair, error) {
content, err := l.Load(path)
if err != nil {
return nil, err
}
return keyValuesFromLines(content)
}
// HandleConfigMapFromEnvFileSource adds the specified env file source information
// into the provided configMap
func (f *ConfigMapFactory) handleConfigMapFromEnvFileSource(configMap *v1.ConfigMap) error {
@@ -161,7 +248,6 @@ func (f *ConfigMapFactory) handleConfigMapFromEnvFileSource(configMap *v1.Config
if f.fSys.IsDir(f.args.EnvSource) {
return fmt.Errorf("env config file %s cannot be a directory", f.args.EnvSource)
}
return addFromEnvFile(f.args.EnvSource, func(key, value string) error {
return addKeyFromLiteralToConfigMap(configMap, key, value)
})
@@ -232,3 +318,18 @@ func ParseLiteralSource(source string) (keyName, value string, err error) {
return items[0], items[1], nil
}
// addKV adds key-value pair to the provided map.
func addKV(m map[string]string, kv kvPair) error {
if errs := validation.IsConfigMapKey(kv.key); len(errs) != 0 {
return fmt.Errorf(
"%q is not a valid key name: %s",
kv.key, strings.Join(errs, ";"))
}
if _, exists := m[kv.key]; exists {
return fmt.Errorf(
"key %s already exists: %v", kv.key, m)
}
m[kv.key] = kv.value
return nil
}

View File

@@ -136,8 +136,8 @@ func TestConstructConfigMap(t *testing.T) {
for _, tc := range testCases {
// TODO: all tests should use a FakeFs
fSys := fs.MakeRealFS()
f := NewConfigMapFactory(&tc.input, fSys)
cm, err := f.MakeConfigMap()
f := NewConfigMapFactory(&tc.input, fSys, nil)
cm, err := f.MakeConfigMap1()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}

View File

@@ -0,0 +1,100 @@
/*
Copyright 2018 The Kubernetes Authors.
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
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package configmapandsecret
import (
"bufio"
"bytes"
"fmt"
"os"
"strings"
"unicode"
"unicode/utf8"
"k8s.io/apimachinery/pkg/util/validation"
)
// kvPair represents a key value pair.
type kvPair struct {
key string
value string
}
// keyValuesFromLines parses given content in to a list of key-value pairs.
func keyValuesFromLines(content []byte) ([]kvPair, error) {
var kvs []kvPair
scanner := bufio.NewScanner(bytes.NewReader(content))
currentLine := 0
for scanner.Scan() {
// Process the current line, retrieving a key/value pair if
// possible.
scannedBytes := scanner.Bytes()
kv, err := kvFromLine(scannedBytes, currentLine)
if err != nil {
return nil, err
}
currentLine++
if len(kv.key) == 0 {
// no key means line was empty or a comment
continue
}
kvs = append(kvs, kv)
}
return kvs, nil
}
// kvFromLine returns a kv with blank key if the line is empty or a comment.
// The value will be retrieved from the environment if necessary.
func kvFromLine(line []byte, currentLine int) (kvPair, error) {
kv := kvPair{}
if !utf8.Valid(line) {
return kv, fmt.Errorf("line %d has invalid utf8 bytes : %v", line, string(line))
}
// We trim UTF8 BOM from the first line of the file but no others
if currentLine == 0 {
line = bytes.TrimPrefix(line, utf8bom)
}
// trim the line from all leading whitespace first
line = bytes.TrimLeftFunc(line, unicode.IsSpace)
// If the line is empty or a comment, we return a blank key/value pair.
if len(line) == 0 || line[0] == '#' {
return kv, nil
}
data := strings.SplitN(string(line), "=", 2)
key := data[0]
if errs := validation.IsEnvVarName(key); len(errs) != 0 {
return kv, fmt.Errorf("%q is not a valid key name: %s", key, strings.Join(errs, ";"))
}
if len(data) == 2 {
kv.value = data[1]
} else {
// No value (no `=` in the line) is a signal to obtain the value
// from the environment.
kv.value = os.Getenv(key)
}
kv.key = key
return kv, nil
}

View File

@@ -0,0 +1,67 @@
/*
Copyright 2018 The Kubernetes Authors.
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
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package configmapandsecret
import (
"reflect"
"testing"
)
func TestKeyValuesFromLines(t *testing.T) {
tests := []struct {
desc string
content string
expectedPairs []kvPair
expectedErr bool
}{
{
desc: "valid kv content parse",
content: `
k1=v1
k2=v2
`,
expectedPairs: []kvPair{
{key: "k1", value: "v1"},
{key: "k2", value: "v2"},
},
expectedErr: false,
},
{
desc: "content with comments",
content: `
k1=v1
#k2=v2
`,
expectedPairs: []kvPair{
{key: "k1", value: "v1"},
},
expectedErr: false,
},
// TODO: add negative testcases
}
for _, test := range tests {
pairs, err := keyValuesFromLines([]byte(test.content))
if test.expectedErr && err == nil {
t.Fatalf("%s should not return error", test.desc)
}
if !reflect.DeepEqual(pairs, test.expectedPairs) {
t.Errorf("%s should succeed, got:%v exptected:%v", test.desc, pairs, test.expectedPairs)
}
}
}