Skip to content

Commit

Permalink
OCM-7253 | feat: Refactor describe machine pool cmd to use new defaul…
Browse files Browse the repository at this point in the history
…t runner
  • Loading branch information
hunterkepley committed Apr 17, 2024
1 parent 2eaeea7 commit 9a0ea4c
Show file tree
Hide file tree
Showing 9 changed files with 322 additions and 140 deletions.
7 changes: 4 additions & 3 deletions cmd/describe/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ func init() {
Cmd.AddCommand(installation.Cmd)
Cmd.AddCommand(upgrade.Cmd)
Cmd.AddCommand(tuningconfigs.Cmd)
Cmd.AddCommand(machinepool.Cmd)
machinePoolCommand := machinepool.NewDescribeMachinePoolCommand()
Cmd.AddCommand(machinePoolCommand)
Cmd.AddCommand(kubeletconfig.Cmd)
Cmd.AddCommand(autoscaler.NewDescribeAutoscalerCommand())
Cmd.AddCommand(externalauthprovider.Cmd)
Expand All @@ -61,10 +62,10 @@ func init() {

globallyAvailableCommands := []*cobra.Command{
tuningconfigs.Cmd, cluster.Cmd, service.Cmd,
machinepool.Cmd, addon.Cmd, upgrade.Cmd,
machinePoolCommand, addon.Cmd, upgrade.Cmd,
admin.Cmd, breakglasscredential.Cmd,
externalauthprovider.Cmd, installation.Cmd,
kubeletconfig.Cmd, machinepool.Cmd, upgrade.Cmd,
kubeletconfig.Cmd, upgrade.Cmd,
}
arguments.MarkRegionDeprecated(Cmd, globallyAvailableCommands)
}
99 changes: 53 additions & 46 deletions cmd/describe/machinepool/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ limitations under the License.
package machinepool

import (
"context"
"fmt"
"os"

cmv1 "github.com/openshift-online/ocm-sdk-go/clustersmgmt/v1"
"github.com/spf13/cobra"
Expand All @@ -29,61 +29,68 @@ import (
"github.com/openshift/rosa/pkg/rosa"
)

var Cmd = &cobra.Command{
Use: "machinepool",
Aliases: []string{"machine-pool"},
Short: "Show details of a machine pool on a cluster",
Long: "Show details of a machine pool on a cluster.",
Example: ` # Show details of a machine pool named "mymachinepool"" on a cluster named "mycluster"
rosa describe machinepool --cluster=mycluster --machinepool=mymachinepool`,
Run: run,
Args: cobra.MaximumNArgs(1),
}
const (
use = "machinepool"
alias = "machine-pool"
short = "Show details of a machine pool on a cluster"
long = "Show details of a machine pool on a cluster."
example = ` # Show details of a machine pool named "mymachinepool"" on a cluster named "mycluster"
rosa describe machinepool --cluster=mycluster --machinepool=mymachinepool`
)

var args struct {
machinePool string
}
func NewDescribeMachinePoolCommand() *cobra.Command {
options := NewDescribeMachinepoolUserOptions()
cmd := &cobra.Command{
Use: use,
Short: short,
Long: long,
Aliases: []string{alias},
Example: example,
Args: cobra.NoArgs,
Run: rosa.DefaultRunner(rosa.RuntimeWithOCM(), DescribeMachinePoolRunner(options)),
}

func init() {
flags := Cmd.Flags()
flags.SortFlags = false
ocm.AddClusterFlag(Cmd)
output.AddFlag(Cmd)
flags := cmd.Flags()
flags.StringVar(
&args.machinePool,
&options.machinepool,
"machinepool",
"",
"Machine pool of the cluster to target",
)
}

func run(cmd *cobra.Command, argv []string) {
r := rosa.NewRuntime().WithAWS().WithOCM()
defer r.Cleanup()
err := runWithRuntime(r, cmd, argv)
if err != nil {
r.Reporter.Errorf(err.Error())
os.Exit(1)
}
output.AddFlag(cmd)
ocm.AddClusterFlag(cmd)
return cmd
}

func runWithRuntime(r *rosa.Runtime, cmd *cobra.Command, argv []string) error {
// Allow the use also directly the machine pool id as positional parameter
if len(argv) == 1 && !cmd.Flag("machinepool").Changed {
args.machinePool = argv[0]
}
if args.machinePool == "" {
return fmt.Errorf("You need to specify a machine pool name")
}
clusterKey := r.GetClusterKey()
cluster := r.FetchCluster()
if cluster.State() != cmv1.ClusterStateReady {
r.Reporter.Errorf("Cluster '%s' is not yet ready", clusterKey)
os.Exit(1)
}
isHypershift := cluster.Hypershift().Enabled()
func DescribeMachinePoolRunner(userOptions DescribeMachinepoolUserOptions) rosa.CommandRunner {
return func(_ context.Context, runtime *rosa.Runtime, cmd *cobra.Command, argv []string) error {
options, err := NewDescribeMachinepoolOptions()
if err != nil {
return fmt.Errorf("unable to create options for Describe Machinepool runner: %s", err)
}
// Allow the use also directly the machine pool id as positional parameter
if len(argv) == 1 && !cmd.Flag("machinepool").Changed {
userOptions.machinepool = argv[0]
} else {
err := cmd.ParseFlags(argv)
if err != nil {
return fmt.Errorf("unable to parse flags: %s", err)
}
}
options.Bind(userOptions)
if options.Machinepool() == "" {
return fmt.Errorf("you need to specify a machine pool name")
}
clusterKey := runtime.GetClusterKey()
cluster := runtime.FetchCluster()
if cluster.State() != cmv1.ClusterStateReady {
return fmt.Errorf("cluster '%s' is not yet ready", clusterKey)
}
isHypershift := cluster.Hypershift().Enabled()

service := machinepool.NewMachinePoolService()
service := machinepool.NewMachinePoolService()

return service.DescribeMachinePool(r, cluster, clusterKey, isHypershift, args.machinePool)
return service.DescribeMachinePool(runtime, cluster, clusterKey, isHypershift, options.Machinepool())
}
}
Loading

0 comments on commit 9a0ea4c

Please sign in to comment.