Skip to content

Commit

Permalink
OCM-5131 | feat: add SG IDs to describe cluster and list machinepools
Browse files Browse the repository at this point in the history
  • Loading branch information
gdbranco committed Dec 1, 2023
1 parent 81ed2ea commit f1d0044
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 23 deletions.
9 changes: 7 additions & 2 deletions cmd/ocm/list/machinepool/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,17 +98,18 @@ func run(cmd *cobra.Command, argv []string) error {
// 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\n")
fmt.Fprintf(writer, "ID\tAUTOSCALING\tREPLICAS\tINSTANCE TYPE\tLABELS\t\tTAINTS\t\tAVAILABILITY ZONES\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\n",
fmt.Fprintf(writer, "%s\t%s\t%s\t%s\t%s\t\t%s\t\t%s\t%s\n",
machinePool.ID(),
printAutoscaling(machinePool.Autoscaling()),
printReplicas(machinePool.Autoscaling(), machinePool.Replicas()),
machinePool.InstanceType(),
printLabels(machinePool.Labels()),
printTaints(machinePool.Taints()),
printAZ(machinePool.AvailabilityZones()),
printAdditionalSecurityGroups(machinePool.AWS().AdditionalSecurityGroupIds()),
)
}
writer.Flush()
Expand All @@ -132,6 +133,10 @@ func printReplicas(autoscaling *cmv1.MachinePoolAutoscaling, replicas int) strin
return fmt.Sprintf("%d", replicas)
}

func printAdditionalSecurityGroups(securityGroups []string) string {
return strings.Join(securityGroups, ", ")
}

func printAZ(az []string) string {
if len(az) == 0 {
return ""
Expand Down
42 changes: 26 additions & 16 deletions pkg/cluster/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ package cluster

import (
"fmt"
"strconv"
"strings"
"time"

sdk "github.com/openshift-online/ocm-sdk-go"
Expand Down Expand Up @@ -109,16 +111,6 @@ func PrintClusterDescription(connection *sdk.Connection, cluster *cmv1.Cluster)
shard = shardPath.Body().HiveConfig().Server()
}

var computesStr string
if cluster.Nodes().AutoscaleCompute() != nil {
computesStr = fmt.Sprintf("%d-%d (Autoscaled)",
cluster.Nodes().AutoscaleCompute().MinReplicas(),
cluster.Nodes().AutoscaleCompute().MaxReplicas(),
)
} else {
computesStr = fmt.Sprintf("%d", cluster.Nodes().Compute())
}

clusterAdminEnabled := false
if cluster.CCS().Enabled() {
clusterAdminEnabled = true
Expand Down Expand Up @@ -189,12 +181,22 @@ func PrintClusterDescription(connection *sdk.Connection, cluster *cmv1.Cluster)
)
}

var computesStr string
if cluster.Nodes().AutoscaleCompute() != nil {
computesStr = fmt.Sprintf("%d-%d (Autoscaled)",
cluster.Nodes().AutoscaleCompute().MinReplicas(),
cluster.Nodes().AutoscaleCompute().MaxReplicas(),
)
} else {
computesStr = strconv.Itoa(cluster.Nodes().Compute())
}

fmt.Printf("API URL: %s\n"+
"API Listening: %s\n"+
"Console URL: %s\n"+
"Masters: %d\n"+
"Infra: %d\n"+
"Computes: %s\n"+
"Control Plane:\n%s\n"+
"Infra:\n%s\n"+
"Compute:\n%s\n"+
"Product: %s\n"+
"Subscription type: %s\n"+
"Provider: %s\n"+
Expand All @@ -217,9 +219,9 @@ func PrintClusterDescription(connection *sdk.Connection, cluster *cmv1.Cluster)
apiURL,
apiListening,
cluster.Console().URL(),
cluster.Nodes().Master(),
cluster.Nodes().Infra(),
computesStr,
printNodeInfo(strconv.Itoa(cluster.Nodes().Master()), cluster.AWS().AdditionalControlPlaneSecurityGroupIds()),
printNodeInfo(strconv.Itoa(cluster.Nodes().Infra()), cluster.AWS().AdditionalInfraSecurityGroupIds()),
printNodeInfo(computesStr, cluster.AWS().AdditionalComputeSecurityGroupIds()),
cluster.Product().ID(),
cluster.BillingModel(),
cluster.CloudProvider().ID(),
Expand Down Expand Up @@ -294,6 +296,14 @@ func PrintClusterDescription(connection *sdk.Connection, cluster *cmv1.Cluster)
return nil
}

func printNodeInfo(replicasInfo string, securityGroups []string) string {
nodeStr := fmt.Sprintf("\tReplicas: %s", replicasInfo)
if len(securityGroups) > 0 {
nodeStr += fmt.Sprintf("\n\tAWS Additional Security Group IDs: %s", strings.Join(securityGroups, ", "))
}
return nodeStr
}

// findHyperShiftMgmtSvcClusters returns the name of a HyperShift cluster's management and service clusters.
// It essentially ignores error as these endpoint is behind specific permissions by returning empty strings when any
// errors are encountered, which results in them not being printed in the output.
Expand Down
10 changes: 5 additions & 5 deletions tests/list_machinepools_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,13 +136,13 @@ var _ = Describe("List machine pools", Ordered, func() {
// The heading and 2 machinepool record information
Expect(lines).To(HaveLen(3))
Expect(lines[0]).To(MatchRegexp(
`^ID\s+AUTOSCALING\s+REPLICAS\s+INSTANCE TYPE\s+LABELS\s+TAINTS\s+AVAILABILITY ZONES$`,
`^ID\s+AUTOSCALING\s+REPLICAS\s+INSTANCE TYPE\s+LABELS\s+TAINTS\s+AVAILABILITY ZONES\s+SG IDs$`,
))
Expect(lines[1]).To(MatchRegexp(
`^worker\s+No\s+4\s+m5.xlarge\s+us-west-2a$`,
`^worker\s+No\s+4\s+m5.xlarge\s+us-west-2a\s+$`,
))
Expect(lines[2]).To(MatchRegexp(
`^worker1\s+No\s+2\s+m5.2xlarge\s+us-west-2a$`,
`^worker1\s+No\s+2\s+m5.2xlarge\s+us-west-2a\s+$`,
))
})

Expand Down Expand Up @@ -180,10 +180,10 @@ var _ = Describe("List machine pools", Ordered, func() {
// The heading and 1 machinepool record information
Expect(lines).To(HaveLen(2))
Expect(lines[0]).To(MatchRegexp(
`^ID\s+AUTOSCALING\s+REPLICAS\s+INSTANCE TYPE\s+LABELS\s+TAINTS\s+AVAILABILITY ZONES$`,
`^ID\s+AUTOSCALING\s+REPLICAS\s+INSTANCE TYPE\s+LABELS\s+TAINTS\s+AVAILABILITY ZONES\s+SG IDs$`,
))
Expect(lines[1]).To(MatchRegexp(
`^default\s+No\s+2\s+m5.xlarge\s+us-west-2a$`,
`^default\s+No\s+2\s+m5.xlarge\s+us-west-2a\s+$`,
))
})

Expand Down

0 comments on commit f1d0044

Please sign in to comment.