diff options
author | Matthew Heon <matthew.heon@pm.me> | 2020-02-14 15:46:18 -0500 |
---|---|---|
committer | Matthew Heon <matthew.heon@pm.me> | 2020-02-14 16:06:20 -0500 |
commit | 36a0ed9702bf4e6ef50650404c838a26f13ba879 (patch) | |
tree | 316a75c4cc66f02fed91cb40918e6396b314bc54 /cmd/podman/shared/parse/parse.go | |
parent | 97fdfd0a80b14b83abca3d8e0aa1b5c64ceac79b (diff) | |
download | podman-36a0ed9702bf4e6ef50650404c838a26f13ba879.tar.gz podman-36a0ed9702bf4e6ef50650404c838a26f13ba879.tar.bz2 podman-36a0ed9702bf4e6ef50650404c838a26f13ba879.zip |
Rework label parsing
We attempted to share all logic for parsing labels and
environment variables, which on the surface makes lots of sense
(both are formatted key=value so parsing logic should be
identical) but has begun to fall apart now that we have added
additional logic to environment variable handling. Environment
variables that are unset, for example, are looked up against
environment variables set for the process. We don't want this for
labels, so we have to split parsing logic.
Fixes #3854
Signed-off-by: Matthew Heon <matthew.heon@pm.me>
Diffstat (limited to 'cmd/podman/shared/parse/parse.go')
-rw-r--r-- | cmd/podman/shared/parse/parse.go | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/cmd/podman/shared/parse/parse.go b/cmd/podman/shared/parse/parse.go index 3a75ff7a8..79449029d 100644 --- a/cmd/podman/shared/parse/parse.go +++ b/cmd/podman/shared/parse/parse.go @@ -79,6 +79,34 @@ func ValidateDomain(val string) (string, error) { return "", fmt.Errorf("%s is not a valid domain", val) } +// GetAllLabels retrieves all labels given a potential label file and a number +// of labels provided from the command line. +func GetAllLabels(labelFile, inputLabels []string) (map[string]string, error) { + labels := make(map[string]string) + for _, file := range labelFile { + // Use of parseEnvFile still seems safe, as it's missing the + // extra parsing logic of parseEnv. + // There's an argument that we SHOULD be doing that parsing for + // all environment variables, even those sourced from files, but + // that would require a substantial rework. + if err := parseEnvFile(labels, file); err != nil { + return nil, err + } + } + for _, label := range inputLabels { + split := strings.SplitN(label, "=", 2) + if split[0] == "" { + return nil, errors.Errorf("invalid label format: %q", label) + } + value := "" + if len(split) > 1 { + value = split[1] + } + labels[split[0]] = value + } + return labels, nil +} + // reads a file of line terminated key=value pairs, and overrides any keys // present in the file with additional pairs specified in the override parameter // for env-file and labels-file flags |