Skip to content

Commit

Permalink
feat: deletes the machine pool created during oc registration (#1844)
Browse files Browse the repository at this point in the history
  • Loading branch information
dimakis authored Mar 29, 2023
1 parent e907b7b commit 2ba52fa
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
14 changes: 14 additions & 0 deletions pkg/cmd/kafka/openshift-cluster/deregister/deregister.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type options struct {
clusterManagementApiUrl string
accessToken string
selectedCluster *clustersmgmtv1.Cluster
machinePoolId string

f *factory.Factory
}
Expand All @@ -34,6 +35,7 @@ const (
strimziAddonId = "managed-kafka"
fleetshardAddonIdQE = "kas-fleetshard-operator-qe"
strimziAddonIdQE = "managed-kafka-qe"
machinePoolTaintKey = "bf2.org/kafkaInstanceProfileType"
)

func NewDeRegisterClusterCommand(f *factory.Factory) *cobra.Command {
Expand Down Expand Up @@ -113,6 +115,18 @@ func runDeRegisterClusterCmd(opts *options) error {
return err
}

// Get the kafka machine pool id
opts.machinePoolId, err = ocmUtils.GetMachinePoolIdByTaintKey(opts.f, opts.clusterManagementApiUrl, opts.accessToken, opts.selectedCluster.ID(), machinePoolTaintKey)
if err != nil {
return err
}

// remove the kafka machine pool from the cluster
err = ocmUtils.DeleteMachinePool(opts.f, opts.clusterManagementApiUrl, opts.accessToken, opts.selectedCluster.ID(), opts.machinePoolId)
if err != nil {
return err
}

return nil
}

Expand Down
34 changes: 34 additions & 0 deletions pkg/shared/connection/api/clustermgmt/ocm_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,40 @@ func CreateMachinePool(f *factory.Factory, clustermgmturl string, accessToken st
return response.Body(), nil
}

func GetMachinePoolIdByTaintKey(f *factory.Factory, clustermgmturl string, accessToken string, clusterId string, machinePoolTaintKey string) (string, error) {
client, closeConnection, err := clustermgmtConnection(f, accessToken, clustermgmturl)
if err != nil {
return "", err
}
defer closeConnection()
response, err := client.Clusters().Cluster(clusterId).MachinePools().List().Send()
if err != nil {
return "", err
}
machinePools := response.Items()
for _, machinePool := range machinePools.Slice() {
for _, taint := range machinePool.Taints() {
if taint.Key() == machinePoolTaintKey {
return machinePool.ID(), nil
}
}
}
return "", nil
}

func DeleteMachinePool(f *factory.Factory, clustermgmturl string, accessToken string, clusterId string, machinePoolId string) error {
client, closeConnection, err := clustermgmtConnection(f, accessToken, clustermgmturl)
if err != nil {
return err
}
defer closeConnection()
_, err = client.Clusters().Cluster(clusterId).MachinePools().MachinePool(machinePoolId).Delete().Send()
if err != nil {
return err
}
return nil
}

func GetMachinePoolNodeCount(machinePool *v1.MachinePool) int {
var nodeCount int
replicas, ok := machinePool.GetReplicas()
Expand Down

0 comments on commit 2ba52fa

Please sign in to comment.