create_quota.go 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 cmd
  14. import (
  15. "fmt"
  16. "io"
  17. "github.com/spf13/cobra"
  18. "k8s.io/kubernetes/pkg/kubectl"
  19. cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
  20. )
  21. const (
  22. quotaLong = `
  23. Create a resourcequota with the specified name, hard limits and optional scopes`
  24. quotaExample = ` // Create a new resourcequota named my-quota
  25. $ kubectl create quota my-quota --hard=cpu=1,memory=1G,pods=2,services=3,replicationcontrollers=2,resourcequotas=1,secrets=5,persistentvolumeclaims=10
  26. // Create a new resourcequota named best-effort
  27. $ kubectl create quota best-effort --hard=pods=100 --scopes=BestEffort`
  28. )
  29. // NewCmdCreateQuota is a macro command to create a new quota
  30. func NewCmdCreateQuota(f *cmdutil.Factory, cmdOut io.Writer) *cobra.Command {
  31. cmd := &cobra.Command{
  32. Use: "quota NAME [--hard=key1=value1,key2=value2] [--scopes=Scope1,Scope2] [--dry-run=bool]",
  33. Aliases: []string{"resourcequota"},
  34. Short: "Create a quota with the specified name.",
  35. Long: quotaLong,
  36. Example: quotaExample,
  37. Run: func(cmd *cobra.Command, args []string) {
  38. err := CreateQuota(f, cmdOut, cmd, args)
  39. cmdutil.CheckErr(err)
  40. },
  41. }
  42. cmdutil.AddApplyAnnotationFlags(cmd)
  43. cmdutil.AddValidateFlags(cmd)
  44. cmdutil.AddPrinterFlags(cmd)
  45. cmdutil.AddGeneratorFlags(cmd, cmdutil.ResourceQuotaV1GeneratorName)
  46. cmd.Flags().String("hard", "", "A comma-delimited set of resource=quantity pairs that define a hard limit.")
  47. cmd.Flags().String("scopes", "", "A comma-delimited set of quota scopes that must all match each object tracked by the quota.")
  48. return cmd
  49. }
  50. // CreateQuota implements the behavior to run the create quota command
  51. func CreateQuota(f *cmdutil.Factory, cmdOut io.Writer, cmd *cobra.Command, args []string) error {
  52. name, err := NameFromCommandArgs(cmd, args)
  53. if err != nil {
  54. return err
  55. }
  56. var generator kubectl.StructuredGenerator
  57. switch generatorName := cmdutil.GetFlagString(cmd, "generator"); generatorName {
  58. case cmdutil.ResourceQuotaV1GeneratorName:
  59. generator = &kubectl.ResourceQuotaGeneratorV1{
  60. Name: name,
  61. Hard: cmdutil.GetFlagString(cmd, "hard"),
  62. Scopes: cmdutil.GetFlagString(cmd, "scopes"),
  63. }
  64. default:
  65. return cmdutil.UsageError(cmd, fmt.Sprintf("Generator: %s not supported.", generatorName))
  66. }
  67. return RunCreateSubcommand(f, cmd, cmdOut, &CreateSubcommandOptions{
  68. Name: name,
  69. StructuredGenerator: generator,
  70. DryRun: cmdutil.GetFlagBool(cmd, "dry-run"),
  71. OutputFormat: cmdutil.GetFlagString(cmd, "output"),
  72. })
  73. }