Skip to content

Commit

Permalink
OCM-9954 | fix: set correct default value for replicas in update mp
Browse files Browse the repository at this point in the history
  • Loading branch information
marcolan018 committed Jul 31, 2024
1 parent bd7b665 commit ac070b9
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 10 deletions.
22 changes: 12 additions & 10 deletions pkg/machinepool/machinepool.go
Original file line number Diff line number Diff line change
Expand Up @@ -1442,19 +1442,21 @@ func getMachinePoolReplicas(cmd *cobra.Command,
return
}
}
} else if interactive.Enabled() || !isReplicasSet && askForScalingParams {
} else {
if !isReplicasSet {
replicas = existingReplicas
}
replicas, err = interactive.GetInt(interactive.Input{
Question: "Replicas",
Help: cmd.Flags().Lookup("replicas").Usage,
Default: replicas,
Required: true,
})
if err != nil {
err = fmt.Errorf("Expected a valid number of replicas: %s", err)
return
if interactive.Enabled() || !isReplicasSet && askForScalingParams {
replicas, err = interactive.GetInt(interactive.Input{
Question: "Replicas",
Help: cmd.Flags().Lookup("replicas").Usage,
Default: replicas,
Required: true,
})
if err != nil {
err = fmt.Errorf("Expected a valid number of replicas: %s", err)
return
}
}
}

Expand Down
71 changes: 71 additions & 0 deletions pkg/machinepool/machinepool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
cmv1 "github.com/openshift-online/ocm-sdk-go/clustersmgmt/v1"
"github.com/spf13/cobra"

"github.com/openshift/rosa/pkg/interactive"
ocmOutput "github.com/openshift/rosa/pkg/ocm/output"
"github.com/openshift/rosa/pkg/output"
"github.com/openshift/rosa/pkg/reporter"
"github.com/openshift/rosa/pkg/rosa"
. "github.com/openshift/rosa/pkg/test"
)
Expand Down Expand Up @@ -308,6 +310,75 @@ var _ = Describe("Machinepool and nodepool", func() {
Expect(MachinePoolKeyRE.MatchString("m123123123123123123123123123")).To(BeTrue())
Expect(MachinePoolKeyRE.MatchString("m#123")).To(BeFalse())
})
Context("Test getMachinePoolReplicas", func() {
var (
cmd *cobra.Command
r *reporter.Object
machinepoolId string
existingReplicas, existingMinReplicas, existingMaxReplicas int
existingAutoscaling *cmv1.MachinePoolAutoscaling
options struct {
replicas int
autoscalingEnabled bool
minReplicas int
maxReplicas int
}
)
BeforeEach(func() {
cmd = &cobra.Command{}
flags := cmd.Flags()
flags.IntVar(
&options.replicas,
"replicas",
0,
"Count of machines for this machine pool.",
)
flags.BoolVar(
&options.autoscalingEnabled,
"enable-autoscaling",
false,
"Enable autoscaling for the machine pool.",
)
flags.IntVar(
&options.minReplicas,
"min-replicas",
0,
"Minimum number of machines for the machine pool.",
)
flags.IntVar(
&options.maxReplicas,
"max-replicas",
0,
"Maximum number of machines for the machine pool.",
)
r = rosa.Runtime{}.Reporter
machinepoolId = "fake-id"
existingReplicas = 3
existingMinReplicas = 1
existingMaxReplicas = 3
existingAutoscaling, _ = cmv1.NewMachinePoolAutoscaling().
MaxReplicas(existingMaxReplicas).MinReplicas(existingMinReplicas).Build()
})
It("Replicas is not set without autoscaling enabled", func() {
autoscaling, replicas, minReplicas, maxReplicas, err :=
getMachinePoolReplicas(cmd, r, machinepoolId, existingReplicas, nil, false)
Expect(autoscaling).To(Equal(false))
Expect(minReplicas).To(Equal(0))
Expect(maxReplicas).To(Equal(0))
Expect(replicas).To(Equal(existingReplicas))
Expect(err).ToNot(HaveOccurred())
})
It("min/maxReplicas are not set with autoscaling enabled", func() {
cmd.Flags().Set("enable-autoscaling", "true")
autoscaling, replicas, minReplicas, maxReplicas, err :=
getMachinePoolReplicas(cmd, r, machinepoolId, 0, existingAutoscaling, false)
Expect(autoscaling).To(Equal(true))
Expect(minReplicas).To(Equal(existingMinReplicas))
Expect(maxReplicas).To(Equal(existingMaxReplicas))
Expect(replicas).To(Equal(0))
Expect(err).ToNot(HaveOccurred())
})
})
})

Describe("Testing getMachinePoolAvailabilityZones function", func() {
Expand Down

0 comments on commit ac070b9

Please sign in to comment.