Skip to content

Commit

Permalink
OCM-7258 | feat: Move delete machinepool/nodepool funcs to pkg
Browse files Browse the repository at this point in the history
  • Loading branch information
hunterkepley committed May 14, 2024
1 parent d1e79d3 commit d0f6390
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 93 deletions.
14 changes: 9 additions & 5 deletions cmd/dlt/machinepool/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/spf13/cobra"

"github.com/openshift/rosa/pkg/interactive/confirm"
"github.com/openshift/rosa/pkg/machinepool"
"github.com/openshift/rosa/pkg/ocm"
"github.com/openshift/rosa/pkg/rosa"
)
Expand Down Expand Up @@ -53,13 +54,16 @@ func run(_ *cobra.Command, argv []string) {
r := rosa.NewRuntime().WithAWS().WithOCM()
defer r.Cleanup()

machinePoolID := argv[0]
machinePoolId := argv[0]
if !machinepool.MachinePoolKeyRE.MatchString(machinePoolId) {
r.Reporter.Errorf("Expected a valid identifier for the machine pool")
}
clusterKey := r.GetClusterKey()
cluster := r.FetchCluster()

if cluster.Hypershift().Enabled() {
deleteNodePool(r, machinePoolID, clusterKey, cluster)
} else {
deleteMachinePool(r, machinePoolID, clusterKey, cluster)
service := machinepool.NewMachinePoolService()
err := service.DeleteMachinePool(r, machinePoolId, clusterKey, cluster)
if err != nil {
r.Reporter.Errorf("Error deleting machinepool: %v", err)
}
}
52 changes: 0 additions & 52 deletions cmd/dlt/machinepool/machinepool.go

This file was deleted.

36 changes: 0 additions & 36 deletions cmd/dlt/machinepool/nodepool.go

This file was deleted.

70 changes: 70 additions & 0 deletions pkg/machinepool/machinepool.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import (
"encoding/json"
"fmt"
"os"
"regexp"
"text/tabwriter"

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

"github.com/openshift/rosa/pkg/interactive/confirm"
ocmOutput "github.com/openshift/rosa/pkg/ocm/output"
"github.com/openshift/rosa/pkg/output"
"github.com/openshift/rosa/pkg/rosa"
Expand All @@ -21,6 +23,7 @@ var notFoundMessage string = "Machine pool '%s' not found"
type MachinePoolService interface {
DescribeMachinePool(r *rosa.Runtime, cluster *cmv1.Cluster, clusterKey string, machinePoolId string) error
ListMachinePools(r *rosa.Runtime, clusterKey string, cluster *cmv1.Cluster) error
DeleteMachinePool(r *rosa.Runtime, machinePoolId string, clusterKey string, cluster *cmv1.Cluster) error
}

type machinePool struct {
Expand Down Expand Up @@ -127,6 +130,73 @@ func (m *machinePool) describeNodePool(r *rosa.Runtime, cluster *cmv1.Cluster, c
return nil
}

// Regular expression to used to make sure that the identifier given by the
// user is safe and that it there is no risk of SQL injection:
var MachinePoolKeyRE = regexp.MustCompile(`^[a-z]([-a-z0-9]*[a-z0-9])?$`)

// DeleteMachinePool deletes a machinepool from a cluster if it is possible- this function also calls the hypershift
// equivalent, deleteNodePool if it is a hypershift cluster
func (m *machinePool) DeleteMachinePool(r *rosa.Runtime, machinePoolId string, clusterKey string,
cluster *cmv1.Cluster) error {
if cluster.Hypershift().Enabled() {
return deleteNodePool(r, machinePoolId, clusterKey, cluster)
}

// Try to find the machine pool:
r.Reporter.Debugf("Loading machine pools for cluster '%s'", clusterKey)
machinePools, err := r.OCMClient.GetMachinePools(cluster.ID())
if err != nil {
return fmt.Errorf("Failed to get machine pools for cluster '%s': %v", clusterKey, err)
}

var machinePool *cmv1.MachinePool
for _, item := range machinePools {
if item.ID() == machinePoolId {
machinePool = item
}
}
if machinePool == nil {
return fmt.Errorf("Failed to get machine pool '%s' for cluster '%s'", machinePoolId, clusterKey)
}

if confirm.Confirm("delete machine pool '%s' on cluster '%s'", machinePoolId, clusterKey) {
r.Reporter.Debugf("Deleting machine pool '%s' on cluster '%s'", machinePool.ID(), clusterKey)
err = r.OCMClient.DeleteMachinePool(cluster.ID(), machinePool.ID())
if err != nil {
return fmt.Errorf("Failed to delete machine pool '%s' on cluster '%s': %s",
machinePool.ID(), clusterKey, err)
}
r.Reporter.Infof("Successfully deleted machine pool '%s' from cluster '%s'", machinePoolId, clusterKey)
}
return nil
}

// deleteNodePool is the hypershift version of DeleteMachinePool - deleteNodePool is called in DeleteMachinePool
// if the cluster is hypershift
func deleteNodePool(r *rosa.Runtime, nodePoolID string, clusterKey string, cluster *cmv1.Cluster) error {
// Try to find the machine pool:
r.Reporter.Debugf("Loading machine pools for hosted cluster '%s'", clusterKey)
nodePool, exists, err := r.OCMClient.GetNodePool(cluster.ID(), nodePoolID)
if err != nil {
return fmt.Errorf("Failed to get machine pools for hosted cluster '%s': %v", clusterKey, err)
}
if !exists {
return fmt.Errorf("Machine pool '%s' does not exist for hosted cluster '%s'", nodePoolID, clusterKey)
}

if confirm.Confirm("delete machine pool '%s' on hosted cluster '%s'", nodePoolID, clusterKey) {
r.Reporter.Debugf("Deleting machine pool '%s' on hosted cluster '%s'", nodePool.ID(), clusterKey)
err = r.OCMClient.DeleteNodePool(cluster.ID(), nodePool.ID())
if err != nil {
return fmt.Errorf("Failed to delete machine pool '%s' on hosted cluster '%s': %s",
nodePool.ID(), clusterKey, err)
}
r.Reporter.Infof("Successfully deleted machine pool '%s' from hosted cluster '%s'", nodePoolID,
clusterKey)
}
return nil
}

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

Expand Down
8 changes: 8 additions & 0 deletions pkg/machinepool/machinepool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,5 +120,13 @@ var _ = Describe("Machinepool and nodepool", func() {
ocmOutput.PrintMachinePoolDiskSize(cluster.MachinePools().Get(0)),
output.PrintStringSlice(cluster.MachinePools().Get(0).AWS().AdditionalSecurityGroupIds()))))
})
It("Validate invalid regex", func() {
Expect(MachinePoolKeyRE.MatchString("$%%$%$%^$%^$%^$%^")).To(BeFalse())
Expect(MachinePoolKeyRE.MatchString("machinepool1")).To(BeTrue())
Expect(MachinePoolKeyRE.MatchString("1machinepool")).To(BeFalse())
Expect(MachinePoolKeyRE.MatchString("#1machinepool")).To(BeFalse())
Expect(MachinePoolKeyRE.MatchString("m123123123123123123123123123")).To(BeTrue())
Expect(MachinePoolKeyRE.MatchString("m#123")).To(BeFalse())
})
})
})

0 comments on commit d0f6390

Please sign in to comment.