refactor kubectl autoscale to use the new generator

This commit is contained in:
wackxu 2017-11-16 17:29:47 +08:00
parent d246cf3864
commit 87054639a2
4 changed files with 24 additions and 37 deletions

View File

@ -75,12 +75,11 @@ func (s HorizontalPodAutoscalerGeneratorV1) validate() error {
if len(s.Name) == 0 { if len(s.Name) == 0 {
return fmt.Errorf("name must be specified") return fmt.Errorf("name must be specified")
} }
if s.MaxReplicas <= 0 { if s.MaxReplicas < 1 {
return fmt.Errorf("'max' is a required parameter and must be greater than zero") return fmt.Errorf("'max' is a required parameter and must be at least 1")
} }
if s.MinReplicas > s.MaxReplicas { if s.MinReplicas > s.MaxReplicas {
return fmt.Errorf("'max' must be greater than or equal to 'min'") return fmt.Errorf("'max' must be greater than or equal to 'min'")
} }
return nil return nil
} }

View File

@ -89,7 +89,7 @@ func TestHPAGenerate(t *testing.T) {
expectErr: true, expectErr: true,
}, },
{ {
name: "'max' must be greater than zero", name: "'max' must be at least 1",
HPAName: "foo", HPAName: "foo",
minReplicas: 1, minReplicas: 1,
maxReplicas: -10, maxReplicas: -10,

View File

@ -64,11 +64,11 @@ func NewCmdAutoscale(f cmdutil.Factory, out io.Writer) *cobra.Command {
ArgAliases: argAliases, ArgAliases: argAliases,
} }
cmdutil.AddPrinterFlags(cmd) cmdutil.AddPrinterFlags(cmd)
cmd.Flags().String("generator", "horizontalpodautoscaler/v1", i18n.T("The name of the API generator to use. Currently there is only 1 generator.")) cmd.Flags().String("generator", cmdutil.HorizontalPodAutoscalerV1GeneratorName, i18n.T("The name of the API generator to use. Currently there is only 1 generator."))
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.") cmd.Flags().Int32("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.")
cmd.Flags().Int("max", -1, "The upper limit for the number of pods that can be set by the autoscaler. Required.") cmd.Flags().Int32("max", -1, "The upper limit for the number of pods that can be set by the autoscaler. Required.")
cmd.MarkFlagRequired("max") cmd.MarkFlagRequired("max")
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.")) cmd.Flags().Int32("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."))
cmd.Flags().String("name", "", i18n.T("The name for the newly created object. If not specified, the name of the input resource will be used.")) cmd.Flags().String("name", "", i18n.T("The name for the newly created object. If not specified, the name of the input resource will be used."))
cmdutil.AddDryRunFlag(cmd) cmdutil.AddDryRunFlag(cmd)
usage := "identifying the resource to autoscale." usage := "identifying the resource to autoscale."
@ -102,15 +102,6 @@ func RunAutoscale(f cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []s
return err return err
} }
// Get the generator, setup and validate all required parameters
generatorName := cmdutil.GetFlagString(cmd, "generator")
generators := f.Generators("autoscale")
generator, found := generators[generatorName]
if !found {
return cmdutil.UsageErrorf(cmd, "generator %q not found.", generatorName)
}
names := generator.ParamNames()
count := 0 count := 0
err = r.Visit(func(info *resource.Info, err error) error { err = r.Visit(func(info *resource.Info, err error) error {
if err != nil { if err != nil {
@ -122,24 +113,25 @@ func RunAutoscale(f cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []s
return err return err
} }
name := info.Name // get the generator
params := kubectl.MakeParams(cmd, names) var generator kubectl.StructuredGenerator
params["default-name"] = name switch generatorName := cmdutil.GetFlagString(cmd, "generator"); generatorName {
case cmdutil.HorizontalPodAutoscalerV1GeneratorName:
params["scaleRef-kind"] = mapping.GroupVersionKind.Kind generator = &kubectl.HorizontalPodAutoscalerGeneratorV1{
params["scaleRef-name"] = name Name: info.Name,
params["scaleRef-apiVersion"] = mapping.GroupVersionKind.GroupVersion().String() MinReplicas: cmdutil.GetFlagInt32(cmd, "min"),
MaxReplicas: cmdutil.GetFlagInt32(cmd, "max"),
if err = kubectl.ValidateParams(names, params); err != nil { CPUPercent: cmdutil.GetFlagInt32(cmd, "cpu-percent"),
return err ScaleRefName: info.Name,
} ScaleRefKind: mapping.GroupVersionKind.Kind,
// Check for invalid flags used against the present generator. ScaleRefApiVersion: mapping.GroupVersionKind.GroupVersion().String(),
if err := kubectl.EnsureFlagsValid(cmd, generators, generatorName); err != nil { }
return err default:
return errUnsupportedGenerator(cmd, generatorName)
} }
// Generate new object // Generate new object
object, err := generator.Generate(params) object, err := generator.StructuredGenerate()
if err != nil { if err != nil {
return err return err
} }
@ -193,7 +185,7 @@ func RunAutoscale(f cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []s
func validateFlags(cmd *cobra.Command) error { func validateFlags(cmd *cobra.Command) error {
errs := []error{} errs := []error{}
max, min := cmdutil.GetFlagInt(cmd, "max"), cmdutil.GetFlagInt(cmd, "min") max, min := cmdutil.GetFlagInt32(cmd, "max"), cmdutil.GetFlagInt32(cmd, "min")
if max < 1 { if max < 1 {
errs = append(errs, fmt.Errorf("--max=MAXPODS is required and must be at least 1, max: %d", max)) errs = append(errs, fmt.Errorf("--max=MAXPODS is required and must be at least 1, max: %d", max))
} }

View File

@ -568,10 +568,6 @@ func DefaultGenerators(cmdName string) map[string]kubectl.Generator {
CronJobV2Alpha1GeneratorName: kubectl.CronJobV2Alpha1{}, CronJobV2Alpha1GeneratorName: kubectl.CronJobV2Alpha1{},
CronJobV1Beta1GeneratorName: kubectl.CronJobV1Beta1{}, CronJobV1Beta1GeneratorName: kubectl.CronJobV1Beta1{},
} }
case "autoscale":
generator = map[string]kubectl.Generator{
HorizontalPodAutoscalerV1GeneratorName: kubectl.HorizontalPodAutoscalerV1{},
}
case "namespace": case "namespace":
generator = map[string]kubectl.Generator{ generator = map[string]kubectl.Generator{
NamespaceV1GeneratorName: kubectl.NamespaceGeneratorV1{}, NamespaceV1GeneratorName: kubectl.NamespaceGeneratorV1{},