Skip to content

Commit

Permalink
Add osdctl mc list command to return info about management clusters
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Shen <[email protected]>
  • Loading branch information
mjlshen committed Nov 21, 2023
1 parent 1b906de commit afaefb1
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 0 deletions.
2 changes: 2 additions & 0 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/openshift/osdctl/cmd/hive"
"github.com/openshift/osdctl/cmd/jira"
"github.com/openshift/osdctl/cmd/jumphost"
"github.com/openshift/osdctl/cmd/mc"
"github.com/openshift/osdctl/cmd/network"
"github.com/openshift/osdctl/cmd/org"
"github.com/openshift/osdctl/cmd/promote"
Expand Down Expand Up @@ -88,6 +89,7 @@ func NewCmdRoot(streams genericclioptions.IOStreams) *cobra.Command {
rootCmd.AddCommand(newCmdCompletion())
rootCmd.AddCommand(env.NewCmdEnv())
rootCmd.AddCommand(jumphost.NewCmdJumphost())
rootCmd.AddCommand(mc.NewCmdMC())
rootCmd.AddCommand(network.NewCmdNetwork(streams, kubeFlags, kubeClient))
rootCmd.AddCommand(servicelog.NewCmdServiceLog())
rootCmd.AddCommand(org.NewCmdOrg())
Expand Down
14 changes: 14 additions & 0 deletions cmd/mc/cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package mc

import "github.com/spf13/cobra"

func NewCmdMC() *cobra.Command {
mc := &cobra.Command{
Use: "mc",
Args: cobra.NoArgs,
}

mc.AddCommand(newCmdList())

return mc
}
75 changes: 75 additions & 0 deletions cmd/mc/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package mc

import (
"fmt"
"log"
"os"
"text/tabwriter"

"github.com/aws/aws-sdk-go-v2/aws/arn"
"github.com/openshift/osdctl/pkg/utils"
"github.com/spf13/cobra"
)

type list struct {
}

func newCmdList() *cobra.Command {
l := &list{}

listCmd := &cobra.Command{
Use: "list",
Short: "List ROSA HCP Management Clusters",
Long: "List ROSA HCP Management Clusters",
Example: "osdctl mc list",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
return l.Run()
},
}

return listCmd
}

func (l *list) Run() error {
ocm, err := utils.CreateConnection()
if err != nil {
return err
}
defer ocm.Close()

managementClusters, err := ocm.OSDFleetMgmt().V1().ManagementClusters().List().Send()
if err != nil {
return fmt.Errorf("failed to list management clusters: %v", err)
}

w := tabwriter.NewWriter(os.Stdout, 1, 1, 1, ' ', 0)
fmt.Fprintln(w, "NAME\tID\tSECTOR\tREGION\tACCOUNT_ID\tSTATUS")
for _, mc := range managementClusters.Items().Slice() {
cluster, err := ocm.ClustersMgmt().V1().Clusters().Cluster(mc.ClusterManagementReference().ClusterId()).Get().Send()
if err != nil {
log.Printf("failed to find clusters_mgmt cluster for %s: %v", mc.Name(), err)
continue
}

awsAccountID := "NON-STS"
supportRole := cluster.Body().AWS().STS().SupportRoleARN()
if supportRole != "" {
supportRoleARN, err := arn.Parse(supportRole)
if err != nil {
log.Printf("failed to convert %s to an ARN: %v", supportRole, err)
}
awsAccountID = supportRoleARN.AccountID
}

fmt.Fprintln(w, mc.Name()+"\t"+
mc.ClusterManagementReference().ClusterId()+"\t"+
mc.Sector()+"\t"+
mc.Region()+"\t"+
awsAccountID+"\t"+
mc.Status())
}
w.Flush()

return nil
}

0 comments on commit afaefb1

Please sign in to comment.