diff --git a/pkg/controller/plan/kubevirt.go b/pkg/controller/plan/kubevirt.go index 18676b093..b6f8cee30 100644 --- a/pkg/controller/plan/kubevirt.go +++ b/pkg/controller/plan/kubevirt.go @@ -931,6 +931,38 @@ func (r *KubeVirt) SetPopulatorPodOwnership(vm *plan.VMStatus) (err error) { return } +// DeletePVCs deletes PVCs associated with the VM, including prime PVCs (populator flows) +func (r *KubeVirt) DeletePVCs(vm *plan.VMStatus) (err error) { + pvcs, err := r.getPVCs(vm.Ref) + for _, pvc := range pvcs { + primePVC := core.PersistentVolumeClaim{} + err = r.Destination.Client.Get(context.TODO(), client.ObjectKey{Namespace: r.Plan.Spec.TargetNamespace, Name: fmt.Sprintf("prime-%s", string(pvc.UID))}, &primePVC) + if err != nil { + continue + } + // Best effort deletion + err = r.DeleteObject(&primePVC, vm, "Deleted prime PVC.", "pvc") + if err != nil && k8serr.IsNotFound(err) { + err = nil + } + if err != nil { + return + } + err = r.DeleteObject(&pvc, vm, "Deleted PVC.", "pvc") + if err != nil && k8serr.IsNotFound(err) { + err = nil + } + if err != nil { + return + } + pvcCopy := pvc.DeepCopy() + pvc.Finalizers = nil + patch := client.MergeFrom(pvcCopy) + err = r.Destination.Client.Patch(context.TODO(), &pvc, patch) + } + return +} + // Delete any populator pods that belong to a VM's migration. func (r *KubeVirt) DeletePopulatorPods(vm *plan.VMStatus) (err error) { list, err := r.getPopulatorPods() diff --git a/pkg/controller/plan/migration.go b/pkg/controller/plan/migration.go index 53817a668..f674433f0 100644 --- a/pkg/controller/plan/migration.go +++ b/pkg/controller/plan/migration.go @@ -423,6 +423,11 @@ func (r *Migration) Cancel() (err error) { return } +func (r *Migration) cleanUpPopulatorPVCs(vm *plan.VMStatus) (err error) { + err = r.kubevirt.DeletePVCs(vm) + return +} + // Delete left over migration resources associated with a VM. func (r *Migration) cleanup(vm *plan.VMStatus) (err error) { if !vm.HasCondition(Succeeded) { @@ -430,6 +435,12 @@ func (r *Migration) cleanup(vm *plan.VMStatus) (err error) { if err != nil { return } + if r.builder.SupportsVolumePopulators() { + err = r.cleanUpPopulatorPVCs(vm) + if err != nil { + return + } + } } err = r.deleteImporterPods(vm) if err != nil {