Skip to content

Commit

Permalink
OCM-7252 | feat: Move describe machinepool funcs to pkg, test, split …
Browse files Browse the repository at this point in the history
…up, make interface
  • Loading branch information
hunterkepley committed Apr 11, 2024
1 parent b3740fd commit 096e58d
Show file tree
Hide file tree
Showing 9 changed files with 391 additions and 181 deletions.
12 changes: 7 additions & 5 deletions cmd/describe/machinepool/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,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"
Expand Down Expand Up @@ -67,12 +68,11 @@ func run(cmd *cobra.Command, argv []string) {
}

func runWithRuntime(r *rosa.Runtime, cmd *cobra.Command, argv []string) error {
machinePool := args.machinePool
// Allow the use also directly the machine pool id as positional parameter
if len(argv) == 1 && !cmd.Flag("machinepool").Changed {
machinePool = argv[0]
args.machinePool = argv[0]
}
if machinePool == "" {
if args.machinePool == "" {
return fmt.Errorf("You need to specify a machine pool name")
}
clusterKey := r.GetClusterKey()
Expand All @@ -83,9 +83,11 @@ func runWithRuntime(r *rosa.Runtime, cmd *cobra.Command, argv []string) error {
}
isHypershift := cluster.Hypershift().Enabled()

service := machinepool.NewMachinePoolService()

if isHypershift {
return describeNodePool(r, cluster, clusterKey, machinePool)
return service.DescribeNodePool(r, cluster, clusterKey, args.machinePool)
} else {
return describeMachinePool(r, cluster, clusterKey, machinePool)
return service.DescribeMachinePool(r, cluster, clusterKey, args.machinePool)
}
}
4 changes: 2 additions & 2 deletions cmd/describe/machinepool/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ var _ = Describe("Upgrade machine pool", func() {
stdout, stderr, err := test.RunWithOutputCaptureAndArgv(runWithRuntime, testRuntime.RosaRuntime,
Cmd, &[]string{nodePoolName})
Expect(err).ToNot(BeNil())
Expect(err.Error()).To(ContainSubstring(fmt.Sprintf("Machine pool '%s' not found", nodePoolName)))
Expect(err.Error()).To(ContainSubstring(fmt.Sprintf("Node pool '%s' not found", nodePoolName)))
Expect(stdout).To(BeEmpty())
Expect(stderr).To(BeEmpty())
})
Expand All @@ -187,7 +187,7 @@ var _ = Describe("Upgrade machine pool", func() {
stdout, stderr, err := test.RunWithOutputCaptureAndArgv(runWithRuntime, testRuntime.RosaRuntime,
Cmd, &[]string{})
Expect(err).ToNot(BeNil())
Expect(err.Error()).To(ContainSubstring(fmt.Sprintf("Machine pool '%s' not found", nodePoolName)))
Expect(err.Error()).To(ContainSubstring(fmt.Sprintf("Node pool '%s' not found", nodePoolName)))
Expect(stdout).To(BeEmpty())
Expect(stderr).To(BeEmpty())
})
Expand Down
57 changes: 0 additions & 57 deletions cmd/describe/machinepool/machinepool.go

This file was deleted.

117 changes: 0 additions & 117 deletions cmd/describe/machinepool/nodepool.go

This file was deleted.

121 changes: 121 additions & 0 deletions pkg/machinepool/machinepool.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package machinepool

import (
"bytes"
"encoding/json"
"fmt"

cmv1 "github.com/openshift-online/ocm-sdk-go/clustersmgmt/v1"

"github.com/openshift/rosa/pkg/output"
"github.com/openshift/rosa/pkg/rosa"
)

var fetchMessage string = "Fetching %s '%s' for cluster '%s'"
var notFoundMessage string = "%s '%s' not found"

//go:generate mockgen -source=machinepool.go -package=mocks -destination=machinepool_mock.go
type MachinePoolService interface {
DescribeMachinePool(*rosa.Runtime, *cmv1.Cluster, string, string) error
DescribeNodePool(*rosa.Runtime, *cmv1.Cluster, string, string) error
}

type machinePool struct {
}

var _ MachinePoolService = &machinePool{}

func NewMachinePoolService() MachinePoolService {
return &machinePool{}
}

func (m machinePool) DescribeMachinePool(r *rosa.Runtime, cluster *cmv1.Cluster, clusterKey string,
machinePoolId string) error {
r.Reporter.Debugf(fetchMessage, "machine pool", machinePoolId, clusterKey)
machinePool, exists, err := r.OCMClient.GetMachinePool(cluster.ID(), machinePoolId)
if err != nil {
return err
}
if !exists {
return fmt.Errorf(notFoundMessage, "Machine pool", machinePoolId)
}

if output.HasFlag() {
return output.Print(machinePool)
}

fmt.Print(machinePoolOutput(cluster.ID(), machinePool))

return nil
}

func (m machinePool) DescribeNodePool(r *rosa.Runtime, cluster *cmv1.Cluster, clusterKey string,
nodePoolId string) error {
r.Reporter.Debugf(fetchMessage, "node pool", nodePoolId, clusterKey)
nodePool, exists, err := r.OCMClient.GetNodePool(cluster.ID(), nodePoolId)
if err != nil {
return err
}
if !exists {
return fmt.Errorf(notFoundMessage, "Node pool", nodePoolId)
}

_, scheduledUpgrade, err := r.OCMClient.GetHypershiftNodePoolUpgrade(cluster.ID(), clusterKey, nodePoolId)
if err != nil {
return err
}

if output.HasFlag() {
var formattedOutput map[string]interface{}
formattedOutput, err = formatNodePoolOutput(nodePool, scheduledUpgrade)
if err != nil {
return err
}
return output.Print(formattedOutput)
}

// Attach and print scheduledUpgrades if they exist, otherwise, print output normally
fmt.Print(appendUpgradesIfExist(scheduledUpgrade, nodePoolOutput(cluster.ID(), nodePool)))

return nil
}

func formatNodePoolOutput(nodePool *cmv1.NodePool,
scheduledUpgrade *cmv1.NodePoolUpgradePolicy) (map[string]interface{}, error) {

var b bytes.Buffer
err := cmv1.MarshalNodePool(nodePool, &b)
if err != nil {
return nil, err
}
ret := make(map[string]interface{})
err = json.Unmarshal(b.Bytes(), &ret)
if err != nil {
return nil, err
}
if scheduledUpgrade != nil &&
scheduledUpgrade.State() != nil &&
len(scheduledUpgrade.Version()) > 0 &&
len(scheduledUpgrade.State().Value()) > 0 {
upgrade := make(map[string]interface{})
upgrade["version"] = scheduledUpgrade.Version()
upgrade["state"] = scheduledUpgrade.State().Value()
upgrade["nextRun"] = scheduledUpgrade.NextRun().Format("2006-01-02 15:04 MST")
ret["scheduledUpgrade"] = upgrade
}

return ret, nil
}

func appendUpgradesIfExist(scheduledUpgrade *cmv1.NodePoolUpgradePolicy, output string) string {
if scheduledUpgrade != nil {
return fmt.Sprintf("%s"+
"Scheduled upgrade: %s %s on %s\n",
output,
scheduledUpgrade.State().Value(),
scheduledUpgrade.Version(),
scheduledUpgrade.NextRun().Format("2006-01-02 15:04 MST"),
)
}
return output
}
13 changes: 13 additions & 0 deletions pkg/machinepool/machinepool_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package machinepool

import (
"testing"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

func TestDescribeUpgrade(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Machinepool/nodepool suite")
}

0 comments on commit 096e58d

Please sign in to comment.