Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OCM-7252 | feat: Move describe machinepool funcs to pkg, test, split up, make interface #1922

Merged
merged 1 commit into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 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,7 @@ func runWithRuntime(r *rosa.Runtime, cmd *cobra.Command, argv []string) error {
}
isHypershift := cluster.Hypershift().Enabled()

if isHypershift {
return describeNodePool(r, cluster, clusterKey, machinePool)
} else {
return describeMachinePool(r, cluster, clusterKey, machinePool)
}
service := machinepool.NewMachinePoolService()

return service.DescribeMachinePool(r, cluster, clusterKey, isHypershift, args.machinePool)
}
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.

126 changes: 126 additions & 0 deletions pkg/machinepool/machinepool.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
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 = "Machine pool '%s' not found"

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

type machinePool struct {
}

var _ MachinePoolService = &machinePool{}

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

// DescribeMachinePool describes either a machinepool, or, a nodepool (if hypershift)
func (m machinePool) DescribeMachinePool(r *rosa.Runtime, cluster *cmv1.Cluster, clusterKey string, isHypershift bool,
machinePoolId string) error {
if isHypershift {
return m.describeNodePool(r, cluster, clusterKey, machinePoolId)
}

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, 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, 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")
}
Loading