Skip to content

Commit

Permalink
OCM-9917 | fix: Bug with min/max replicas (edit machinepool)
Browse files Browse the repository at this point in the history
  • Loading branch information
hunterkepley committed Jul 25, 2024
1 parent 5f4f140 commit 791cd28
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 17 deletions.
7 changes: 3 additions & 4 deletions cmd/edit/machinepool/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ var _ = Describe("Edit Machinepool", func() {
c.State(cmv1.ClusterStateReady)
c.Hypershift(cmv1.NewHypershift().Enabled(false))
})
classicClusterReady := test.FormatClusterList([]*cmv1.Cluster{mockClassicClusterReady})

version := cmv1.NewVersion().ID("4.12.24").RawID("openshift-4.12.24")
awsNodePool := cmv1.NewAWSNodePool().InstanceType("m5.xlarge")
Expand Down Expand Up @@ -69,9 +68,8 @@ var _ = Describe("Edit Machinepool", func() {
t.SetCluster("", nil)
})

Describe("Machinepools", func() {
Describe("Machinepools", Ordered, func() {
It("Able to edit machinepool with no issues", func() {
t.ApiServer.AppendHandlers(RespondWithJSON(http.StatusOK, classicClusterReady))
// First get
t.ApiServer.AppendHandlers(RespondWithJSON(http.StatusOK, mpResponse))
// Edit
Expand All @@ -87,7 +85,6 @@ var _ = Describe("Edit Machinepool", func() {
nodePoolId, "--min-replicas", "2", "--enable-autoscaling", "true", "-y"})).To(Succeed())
})
It("Machinepool ID passed in without flag in random location", func() {
t.ApiServer.AppendHandlers(RespondWithJSON(http.StatusOK, classicClusterReady))
// First get
t.ApiServer.AppendHandlers(RespondWithJSON(http.StatusOK, mpResponse))
// Edit
Expand Down Expand Up @@ -135,6 +132,8 @@ var _ = Describe("Edit Machinepool", func() {
args := NewEditMachinepoolUserOptions()
args.machinepool = nodePoolId
args.autoscalingEnabled = true
args.maxReplicas = 10
args.minReplicas = 2
runner := EditMachinePoolRunner(args)
cmd := NewEditMachinePoolCommand()
Expect(cmd.Flag("cluster").Value.Set(clusterId)).To(Succeed())
Expand Down
14 changes: 13 additions & 1 deletion cmd/edit/machinepool/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func (m *EditMachinepoolOptions) Bind(args *EditMachinepoolUserOptions, argv []s
}

if m.args.machinepool == "" {
return fmt.Errorf("you need to specify a machine pool name")
return fmt.Errorf("You need to specify a machine pool name")
}

if m.args.labels != "" {
Expand All @@ -63,5 +63,17 @@ func (m *EditMachinepoolOptions) Bind(args *EditMachinepoolUserOptions, argv []s
}
}

if m.args.autoscalingEnabled {
if m.args.minReplicas <= 0 {
return fmt.Errorf("Min replicas must be greater than zero when autoscaling is enabled")
}
if m.args.maxReplicas <= 0 {
return fmt.Errorf("Max replicas must be greater than zero when autoscaling is enabled")
}
if m.args.minReplicas > m.args.maxReplicas {
return fmt.Errorf("Min replicas must be less than max replicas")
}
}

return nil
}
29 changes: 28 additions & 1 deletion cmd/edit/machinepool/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ var _ = Describe("Test edit machinepool options", func() {
})
Context("Edit Machinepool Options", func() {
var options EditMachinepoolOptions
BeforeEach(func() {
args = NewEditMachinepoolUserOptions()
})
It("Create args from options using Bind (also tests MachinePool func)", func() {
// Set value then bind
testMachinepool := "test"
Expand All @@ -25,7 +28,7 @@ var _ = Describe("Test edit machinepool options", func() {
It("Fail to bind args due to empty machinepool name", func() {
args.machinepool = ""
err := options.Bind(args, []string{})
Expect(err).To(MatchError("you need to specify a machine pool name"))
Expect(err).To(MatchError("You need to specify a machine pool name"))
})
It("Test Bind with argv instead of normal args (single arg, no flag for machinepool)", func() {
argv := []string{"test-id"}
Expand All @@ -48,5 +51,29 @@ var _ = Describe("Test edit machinepool options", func() {
err := options.Bind(args, []string{})
Expect(err).To(MatchError("Expected key=value format for labels"))
})
It("Test min replicas negative value (fail)", func() {
args.minReplicas = -1
args.maxReplicas = 1
args.autoscalingEnabled = true
args.machinepool = "test"
err := options.Bind(args, []string{})
Expect(err).To(MatchError("Min replicas must be greater than zero when autoscaling is enabled"))
})
It("Test max replicas negative value (fail)", func() {
args.maxReplicas = -1
args.minReplicas = 1
args.autoscalingEnabled = true
args.machinepool = "test"
err := options.Bind(args, []string{})
Expect(err).To(MatchError("Max replicas must be greater than zero when autoscaling is enabled"))
})
It("Test min replicas > max replicas (fail)", func() {
args.maxReplicas = 1
args.minReplicas = 5
args.autoscalingEnabled = true
args.machinepool = "test"
err := options.Bind(args, []string{})
Expect(err).To(MatchError("Min replicas must be less than max replicas"))
})
})
})
25 changes: 14 additions & 11 deletions pkg/machinepool/machinepool.go
Original file line number Diff line number Diff line change
Expand Up @@ -1321,8 +1321,7 @@ func (m *machinePool) EditMachinePool(cmd *cobra.Command, machinePoolId string,
if cluster.Hypershift().Enabled() {
return editNodePool(cmd, machinePoolId, clusterKey, cluster, r)
}
editMachinePool(cmd, machinePoolId, clusterKey, cluster, r)
return nil
return editMachinePool(cmd, machinePoolId, clusterKey, cluster, r)
}

// fillAutoScalingAndReplicas is filling either autoscaling or replicas value in the builder
Expand Down Expand Up @@ -1531,16 +1530,10 @@ func editMachinePool(cmd *cobra.Command, machinePoolId string,
return fmt.Errorf("Multi AZ clusters require that the number of MachinePool replicas be a multiple of 3")
}

labels, err := cmd.Flags().GetString("labels")
if err != nil {
return fmt.Errorf("Failed to get inputted labels: '%s'", err)
}
labels := cmd.Flags().Lookup("labels").Value.String()
labelMap := mpHelpers.GetLabelMap(cmd, r, machinePool.Labels(), labels)

taints, err := cmd.Flags().GetString("taints")
if err != nil {
return fmt.Errorf("Failed to get inputted taints: '%s'", err)
}
taints := cmd.Flags().Lookup("taints").Value.String()
taintBuilders := mpHelpers.GetTaints(cmd, r, machinePool.Taints(), taints)

mpBuilder := cmv1.NewMachinePool().
Expand Down Expand Up @@ -1628,7 +1621,17 @@ func editNodePool(cmd *cobra.Command, nodePoolID string,
}

if autoscaling && cmd.Flags().Changed("min-replicas") && minReplicas < 1 {
return fmt.Errorf("The number of machine pool min-replicas needs to be greater than zero")
return fmt.Errorf("The number of machine pool min-replicas needs to be a non-negative integer")
}

if autoscaling && cmd.Flags().Changed("max-replicas") && maxReplicas < 1 {
return fmt.Errorf("The number of machine pool max-replicas needs to be a non-negative integer")
}

if autoscaling && cmd.Flags().Changed("max-replicas") && cmd.Flags().Changed(
"min-replicas") && minReplicas > maxReplicas {
return fmt.Errorf("The number of machine pool min-replicas needs to be less than the number of " +
"machine pool max-replicas")
}

labels := cmd.Flags().Lookup("labels").Value.String()
Expand Down

0 comments on commit 791cd28

Please sign in to comment.