Skip to content

Commit

Permalink
OCM-7256 | feat: Move list machinepool non-cmd funcs to pkg, split, test
Browse files Browse the repository at this point in the history
  • Loading branch information
hunterkepley committed May 7, 2024
1 parent dc82090 commit 640be58
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 38 deletions.
3 changes: 1 addition & 2 deletions cmd/describe/machinepool/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,9 @@ func DescribeMachinePoolRunner(userOptions DescribeMachinepoolUserOptions) rosa.
if cluster.State() != cmv1.ClusterStateReady {
return fmt.Errorf("cluster '%s' is not yet ready", clusterKey)
}
isHypershift := cluster.Hypershift().Enabled()

service := machinepool.NewMachinePoolService()

return service.DescribeMachinePool(runtime, cluster, clusterKey, isHypershift, options.Machinepool())
return service.DescribeMachinePool(runtime, cluster, clusterKey, options.Machinepool())
}
}
5 changes: 4 additions & 1 deletion cmd/list/machinepool/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,8 @@ func run(_ *cobra.Command, _ []string) {
}

service := machinepool.NewMachinePoolService()
service.ListMachinePools(r, clusterKey, cluster, cluster.Hypershift().Enabled())
err := service.ListMachinePools(r, clusterKey, cluster)
if err != nil {
r.Reporter.Errorf("Failed to list machinepools: %s", err)
}
}
57 changes: 26 additions & 31 deletions pkg/machinepool/machinepool.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@ var notFoundMessage string = "Machine pool '%s' not found"

//go:generate mockgen -source=machinepool.go -package=mocks -destination=machinepool_mock.go
type MachinePoolService interface {
DescribeMachinePool(r *rosa.Runtime, cluster *cmv1.Cluster, clusterKey string, isHypershift bool,
machinePoolId string) error
ListMachinePools(r *rosa.Runtime, clusterKey string, cluster *cmv1.Cluster, isHypershift bool)
DescribeMachinePool(r *rosa.Runtime, cluster *cmv1.Cluster, clusterKey string, machinePoolId string) error
ListMachinePools(r *rosa.Runtime, clusterKey string, cluster *cmv1.Cluster) error
}

