Make sure snapshots, restores are cleanup after the test
@@ -209,6 +209,35 @@ func CleanNamespaces() {
for _, clone := range clonesList.Items {
util.PanicOnError(virtCli.VirtualMachineClone(namespace).Delete(context.Background(), clone.Name, metav1.DeleteOptions{}))
}
+
+ // Remove vm snapshots
+ util.PanicOnError(virtCli.VirtualMachineSnapshot(namespace).DeleteCollection(context.Background(), metav1.DeleteOptions{}, metav1.ListOptions{}))
+ snapshots, err := virtCli.VirtualMachineSnapshot(namespace).List(context.Background(), metav1.ListOptions{})
+ util.PanicOnError(err)
+ vmSnapshotFinalizer := "snapshot.kubevirt.io/vmsnapshot-protection"
+ for _, snapshot := range snapshots.Items {
+ if controller.HasFinalizer(&snapshot, vmSnapshotFinalizer) {
+ _, err := virtCli.VirtualMachineSnapshot(snapshot.Namespace).Patch(context.Background(), snapshot.Name, types.JSONPatchType, []byte("[{ \"op\": \"remove\", \"path\": \"/metadata/finalizers\" }]"), metav1.PatchOptions{})
+ if !errors.IsNotFound(err) {
+ util.PanicOnError(err)
+ }
+ }
+ }
+
+ util.PanicOnError(virtCli.VirtualMachineSnapshotContent(namespace).DeleteCollection(context.Background(), metav1.DeleteOptions{}, metav1.ListOptions{}))
+ snapshotContentList, err := virtCli.VirtualMachineSnapshotContent(namespace).List(context.Background(), metav1.ListOptions{})
+ util.PanicOnError(err)
+ vmSnapshotContentFinalizer := "snapshot.kubevirt.io/vmsnapshotcontent-protection"
+ for _, snapshotContent := range snapshotContentList.Items {
+ if controller.HasFinalizer(&snapshotContent, vmSnapshotContentFinalizer) {
+ _, err := virtCli.VirtualMachineSnapshot(snapshotContent.Namespace).Patch(context.Background(), snapshotContent.Name, types.JSONPatchType, []byte("[{ \"op\": \"remove\", \"path\": \"/metadata/finalizers\" }]"), metav1.PatchOptions{})
+ if !errors.IsNotFound(err) {
+ util.PanicOnError(err)
+ }
+ }
+ }
+
+ util.PanicOnError(virtCli.VirtualMachineRestore(namespace).DeleteCollection(context.Background(), metav1.DeleteOptions{}, metav1.ListOptions{}))
}
}
