-
Notifications
You must be signed in to change notification settings - Fork 196
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
OCM-7252 | feat: Move describe machinepool funcs to pkg, test, split …
…up, make interface
- Loading branch information
1 parent
b3740fd
commit 149ab9a
Showing
8 changed files
with
389 additions
and
179 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = "Machine pool '%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, 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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
Oops, something went wrong.