Skip to content

Commit bea53d8

Browse files
committed
node pools
1 parent 75d64ee commit bea53d8

File tree

2 files changed

+22
-110
lines changed

2 files changed

+22
-110
lines changed

pkg/machinepool/machinepool.go

Lines changed: 13 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1031,9 +1031,7 @@ func (m *machinePool) CreateNodePools(r *rosa.Runtime, cmd *cobra.Command, clust
10311031
return nil
10321032
}
10331033

1034-
// ListMachinePools lists all machinepools (or, nodepools if hypershift) in a cluster
10351034
func (m *machinePool) ListMachinePools(r *rosa.Runtime, clusterKey string, cluster *cmv1.Cluster, args ListMachinePoolArgs) error {
1036-
// Load any existing machine pools for this cluster
10371035
r.Reporter.Debugf("Loading machine pools for cluster '%s'", clusterKey)
10381036
isHypershift := cluster.Hypershift().Enabled()
10391037
var err error
@@ -1058,49 +1056,44 @@ func (m *machinePool) ListMachinePools(r *rosa.Runtime, clusterKey string, clust
10581056
return output.Print(machinePools)
10591057
}
10601058

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

1064-
// Get table as string first (hunterkepley's suggestion - less invasive approach)
1065-
var tableString string
1061+
var headers []string
1062+
var tableData [][]string
1063+
10661064
if isHypershift {
1067-
tableString = getNodePoolsString(nodePools)
1065+
headers, tableData = getNodePoolsData(nodePools)
10681066
} else {
1069-
tableString = getMachinePoolsString(r, machinePools, args)
1067+
headers, tableData = getMachinePoolsData(r, machinePools, args)
10701068
}
10711069

1072-
// Parse the string output into headers and data
1073-
headers, tableData := parseTableString(tableString)
1074-
1075-
// Only hide empty columns if the flag is set
10761070
if output.ShouldHideEmptyColumns() {
1077-
// Find which columns are special (controlled by flags)
1078-
specialColumns := make(map[int]bool)
10791071
if !isHypershift {
1072+
specialColumns := make(map[int]bool)
10801073
for i, header := range headers {
10811074
if (header == "AZ TYPE" && (args.ShowAZType || args.ShowAll)) ||
10821075
(header == "WIN-LI ENABLED" && (args.ShowWindowsLI || args.ShowAll)) ||
10831076
(header == "DEDICATED HOST" && (args.ShowDedicated || args.ShowAll)) {
10841077
specialColumns[i] = true
10851078
}
10861079
}
1080+
headers, tableData = output.FilterColumnsWithConditions(headers, tableData, specialColumns)
1081+
tableData = append([][]string{headers}, tableData...)
1082+
} else {
1083+
tableData = output.RemoveEmptyColumns(headers, tableData)
10871084
}
1088-
1089-
// Remove empty columns but preserve special columns when their flags are set
1090-
headers, tableData = output.FilterColumnsWithConditions(headers, tableData, specialColumns)
1085+
} else {
1086+
tableData = append([][]string{headers}, tableData...)
10911087
}
10921088

1093-
fullTableData := [][]string{headers}
1094-
fullTableData = append(fullTableData, tableData...)
1095-
output.BuildTable(writer, "\t", fullTableData)
1089+
output.BuildTable(writer, "\t", tableData)
10961090

10971091
if err := writer.Flush(); err != nil {
10981092
return err
10991093
}
11001094
return nil
11011095
}
11021096

