Fixed set env did not support keys with dot in it
Kubernetes-commit: ab5a81cd1502418b1c8a8f55da7f0550b42e7ab7
This commit is contained in:
parent
d16569cfc0
commit
c66790ded2
|
@ -25,21 +25,16 @@ import (
|
|||
|
||||
"k8s.io/api/core/v1"
|
||||
"k8s.io/apimachinery/pkg/util/sets"
|
||||
"k8s.io/apimachinery/pkg/util/validation"
|
||||
)
|
||||
|
||||
var argumentEnvironment = regexp.MustCompile("(?ms)^(.+)\\=(.*)$")
|
||||
var validArgumentEnvironment = regexp.MustCompile("(?ms)^(\\w+)\\=(.*)$")
|
||||
|
||||
// IsEnvironmentArgument checks whether a string is an environment argument, that is, whether it matches the "anycharacters=anycharacters" pattern.
|
||||
func IsEnvironmentArgument(s string) bool {
|
||||
return argumentEnvironment.MatchString(s)
|
||||
}
|
||||
|
||||
// IsValidEnvironmentArgument checks whether a string is a valid environment argument, that is, whether it matches the "wordcharacters=anycharacters" pattern. Word characters can be letters, numbers, and underscores.
|
||||
func IsValidEnvironmentArgument(s string) bool {
|
||||
return validArgumentEnvironment.MatchString(s)
|
||||
}
|
||||
|
||||
// SplitEnvironmentFromResources separates resources from environment arguments.
|
||||
// Resources must come first. Arguments may have the "DASH-" syntax.
|
||||
func SplitEnvironmentFromResources(args []string) (resources, envArgs []string, ok bool) {
|
||||
|
@ -70,8 +65,6 @@ func parseIntoEnvVar(spec []string, defaultReader io.Reader, envVarType string)
|
|||
var remove []string
|
||||
for _, envSpec := range spec {
|
||||
switch {
|
||||
case !IsValidEnvironmentArgument(envSpec) && !strings.HasSuffix(envSpec, "-"):
|
||||
return nil, nil, fmt.Errorf("%ss must be of the form key=value and can only contain letters, numbers, and underscores", envVarType)
|
||||
case envSpec == "-":
|
||||
if defaultReader == nil {
|
||||
return nil, nil, fmt.Errorf("when '-' is used, STDIN must be open")
|
||||
|
@ -86,6 +79,9 @@ func parseIntoEnvVar(spec []string, defaultReader io.Reader, envVarType string)
|
|||
if len(parts) != 2 {
|
||||
return nil, nil, fmt.Errorf("invalid %s: %v", envVarType, envSpec)
|
||||
}
|
||||
if errs := validation.IsEnvVarName(parts[0]); len(errs) != 0 {
|
||||
return nil, nil, fmt.Errorf("%q is not a valid key name: %s", parts[0], strings.Join(errs, ";"))
|
||||
}
|
||||
exists.Insert(parts[0])
|
||||
env = append(env, v1.EnvVar{
|
||||
Name: parts[0],
|
||||
|
|
|
@ -34,18 +34,6 @@ func ExampleIsEnvironmentArgument_false() {
|
|||
// Output: false
|
||||
}
|
||||
|
||||
func ExampleIsValidEnvironmentArgument_true() {
|
||||
test := "wordcharacters=true"
|
||||
fmt.Println(IsValidEnvironmentArgument(test))
|
||||
// Output: true
|
||||
}
|
||||
|
||||
func ExampleIsValidEnvironmentArgument_false() {
|
||||
test := "not$word^characters=test"
|
||||
fmt.Println(IsValidEnvironmentArgument(test))
|
||||
// Output: false
|
||||
}
|
||||
|
||||
func ExampleSplitEnvironmentFromResources() {
|
||||
args := []string{`resource`, "ENV\\=ARG", `ONE\=MORE`, `DASH-`}
|
||||
fmt.Println(SplitEnvironmentFromResources(args))
|
||||
|
@ -54,16 +42,40 @@ func ExampleSplitEnvironmentFromResources() {
|
|||
|
||||
func ExampleParseEnv_good() {
|
||||
r := strings.NewReader("FROM=READER")
|
||||
ss := []string{"ENV=VARIABLE", "AND=ANOTHER", "REMOVE-", "-"}
|
||||
ss := []string{"ENV=VARIABLE", "ENV.TEST=VARIABLE", "AND=ANOTHER", "REMOVE-", "-"}
|
||||
fmt.Println(ParseEnv(ss, r))
|
||||
// Output:
|
||||
// [{ENV VARIABLE nil} {AND ANOTHER nil} {FROM READER nil}] [REMOVE] <nil>
|
||||
// [{ENV VARIABLE nil} {ENV.TEST VARIABLE nil} {AND ANOTHER nil} {FROM READER nil}] [REMOVE] <nil>
|
||||
}
|
||||
|
||||
func ExampleParseEnv_bad() {
|
||||
func ExampleParseEnv_bad_first() {
|
||||
var r io.Reader
|
||||
bad := []string{"This not in the key=value format."}
|
||||
fmt.Println(ParseEnv(bad, r))
|
||||
// Output:
|
||||
// [] [] environment variables must be of the form key=value and can only contain letters, numbers, and underscores
|
||||
// [] [] "This not in the key" is not a valid key name: a valid environment variable name must consist of alphabetic characters, digits, '_', '-', or '.', and must not start with a digit (e.g. 'my.env-name', or 'MY_ENV.NAME', or 'MyEnvName1', regex used for validation is '[-._a-zA-Z][-._a-zA-Z0-9]*')
|
||||
}
|
||||
|
||||
func ExampleParseEnv_bad_second() {
|
||||
var r io.Reader
|
||||
bad := []string{".=VARIABLE"}
|
||||
fmt.Println(ParseEnv(bad, r))
|
||||
// Output:
|
||||
// [] [] "." is not a valid key name: must not be '.'
|
||||
}
|
||||
|
||||
func ExampleParseEnv_bad_third() {
|
||||
var r io.Reader
|
||||
bad := []string{"..=VARIABLE"}
|
||||
fmt.Println(ParseEnv(bad, r))
|
||||
// Output:
|
||||
// [] [] ".." is not a valid key name: must not be '..'
|
||||
}
|
||||
|
||||
func ExampleParseEnv_bad_fourth() {
|
||||
var r io.Reader
|
||||
bad := []string{"..ENV=VARIABLE"}
|
||||
fmt.Println(ParseEnv(bad, r))
|
||||
// Output:
|
||||
// [] [] "..ENV" is not a valid key name: must not start with '..'
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue