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

rework orphan deletion #65

Merged
merged 3 commits into from
Jul 11, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ NAME := machine-controller-manager-provider-openstack
IMAGE_NAME := $(IMAGE_PREFIX)/$(NAME)
VERSION := $(shell cat VERSION)
CONTROL_NAMESPACE := default
CONTROL_KUBECONFIG := dev/control-kubeconfig.yaml
TARGET_KUBECONFIG := dev/target-kubeconfig.yaml
CONTROL_KUBECONFIG ?= dev/control-kubeconfig.yaml
TARGET_KUBECONFIG ?= dev/target-kubeconfig.yaml

# Below ones are used in tests
MACHINECLASS_V1 := dev/machineclassv1.yaml
Expand Down
74 changes: 68 additions & 6 deletions test/integration/provider/openstack.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ import (
"context"
"fmt"

"github.com/gardener/machine-controller-manager-provider-openstack/pkg/client"
"github.com/gardener/machine-controller-manager-provider-openstack/pkg/driver"
"github.com/gardener/machine-controller-manager-provider-openstack/pkg/driver/executor"
v1alpha1 "github.com/gardener/machine-controller-manager/pkg/apis/machine/v1alpha1"
"github.com/gardener/machine-controller-manager/pkg/apis/machine/v1alpha1"
"github.com/gophercloud/gophercloud/openstack/blockstorage/v3/volumes"
"github.com/gophercloud/gophercloud/openstack/compute/v2/servers"
"github.com/gophercloud/gophercloud/openstack/networking/v2/ports"

"github.com/gardener/machine-controller-manager-provider-openstack/pkg/client"
"github.com/gardener/machine-controller-manager-provider-openstack/pkg/driver"
"github.com/gardener/machine-controller-manager-provider-openstack/pkg/driver/executor"
)

