Skip to content

Commit

Permalink
OCM-7256 | feat: Move non-cmd funcs to pkg, split, test
Browse files Browse the repository at this point in the history
  • Loading branch information
hunterkepley committed Apr 24, 2024
1 parent 93a653f commit 47127b3
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 116 deletions.
8 changes: 3 additions & 5 deletions cmd/list/machinepool/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
cmv1 "github.com/openshift-online/ocm-sdk-go/clustersmgmt/v1"
"github.com/spf13/cobra"

"github.com/openshift/rosa/pkg/machinepool"
"github.com/openshift/rosa/pkg/ocm"
"github.com/openshift/rosa/pkg/output"
"github.com/openshift/rosa/pkg/rosa"
Expand Down Expand Up @@ -56,9 +57,6 @@ func run(_ *cobra.Command, _ []string) {
os.Exit(1)
}

if cluster.Hypershift().Enabled() {
listNodePools(r, clusterKey, cluster)
} else {
listMachinePools(r, clusterKey, cluster)
}
service := machinepool.NewMachinePoolService()
service.ListMachinePools(r, clusterKey, cluster, cluster.Hypershift().Enabled())
}
55 changes: 0 additions & 55 deletions cmd/list/machinepool/machinepool.go

This file was deleted.

56 changes: 0 additions & 56 deletions cmd/list/machinepool/nodepool.go

This file was deleted.

91 changes: 91 additions & 0 deletions pkg/machinepool/machinepool.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ import (
"bytes"
"encoding/json"
"fmt"
"os"
"text/tabwriter"

cmv1 "github.com/openshift-online/ocm-sdk-go/clustersmgmt/v1"

ocmOutput "github.com/openshift/rosa/pkg/ocm/output"
"github.com/openshift/rosa/pkg/output"
"github.com/openshift/rosa/pkg/rosa"
)
Expand All @@ -18,6 +21,7 @@ var notFoundMessage string = "Machine pool '%s' not found"
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)
}

type machinePool struct {
Expand All @@ -29,6 +33,49 @@ func NewMachinePoolService() MachinePoolService {
return &machinePool{}
}

// 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) {
// Load any existing machine pools for this cluster
r.Reporter.Debugf("Loading machine pools for cluster '%s'", clusterKey)
var err error
var machinePools []*cmv1.MachinePool
var nodePools []*cmv1.NodePool
if !isHypershift {
machinePools, err = r.OCMClient.GetMachinePools(cluster.ID())
} else {
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 output.HasFlag() {
if !isHypershift {
err = output.Print(machinePools)
} else {
err = output.Print(nodePools)
}
if err != nil {
r.Reporter.Errorf("%s", err)
os.Exit(1)
}
os.Exit(0)
}

// 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)
}
fmt.Fprintf(writer, finalStringToOutput)
writer.Flush()
}

// DescribeMachinePool describes either a machinepool, or, a nodepool (if hypershift)
func (m machinePool) DescribeMachinePool(r *rosa.Runtime, cluster *cmv1.Cluster, clusterKey string, isHypershift bool,
machinePoolId string) error {
Expand Down Expand Up @@ -124,3 +171,47 @@ func appendUpgradesIfExist(scheduledUpgrade *cmv1.NodePoolUpgradePolicy, output
}
return output
}

func printMachinePools(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 {
outputString += fmt.Sprintf("%s\t%s\t%s\t%s\t%s\t\t%s\t\t%s\t\t%s\t\t%s\t%s\t%s\n",
machinePool.ID(),
ocmOutput.PrintMachinePoolAutoscaling(machinePool.Autoscaling()),
ocmOutput.PrintMachinePoolReplicas(machinePool.Autoscaling(), machinePool.Replicas()),
machinePool.InstanceType(),
ocmOutput.PrintLabels(machinePool.Labels()),
ocmOutput.PrintTaints(machinePool.Taints()),
output.PrintStringSlice(machinePool.AvailabilityZones()),
output.PrintStringSlice(machinePool.Subnets()),
ocmOutput.PrintMachinePoolSpot(machinePool),
ocmOutput.PrintMachinePoolDiskSize(machinePool),
output.PrintStringSlice(machinePool.AWS().AdditionalSecurityGroupIds()),
)
}
return outputString
}

func printNodePools(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 {
outputString += fmt.Sprintf("%s\t%s\t%s\t%s\t%s\t\t%s\t\t%s\t%s\t%s\t%s\t\n",
nodePool.ID(),
ocmOutput.PrintNodePoolAutoscaling(nodePool.Autoscaling()),
ocmOutput.PrintNodePoolReplicasShort(
ocmOutput.PrintNodePoolCurrentReplicas(nodePool.Status()),
ocmOutput.PrintNodePoolReplicas(nodePool.Autoscaling(), nodePool.Replicas()),
),
ocmOutput.PrintNodePoolInstanceType(nodePool.AWSNodePool()),
ocmOutput.PrintLabels(nodePool.Labels()),
ocmOutput.PrintTaints(nodePool.Taints()),
nodePool.AvailabilityZone(),
nodePool.Subnet(),
ocmOutput.PrintNodePoolVersion(nodePool.Version()),
ocmOutput.PrintNodePoolAutorepair(nodePool.AutoRepair()),
)
}
return outputString
}
60 changes: 60 additions & 0 deletions pkg/machinepool/machinepool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
cmv1 "github.com/openshift-online/ocm-sdk-go/clustersmgmt/v1"

