diff --git a/pkg/configmapandsecret/util/env_file.go b/pkg/configmapandsecret/addfromenvfile.go similarity index 96% rename from pkg/configmapandsecret/util/env_file.go rename to pkg/configmapandsecret/addfromenvfile.go index ffa875adf..f403a5040 100644 --- a/pkg/configmapandsecret/util/env_file.go +++ b/pkg/configmapandsecret/addfromenvfile.go @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package util +package configmapandsecret import ( "bufio" @@ -71,7 +71,7 @@ func processEnvFileLine(line []byte, filePath string, // AddFromEnvFile processes an env file allows a generic addTo to handle the // collection of key value pairs or returns an error. -func AddFromEnvFile(filePath string, addTo func(key, value string) error) error { +func addFromEnvFile(filePath string, addTo func(key, value string) error) error { f, err := os.Open(filePath) if err != nil { return err diff --git a/pkg/configmapandsecret/configmapfactory.go b/pkg/configmapandsecret/configmapfactory.go index 1b5a09e5a..0fbff0787 100644 --- a/pkg/configmapandsecret/configmapfactory.go +++ b/pkg/configmapandsecret/configmapfactory.go @@ -19,12 +19,12 @@ package configmapandsecret import ( "encoding/json" + "errors" "fmt" "io/ioutil" "path" "strings" - cutil "github.com/kubernetes-sigs/kustomize/pkg/configmapandsecret/util" "github.com/kubernetes-sigs/kustomize/pkg/fs" "github.com/kubernetes-sigs/kustomize/pkg/hash" "github.com/kubernetes-sigs/kustomize/pkg/types" @@ -102,7 +102,7 @@ func (f *ConfigMapFactory) MakeConfigMap() (*corev1.ConfigMap, error) { // information into the provided configMap. func (f *ConfigMapFactory) handleConfigMapFromLiteralSources(configMap *v1.ConfigMap) error { for _, literalSource := range f.args.LiteralSources { - keyName, value, err := cutil.ParseLiteralSource(literalSource) + keyName, value, err := ParseLiteralSource(literalSource) if err != nil { return err } @@ -118,7 +118,7 @@ func (f *ConfigMapFactory) handleConfigMapFromLiteralSources(configMap *v1.Confi // into the provided configMap func (f *ConfigMapFactory) handleConfigMapFromFileSources(configMap *v1.ConfigMap) error { for _, fileSource := range f.args.FileSources { - keyName, filePath, err := cutil.ParseFileSource(fileSource) + keyName, filePath, err := ParseFileSource(fileSource) if err != nil { return err } @@ -162,7 +162,7 @@ func (f *ConfigMapFactory) handleConfigMapFromEnvFileSource(configMap *v1.Config return fmt.Errorf("env config file %s cannot be a directory", f.args.EnvSource) } - return cutil.AddFromEnvFile(f.args.EnvSource, func(key, value string) error { + return addFromEnvFile(f.args.EnvSource, func(key, value string) error { return addKeyFromLiteralToConfigMap(configMap, key, value) }) } @@ -190,3 +190,45 @@ func addKeyFromLiteralToConfigMap(configMap *v1.ConfigMap, keyName, data string) configMap.Data[keyName] = data return nil } + +// ParseFileSource parses the source given. +// +// Acceptable formats include: +// 1. source-path: the basename will become the key name +// 2. source-name=source-path: the source-name will become the key name and +// source-path is the path to the key file. +// +// Key names cannot include '='. +func ParseFileSource(source string) (keyName, filePath string, err error) { + numSeparators := strings.Count(source, "=") + switch { + case numSeparators == 0: + return path.Base(source), source, nil + case numSeparators == 1 && strings.HasPrefix(source, "="): + return "", "", fmt.Errorf("key name for file path %v missing", strings.TrimPrefix(source, "=")) + case numSeparators == 1 && strings.HasSuffix(source, "="): + return "", "", fmt.Errorf("file path for key name %v missing", strings.TrimSuffix(source, "=")) + case numSeparators > 1: + return "", "", errors.New("key names or file paths cannot contain '='") + default: + components := strings.Split(source, "=") + return components[0], components[1], nil + } +} + +// ParseLiteralSource parses the source key=val pair into its component pieces. +// This functionality is distinguished from strings.SplitN(source, "=", 2) since +// it returns an error in the case of empty keys, values, or a missing equals sign. +func ParseLiteralSource(source string) (keyName, value string, err error) { + // leading equal is invalid + if strings.Index(source, "=") == 0 { + return "", "", fmt.Errorf("invalid literal source %v, expected key=value", source) + } + // split after the first equal (so values can have the = character) + items := strings.SplitN(source, "=", 2) + if len(items) != 2 { + return "", "", fmt.Errorf("invalid literal source %v, expected key=value", source) + } + + return items[0], items[1], nil +} diff --git a/pkg/configmapandsecret/util/util.go b/pkg/configmapandsecret/util/util.go deleted file mode 100644 index 1479cc279..000000000 --- a/pkg/configmapandsecret/util/util.go +++ /dev/null @@ -1,67 +0,0 @@ -/* -Copyright 2017 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 util offers Configmap and Secret generation utilities. -package util - -import ( - "errors" - "fmt" - "path" - "strings" -) - -// ParseFileSource parses the source given. -// -// Acceptable formats include: -// 1. source-path: the basename will become the key name -// 2. source-name=source-path: the source-name will become the key name and -// source-path is the path to the key file. -// -// Key names cannot include '='. -func ParseFileSource(source string) (keyName, filePath string, err error) { - numSeparators := strings.Count(source, "=") - switch { - case numSeparators == 0: - return path.Base(source), source, nil - case numSeparators == 1 && strings.HasPrefix(source, "="): - return "", "", fmt.Errorf("key name for file path %v missing", strings.TrimPrefix(source, "=")) - case numSeparators == 1 && strings.HasSuffix(source, "="): - return "", "", fmt.Errorf("file path for key name %v missing", strings.TrimSuffix(source, "=")) - case numSeparators > 1: - return "", "", errors.New("key names or file paths cannot contain '='") - default: - components := strings.Split(source, "=") - return components[0], components[1], nil - } -} - -// ParseLiteralSource parses the source key=val pair into its component pieces. -// This functionality is distinguished from strings.SplitN(source, "=", 2) since -// it returns an error in the case of empty keys, values, or a missing equals sign. -func ParseLiteralSource(source string) (keyName, value string, err error) { - // leading equal is invalid - if strings.Index(source, "=") == 0 { - return "", "", fmt.Errorf("invalid literal source %v, expected key=value", source) - } - // split after the first equal (so values can have the = character) - items := strings.SplitN(source, "=", 2) - if len(items) != 2 { - return "", "", fmt.Errorf("invalid literal source %v, expected key=value", source) - } - - return items[0], items[1], nil -} diff --git a/pkg/resmap/configmap.go b/pkg/resmap/configmap.go index e60e86e72..9f00ac7d9 100644 --- a/pkg/resmap/configmap.go +++ b/pkg/resmap/configmap.go @@ -20,7 +20,7 @@ import ( "fmt" "strings" - cutil "github.com/kubernetes-sigs/kustomize/pkg/configmapandsecret/util" + "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" @@ -91,7 +91,7 @@ func keyValuesFromLiteralSources(sources []string) ([]kvPair, error) { var kvs []kvPair for _, s := range sources { // TODO: move ParseLiteralSource in this file - k, v, err := cutil.ParseLiteralSource(s) + k, v, err := configmapandsecret.ParseLiteralSource(s) if err != nil { return nil, err } @@ -104,7 +104,7 @@ func keyValuesFromFileSources(l loader.Loader, sources []string) ([]kvPair, erro var kvs []kvPair for _, s := range sources { - key, path, err := cutil.ParseFileSource(s) + key, path, err := configmapandsecret.ParseFileSource(s) if err != nil { return nil, err }