diff --git a/cmd/list/machinepool/cmd.go b/cmd/list/machinepool/cmd.go index 9c236a772d..2388da67e9 100644 --- a/cmd/list/machinepool/cmd.go +++ b/cmd/list/machinepool/cmd.go @@ -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" @@ -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()) } diff --git a/cmd/list/machinepool/machinepool.go b/cmd/list/machinepool/machinepool.go deleted file mode 100644 index 3dc95bd38e..0000000000 --- a/cmd/list/machinepool/machinepool.go +++ /dev/null @@ -1,55 +0,0 @@ -package machinepool - -import ( - "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" -) - -func listMachinePools(r *rosa.Runtime, clusterKey string, cluster *cmv1.Cluster) { - // Load any existing machine pools for this cluster - r.Reporter.Debugf("Loading machine pools for cluster '%s'", clusterKey) - machinePools, err := r.OCMClient.GetMachinePools(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() { - err = output.Print(machinePools) - 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) - - fmt.Fprintf(writer, - "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 { - fmt.Fprintf(writer, "%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()), - ) - } - writer.Flush() -} diff --git a/cmd/list/machinepool/nodepool.go b/cmd/list/machinepool/nodepool.go deleted file mode 100644 index 67923c427a..0000000000 --- a/cmd/list/machinepool/nodepool.go +++ /dev/null @@ -1,56 +0,0 @@ -package machinepool - -import ( - "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" -) - -func listNodePools(r *rosa.Runtime, clusterKey string, cluster *cmv1.Cluster) { - // Load any existing machine pools for this cluster - r.Reporter.Debugf("Loading machine pools for cluster '%s'", clusterKey) - 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() { - 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) - - fmt.Fprintf(writer, "ID\tAUTOSCALING\tREPLICAS\t"+ - "INSTANCE TYPE\tLABELS\t\tTAINTS\t\tAVAILABILITY ZONE\tSUBNET\tVERSION\tAUTOREPAIR\t\n") - for _, nodePool := range nodePools { - fmt.Fprintf(writer, "%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()), - ) - } - writer.Flush() -} diff --git a/pkg/machinepool/machinepool.go b/pkg/machinepool/machinepool.go index 8910e4be54..528f461ad4 100644 --- a/pkg/machinepool/machinepool.go +++ b/pkg/machinepool/machinepool.go @@ -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" ) @@ -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 { @@ -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 { @@ -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 +} diff --git a/pkg/machinepool/machinepool_test.go b/pkg/machinepool/machinepool_test.go index 4867dba0af..73a9c6a0b9 100644 --- a/pkg/machinepool/machinepool_test.go +++ b/pkg/machinepool/machinepool_test.go @@ -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 @@ -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()) @@ -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())))) + }) + }) })