env_parse.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. Copyright 2017 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package env
  14. import (
  15. "bufio"
  16. "fmt"
  17. "io"
  18. "regexp"
  19. "strings"
  20. "k8s.io/api/core/v1"
  21. "k8s.io/apimachinery/pkg/util/sets"
  22. )
  23. var argumentEnvironment = regexp.MustCompile("(?ms)^(.+)\\=(.*)$")
  24. var validArgumentEnvironment = regexp.MustCompile("(?ms)^(\\w+)\\=(.*)$")
  25. // IsEnvironmentArgument checks whether a string is an environment argument, that is, whether it matches the "anycharacters=anycharacters" pattern.
  26. func IsEnvironmentArgument(s string) bool {
  27. return argumentEnvironment.MatchString(s)
  28. }
  29. // 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.
  30. func IsValidEnvironmentArgument(s string) bool {
  31. return validArgumentEnvironment.MatchString(s)
  32. }
  33. // SplitEnvironmentFromResources separates resources from environment arguments.
  34. // Resources must come first. Arguments may have the "DASH-" syntax.
  35. func SplitEnvironmentFromResources(args []string) (resources, envArgs []string, ok bool) {
  36. first := true
  37. for _, s := range args {
  38. // this method also has to understand env removal syntax, i.e. KEY-
  39. isEnv := IsEnvironmentArgument(s) || strings.HasSuffix(s, "-")
  40. switch {
  41. case first && isEnv:
  42. first = false
  43. fallthrough
  44. case !first && isEnv:
  45. envArgs = append(envArgs, s)
  46. case first && !isEnv:
  47. resources = append(resources, s)
  48. case !first && !isEnv:
  49. return nil, nil, false
  50. }
  51. }
  52. return resources, envArgs, true
  53. }
  54. // parseIntoEnvVar parses the list of key-value pairs into kubernetes EnvVar.
  55. // envVarType is for making errors more specific to user intentions.
  56. func parseIntoEnvVar(spec []string, defaultReader io.Reader, envVarType string) ([]v1.EnvVar, []string, error) {
  57. env := []v1.EnvVar{}
  58. exists := sets.NewString()
  59. var remove []string
  60. for _, envSpec := range spec {
  61. switch {
  62. case !IsValidEnvironmentArgument(envSpec) && !strings.HasSuffix(envSpec, "-"):
  63. return nil, nil, fmt.Errorf("%ss must be of the form key=value and can only contain letters, numbers, and underscores", envVarType)
  64. case envSpec == "-":
  65. if defaultReader == nil {
  66. return nil, nil, fmt.Errorf("when '-' is used, STDIN must be open")
  67. }
  68. fileEnv, err := readEnv(defaultReader, envVarType)
  69. if err != nil {
  70. return nil, nil, err
  71. }
  72. env = append(env, fileEnv...)
  73. case strings.Index(envSpec, "=") != -1:
  74. parts := strings.SplitN(envSpec, "=", 2)
  75. if len(parts) != 2 {
  76. return nil, nil, fmt.Errorf("invalid %s: %v", envVarType, envSpec)
  77. }
  78. exists.Insert(parts[0])
  79. env = append(env, v1.EnvVar{
  80. Name: parts[0],
  81. Value: parts[1],
  82. })
  83. case strings.HasSuffix(envSpec, "-"):
  84. remove = append(remove, envSpec[:len(envSpec)-1])
  85. default:
  86. return nil, nil, fmt.Errorf("unknown %s: %v", envVarType, envSpec)
  87. }
  88. }
  89. for _, removeLabel := range remove {
  90. if _, found := exists[removeLabel]; found {
  91. return nil, nil, fmt.Errorf("can not both modify and remove the same %s in the same command", envVarType)
  92. }
  93. }
  94. return env, remove, nil
  95. }
  96. // ParseEnv parses the elements of the first argument looking for environment variables in key=value form and, if one of those values is "-", it also scans the reader.
  97. // The same environment variable cannot be both modified and removed in the same command.
  98. func ParseEnv(spec []string, defaultReader io.Reader) ([]v1.EnvVar, []string, error) {
  99. return parseIntoEnvVar(spec, defaultReader, "environment variable")
  100. }
  101. func readEnv(r io.Reader, envVarType string) ([]v1.EnvVar, error) {
  102. env := []v1.EnvVar{}
  103. scanner := bufio.NewScanner(r)
  104. for scanner.Scan() {
  105. envSpec := scanner.Text()
  106. if pos := strings.Index(envSpec, "#"); pos != -1 {
  107. envSpec = envSpec[:pos]
  108. }
  109. if strings.Index(envSpec, "=") != -1 {
  110. parts := strings.SplitN(envSpec, "=", 2)
  111. if len(parts) != 2 {
  112. return nil, fmt.Errorf("invalid %s: %v", envVarType, envSpec)
  113. }
  114. env = append(env, v1.EnvVar{
  115. Name: parts[0],
  116. Value: parts[1],
  117. })
  118. }
  119. }
  120. if err := scanner.Err(); err != nil && err != io.EOF {
  121. return nil, err
  122. }
  123. return env, nil
  124. }