From 9483f5ef3ffa8ae05ff6f07efe5d6f223b411c68 Mon Sep 17 00:00:00 2001 From: den-rgb Date: Tue, 3 Sep 2024 11:44:14 +0100 Subject: [PATCH] OCM-10777 | fix: fixing OCM-10777 + OCM-10818 --- pkg/helper/machinepools/helpers.go | 32 -------- pkg/helper/machinepools/helpers_test.go | 58 ------------- pkg/machinepool/helper.go | 23 +++--- pkg/machinepool/helper_test.go | 89 ++++++++++++++++++++ pkg/machinepool/machinepool.go | 54 +++++-------- pkg/machinepool/machinepool_test.go | 103 +++++++++++++++--------- 6 files changed, 185 insertions(+), 174 deletions(-) diff --git a/pkg/helper/machinepools/helpers.go b/pkg/helper/machinepools/helpers.go index 39310a0f8b..64ef4367dd 100644 --- a/pkg/helper/machinepools/helpers.go +++ b/pkg/helper/machinepools/helpers.go @@ -38,38 +38,6 @@ var allowedTaintEffects = []string{ "PreferNoSchedule", } -func MinNodePoolReplicaValidator(autoscaling bool) interactive.Validator { - return func(val interface{}) error { - minReplicas, err := strconv.Atoi(fmt.Sprintf("%v", val)) - if err != nil { - return err - } - if autoscaling { - if minReplicas < 1 { - return fmt.Errorf("min-replicas must be greater than zero") - } - } else { - if minReplicas < 0 { - return fmt.Errorf("replicas must be a non-negative integer") - } - } - return nil - } -} - -func MaxNodePoolReplicaValidator(minReplicas int) interactive.Validator { - return func(val interface{}) error { - maxReplicas, err := strconv.Atoi(fmt.Sprintf("%v", val)) - if err != nil { - return err - } - if minReplicas > maxReplicas { - return fmt.Errorf("max-replicas must be greater or equal to min-replicas") - } - return nil - } -} - func ParseLabels(labels string) (map[string]string, error) { labelMap := make(map[string]string) if labels == "" || labels == interactiveModeEmptyLabels { diff --git a/pkg/helper/machinepools/helpers_test.go b/pkg/helper/machinepools/helpers_test.go index 45ff0bac48..bcf2e467fd 100644 --- a/pkg/helper/machinepools/helpers_test.go +++ b/pkg/helper/machinepools/helpers_test.go @@ -89,64 +89,6 @@ var _ = Describe("MachinePool", func() { }) -var _ = Describe("Machine pool for hosted clusters", func() { - DescribeTable("Machine pool min replicas validation", - func(minReplicas int, autoscaling bool, hasError bool) { - err := MinNodePoolReplicaValidator(autoscaling)(minReplicas) - if hasError { - Expect(err).To(HaveOccurred()) - } else { - Expect(err).ToNot(HaveOccurred()) - } - }, - Entry("Zero replicas - no autoscaling", - 0, - false, - false, - ), - Entry("Negative replicas - no autoscaling", - -1, - false, - true, - ), - Entry("Zero replicas - autoscaling", - 0, - true, - true, - ), - Entry("One replicas - autoscaling", - 1, - true, - false, - ), - ) - DescribeTable("Machine pool max replicas validation", - func(minReplicas int, maxReplicas int, hasError bool) { - err := MaxNodePoolReplicaValidator(minReplicas)(maxReplicas) - if hasError { - Expect(err).To(HaveOccurred()) - } else { - Expect(err).ToNot(HaveOccurred()) - } - }, - Entry("Max > Min -> OK", - 1, - 2, - false, - ), - Entry("Min < Max -> OK", - 2, - 1, - true, - ), - Entry("Min = Max -> OK", - 2, - 2, - false, - ), - ) -}) - var _ = Describe("Label validations", func() { DescribeTable("Label validation", func(key string, value string, hasError bool) { diff --git a/pkg/machinepool/helper.go b/pkg/machinepool/helper.go index 937d31d9d0..e1c63810b0 100644 --- a/pkg/machinepool/helper.go +++ b/pkg/machinepool/helper.go @@ -189,32 +189,35 @@ func getMachinePoolAvailabilityZones(r *rosa.Runtime, cluster *cmv1.Cluster, mul return cluster.Nodes().AvailabilityZones(), nil } -func minReplicaValidator(multiAZMachinePool bool) interactive.Validator { +func maxReplicaValidator(minReplicas int, multiAZMachinePool bool) interactive.Validator { return func(val interface{}) error { - minReplicas, err := strconv.Atoi(fmt.Sprintf("%v", val)) + maxReplicas, err := strconv.Atoi(fmt.Sprintf("%v", val)) if err != nil { return err } - if minReplicas < 0 { - return fmt.Errorf("min-replicas must be a non-negative integer") + if minReplicas > maxReplicas { + return fmt.Errorf("max-replicas must be greater or equal to min-replicas") } - if multiAZMachinePool && minReplicas%3 != 0 { + if multiAZMachinePool && maxReplicas%3 != 0 { return fmt.Errorf("Multi AZ clusters require that the replicas be a multiple of 3") } return nil } } -func maxReplicaValidator(minReplicas int, multiAZMachinePool bool) interactive.Validator { +func minReplicaValidator(multiAZMachinePool bool, autoscaling bool) interactive.Validator { return func(val interface{}) error { - maxReplicas, err := strconv.Atoi(fmt.Sprintf("%v", val)) + minReplicas, err := strconv.Atoi(fmt.Sprintf("%v", val)) if err != nil { return err } - if minReplicas > maxReplicas { - return fmt.Errorf("max-replicas must be greater or equal to min-replicas") + if autoscaling && minReplicas < 1 { + return fmt.Errorf("min-replicas must be greater than zero") } - if multiAZMachinePool && maxReplicas%3 != 0 { + if !autoscaling && minReplicas < 0 { + return fmt.Errorf("Replicas must be a non-negative integer") + } + if multiAZMachinePool && minReplicas%3 != 0 { return fmt.Errorf("Multi AZ clusters require that the replicas be a multiple of 3") } return nil diff --git a/pkg/machinepool/helper_test.go b/pkg/machinepool/helper_test.go index 62dc5e94db..3c8328b1ac 100644 --- a/pkg/machinepool/helper_test.go +++ b/pkg/machinepool/helper_test.go @@ -414,6 +414,95 @@ var _ = Describe("getSecurityGroupsOption", func() { }) }) +var _ = Describe("Machine pool min/max replicas validation", func() { + DescribeTable("Machine pool min replicas validation", + func(minReplicas int, autoscaling bool, multiAZ bool, hasError bool) { + err := minReplicaValidator(multiAZ, autoscaling)(minReplicas) + if hasError { + Expect(err).To(HaveOccurred()) + } else { + Expect(err).ToNot(HaveOccurred()) + } + }, + Entry("Zero replicas - no autoscaling", + 0, + false, + false, + false, + ), + Entry("Negative replicas - no autoscaling", + -1, + false, + false, + true, + ), + Entry("Zero replicas - autoscaling", + 0, + true, + false, + true, + ), + Entry("One replicas - autoscaling", + 1, + true, + false, + false, + ), + Entry("Multi-AZ - 3 replicas", + 3, + true, + true, + false, + ), + Entry("Multi-AZ - 2 replicas", + 2, + true, + true, + true, + ), + ) + DescribeTable("Machine pool max replicas validation", + func(minReplicas int, maxReplicas int, multiAZ bool, hasError bool) { + err := maxReplicaValidator(minReplicas, multiAZ)(maxReplicas) + if hasError { + Expect(err).To(HaveOccurred()) + } else { + Expect(err).ToNot(HaveOccurred()) + } + }, + Entry("Max > Min -> OK", + 1, + 2, + false, + false, + ), + Entry("Min < Max -> OK", + 2, + 1, + false, + true, + ), + Entry("Min = Max -> OK", + 2, + 2, + false, + false, + ), + Entry("Not % 3 -> NOT OK", + 2, + 4, + true, + true, + ), + Entry("Multi-AZ -> OK", + 3, + 6, + true, + false, + ), + ) +}) + var _ = Describe("CreateAwsNodePoolBuilder", func() { It("correctly initializes AWSNodePoolBuilder with given parameters", func() { instanceType := "t2.micro" diff --git a/pkg/machinepool/machinepool.go b/pkg/machinepool/machinepool.go index 4955475ea3..0af23ae41b 100644 --- a/pkg/machinepool/machinepool.go +++ b/pkg/machinepool/machinepool.go @@ -228,7 +228,7 @@ func (m *machinePool) CreateMachinePool(r *rosa.Runtime, cmd *cobra.Command, clu } } - minReplicas, maxReplicas, replicas, autoscaling, err := manageReplicas(cmd, args, multiAZMachinePool, true) + minReplicas, maxReplicas, replicas, autoscaling, err := manageReplicas(cmd, args, multiAZMachinePool) if err != nil { return err } @@ -588,7 +588,7 @@ func (m *machinePool) CreateNodePools(r *rosa.Runtime, cmd *cobra.Command, clust } } - minReplicas, maxReplicas, replicas, autoscaling, err := manageReplicas(cmd, args, false, false) + minReplicas, maxReplicas, replicas, autoscaling, err := manageReplicas(cmd, args, false) if err != nil { return err } @@ -1245,7 +1245,8 @@ func getMachinePoolReplicas(cmd *cobra.Command, machinePoolID string, existingReplicas int, existingAutoscaling *cmv1.MachinePoolAutoscaling, - askForScalingParams bool) (autoscaling bool, + askForScalingParams bool, + multiAZ bool) (autoscaling bool, replicas, minReplicas, maxReplicas int, err error) { isMinReplicasSet := cmd.Flags().Changed("min-replicas") isMaxReplicasSet := cmd.Flags().Changed("max-replicas") @@ -1322,7 +1323,7 @@ func getMachinePoolReplicas(cmd *cobra.Command, Default: minReplicas, Required: replicasRequired, Validators: []interactive.Validator{ - mpHelpers.MinNodePoolReplicaValidator(false), + minReplicaValidator(multiAZ, false), }, }) if err != nil { @@ -1340,7 +1341,7 @@ func getMachinePoolReplicas(cmd *cobra.Command, Default: maxReplicas, Required: replicasRequired, Validators: []interactive.Validator{ - mpHelpers.MaxNodePoolReplicaValidator(minReplicas), + maxReplicaValidator(minReplicas, multiAZ), }, }) if err != nil { @@ -1433,7 +1434,7 @@ func editMachinePool(cmd *cobra.Command, machinePoolId string, autoscaling, replicas, minReplicas, maxReplicas, err := getMachinePoolReplicas(cmd, r.Reporter, machinePoolId, machinePool.Replicas(), machinePool.Autoscaling(), - !isLabelsSet && !isTaintsSet) + !isLabelsSet && !isTaintsSet, isMultiAZMachinePool(machinePool)) if err != nil { return fmt.Errorf("Failed to get autoscaling or replicas: '%s'", err) @@ -1808,7 +1809,8 @@ func getNodePoolReplicas(cmd *cobra.Command, r *rosa.Runtime, nodePoolID string, existingReplicas int, - existingAutoscaling *cmv1.NodePoolAutoscaling, isAnyAdditionalParameterSet bool) (autoscaling bool, + existingAutoscaling *cmv1.NodePoolAutoscaling, + isAnyAdditionalParameterSet bool) (autoscaling bool, replicas, minReplicas, maxReplicas int, err error) { isMinReplicasSet := cmd.Flags().Changed("min-replicas") @@ -1877,7 +1879,7 @@ func getNodePoolReplicas(cmd *cobra.Command, Default: existingAutoscaling.MinReplica(), Required: replicasRequired, Validators: []interactive.Validator{ - mpHelpers.MinNodePoolReplicaValidator(true), + minReplicaValidator(false, true), }, }) if err != nil { @@ -1894,7 +1896,7 @@ func getNodePoolReplicas(cmd *cobra.Command, Default: existingAutoscaling.MaxReplica(), Required: replicasRequired, Validators: []interactive.Validator{ - mpHelpers.MaxNodePoolReplicaValidator(minReplicas), + maxReplicaValidator(minReplicas, false), }, }) if err != nil { @@ -1916,7 +1918,7 @@ func getNodePoolReplicas(cmd *cobra.Command, Default: replicas, Required: true, Validators: []interactive.Validator{ - mpHelpers.MinNodePoolReplicaValidator(false), + minReplicaValidator(false, false), }, }) if err != nil { @@ -1953,8 +1955,8 @@ func editAutoscaling(nodePool *cmv1.NodePool, minReplicas int, maxReplicas int) return nil } -func manageReplicas(cmd *cobra.Command, args *mpOpts.CreateMachinepoolUserOptions, multiAZMachinePool bool, - isMachinePool bool) (minReplicas, maxReplicas, replicas int, autoscaling bool, err error) { +func manageReplicas(cmd *cobra.Command, args *mpOpts.CreateMachinepoolUserOptions, + multiAZMachinePool bool) (minReplicas, maxReplicas, replicas int, autoscaling bool, err error) { isMinReplicasSet := cmd.Flags().Changed("min-replicas") isMaxReplicasSet := cmd.Flags().Changed("max-replicas") isAutoscalingSet := cmd.Flags().Changed("enable-autoscaling") @@ -1965,22 +1967,6 @@ func manageReplicas(cmd *cobra.Command, args *mpOpts.CreateMachinepoolUserOption autoscaling = args.AutoscalingEnabled replicas = args.Replicas - getMinValidator := func(multiAZ bool) interactive.Validator { - if isMachinePool { - return minReplicaValidator(multiAZMachinePool) - } else { - return machinepools.MinNodePoolReplicaValidator(multiAZ) - } - } - - getMaxValidator := func(minReplicas int, multiAZMachinePool bool) interactive.Validator { - if isMachinePool { - return maxReplicaValidator(minReplicas, multiAZMachinePool) - } else { - return machinepools.MaxNodePoolReplicaValidator(minReplicas) - } - } - // Autoscaling if !isReplicasSet && !autoscaling && !isAutoscalingSet && interactive.Enabled() { autoscaling, err = interactive.GetBool(interactive.Input{ @@ -2010,7 +1996,7 @@ func manageReplicas(cmd *cobra.Command, args *mpOpts.CreateMachinepoolUserOption Default: minReplicas, Required: true, Validators: []interactive.Validator{ - getMinValidator(multiAZMachinePool), + minReplicaValidator(multiAZMachinePool, autoscaling), }, }) if err != nil { @@ -2018,7 +2004,7 @@ func manageReplicas(cmd *cobra.Command, args *mpOpts.CreateMachinepoolUserOption fmt.Errorf("Expected a valid number of min replicas: %s", err) } } - err := getMinValidator(multiAZMachinePool)(minReplicas) + err = minReplicaValidator(multiAZMachinePool, autoscaling)(minReplicas) if err != nil { return minReplicas, maxReplicas, replicas, autoscaling, err } @@ -2031,7 +2017,7 @@ func manageReplicas(cmd *cobra.Command, args *mpOpts.CreateMachinepoolUserOption Default: maxReplicas, Required: true, Validators: []interactive.Validator{ - getMaxValidator(maxReplicas, multiAZMachinePool), + maxReplicaValidator(maxReplicas, multiAZMachinePool), }, }) if err != nil { @@ -2039,7 +2025,7 @@ func manageReplicas(cmd *cobra.Command, args *mpOpts.CreateMachinepoolUserOption fmt.Errorf("Expected a valid number of max replicas: %s", err) } } - err = getMaxValidator(maxReplicas, multiAZMachinePool)(maxReplicas) + err = maxReplicaValidator(maxReplicas, multiAZMachinePool)(maxReplicas) if err != nil { return minReplicas, maxReplicas, replicas, autoscaling, err } @@ -2058,14 +2044,14 @@ func manageReplicas(cmd *cobra.Command, args *mpOpts.CreateMachinepoolUserOption Default: replicas, Required: true, Validators: []interactive.Validator{ - getMinValidator(multiAZMachinePool), + minReplicaValidator(multiAZMachinePool, autoscaling), }, }) if err != nil { return minReplicas, maxReplicas, replicas, autoscaling, fmt.Errorf("Expected a valid number of replicas: %s", err) } } - err := getMinValidator(multiAZMachinePool)(replicas) + err = minReplicaValidator(multiAZMachinePool, autoscaling)(replicas) if err != nil { return minReplicas, maxReplicas, replicas, autoscaling, err } diff --git a/pkg/machinepool/machinepool_test.go b/pkg/machinepool/machinepool_test.go index eafd7068f3..f7f8346e6f 100644 --- a/pkg/machinepool/machinepool_test.go +++ b/pkg/machinepool/machinepool_test.go @@ -515,7 +515,7 @@ var _ = Describe("Utility Functions", func() { var validator interactive.Validator BeforeEach(func() { - validator = minReplicaValidator(true) // or false for non-multiAZ + validator = minReplicaValidator(true, false) // or false for non-multiAZ }) When("input is non-integer", func() { @@ -842,7 +842,7 @@ var _ = Describe("MachinePools", func() { cmd.Flags().IntVar(&args.Replicas, "replicas", 0, "Replicas of the machine pool") cmd.Flags().Set("replicas", "3") args.AutoscalingEnabled = true - + args.Replicas = 3 err = machinePool.CreateMachinePool(t.RosaRuntime, cmd, clusterKey, cluster, &args) Expect(err).To(HaveOccurred()) Expect(err.Error()).To(Equal("Replicas can't be set when autoscaling is enabled")) @@ -858,11 +858,13 @@ var _ = Describe("MachinePools", func() { cmd.Flags().Set("multi-availability-zone", "true") cmd.Flags().Bool("enable-autoscaling", true, "") cmd.Flags().Set("enable-autoscaling", "true") - cmd.Flags().Int32("min-replicas", 0, "Replicas of the machine pool") + cmd.Flags().Int32("min-replicas", 1, "Replicas of the machine pool") cmd.Flags().Set("min-replicas", "1") - cmd.Flags().Int32("max-replicas", 0, "Replicas of the machine pool") + cmd.Flags().Int32("max-replicas", 3, "Replicas of the machine pool") cmd.Flags().Set("max-replicas", "3") args.AutoscalingEnabled = true + args.MinReplicas = 1 + args.MaxReplicas = 3 err = machinePool.CreateMachinePool(t.RosaRuntime, cmd, clusterKey, cluster, &args) Expect(err).To(HaveOccurred()) Expect(err.Error()).To(Equal("You must supply a valid instance type")) @@ -876,10 +878,12 @@ var _ = Describe("MachinePools", func() { cmd.Flags().Set("name", "mp-1") cmd.Flags().Bool("multi-availability-zone", true, "") cmd.Flags().Set("multi-availability-zone", "true") - cmd.Flags().Int32("min-replicas", 0, "Replicas of the machine pool") + cmd.Flags().Int32("min-replicas", 1, "Replicas of the machine pool") cmd.Flags().Set("min-replicas", "1") - cmd.Flags().Int32("max-replicas", 0, "Replicas of the machine pool") + cmd.Flags().Int32("max-replicas", 3, "Replicas of the machine pool") cmd.Flags().Set("max-replicas", "3") + args.MinReplicas = 1 + args.MaxReplicas = 3 err = machinePool.CreateMachinePool(t.RosaRuntime, cmd, clusterKey, cluster, &args) Expect(err).To(HaveOccurred()) Expect(err.Error()).To(Equal("Autoscaling must be enabled in order to set min and max replicas")) @@ -892,8 +896,9 @@ var _ = Describe("MachinePools", func() { cmd.Flags().Set("name", "mp-1") cmd.Flags().Bool("multi-availability-zone", true, "") cmd.Flags().Set("multi-availability-zone", "true") - cmd.Flags().IntVar(&args.Replicas, "replicas", 0, "Replicas of the machine pool") + cmd.Flags().IntVar(&args.Replicas, "replicas", 3, "Replicas of the machine pool") cmd.Flags().Set("replicas", "3") + args.Replicas = 3 args.InstanceType = "test" mt, err := cmv1.NewMachineType().ID("t3.small").Name("t3.small").Build() Expect(err).ToNot(HaveOccurred()) @@ -918,8 +923,9 @@ var _ = Describe("MachinePools", func() { cmd.Flags().Set("name", "mp-1") cmd.Flags().Bool("multi-availability-zone", true, "") cmd.Flags().Set("multi-availability-zone", "true") - cmd.Flags().IntVar(&args.Replicas, "replicas", 0, "Replicas of the machine pool") + cmd.Flags().IntVar(&args.Replicas, "replicas", 3, "Replicas of the machine pool") cmd.Flags().Set("replicas", "3") + args.Replicas = 3 cmd.Flags().BoolVar(&args.UseSpotInstances, "use-spot-instances", false, "") cmd.Flags().Set("use-spot-instances", "false") cmd.Flags().Changed("use-spot-instances") @@ -949,8 +955,9 @@ var _ = Describe("MachinePools", func() { cmd.Flags().Set("name", "mp-1") cmd.Flags().Bool("multi-availability-zone", true, "") cmd.Flags().Set("multi-availability-zone", "true") - cmd.Flags().IntVar(&args.Replicas, "replicas", 0, "Replicas of the machine pool") + cmd.Flags().IntVar(&args.Replicas, "replicas", 3, "Replicas of the machine pool") cmd.Flags().Set("replicas", "3") + args.Replicas = 3 args.InstanceType = "t3.small" args.UseSpotInstances = true mt, err := cmv1.NewMachineType().ID("t3.small").Name("t3.small").Build() @@ -978,8 +985,9 @@ var _ = Describe("MachinePools", func() { cmd.Flags().Set("name", "mp-1") cmd.Flags().Bool("multi-availability-zone", true, "") cmd.Flags().Set("multi-availability-zone", "true") - cmd.Flags().IntVar(&args.Replicas, "replicas", 0, "Replicas of the machine pool") + cmd.Flags().IntVar(&args.Replicas, "replicas", 3, "Replicas of the machine pool") cmd.Flags().Set("replicas", "3") + args.Replicas = 3 args.InstanceType = "t3.small" args.UseSpotInstances = true args.SpotMaxPrice = "1.00" @@ -1009,8 +1017,9 @@ var _ = Describe("MachinePools", func() { cmd.Flags().Set("name", "mp-1") cmd.Flags().Bool("multi-availability-zone", true, "") cmd.Flags().Set("multi-availability-zone", "true") - cmd.Flags().IntVar(&args.Replicas, "replicas", 0, "Replicas of the machine pool") + cmd.Flags().IntVar(&args.Replicas, "replicas", 3, "Replicas of the machine pool") cmd.Flags().Set("replicas", "3") + args.Replicas = 3 args.InstanceType = "t3.small" args.UseSpotInstances = true args.SpotMaxPrice = "1.00" @@ -1040,8 +1049,9 @@ var _ = Describe("MachinePools", func() { cmd.Flags().Set("name", "mp-1") cmd.Flags().Bool("multi-availability-zone", true, "") cmd.Flags().Set("multi-availability-zone", "true") - cmd.Flags().IntVar(&args.Replicas, "replicas", 0, "Replicas of the machine pool") + cmd.Flags().IntVar(&args.Replicas, "replicas", 3, "Replicas of the machine pool") cmd.Flags().Set("replicas", "3") + args.Replicas = 3 args.InstanceType = "t3.small" args.UseSpotInstances = true args.SpotMaxPrice = "1.00" @@ -1073,10 +1083,12 @@ var _ = Describe("MachinePools", func() { cmd.Flags().Set("multi-availability-zone", "true") cmd.Flags().Bool("enable-autoscaling", true, "") cmd.Flags().Set("enable-autoscaling", "true") - cmd.Flags().Int32("min-replicas", 0, "Replicas of the machine pool") + cmd.Flags().Int32("min-replicas", 1, "Replicas of the machine pool") cmd.Flags().Set("min-replicas", "1") - cmd.Flags().Int32("max-replicas", 0, "Replicas of the machine pool") + cmd.Flags().Int32("max-replicas", 3, "Replicas of the machine pool") cmd.Flags().Set("max-replicas", "3") + args.MinReplicas = 1 + args.MaxReplicas = 3 args.InstanceType = "t3.small" args.UseSpotInstances = true args.SpotMaxPrice = "1.00" @@ -1208,10 +1220,12 @@ var _ = Describe("NodePools", func() { cmd.Flags().Bool("enable-autoscaling", true, "") cmd.Flags().Set("enable-autoscaling", "true") - cmd.Flags().Int32("min-replicas", 0, "Replicas of the machine pool") + cmd.Flags().Int32("min-replicas", 1, "Replicas of the machine pool") cmd.Flags().Set("min-replicas", "1") - cmd.Flags().Int32("max-replicas", 0, "Replicas of the machine pool") + cmd.Flags().Int32("max-replicas", 3, "Replicas of the machine pool") cmd.Flags().Set("max-replicas", "3") + args.MinReplicas = 1 + args.MaxReplicas = 3 args.AutoscalingEnabled = true versionObj, err := v.Build() @@ -1246,10 +1260,12 @@ var _ = Describe("NodePools", func() { cmd.Flags().Bool("enable-autoscaling", true, "") cmd.Flags().Set("enable-autoscaling", "true") - cmd.Flags().Int32("min-replicas", 0, "Replicas of the machine pool") + cmd.Flags().Int32("min-replicas", 1, "Replicas of the machine pool") cmd.Flags().Set("min-replicas", "1") - cmd.Flags().Int32("max-replicas", 0, "Replicas of the machine pool") + cmd.Flags().Int32("max-replicas", 3, "Replicas of the machine pool") cmd.Flags().Set("max-replicas", "3") + args.MinReplicas = 1 + args.MaxReplicas = 3 args.AutoscalingEnabled = true args.InstanceType = "t3.small" @@ -1308,11 +1324,12 @@ var _ = Describe("NodePools", func() { cmd.Flags().Bool("enable-autoscaling", true, "") cmd.Flags().Set("enable-autoscaling", "true") - cmd.Flags().Int32("min-replicas", 0, "Replicas of the machine pool") + cmd.Flags().Int32("min-replicas", 1, "Replicas of the machine pool") cmd.Flags().Set("min-replicas", "1") - cmd.Flags().Int32("max-replicas", 0, "Replicas of the machine pool") + cmd.Flags().Int32("max-replicas", 3, "Replicas of the machine pool") cmd.Flags().Set("max-replicas", "3") - + args.MinReplicas = 1 + args.MaxReplicas = 3 args.AutoscalingEnabled = true args.InstanceType = "t3.small" args.TuningConfigs = "test" @@ -1364,10 +1381,12 @@ var _ = Describe("NodePools", func() { cmd.Flags().Bool("enable-autoscaling", true, "") cmd.Flags().Set("enable-autoscaling", "true") - cmd.Flags().Int32("min-replicas", 0, "Replicas of the machine pool") + cmd.Flags().Int32("min-replicas", 1, "Replicas of the machine pool") cmd.Flags().Set("min-replicas", "1") - cmd.Flags().Int32("max-replicas", 0, "Replicas of the machine pool") + cmd.Flags().Int32("max-replicas", 3, "Replicas of the machine pool") cmd.Flags().Set("max-replicas", "3") + args.MinReplicas = 1 + args.MaxReplicas = 3 args.AutoscalingEnabled = true args.InstanceType = "t3.small" args.TuningConfigs = "test" @@ -1437,15 +1456,17 @@ var _ = Describe("NodePools", func() { cmd.Flags().Bool("enable-autoscaling", true, "") cmd.Flags().Set("enable-autoscaling", "true") - cmd.Flags().Int32("min-replicas", 0, "Replicas of the machine pool") + cmd.Flags().Int32("min-replicas", 1, "Replicas of the machine pool") cmd.Flags().Set("min-replicas", "1") - cmd.Flags().Int32("max-replicas", 0, "Replicas of the machine pool") + cmd.Flags().Int32("max-replicas", 3, "Replicas of the machine pool") cmd.Flags().Set("max-replicas", "3") args.AutoscalingEnabled = true args.InstanceType = "t3.small" args.TuningConfigs = "test" args.KubeletConfigs = "test" args.NodeDrainGracePeriod = "30" + args.MinReplicas = 1 + args.MaxReplicas = 3 args.RootDiskSize = "200000000000GB" @@ -1499,32 +1520,32 @@ var _ = Describe("ManageReplicas", func() { var cmd *cobra.Command var args *mpOpts.CreateMachinepoolUserOptions var multiAZMachinePool bool - var isMachinePool bool BeforeEach(func() { cmd = &cobra.Command{} args = &mpOpts.CreateMachinepoolUserOptions{} multiAZMachinePool = true - isMachinePool = true }) When("when autoscaling is enabled", func() { It("should not allow setting replicas directly", func() { args.AutoscalingEnabled = true - cmd.Flags().Int32("replicas", 0, "Replicas of the machine pool") + cmd.Flags().Int32("replicas", 1, "Replicas of the machine pool") cmd.Flags().Set("replicas", "1") - _, _, _, autoscaling, err := manageReplicas(cmd, args, multiAZMachinePool, isMachinePool) + _, _, _, autoscaling, err := manageReplicas(cmd, args, multiAZMachinePool) Expect(err).To(HaveOccurred()) Expect(err.Error()).To(ContainSubstring("Replicas can't be set when autoscaling is enabled")) Expect(autoscaling).To(BeTrue()) }) It("should pass successfully", func() { args.AutoscalingEnabled = true - cmd.Flags().Int32("min-replicas", 0, "Replicas of the machine pool") - cmd.Flags().Set("min-replicas", "1") - cmd.Flags().Int32("max-replicas", 0, "Replicas of the machine pool") - cmd.Flags().Set("max-replicas", "3") - _, _, _, _, err := manageReplicas(cmd, args, multiAZMachinePool, isMachinePool) + cmd.Flags().Int32("min-replicas", 3, "Replicas of the machine pool") + cmd.Flags().Set("min-replicas", "3") + cmd.Flags().Int32("max-replicas", 6, "Replicas of the machine pool") + cmd.Flags().Set("max-replicas", "6") + args.MinReplicas = 3 + args.MaxReplicas = 6 + _, _, _, _, err := manageReplicas(cmd, args, multiAZMachinePool) Expect(err).ToNot(HaveOccurred()) }) }) @@ -1532,20 +1553,22 @@ var _ = Describe("ManageReplicas", func() { When("when autoscaling is not enabled", func() { It("should not allow setting min and max replicas", func() { args.AutoscalingEnabled = false - cmd.Flags().Int32("min-replicas", 0, "Replicas of the machine pool") + cmd.Flags().Int32("min-replicas", 1, "Replicas of the machine pool") cmd.Flags().Set("min-replicas", "1") - cmd.Flags().Int32("max-replicas", 0, "Replicas of the machine pool") + cmd.Flags().Int32("max-replicas", 3, "Replicas of the machine pool") cmd.Flags().Set("max-replicas", "3") - _, _, _, autoscaling, err := manageReplicas(cmd, args, multiAZMachinePool, isMachinePool) + args.MinReplicas = 1 + args.MaxReplicas = 3 + _, _, _, autoscaling, err := manageReplicas(cmd, args, multiAZMachinePool) Expect(err).To(HaveOccurred()) Expect(err.Error()).To(ContainSubstring("Autoscaling must be enabled in order to set min and max replicas")) Expect(autoscaling).To(BeFalse()) }) It("should pass successfully", func() { args.AutoscalingEnabled = false - cmd.Flags().Int32("replicas", 0, "Replicas of the machine pool") + cmd.Flags().Int32("replicas", 1, "Replicas of the machine pool") cmd.Flags().Set("replicas", "1") - _, _, _, autoscaling, err := manageReplicas(cmd, args, multiAZMachinePool, isMachinePool) + _, _, _, autoscaling, err := manageReplicas(cmd, args, multiAZMachinePool) Expect(err).ToNot(HaveOccurred()) Expect(autoscaling).To(BeFalse()) }) @@ -1571,7 +1594,7 @@ var _ = Describe("Utility Functions", func() { var validator interactive.Validator BeforeEach(func() { - validator = minReplicaValidator(true) // or false for non-multiAZ + validator = minReplicaValidator(true, false) // or false for non-multiAZ }) It("should return error for non-integer input", func() {