autoscale.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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 cmd
  14. import (
  15. "fmt"
  16. "io"
  17. "github.com/renstrom/dedent"
  18. "k8s.io/kubernetes/pkg/kubectl"
  19. cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
  20. "k8s.io/kubernetes/pkg/kubectl/resource"
  21. utilerrors "k8s.io/kubernetes/pkg/util/errors"
  22. "github.com/spf13/cobra"
  23. )
  24. // AutoscaleOptions is the start of the data required to perform the operation. As new fields are added, add them here instead of
  25. // referencing the cmd.Flags()
  26. type AutoscaleOptions struct {
  27. Filenames []string
  28. Recursive bool
  29. }
  30. var (
  31. autoscaleLong = dedent.Dedent(`
  32. Creates an autoscaler that automatically chooses and sets the number of pods that run in a kubernetes cluster.
  33. Looks up a Deployment, ReplicaSet, or ReplicationController by name and creates an autoscaler that uses the given resource as a reference.
  34. An autoscaler can automatically increase or decrease number of pods deployed within the system as needed.`)
  35. autoscaleExample = dedent.Dedent(`
  36. # Auto scale a deployment "foo", with the number of pods between 2 and 10, target CPU utilization specified so a default autoscaling policy will be used:
  37. kubectl autoscale deployment foo --min=2 --max=10
  38. # Auto scale a replication controller "foo", with the number of pods between 1 and 5, target CPU utilization at 80%:
  39. kubectl autoscale rc foo --max=5 --cpu-percent=80`)
  40. )
  41. func NewCmdAutoscale(f *cmdutil.Factory, out io.Writer) *cobra.Command {
  42. options := &AutoscaleOptions{}
  43. validArgs := []string{"deployment", "replicaset", "replicationcontroller"}
  44. argAliases := kubectl.ResourceAliases(validArgs)
  45. cmd := &cobra.Command{
  46. Use: "autoscale (-f FILENAME | TYPE NAME | TYPE/NAME) [--min=MINPODS] --max=MAXPODS [--cpu-percent=CPU] [flags]",
  47. Short: "Auto-scale a Deployment, ReplicaSet, or ReplicationController",
  48. Long: autoscaleLong,
  49. Example: autoscaleExample,
  50. Run: func(cmd *cobra.Command, args []string) {
  51. err := RunAutoscale(f, out, cmd, args, options)
  52. cmdutil.CheckErr(err)
  53. },
  54. ValidArgs: validArgs,
  55. ArgAliases: argAliases,
  56. }
  57. cmdutil.AddPrinterFlags(cmd)
  58. cmd.Flags().String("generator", "horizontalpodautoscaler/v1", "The name of the API generator to use. Currently there is only 1 generator.")
  59. cmd.Flags().Int("min", -1, "The lower limit for the number of pods that can be set by the autoscaler. If it's not specified or negative, the server will apply a default value.")
  60. cmd.Flags().Int("max", -1, "The upper limit for the number of pods that can be set by the autoscaler. Required.")
  61. cmd.MarkFlagRequired("max")
  62. cmd.Flags().Int("cpu-percent", -1, fmt.Sprintf("The target average CPU utilization (represented as a percent of requested CPU) over all the pods. If it's not specified or negative, a default autoscaling policy will be used."))
  63. cmd.Flags().String("name", "", "The name for the newly created object. If not specified, the name of the input resource will be used.")
  64. cmdutil.AddDryRunFlag(cmd)
  65. usage := "Filename, directory, or URL to a file identifying the resource to autoscale."
  66. kubectl.AddJsonFilenameFlag(cmd, &options.Filenames, usage)
  67. cmdutil.AddRecursiveFlag(cmd, &options.Recursive)
  68. cmdutil.AddApplyAnnotationFlags(cmd)
  69. cmdutil.AddRecordFlag(cmd)
  70. cmdutil.AddInclude3rdPartyFlags(cmd)
  71. return cmd
  72. }
  73. func RunAutoscale(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []string, options *AutoscaleOptions) error {
  74. namespace, enforceNamespace, err := f.DefaultNamespace()
  75. if err != nil {
  76. return err
  77. }
  78. // validate flags
  79. if err := validateFlags(cmd); err != nil {
  80. return err
  81. }
  82. mapper, typer := f.Object(cmdutil.GetIncludeThirdPartyAPIs(cmd))
  83. r := resource.NewBuilder(mapper, typer, resource.ClientMapperFunc(f.ClientForMapping), f.Decoder(true)).
  84. ContinueOnError().
  85. NamespaceParam(namespace).DefaultNamespace().
  86. FilenameParam(enforceNamespace, options.Recursive, options.Filenames...).
  87. ResourceTypeOrNameArgs(false, args...).
  88. Flatten().
  89. Do()
  90. err = r.Err()
  91. if err != nil {
  92. return err
  93. }
  94. // Get the generator, setup and validate all required parameters
  95. generatorName := cmdutil.GetFlagString(cmd, "generator")
  96. generators := f.Generators("autoscale")
  97. generator, found := generators[generatorName]
  98. if !found {
  99. return cmdutil.UsageError(cmd, fmt.Sprintf("generator %q not found.", generatorName))
  100. }
  101. names := generator.ParamNames()
  102. count := 0
  103. err = r.Visit(func(info *resource.Info, err error) error {
  104. if err != nil {
  105. return err
  106. }
  107. mapping := info.ResourceMapping()
  108. if err := f.CanBeAutoscaled(mapping.GroupVersionKind.GroupKind()); err != nil {
  109. return err
  110. }
  111. name := info.Name
  112. params := kubectl.MakeParams(cmd, names)
  113. params["default-name"] = name
  114. params["scaleRef-kind"] = mapping.GroupVersionKind.Kind
  115. params["scaleRef-name"] = name
  116. params["scaleRef-apiVersion"] = mapping.GroupVersionKind.GroupVersion().String()
  117. if err = kubectl.ValidateParams(names, params); err != nil {
  118. return err
  119. }
  120. // Check for invalid flags used against the present generator.
  121. if err := kubectl.EnsureFlagsValid(cmd, generators, generatorName); err != nil {
  122. return err
  123. }
  124. // Generate new object
  125. object, err := generator.Generate(params)
  126. if err != nil {
  127. return err
  128. }
  129. resourceMapper := &resource.Mapper{
  130. ObjectTyper: typer,
  131. RESTMapper: mapper,
  132. ClientMapper: resource.ClientMapperFunc(f.ClientForMapping),
  133. Decoder: f.Decoder(true),
  134. }
  135. hpa, err := resourceMapper.InfoForObject(object, nil)
  136. if err != nil {
  137. return err
  138. }
  139. if cmdutil.ShouldRecord(cmd, hpa) {
  140. if err := cmdutil.RecordChangeCause(hpa.Object, f.Command()); err != nil {
  141. return err
  142. }
  143. object = hpa.Object
  144. }
  145. if cmdutil.GetDryRunFlag(cmd) {
  146. return f.PrintObject(cmd, mapper, object, out)
  147. }
  148. if err := kubectl.CreateOrUpdateAnnotation(cmdutil.GetFlagBool(cmd, cmdutil.ApplyAnnotationsFlag), hpa, f.JSONEncoder()); err != nil {
  149. return err
  150. }
  151. object, err = resource.NewHelper(hpa.Client, hpa.Mapping).Create(namespace, false, object)
  152. if err != nil {
  153. return err
  154. }
  155. count++
  156. if len(cmdutil.GetFlagString(cmd, "output")) > 0 {
  157. return f.PrintObject(cmd, mapper, object, out)
  158. }
  159. cmdutil.PrintSuccess(mapper, false, out, info.Mapping.Resource, info.Name, "autoscaled")
  160. return nil
  161. })
  162. if err != nil {
  163. return err
  164. }
  165. if count == 0 {
  166. return fmt.Errorf("no objects passed to autoscale")
  167. }
  168. return nil
  169. }
  170. func validateFlags(cmd *cobra.Command) error {
  171. errs := []error{}
  172. max, min := cmdutil.GetFlagInt(cmd, "max"), cmdutil.GetFlagInt(cmd, "min")
  173. if max < 1 {
  174. errs = append(errs, fmt.Errorf("--max=MAXPODS is required and must be at least 1, max: %d", max))
  175. }
  176. if max < min {
  177. errs = append(errs, fmt.Errorf("--max=MAXPODS must be larger or equal to --min=MINPODS, max: %d, min: %d", max, min))
  178. }
  179. return utilerrors.NewAggregate(errs)
  180. }