create_configmap.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 create
  14. import (
  15. "github.com/spf13/cobra"
  16. "k8s.io/cli-runtime/pkg/genericclioptions"
  17. cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
  18. "k8s.io/kubernetes/pkg/kubectl/generate"
  19. generateversioned "k8s.io/kubernetes/pkg/kubectl/generate/versioned"
  20. "k8s.io/kubernetes/pkg/kubectl/util/i18n"
  21. "k8s.io/kubernetes/pkg/kubectl/util/templates"
  22. )
  23. var (
  24. configMapLong = templates.LongDesc(i18n.T(`
  25. Create a configmap based on a file, directory, or specified literal value.
  26. A single configmap may package one or more key/value pairs.
  27. When creating a configmap based on a file, the key will default to the basename of the file, and the value will
  28. default to the file content. If the basename is an invalid key, you may specify an alternate key.
  29. When creating a configmap based on a directory, each file whose basename is a valid key in the directory will be
  30. packaged into the configmap. Any directory entries except regular files are ignored (e.g. subdirectories,
  31. symlinks, devices, pipes, etc).`))
  32. configMapExample = templates.Examples(i18n.T(`
  33. # Create a new configmap named my-config based on folder bar
  34. kubectl create configmap my-config --from-file=path/to/bar
  35. # Create a new configmap named my-config with specified keys instead of file basenames on disk
  36. kubectl create configmap my-config --from-file=key1=/path/to/bar/file1.txt --from-file=key2=/path/to/bar/file2.txt
  37. # Create a new configmap named my-config with key1=config1 and key2=config2
  38. kubectl create configmap my-config --from-literal=key1=config1 --from-literal=key2=config2
  39. # Create a new configmap named my-config from the key=value pairs in the file
  40. kubectl create configmap my-config --from-file=path/to/bar
  41. # Create a new configmap named my-config from an env file
  42. kubectl create configmap my-config --from-env-file=path/to/bar.env`))
  43. )
  44. // ConfigMapOpts holds properties for create configmap sub-command
  45. type ConfigMapOpts struct {
  46. CreateSubcommandOptions *CreateSubcommandOptions
  47. }
  48. // NewCmdCreateConfigMap initializes and returns ConfigMapOpts
  49. func NewCmdCreateConfigMap(f cmdutil.Factory, ioStreams genericclioptions.IOStreams) *cobra.Command {
  50. options := &ConfigMapOpts{
  51. CreateSubcommandOptions: NewCreateSubcommandOptions(ioStreams),
  52. }
  53. cmd := &cobra.Command{
  54. Use: "configmap NAME [--from-file=[key=]source] [--from-literal=key1=value1] [--dry-run]",
  55. DisableFlagsInUseLine: true,
  56. Aliases: []string{"cm"},
  57. Short: i18n.T("Create a configmap from a local file, directory or literal value"),
  58. Long: configMapLong,
  59. Example: configMapExample,
  60. Run: func(cmd *cobra.Command, args []string) {
  61. cmdutil.CheckErr(options.Complete(f, cmd, args))
  62. cmdutil.CheckErr(options.Run())
  63. },
  64. }
  65. options.CreateSubcommandOptions.PrintFlags.AddFlags(cmd)
  66. cmdutil.AddApplyAnnotationFlags(cmd)
  67. cmdutil.AddValidateFlags(cmd)
  68. cmdutil.AddGeneratorFlags(cmd, generateversioned.ConfigMapV1GeneratorName)
  69. cmd.Flags().StringSlice("from-file", []string{}, "Key file can be specified using its file path, in which case file basename will be used as configmap key, or optionally with a key and file path, in which case the given key will be used. Specifying a directory will iterate each named file in the directory whose basename is a valid configmap key.")
  70. cmd.Flags().StringArray("from-literal", []string{}, "Specify a key and literal value to insert in configmap (i.e. mykey=somevalue)")
  71. cmd.Flags().String("from-env-file", "", "Specify the path to a file to read lines of key=val pairs to create a configmap (i.e. a Docker .env file).")
  72. cmd.Flags().Bool("append-hash", false, "Append a hash of the configmap to its name.")
  73. return cmd
  74. }
  75. // Complete completes all the required options
  76. func (o *ConfigMapOpts) Complete(f cmdutil.Factory, cmd *cobra.Command, args []string) error {
  77. name, err := NameFromCommandArgs(cmd, args)
  78. if err != nil {
  79. return err
  80. }
  81. var generator generate.StructuredGenerator
  82. switch generatorName := cmdutil.GetFlagString(cmd, "generator"); generatorName {
  83. case generateversioned.ConfigMapV1GeneratorName:
  84. generator = &generateversioned.ConfigMapGeneratorV1{
  85. Name: name,
  86. FileSources: cmdutil.GetFlagStringSlice(cmd, "from-file"),
  87. LiteralSources: cmdutil.GetFlagStringArray(cmd, "from-literal"),
  88. EnvFileSource: cmdutil.GetFlagString(cmd, "from-env-file"),
  89. AppendHash: cmdutil.GetFlagBool(cmd, "append-hash"),
  90. }
  91. default:
  92. return errUnsupportedGenerator(cmd, generatorName)
  93. }
  94. return o.CreateSubcommandOptions.Complete(f, cmd, args, generator)
  95. }
  96. // Run performs the execution of 'create' sub command options
  97. func (o *ConfigMapOpts) Run() error {
  98. return o.CreateSubcommandOptions.Run()
  99. }