ocmOutput "github.com/openshift/rosa/pkg/ocm/output"
"github.com/openshift/rosa/pkg/output"
)

var policyBuilder cmv1.NodePoolUpgradePolicyBuilder
Expand All @@ -23,6 +26,34 @@ var _ = Describe("Machinepool and nodepool", func() {
Value(cmv1.UpgradePolicyStateValueScheduled)).
NextRun(date)
})
It("Test printNodePools", func() {
clusterBuilder := cmv1.NewCluster().ID("test").State(cmv1.ClusterStateReady).
Hypershift(cmv1.NewHypershift().Enabled(true)).NodePools(cmv1.NewNodePoolList().
Items(cmv1.NewNodePool().ID("np").Replicas(8).AvailabilityZone("az").
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())
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"+
"%s\t%s\t%s\t%s\t%s\t\t%s\t\t%s\t%s\t%s\t%s\t\n",
cluster.NodePools().Get(0).ID(),
ocmOutput.PrintNodePoolAutoscaling(cluster.NodePools().Get(0).Autoscaling()),
ocmOutput.PrintNodePoolReplicasShort(
ocmOutput.PrintNodePoolCurrentReplicas(cluster.NodePools().Get(0).Status()),
ocmOutput.PrintNodePoolReplicas(cluster.NodePools().Get(0).Autoscaling(),
cluster.NodePools().Get(0).Replicas()),
),
ocmOutput.PrintNodePoolInstanceType(cluster.NodePools().Get(0).AWSNodePool()),
ocmOutput.PrintLabels(cluster.NodePools().Get(0).Labels()),
ocmOutput.PrintTaints(cluster.NodePools().Get(0).Taints()),
cluster.NodePools().Get(0).AvailabilityZone(),
cluster.NodePools().Get(0).Subnet(),
ocmOutput.PrintNodePoolVersion(cluster.NodePools().Get(0).Version()),
ocmOutput.PrintNodePoolAutorepair(cluster.NodePools().Get(0).AutoRepair()))))
})
It("Test appendUpgradesIfExist", func() {
policy, err := policyBuilder.Build()
Expect(err).ToNot(HaveOccurred())
Expand Down Expand Up @@ -63,4 +94,33 @@ var _ = Describe("Machinepool and nodepool", func() {
Expect(fmt.Sprint(out)).To(Equal(fmt.Sprint(expectedOutput)))
})
})
Context("MachinePools", func() {
It("Test printMachinePools", func() {
clusterBuilder := cmv1.NewCluster().ID("test").State(cmv1.ClusterStateReady).
MachinePools(cmv1.NewMachinePoolList().
Items(cmv1.NewMachinePool().ID("np").Replicas(8).Subnets("sn1", "sn2").
InstanceType("test instance type").Taints(cmv1.NewTaint().Value("test").
Key("taint"))))
cluster, err := clusterBuilder.Build()
Expect(err).ToNot(HaveOccurred())
Expect(err).ToNot(HaveOccurred())
out := printMachinePools(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"+
"%s\t%s\t%s\t%s\t%s\t\t%s\t\t%s\t\t%s\t\t%s\t%s\t%s\n",
cluster.MachinePools().Get(0).ID(),
ocmOutput.PrintMachinePoolAutoscaling(cluster.MachinePools().Get(0).Autoscaling()),
ocmOutput.PrintMachinePoolReplicas(cluster.MachinePools().Get(0).Autoscaling(),
cluster.MachinePools().Get(0).Replicas()),
cluster.MachinePools().Get(0).InstanceType(),
ocmOutput.PrintLabels(cluster.MachinePools().Get(0).Labels()),
ocmOutput.PrintTaints(cluster.MachinePools().Get(0).Taints()),
output.PrintStringSlice(cluster.MachinePools().Get(0).AvailabilityZones()),
output.PrintStringSlice(cluster.MachinePools().Get(0).Subnets()),
ocmOutput.PrintMachinePoolSpot(cluster.MachinePools().Get(0)),
ocmOutput.PrintMachinePoolDiskSize(cluster.MachinePools().Get(0)),
output.PrintStringSlice(cluster.MachinePools().Get(0).AWS().AdditionalSecurityGroupIds()))))
})
})
})

0 comments on commit 47127b3

Please sign in to comment.