create_quota.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. quotaLong = templates.LongDesc(i18n.T(`
  25. Create a resourcequota with the specified name, hard limits and optional scopes`))
  26. quotaExample = templates.Examples(i18n.T(`
  27. # Create a new resourcequota named my-quota
  28. kubectl create quota my-quota --hard=cpu=1,memory=1G,pods=2,services=3,replicationcontrollers=2,resourcequotas=1,secrets=5,persistentvolumeclaims=10
  29. # Create a new resourcequota named best-effort
  30. kubectl create quota best-effort --hard=pods=100 --scopes=BestEffort`))
  31. )
  32. // QuotaOpts holds the command-line options for 'create quota' sub command
  33. type QuotaOpts struct {
  34. CreateSubcommandOptions *CreateSubcommandOptions
  35. }
  36. // NewCmdCreateQuota is a macro command to create a new quota
  37. func NewCmdCreateQuota(f cmdutil.Factory, ioStreams genericclioptions.IOStreams) *cobra.Command {
  38. options := &QuotaOpts{
  39. CreateSubcommandOptions: NewCreateSubcommandOptions(ioStreams),
  40. }
  41. cmd := &cobra.Command{
  42. Use: "quota NAME [--hard=key1=value1,key2=value2] [--scopes=Scope1,Scope2] [--dry-run=bool]",
  43. DisableFlagsInUseLine: true,
  44. Aliases: []string{"resourcequota"},
  45. Short: i18n.T("Create a quota with the specified name."),
  46. Long: quotaLong,
  47. Example: quotaExample,
  48. Run: func(cmd *cobra.Command, args []string) {
  49. cmdutil.CheckErr(options.Complete(f, cmd, args))
  50. cmdutil.CheckErr(options.Run())
  51. },
  52. }
  53. options.CreateSubcommandOptions.PrintFlags.AddFlags(cmd)
  54. cmdutil.AddApplyAnnotationFlags(cmd)
  55. cmdutil.AddValidateFlags(cmd)
  56. cmdutil.AddGeneratorFlags(cmd, generateversioned.ResourceQuotaV1GeneratorName)
  57. cmd.Flags().String("hard", "", i18n.T("A comma-delimited set of resource=quantity pairs that define a hard limit."))
  58. cmd.Flags().String("scopes", "", i18n.T("A comma-delimited set of quota scopes that must all match each object tracked by the quota."))
  59. return cmd
  60. }
  61. // Complete completes all the required options
  62. func (o *QuotaOpts) Complete(f cmdutil.Factory, cmd *cobra.Command, args []string) error {
  63. name, err := NameFromCommandArgs(cmd, args)
  64. if err != nil {
  65. return err
  66. }
  67. var generator generate.StructuredGenerator
  68. switch generatorName := cmdutil.GetFlagString(cmd, "generator"); generatorName {
  69. case generateversioned.ResourceQuotaV1GeneratorName:
  70. generator = &generateversioned.ResourceQuotaGeneratorV1{
  71. Name: name,
  72. Hard: cmdutil.GetFlagString(cmd, "hard"),
  73. Scopes: cmdutil.GetFlagString(cmd, "scopes"),
  74. }
  75. default:
  76. return errUnsupportedGenerator(cmd, generatorName)
  77. }
  78. return o.CreateSubcommandOptions.Complete(f, cmd, args, generator)
  79. }
  80. // Run calls the CreateSubcommandOptions.Run in QuotaOpts instance
  81. func (o *QuotaOpts) Run() error {
  82. return o.CreateSubcommandOptions.Run()
  83. }