create_namespace.go 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. Copyright 2015 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. namespaceLong = templates.LongDesc(i18n.T(`
  25. Create a namespace with the specified name.`))
  26. namespaceExample = templates.Examples(i18n.T(`
  27. # Create a new namespace named my-namespace
  28. kubectl create namespace my-namespace`))
  29. )
  30. // NamespaceOpts is the options for 'create namespare' sub command
  31. type NamespaceOpts struct {
  32. CreateSubcommandOptions *CreateSubcommandOptions
  33. }
  34. // NewCmdCreateNamespace is a macro command to create a new namespace
  35. func NewCmdCreateNamespace(f cmdutil.Factory, ioStreams genericclioptions.IOStreams) *cobra.Command {
  36. options := &NamespaceOpts{
  37. CreateSubcommandOptions: NewCreateSubcommandOptions(ioStreams),
  38. }
  39. cmd := &cobra.Command{
  40. Use: "namespace NAME [--dry-run]",
  41. DisableFlagsInUseLine: true,
  42. Aliases: []string{"ns"},
  43. Short: i18n.T("Create a namespace with the specified name"),
  44. Long: namespaceLong,
  45. Example: namespaceExample,
  46. Run: func(cmd *cobra.Command, args []string) {
  47. cmdutil.CheckErr(options.Complete(f, cmd, args))
  48. cmdutil.CheckErr(options.Run())
  49. },
  50. }
  51. options.CreateSubcommandOptions.PrintFlags.AddFlags(cmd)
  52. cmdutil.AddApplyAnnotationFlags(cmd)
  53. cmdutil.AddValidateFlags(cmd)
  54. cmdutil.AddGeneratorFlags(cmd, generateversioned.NamespaceV1GeneratorName)
  55. return cmd
  56. }
  57. // Complete completes all the required options
  58. func (o *NamespaceOpts) Complete(f cmdutil.Factory, cmd *cobra.Command, args []string) error {
  59. name, err := NameFromCommandArgs(cmd, args)
  60. if err != nil {
  61. return err
  62. }
  63. var generator generate.StructuredGenerator
  64. switch generatorName := cmdutil.GetFlagString(cmd, "generator"); generatorName {
  65. case generateversioned.NamespaceV1GeneratorName:
  66. generator = &generateversioned.NamespaceGeneratorV1{Name: name}
  67. default:
  68. return errUnsupportedGenerator(cmd, generatorName)
  69. }
  70. return o.CreateSubcommandOptions.Complete(f, cmd, args, generator)
  71. }
  72. // Run calls the CreateSubcommandOptions.Run in NamespaceOpts instance
  73. func (o *NamespaceOpts) Run() error {
  74. return o.CreateSubcommandOptions.Run()
  75. }