1103-
// DescribeMachinePool describes either a machinepool, or, a nodepool (if hypershift)
11041097
func (m *machinePool) DescribeMachinePool(r *rosa.Runtime, cluster *cmv1.Cluster, clusterKey string,
11051098
machinePoolId string) error {
11061099
if cluster.Hypershift().Enabled() {
@@ -1265,50 +1258,6 @@ func appendUpgradesIfExist(scheduledUpgrade *cmv1.NodePoolUpgradePolicy, output
12651258
return output
12661259
}
12671260

1268-
// parseTableString parses a tab-delimited string into headers and table data
1269-
// This implements hunterkepley's suggestion to parse string output
1270-
func parseTableString(tableString string) ([]string, [][]string) {
1271-
lines := strings.Split(strings.TrimSpace(tableString), "\n")
1272-
if len(lines) == 0 {
1273-
return nil, nil
1274-
}
1275-
1276-
// First line is headers
1277-
headers := strings.Split(lines[0], "\t")
1278-
1279-
// Rest are data rows
1280-
var tableData [][]string
1281-
for i := 1; i < len(lines); i++ {
1282-
if lines[i] != "" {
1283-
row := strings.Split(lines[i], "\t")
1284-
tableData = append(tableData, row)
1285-
}
1286-
}
1287-
1288-
return headers, tableData
1289-
}
1290-
1291-
// getMachinePoolsString returns machine pools as a formatted string
1292-
func getMachinePoolsString(
1293-
runtime *rosa.Runtime,
1294-
machinePools []*cmv1.MachinePool,
1295-
args ListMachinePoolArgs,
1296-
) string {
1297-
headers, tableData := getMachinePoolsData(runtime, machinePools, args)
1298-
1299-
// Build string output
1300-
var result strings.Builder
1301-
result.WriteString(strings.Join(headers, "\t"))
1302-
result.WriteString("\n")
1303-
1304-
for _, row := range tableData {
1305-
result.WriteString(strings.Join(row, "\t"))
1306-
result.WriteString("\n")
1307-
}
1308-
1309-
return result.String()
1310-
}
1311-
13121261
func getMachinePoolsData(
13131262
runtime *rosa.Runtime,
13141263
machinePools []*cmv1.MachinePool,
@@ -1369,23 +1318,6 @@ func getMachinePoolsData(
13691318
return headers, tableData
13701319
}
13711320

1372-
// getNodePoolsString returns node pools as a formatted string
1373-
func getNodePoolsString(nodePools []*cmv1.NodePool) string {
1374-
headers, tableData := getNodePoolsData(nodePools)
1375-
1376-
// Build string output
1377-
var result strings.Builder
1378-
result.WriteString(strings.Join(headers, "\t"))
1379-
result.WriteString("\n")
1380-
1381-
for _, row := range tableData {
1382-
result.WriteString(strings.Join(row, "\t"))
1383-
result.WriteString("\n")
1384-
}
1385-
1386-
return result.String()
1387-
}
1388-
13891321
func getNodePoolsData(nodePools []*cmv1.NodePool) ([]string, [][]string) {
13901322
headers := []string{"ID", "AUTOSCALING", "REPLICAS", "INSTANCE TYPE", "LABELS", "TAINTS",
13911323
"AVAILABILITY ZONE", "SUBNET", "DISK SIZE", "VERSION", "AUTOREPAIR"}

pkg/machinepool/machinepool_test.go

Lines changed: 9 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -36,20 +36,16 @@ import (
3636
var policyBuilder cmv1.NodePoolUpgradePolicyBuilder
3737
var date time.Time
3838

39-
// Helper function to convert headers and tableData back to string format for testing
4039
func formatTableString(headers []string, tableData [][]string) string {
4140
return formatTableStringWithSeparators(headers, tableData, "\t")
4241
}
4342

44-
// Helper function that supports custom separators per column
4543
func formatTableStringWithSeparators(headers []string, tableData [][]string, separatorSpec string) string {
4644
var result strings.Builder
4745

48-
// Parse separator specification (same logic as BuildTable)
4946
var separators []string
5047
if strings.Contains(separatorSpec, ",") {
5148
separators = strings.Split(separatorSpec, ",")
52-
// Pad if needed
5349
maxCols := len(headers)
5450
if len(separators) < maxCols {
5551
padSeparator := "\t"
@@ -61,14 +57,12 @@ func formatTableStringWithSeparators(headers []string, tableData [][]string, sep
6157
}
6258
}
6359
} else {
64-
// Single separator - replicate for all columns
6560
separators = make([]string, len(headers))
6661
for i := range separators {
6762
separators[i] = separatorSpec
6863
}
6964
}
7065

71-
// Format headers with custom separators
7266
for i, header := range headers {
7367
result.WriteString(header)
7468
if i < len(headers)-1 && i < len(separators) {
@@ -77,7 +71,6 @@ func formatTableStringWithSeparators(headers []string, tableData [][]string, sep
7771
}
7872
result.WriteString("\n")
7973

80-
// Format data rows with custom separators
8174
for _, row := range tableData {
8275
for i, col := range row {
8376
result.WriteString(col)
@@ -237,7 +230,7 @@ var _ = Describe("Machinepool and nodepool", func() {
237230
Expect(mockPromptFuncInvoked).To(BeFalse())
238231
})
239232
})
240-
It("Test printNodePools", func() {
233+
It("Test NodePools data", func() {
241234
clusterBuilder := cmv1.NewCluster().ID("test").State(cmv1.ClusterStateReady).
242235
Hypershift(cmv1.NewHypershift().Enabled(true)).NodePools(cmv1.NewNodePoolList().
243236
Items(cmv1.NewNodePool().ID("np").Replicas(8).AvailabilityZone("az").
@@ -247,27 +240,14 @@ var _ = Describe("Machinepool and nodepool", func() {
247240
Expect(err).ToNot(HaveOccurred())
248241
headers, tableData := getNodePoolsData(cluster.NodePools().Slice())
249242

250-
// Test with UNIFORM separators (backward compatibility)
251-
out := formatTableString(headers, tableData)
252-
expectedOutput := fmt.Sprintf("ID\tAUTOSCALING\tREPLICAS\tINSTANCE TYPE\tLABELS\tTAINTS\tAVAILABILITY ZONE\tSUBNET\tDISK SIZE\tVERSION\tAUTOREPAIR\n"+
253-
"%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n",
254-
cluster.NodePools().Get(0).ID(),
255-
ocmOutput.PrintNodePoolAutoscaling(cluster.NodePools().Get(0).Autoscaling()),
256-
ocmOutput.PrintNodePoolReplicasShort(
257-
ocmOutput.PrintNodePoolCurrentReplicas(cluster.NodePools().Get(0).Status()),
258-
ocmOutput.PrintNodePoolReplicasInline(cluster.NodePools().Get(0).Autoscaling(),
259-
cluster.NodePools().Get(0).Replicas()),
260-
),
261-
ocmOutput.PrintNodePoolInstanceType(cluster.NodePools().Get(0).AWSNodePool()),
262-
ocmOutput.PrintLabels(cluster.NodePools().Get(0).Labels()),
263-
ocmOutput.PrintTaints(cluster.NodePools().Get(0).Taints()),
264-
cluster.NodePools().Get(0).AvailabilityZone(),
265-
cluster.NodePools().Get(0).Subnet(),
266-
ocmOutput.PrintNodePoolDiskSize(cluster.NodePools().Get(0).AWSNodePool()),
267-
ocmOutput.PrintNodePoolVersion(cluster.NodePools().Get(0).Version()),
268-
ocmOutput.PrintNodePoolAutorepair(cluster.NodePools().Get(0).AutoRepair()))
269-
Expect(out).To(Equal(expectedOutput))
270-
243+
expectedHeaders := []string{"ID", "AUTOSCALING", "REPLICAS", "INSTANCE TYPE", "LABELS", "TAINTS",
244+
"AVAILABILITY ZONE", "SUBNET", "DISK SIZE", "VERSION", "AUTOREPAIR"}
245+
Expect(headers).To(Equal(expectedHeaders))
246+
Expect(len(tableData)).To(Equal(1))
247+
Expect(tableData[0][0]).To(Equal("np"))
248+
Expect(tableData[0][2]).To(Equal("/8"))
249+
Expect(tableData[0][6]).To(Equal("az"))
250+
Expect(tableData[0][7]).To(Equal("sn"))
271251
})
272252
It("Test appendUpgradesIfExist", func() {
273253
policy, err := policyBuilder.Build()

0 commit comments

Comments
 (0)