type machinePool struct {
Expand All @@ -34,52 +33,48 @@ func NewMachinePoolService() MachinePoolService {
}

// ListMachinePools lists all machinepools (or, nodepools if hypershift) in a cluster
func (m *machinePool) ListMachinePools(r *rosa.Runtime, clusterKey string, cluster *cmv1.Cluster, isHypershift bool) {
func (m *machinePool) ListMachinePools(r *rosa.Runtime, clusterKey string, cluster *cmv1.Cluster) error {
// Load any existing machine pools for this cluster
r.Reporter.Debugf("Loading machine pools for cluster '%s'", clusterKey)
isHypershift := cluster.Hypershift().Enabled()
var err error
var machinePools []*cmv1.MachinePool
var nodePools []*cmv1.NodePool
if !isHypershift {
machinePools, err = r.OCMClient.GetMachinePools(cluster.ID())
} else {
if isHypershift {
nodePools, err = r.OCMClient.GetNodePools(cluster.ID())
}
if err != nil {
r.Reporter.Errorf("Failed to get machine pools for cluster '%s': %v", clusterKey, err)
os.Exit(1)
if err != nil {
return err
}
} else {
machinePools, err = r.OCMClient.GetMachinePools(cluster.ID())
if err != nil {
return err
}
}

if output.HasFlag() {
if !isHypershift {
err = output.Print(machinePools)
} else {
err = output.Print(nodePools)
}
if err != nil {
r.Reporter.Errorf("%s", err)
os.Exit(1)
if isHypershift {
return output.Print(nodePools)
}
os.Exit(0)
return output.Print(machinePools)
}

// Create the writer that will be used to print the tabulated results:
writer := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)

var finalStringToOutput string
if !isHypershift {
finalStringToOutput = printMachinePools(machinePools)
} else {
finalStringToOutput = printNodePools(nodePools)
finalStringToOutput := getMachinePoolsString(machinePools)
if isHypershift {
finalStringToOutput = getNodePoolsString(nodePools)
}
fmt.Fprintf(writer, finalStringToOutput)
fmt.Fprint(writer, finalStringToOutput)
writer.Flush()
return nil
}

// DescribeMachinePool describes either a machinepool, or, a nodepool (if hypershift)
func (m machinePool) DescribeMachinePool(r *rosa.Runtime, cluster *cmv1.Cluster, clusterKey string, isHypershift bool,
func (m *machinePool) DescribeMachinePool(r *rosa.Runtime, cluster *cmv1.Cluster, clusterKey string,
machinePoolId string) error {
if isHypershift {
if cluster.Hypershift().Enabled() {
return m.describeNodePool(r, cluster, clusterKey, machinePoolId)
}

Expand All @@ -101,7 +96,7 @@ func (m machinePool) DescribeMachinePool(r *rosa.Runtime, cluster *cmv1.Cluster,
return nil
}

func (m machinePool) describeNodePool(r *rosa.Runtime, cluster *cmv1.Cluster, clusterKey string,
func (m *machinePool) describeNodePool(r *rosa.Runtime, cluster *cmv1.Cluster, clusterKey string,
nodePoolId string) error {
r.Reporter.Debugf(fetchMessage, "node pool", nodePoolId, clusterKey)
nodePool, exists, err := r.OCMClient.GetNodePool(cluster.ID(), nodePoolId)
Expand Down Expand Up @@ -172,7 +167,7 @@ func appendUpgradesIfExist(scheduledUpgrade *cmv1.NodePoolUpgradePolicy, output
return output
}

func printMachinePools(machinePools []*cmv1.MachinePool) string {
func getMachinePoolsString(machinePools []*cmv1.MachinePool) string {
outputString := "ID\tAUTOSCALING\tREPLICAS\tINSTANCE TYPE\tLABELS\t\tTAINTS\t" +
"\tAVAILABILITY ZONES\t\tSUBNETS\t\tSPOT INSTANCES\tDISK SIZE\tSG IDs\n"
for _, machinePool := range machinePools {
Expand All @@ -193,7 +188,7 @@ func printMachinePools(machinePools []*cmv1.MachinePool) string {
return outputString
}

func printNodePools(nodePools []*cmv1.NodePool) string {
func getNodePoolsString(nodePools []*cmv1.NodePool) string {
outputString := "ID\tAUTOSCALING\tREPLICAS\t" +
"INSTANCE TYPE\tLABELS\t\tTAINTS\t\tAVAILABILITY ZONE\tSUBNET\tVERSION\tAUTOREPAIR\t\n"
for _, nodePool := range nodePools {
Expand Down
6 changes: 2 additions & 4 deletions pkg/machinepool/machinepool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ var _ = Describe("Machinepool and nodepool", func() {
Subnet("sn").Version(cmv1.NewVersion().ID("1")).AutoRepair(false)))
cluster, err := clusterBuilder.Build()
Expect(err).ToNot(HaveOccurred())
Expect(err).ToNot(HaveOccurred())
out := printNodePools(cluster.NodePools().Slice())
out := getNodePoolsString(cluster.NodePools().Slice())
Expect(err).ToNot(HaveOccurred())
Expect(out).To(Equal(fmt.Sprintf("ID\tAUTOSCALING\tREPLICAS\t"+
"INSTANCE TYPE\tLABELS\t\tTAINTS\t\tAVAILABILITY ZONE\tSUBNET\tVERSION\tAUTOREPAIR\t\n"+
Expand Down Expand Up @@ -103,8 +102,7 @@ var _ = Describe("Machinepool and nodepool", func() {
Key("taint"))))
cluster, err := clusterBuilder.Build()
Expect(err).ToNot(HaveOccurred())
Expect(err).ToNot(HaveOccurred())
out := printMachinePools(cluster.MachinePools().Slice())
out := getMachinePoolsString(cluster.MachinePools().Slice())
Expect(err).ToNot(HaveOccurred())
Expect(out).To(Equal(fmt.Sprintf("ID\tAUTOSCALING\tREPLICAS\tINSTANCE TYPE\tLABELS\t\tTAINTS\t"+
"\tAVAILABILITY ZONES\t\tSUBNETS\t\tSPOT INSTANCES\tDISK SIZE\tSG IDs\n"+
Expand Down

0 comments on commit 640be58

Please sign in to comment.