-
Notifications
You must be signed in to change notification settings - Fork 104
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add possibility for pipe-tasks to pipe env-files (#1484)
Summary: When creating `ConfigMap`s or `Secret`s k8s allows for taking an env-file using the `--from-env-file` option. Env-files contain a list of environment variables. These syntax rules apply: - Each line in an env file has to be in VAR=VAL format. - Lines beginning with # (i.e. comments) are ignored. - Blank lines are ignored. - There is no special handling of quotation marks (i.e. they will be part of the ConfigMap value)) New pipe-task `fnvFile` field can be used instead of `file` to signal that the pipe-file should be treated as an env-file: ```yaml spec: pipe: - envFile: /tmp/foo.env kind: ConfigMap key: foo ``` Note, that **either** `file` or `envFile` can be used but not both. For more information on env-files see [k8s documentation](https://kubernetes.io/docs/tasks/configure-pod-container/configure-pod-configmap/#create-configmaps-from-files) Fixes: #1394 Signed-off-by: Aleksey Dukhovniy <alex.dukhovniy@googlemail.com>
- Loading branch information
Aleksey Dukhovniy
committed
Apr 29, 2020
1 parent
2eecbfe
commit b92b50c
Showing
12 changed files
with
222 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package task | ||
|
||
// This file has been adopted from kubectl/pkg/generate/versioned/env_file.go and used to read files containing | ||
// env var pairs in pipe-tasks. | ||
|
||
import ( | ||
"bufio" | ||
"bytes" | ||
"fmt" | ||
"strings" | ||
"unicode" | ||
"unicode/utf8" | ||
|
||
"k8s.io/apimachinery/pkg/util/validation" | ||
) | ||
|
||
var utf8bom = []byte{0xEF, 0xBB, 0xBF} | ||
|
||
// proccessEnvFileLine returns a blank key if the line is empty or a comment. | ||
// The value will be retrieved from the environment if necessary. | ||
func proccessEnvFileLine(line []byte, currentLine int) (key, value string, err error) { | ||
|
||
if !utf8.Valid(line) { | ||
return ``, ``, fmt.Errorf("invalid utf8 bytes at line %d: %v", currentLine+1, 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 ``, ``, nil | ||
} | ||
|
||
data := strings.SplitN(string(line), "=", 2) | ||
if len(data) != 2 { | ||
return ``, ``, fmt.Errorf("%q is not a valid env var definition (KEY=VAL)", line) | ||
} | ||
|
||
key = data[0] | ||
if errs := validation.IsEnvVarName(key); len(errs) != 0 { | ||
return ``, ``, fmt.Errorf("%q is not a valid key name: %s", key, strings.Join(errs, ";")) | ||
} | ||
value = data[1] | ||
|
||
return key, value, nil | ||
} | ||
|
||
// addFromEnvFile processes an env file allows a generic addTo to handle the | ||
// collection of key value pairs or returns an error. | ||
func addFromEnvFile(data []byte, addTo func(key, value string)) error { | ||
r := bytes.NewReader(data) | ||
scanner := bufio.NewScanner(r) | ||
currentLine := 0 | ||
for scanner.Scan() { | ||
// Process the current line, retrieving a key/value pair if possible. | ||
scannedBytes := scanner.Bytes() | ||
key, value, err := proccessEnvFileLine(scannedBytes, currentLine) | ||
if err != nil { | ||
return err | ||
} | ||
currentLine++ | ||
|
||
if len(key) == 0 { | ||
// no key means line was empty or a comment | ||
continue | ||
} | ||
|
||
addTo(key, value) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.