From dd54aa529b58502841649e5f160675253e283d0b Mon Sep 17 00:00:00 2001 From: Liran Rotenberg Date: Tue, 12 Sep 2023 18:35:43 +0300 Subject: [PATCH] Clean up PVCs when canceling populator flows This patch will clean up the PVCs (final and prime) associated with VMs that the migration was cancled for them. Signed-off-by: Liran Rotenberg --- pkg/controller/plan/kubevirt.go | 23 +++++++++++++++++++++++ pkg/controller/plan/migration.go | 15 +++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/pkg/controller/plan/kubevirt.go b/pkg/controller/plan/kubevirt.go index a2129d994..08e466449 100644 --- a/pkg/controller/plan/kubevirt.go +++ b/pkg/controller/plan/kubevirt.go @@ -893,6 +893,29 @@ 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 + } + err = r.DeleteObject(&pvc, vm, "Deleted PVC.", "pvc") + 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 f3f060b1e..0f4d61ed4 100644 --- a/pkg/controller/plan/migration.go +++ b/pkg/controller/plan/migration.go @@ -392,6 +392,16 @@ func (r *Migration) Cancel() (err error) { vm.String()) err = nil } + if r.builder.SupportsVolumePopulators() { + err = r.CleanUpPopulatorPVCs(vm) + if err != nil { + r.Log.Error(err, + "Couldn't clean up after canceled VM migration.", + "vm", + vm.String()) + err = nil + } + } if vm.RestorePowerState == On { err = r.provider.PowerOn(vm.Ref) if err != nil { @@ -414,6 +424,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) {