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

@@ -17,123 +17,27 @@ limitations under the License.
package resmap
import (
"fmt"
"strings"
"github.com/kubernetes-sigs/kustomize/pkg/configmapandsecret"
"github.com/kubernetes-sigs/kustomize/pkg/loader"
"github.com/kubernetes-sigs/kustomize/pkg/resource"
"github.com/kubernetes-sigs/kustomize/pkg/types"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/validation"
)
func newResourceFromConfigMap(l loader.Loader, cmArgs types.ConfigMapArgs) (*resource.Resource, error) {
cm, err := makeConfigMap(l, cmArgs)
if err != nil {
return nil, err
}
if cmArgs.Behavior == "" {
cmArgs.Behavior = "create"
}
return resource.NewResourceWithBehavior(cm, resource.NewGenerationBehavior(cmArgs.Behavior))
}
func makeConfigMap(l loader.Loader, cmArgs types.ConfigMapArgs) (*corev1.ConfigMap, error) {
var envPairs, literalPairs, filePairs []kvPair
var err error
cm := &corev1.ConfigMap{}
cm.APIVersion = "v1"
cm.Kind = "ConfigMap"
cm.Name = cmArgs.Name
cm.Data = map[string]string{}
if cmArgs.EnvSource != "" {
envPairs, err = keyValuesFromEnvFile(l, cmArgs.EnvSource)
if err != nil {
return nil, fmt.Errorf("error reading keys from env source file: %s %v", cmArgs.EnvSource, err)
}
}
literalPairs, err = keyValuesFromLiteralSources(cmArgs.LiteralSources)
if err != nil {
return nil, fmt.Errorf("error reading key values from literal sources: %v", err)
}
filePairs, err = keyValuesFromFileSources(l, cmArgs.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 keyValuesFromEnvFile(l loader.Loader, path string) ([]kvPair, error) {
content, err := l.Load(path)
if err != nil {
return nil, err
}
return keyValuesFromLines(content)
}
func keyValuesFromLiteralSources(sources []string) ([]kvPair, error) {
var kvs []kvPair
for _, s := range sources {
// TODO: move ParseLiteralSource in this file
k, v, err := configmapandsecret.ParseLiteralSource(s)
if err != nil {
return nil, err
}
kvs = append(kvs, kvPair{key: k, value: v})
}
return kvs, nil
}
func keyValuesFromFileSources(l loader.Loader, sources []string) ([]kvPair, error) {
var kvs []kvPair
for _, s := range sources {
key, path, err := configmapandsecret.ParseFileSource(s)
if err != nil {
return nil, err
}
fileContent, err := l.Load(path)
if err != nil {
return nil, err
}
kvs = append(kvs, kvPair{key: key, value: string(fileContent)})
}
return kvs, 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
}
// NewResMapFromConfigMapArgs returns a Resource slice given a configmap metadata slice from kustomization file.
func NewResMapFromConfigMapArgs(loader loader.Loader, cmList []types.ConfigMapArgs) (ResMap, error) {
func NewResMapFromConfigMapArgs(
ldr loader.Loader, cmArgsList []types.ConfigMapArgs) (ResMap, error) {
var allResources []*resource.Resource
for _, cm := range cmList {
res, err := newResourceFromConfigMap(loader, cm)
for _, cmArgs := range cmArgsList {
if cmArgs.Behavior == "" {
cmArgs.Behavior = "create"
}
f := configmapandsecret.NewConfigMapFactory(&cmArgs, nil, ldr)
cm, err := f.MakeConfigMap2()
if err != nil {
return nil, err
}
res, err := resource.NewResourceWithBehavior(
cm, resource.NewGenerationBehavior(cmArgs.Behavior))
if err != nil {
return nil, err
}

View File

@@ -1,102 +0,0 @@
/*
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 resmap
import (
"bufio"
"bytes"
"fmt"
"os"
"strings"
"unicode"
"unicode/utf8"
"k8s.io/apimachinery/pkg/util/validation"
)
var utf8bom = []byte{0xEF, 0xBB, 0xBF}
// 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

@@ -1,67 +0,0 @@
/*
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 resmap
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)
}
}
}