Skip to content

Commit

Permalink
Clean up PVCs when canceling populator flows
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
liranr23 committed Oct 2, 2023
1 parent a32837b commit 3597547
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
23 changes: 23 additions & 0 deletions pkg/controller/plan/kubevirt.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Check failure on line 908 in pkg/controller/plan/kubevirt.go

View workflow job for this annotation

GitHub Actions / lint

ineffectual assignment to err (ineffassign)
}
err = r.DeleteObject(&pvc, vm, "Deleted PVC.", "pvc")

Check failure on line 910 in pkg/controller/plan/kubevirt.go

View workflow job for this annotation

GitHub Actions / lint

ineffectual assignment to err (ineffassign)
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()
Expand Down
11 changes: 11 additions & 0 deletions pkg/controller/plan/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -414,13 +414,24 @@ 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) {
err = r.kubevirt.DeleteVM(vm)
if err != nil {
return
}
if r.builder.SupportsVolumePopulators() {
err = r.CleanUpPopulatorPVCs(vm)
if err != nil {
return
}
}
}
err = r.deleteImporterPods(vm)
if err != nil {
Expand Down

0 comments on commit 3597547

Please sign in to comment.