diff --git a/Makefile b/Makefile index 020c6f8d..6905c658 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/test/integration/provider/openstack.go b/test/integration/provider/openstack.go index 5e664225..b977cecd 100644 --- a/test/integration/provider/openstack.go +++ b/test/integration/provider/openstack.go @@ -8,30 +8,27 @@ 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) { +func getOrphanedInstances(factory *client.Factory) ([]string, error) { compute, err := factory.Compute() if err != nil { return nil, err } instances, err := compute.ListServers(&servers.ListOpts{}) - orphans := []string{} + var orphans []string for _, instance := range instances { if _, ok := instance.Metadata[ITResourceTagKey]; ok { - if err := compute.DeleteServer(instance.ID); err != nil { - orphans = append(orphans, instance.ID) - } else { - fmt.Printf("deleted orphan VM: %s", instance.Name) - } + orphans = append(orphans, instance.ID) } } return orphans, nil @@ -61,7 +58,7 @@ func getMachines(machineClass *v1alpha1.MachineClass, factory *client.Factory) ( return machines, nil } -func getOrphanedNICs(machineclass *v1alpha1.MachineClass, factory *client.Factory) ([]string, error) { +func getOrphanedNICs(factory *client.Factory) ([]string, error) { network, err := factory.Network() if err != nil { return nil, err @@ -73,18 +70,14 @@ func getOrphanedNICs(machineclass *v1alpha1.MachineClass, factory *client.Factor if err != nil { return nil, err } - orphans := []string{} + var orphans []string for _, port := range ports { - if err := network.DeletePort(port.ID); err != nil { - orphans = append(orphans, port.Name) - } else { - fmt.Printf("deleted orphan port: %s", port.Name) - } + orphans = append(orphans, port.ID) } return orphans, nil } -func getOrphanedDisks(machineclass *v1alpha1.MachineClass, factory *client.Factory) ([]string, error) { +func getOrphanedDisks(factory *client.Factory) ([]string, error) { storage, err := factory.Storage() if err != nil { return nil, err @@ -95,16 +88,73 @@ func getOrphanedDisks(machineclass *v1alpha1.MachineClass, factory *client.Facto return nil, err } - orphans := []string{} + var orphans []string for _, v := range vols { if _, ok := v.Metadata[ITResourceTagKey]; !ok { continue } - if err := storage.DeleteVolume(v.ID); err != nil { - orphans = append(orphans, v.Name) + orphans = append(orphans, v.ID) + } + 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 { - fmt.Printf("deleted orphan volume: %s", v.Name) + delErrOrphanVms = orphanVms } } - return orphans, nil + + 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 } diff --git a/test/integration/provider/rti.go b/test/integration/provider/rti.go index 696653f5..6e4150de 100644 --- a/test/integration/provider/rti.go +++ b/test/integration/provider/rti.go @@ -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 @@ -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 @@ -53,17 +60,17 @@ func (r *ResourcesTrackerImpl) probeResources() ([]string, []string, []string, [ return nil, nil, nil, nil, fmt.Errorf("failed to find available machines: %s", err) } - orphanVMs, err := getOrphanedInstances(r.MachineClass, factory) + orphanVMs, err := getOrphanedInstances(factory) if err != nil { return nil, nil, nil, nil, fmt.Errorf("failed to find orphaned instances: %s", err) } - orphanNICs, err := getOrphanedNICs(r.MachineClass, factory) + orphanNICs, err := getOrphanedNICs(factory) if err != nil { return nil, nil, nil, nil, fmt.Errorf("failed to find available ports: %s", err) } - orphanDisks, err := getOrphanedDisks(r.MachineClass, factory) + orphanDisks, err := getOrphanedDisks(factory) if err != nil { return nil, nil, nil, nil, fmt.Errorf("failed to find available disks: %s", err) } @@ -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 ... waiting for them to be deleted \n") fmt.Printf("Virtual Machines: %v\nNICs: %v\nMCM Machines: %v\n", afterTestExecutionVMs, afterTestExecutionNICs, afterTestExecutionAvailmachines) return true }