configmap.go 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /*
  2. Copyright 2016 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 versioned
  14. import (
  15. "fmt"
  16. "io/ioutil"
  17. "os"
  18. "path"
  19. "strings"
  20. "unicode/utf8"
  21. "k8s.io/api/core/v1"
  22. "k8s.io/apimachinery/pkg/runtime"
  23. "k8s.io/apimachinery/pkg/util/validation"
  24. "k8s.io/kubernetes/pkg/kubectl/generate"
  25. "k8s.io/kubernetes/pkg/kubectl/util"
  26. "k8s.io/kubernetes/pkg/kubectl/util/hash"
  27. )
  28. // ConfigMapGeneratorV1 supports stable generation of a configMap.
  29. type ConfigMapGeneratorV1 struct {
  30. // Name of configMap (required)
  31. Name string
  32. // Type of configMap (optional)
  33. Type string
  34. // FileSources to derive the configMap from (optional)
  35. FileSources []string
  36. // LiteralSources to derive the configMap from (optional)
  37. LiteralSources []string
  38. // EnvFileSource to derive the configMap from (optional)
  39. EnvFileSource string
  40. // AppendHash; if true, derive a hash from the ConfigMap and append it to the name
  41. AppendHash bool
  42. }
  43. // Ensure it supports the generator pattern that uses parameter injection.
  44. var _ generate.Generator = &ConfigMapGeneratorV1{}
  45. // Ensure it supports the generator pattern that uses parameters specified during construction.
  46. var _ generate.StructuredGenerator = &ConfigMapGeneratorV1{}
  47. // Generate returns a configMap using the specified parameters.
  48. func (s ConfigMapGeneratorV1) Generate(genericParams map[string]interface{}) (runtime.Object, error) {
  49. err := generate.ValidateParams(s.ParamNames(), genericParams)
  50. if err != nil {
  51. return nil, err
  52. }
  53. delegate := &ConfigMapGeneratorV1{}
  54. fromFileStrings, found := genericParams["from-file"]
  55. if found {
  56. fromFileArray, isArray := fromFileStrings.([]string)
  57. if !isArray {
  58. return nil, fmt.Errorf("expected []string, found :%v", fromFileStrings)
  59. }
  60. delegate.FileSources = fromFileArray
  61. delete(genericParams, "from-file")
  62. }
  63. fromLiteralStrings, found := genericParams["from-literal"]
  64. if found {
  65. fromLiteralArray, isArray := fromLiteralStrings.([]string)
  66. if !isArray {
  67. return nil, fmt.Errorf("expected []string, found :%v", fromLiteralStrings)
  68. }
  69. delegate.LiteralSources = fromLiteralArray
  70. delete(genericParams, "from-literal")
  71. }
  72. fromEnvFileString, found := genericParams["from-env-file"]
  73. if found {
  74. fromEnvFile, isString := fromEnvFileString.(string)
  75. if !isString {
  76. return nil, fmt.Errorf("expected string, found :%v", fromEnvFileString)
  77. }
  78. delegate.EnvFileSource = fromEnvFile
  79. delete(genericParams, "from-env-file")
  80. }
  81. hashParam, found := genericParams["append-hash"]
  82. if found {
  83. hashBool, isBool := hashParam.(bool)
  84. if !isBool {
  85. return nil, fmt.Errorf("expected bool, found :%v", hashParam)
  86. }
  87. delegate.AppendHash = hashBool
  88. delete(genericParams, "append-hash")
  89. }
  90. params := map[string]string{}
  91. for key, value := range genericParams {
  92. strVal, isString := value.(string)
  93. if !isString {
  94. return nil, fmt.Errorf("expected string, saw %v for '%s'", value, key)
  95. }
  96. params[key] = strVal
  97. }
  98. delegate.Name = params["name"]
  99. delegate.Type = params["type"]
  100. return delegate.StructuredGenerate()
  101. }
  102. // ParamNames returns the set of supported input parameters when using the parameter injection generator pattern.
  103. func (s ConfigMapGeneratorV1) ParamNames() []generate.GeneratorParam {
  104. return []generate.GeneratorParam{
  105. {Name: "name", Required: true},
  106. {Name: "type", Required: false},
  107. {Name: "from-file", Required: false},
  108. {Name: "from-literal", Required: false},
  109. {Name: "from-env-file", Required: false},
  110. {Name: "force", Required: false},
  111. {Name: "hash", Required: false},
  112. }
  113. }
  114. // StructuredGenerate outputs a configMap object using the configured fields.
  115. func (s ConfigMapGeneratorV1) StructuredGenerate() (runtime.Object, error) {
  116. if err := s.validate(); err != nil {
  117. return nil, err
  118. }
  119. configMap := &v1.ConfigMap{}
  120. configMap.Name = s.Name
  121. configMap.Data = map[string]string{}
  122. configMap.BinaryData = map[string][]byte{}
  123. if len(s.FileSources) > 0 {
  124. if err := handleConfigMapFromFileSources(configMap, s.FileSources); err != nil {
  125. return nil, err
  126. }
  127. }
  128. if len(s.LiteralSources) > 0 {
  129. if err := handleConfigMapFromLiteralSources(configMap, s.LiteralSources); err != nil {
  130. return nil, err
  131. }
  132. }
  133. if len(s.EnvFileSource) > 0 {
  134. if err := handleConfigMapFromEnvFileSource(configMap, s.EnvFileSource); err != nil {
  135. return nil, err
  136. }
  137. }
  138. if s.AppendHash {
  139. h, err := hash.ConfigMapHash(configMap)
  140. if err != nil {
  141. return nil, err
  142. }
  143. configMap.Name = fmt.Sprintf("%s-%s", configMap.Name, h)
  144. }
  145. return configMap, nil
  146. }
  147. // validate validates required fields are set to support structured generation.
  148. func (s ConfigMapGeneratorV1) validate() error {
  149. if len(s.Name) == 0 {
  150. return fmt.Errorf("name must be specified")
  151. }
  152. if len(s.EnvFileSource) > 0 && (len(s.FileSources) > 0 || len(s.LiteralSources) > 0) {
  153. return fmt.Errorf("from-env-file cannot be combined with from-file or from-literal")
  154. }
  155. return nil
  156. }
  157. // handleConfigMapFromLiteralSources adds the specified literal source
  158. // information into the provided configMap.
  159. func handleConfigMapFromLiteralSources(configMap *v1.ConfigMap, literalSources []string) error {
  160. for _, literalSource := range literalSources {
  161. keyName, value, err := util.ParseLiteralSource(literalSource)
  162. if err != nil {
  163. return err
  164. }
  165. err = addKeyFromLiteralToConfigMap(configMap, keyName, value)
  166. if err != nil {
  167. return err
  168. }
  169. }
  170. return nil
  171. }
  172. // handleConfigMapFromFileSources adds the specified file source information
  173. // into the provided configMap
  174. func handleConfigMapFromFileSources(configMap *v1.ConfigMap, fileSources []string) error {
  175. for _, fileSource := range fileSources {
  176. keyName, filePath, err := util.ParseFileSource(fileSource)
  177. if err != nil {
  178. return err
  179. }
  180. info, err := os.Stat(filePath)
  181. if err != nil {
  182. switch err := err.(type) {
  183. case *os.PathError:
  184. return fmt.Errorf("error reading %s: %v", filePath, err.Err)
  185. default:
  186. return fmt.Errorf("error reading %s: %v", filePath, err)
  187. }
  188. }
  189. if info.IsDir() {
  190. if strings.Contains(fileSource, "=") {
  191. return fmt.Errorf("cannot give a key name for a directory path.")
  192. }
  193. fileList, err := ioutil.ReadDir(filePath)
  194. if err != nil {
  195. return fmt.Errorf("error listing files in %s: %v", filePath, err)
  196. }
  197. for _, item := range fileList {
  198. itemPath := path.Join(filePath, item.Name())
  199. if item.Mode().IsRegular() {
  200. keyName = item.Name()
  201. err = addKeyFromFileToConfigMap(configMap, keyName, itemPath)
  202. if err != nil {
  203. return err
  204. }
  205. }
  206. }
  207. } else {
  208. if err := addKeyFromFileToConfigMap(configMap, keyName, filePath); err != nil {
  209. return err
  210. }
  211. }
  212. }
  213. return nil
  214. }
  215. // handleConfigMapFromEnvFileSource adds the specified env file source information
  216. // into the provided configMap
  217. func handleConfigMapFromEnvFileSource(configMap *v1.ConfigMap, envFileSource string) error {
  218. info, err := os.Stat(envFileSource)
  219. if err != nil {
  220. switch err := err.(type) {
  221. case *os.PathError:
  222. return fmt.Errorf("error reading %s: %v", envFileSource, err.Err)
  223. default:
  224. return fmt.Errorf("error reading %s: %v", envFileSource, err)
  225. }
  226. }
  227. if info.IsDir() {
  228. return fmt.Errorf("env config file cannot be a directory")
  229. }
  230. return addFromEnvFile(envFileSource, func(key, value string) error {
  231. return addKeyFromLiteralToConfigMap(configMap, key, value)
  232. })
  233. }
  234. // addKeyFromFileToConfigMap adds a key with the given name to a ConfigMap, populating
  235. // the value with the content of the given file path, or returns an error.
  236. func addKeyFromFileToConfigMap(configMap *v1.ConfigMap, keyName, filePath string) error {
  237. data, err := ioutil.ReadFile(filePath)
  238. if err != nil {
  239. return err
  240. }
  241. if utf8.Valid(data) {
  242. return addKeyFromLiteralToConfigMap(configMap, keyName, string(data))
  243. }
  244. err = validateNewConfigMap(configMap, keyName)
  245. if err != nil {
  246. return err
  247. }
  248. configMap.BinaryData[keyName] = data
  249. return nil
  250. }
  251. // addKeyFromLiteralToConfigMap adds the given key and data to the given config map,
  252. // returning an error if the key is not valid or if the key already exists.
  253. func addKeyFromLiteralToConfigMap(configMap *v1.ConfigMap, keyName, data string) error {
  254. err := validateNewConfigMap(configMap, keyName)
  255. if err != nil {
  256. return err
  257. }
  258. configMap.Data[keyName] = data
  259. return nil
  260. }
  261. func validateNewConfigMap(configMap *v1.ConfigMap, keyName string) error {
  262. // Note, the rules for ConfigMap keys are the exact same as the ones for SecretKeys.
  263. if errs := validation.IsConfigMapKey(keyName); len(errs) != 0 {
  264. return fmt.Errorf("%q is not a valid key name for a ConfigMap: %s", keyName, strings.Join(errs, ";"))
  265. }
  266. if _, exists := configMap.Data[keyName]; exists {
  267. return fmt.Errorf("cannot add key %q, another key by that name already exists in Data for ConfigMap %q", keyName, configMap.Name)
  268. }
  269. if _, exists := configMap.BinaryData[keyName]; exists {
  270. return fmt.Errorf("cannot add key %q, another key by that name already exists in BinaryData for ConfigMap %q", keyName, configMap.Name)
  271. }
  272. return nil
  273. }