create configmap from-env-file

This commit is contained in:
Michael Fraenkel
2016-12-09 10:35:23 -07:00
parent c3d7ee11d1
commit 7eb49628c6
5 changed files with 221 additions and 1 deletions

View File

@@ -38,6 +38,8 @@ type ConfigMapGeneratorV1 struct {
FileSources []string
// LiteralSources to derive the configMap from (optional)
LiteralSources []string
// EnvFileSource to derive the configMap from (optional)
EnvFileSource string
}
// Ensure it supports the generator pattern that uses parameter injection.
@@ -79,6 +81,15 @@ func (s ConfigMapGeneratorV1) Generate(genericParams map[string]interface{}) (ru
}
params[key] = strVal
}
fromEnvFileString, found := genericParams["from-env-file"]
if found {
fromEnvFile, isString := fromEnvFileString.(string)
if !isString {
return nil, fmt.Errorf("expected string, found :%v", fromEnvFileString)
}
delegate.EnvFileSource = fromEnvFile
delete(genericParams, "from-env-file")
}
delegate.Name = params["name"]
delegate.Type = params["type"]
return delegate.StructuredGenerate()
@@ -91,6 +102,7 @@ func (s ConfigMapGeneratorV1) ParamNames() []GeneratorParam {
{"type", false},
{"from-file", false},
{"from-literal", false},
{"from-env-file", false},
{"force", false},
}
}
@@ -113,6 +125,11 @@ func (s ConfigMapGeneratorV1) StructuredGenerate() (runtime.Object, error) {
return nil, err
}
}
if len(s.EnvFileSource) > 0 {
if err := handleConfigMapFromEnvFileSource(configMap, s.EnvFileSource); err != nil {
return nil, err
}
}
return configMap, nil
}
@@ -121,6 +138,9 @@ func (s ConfigMapGeneratorV1) validate() error {
if len(s.Name) == 0 {
return fmt.Errorf("name must be specified")
}
if len(s.EnvFileSource) > 0 && (len(s.FileSources) > 0 || len(s.LiteralSources) > 0) {
return fmt.Errorf("from-env-file cannot be combined with from-file or from-literal")
}
return nil
}
@@ -186,6 +206,24 @@ func handleConfigMapFromFileSources(configMap *api.ConfigMap, fileSources []stri
return nil
}
// handleConfigMapFromEnvFileSource adds the specified env file source information
// into the provided configMap
func handleConfigMapFromEnvFileSource(configMap *api.ConfigMap, envFileSource string) error {
info, err := os.Stat(envFileSource)
if err != nil {
switch err := err.(type) {
case *os.PathError:
return fmt.Errorf("error reading %s: %v", envFileSource, err.Err)
default:
return fmt.Errorf("error reading %s: %v", envFileSource, err)
}
}
if info.IsDir() {
return fmt.Errorf("must be a file")
}
return addFromEnvFileToConfigMap(configMap, envFileSource)
}
// addKeyFromFileToConfigMap adds a key with the given name to a ConfigMap, populating
// the value with the content of the given file path, or returns an error.
func addKeyFromFileToConfigMap(configMap *api.ConfigMap, keyName, filePath string) error {