func getOrphanedInstances(machineClass *v1alpha1.MachineClass, factory *client.Factory) ([]string, error) {
Expand Down Expand Up @@ -76,7 +77,7 @@ func getOrphanedNICs(machineclass *v1alpha1.MachineClass, factory *client.Factor
orphans := []string{}
for _, port := range ports {
if err := network.DeletePort(port.ID); err != nil {
orphans = append(orphans, port.Name)
orphans = append(orphans, port.ID)
himanshu-kun marked this conversation as resolved.
Show resolved Hide resolved
} else {
fmt.Printf("deleted orphan port: %s", port.Name)
}
Expand All @@ -101,10 +102,71 @@ func getOrphanedDisks(machineclass *v1alpha1.MachineClass, factory *client.Facto
continue
}
if err := storage.DeleteVolume(v.ID); err != nil {
orphans = append(orphans, v.Name)
orphans = append(orphans, v.ID)
} else {
fmt.Printf("deleted orphan volume: %s", v.Name)
}
}
return orphans, nil
}

func cleanOrphanResources(orphanVms []string, orphanVolumes []string, orphanNICs []string, machineClass *v1alpha1.MachineClass, secretData map[string][]byte) (delErrOrphanVms []string, delErrOrphanVolumes []string, delErrOrphanNICs []string) {
factory, err := client.NewFactoryFromSecretData(secretData)
if err != nil {
fmt.Printf("failed to create Openstack client: %v", err)
if len(orphanVms) != 0 {
delErrOrphanVms = orphanVms
}
if len(orphanNICs) != 0 {
delErrOrphanNICs = orphanNICs
}
if len(orphanVolumes) != 0 {
delErrOrphanVolumes = orphanVolumes
}
return
}

if len(orphanVms) != 0 {
compute, err := factory.Compute()
if err == nil {
for _, instanceID := range orphanVms {
if err := compute.DeleteServer(instanceID); err != nil {
fmt.Printf("failed to delete instance %v: %v", instanceID, err)
delErrOrphanVms = append(delErrOrphanVms, instanceID)
}
}
} else {
delErrOrphanVms = orphanVms
}
}

if len(orphanNICs) != 0 {
network, err := factory.Network()
if err == nil {
for _, portID := range orphanNICs {
if err := network.DeletePort(portID); err != nil {
fmt.Printf("failed to delete port %v: %v", portID, err)
delErrOrphanNICs = append(delErrOrphanNICs, portID)
}
}
} else {
delErrOrphanNICs = orphanNICs
}
}

if len(orphanVolumes) != 0 {
storage, err := factory.Storage()
if err == nil {
for _, volumeID := range orphanVolumes {
if err := storage.DeleteVolume(volumeID); err != nil {
fmt.Printf("failed to delete volume %v: %v", volumeID, err)
delErrOrphanNICs = append(delErrOrphanVolumes, volumeID)
}
}
} else {
delErrOrphanVolumes = orphanVolumes
}
}

return
}
16 changes: 12 additions & 4 deletions test/integration/provider/rti.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ package provider
import (
"fmt"

"github.com/gardener/machine-controller-manager/pkg/apis/machine/v1alpha1"

"github.com/gardener/machine-controller-manager-provider-openstack/pkg/client"
v1alpha1 "github.com/gardener/machine-controller-manager/pkg/apis/machine/v1alpha1"
)

// ITResourceTagKey is specifically used for integration test
Expand All @@ -24,18 +25,24 @@ type ResourcesTrackerImpl struct {

// InitializeResourcesTracker initializes the type ResourcesTrackerImpl variable and tries
// to delete the orphan resources present before the actual IT runs.
// create a cleanup function to delete the list of orphan resources.
// 1. get list of orphan resources.
// 2. Mark them for deletion and call cleanup.
// 3. Print the orphan resources which got error in deletion.
func (r *ResourcesTrackerImpl) InitializeResourcesTracker(machineClass *v1alpha1.MachineClass, secretData map[string][]byte, _ string) error {

r.MachineClass = machineClass
r.SecretData = secretData

initialVMs, initialNICs, initialDisks, initialMachines, err := r.probeResources()
initialVMs, initialNICs, initialVolumes, initialMachines, err := r.probeResources()
if err != nil {
fmt.Printf("Error in initial probe of orphaned resources: %s", err.Error())
return err
}

if len(initialVMs) != 0 || len(initialMachines) != 0 || len(initialNICs) != 0 || len(initialDisks) != 0 {
err := fmt.Errorf("orphan resources are available. Clean them up before proceeding with the test.\nvirtual machines: %v\nmcm machines: %v\nnics: %v", initialVMs, initialMachines, initialNICs)
delErrOrphanVMs, delErrOrphanVolumes, delErrOrphanNICs := cleanOrphanResources(initialVMs, initialVolumes, initialNICs, r.MachineClass, r.SecretData)
if len(delErrOrphanVMs) != 0 || len(delErrOrphanVolumes) != 0 || len(initialMachines) != 0 || len(delErrOrphanNICs) != 0 {
err = fmt.Errorf("error in cleaning the following orphan resources. Clean them up before proceeding with the test.\nvirtual machines: %v\ndisks: %v\nmcm machines: %v\nnics: %v", delErrOrphanVMs, delErrOrphanVolumes, initialMachines, delErrOrphanNICs)
return err
}
return nil
Expand Down Expand Up @@ -81,6 +88,7 @@ func (r *ResourcesTrackerImpl) IsOrphanedResourcesAvailable() bool {
}

if len(afterTestExecutionVMs) != 0 || len(afterTestExecutionAvailmachines) != 0 || len(afterTestExecutionNICs) != 0 || len(afterTestExecutionDisks) != 0 {
fmt.Printf("The following resources are orphans ... trying to delete them \n")
kon-angelo marked this conversation as resolved.
Show resolved Hide resolved
fmt.Printf("Virtual Machines: %v\nNICs: %v\nMCM Machines: %v\n", afterTestExecutionVMs, afterTestExecutionNICs, afterTestExecutionAvailmachines)
return true
}
Expand Down