From d2f09cdf91251dd7614c65f4d632cbf554f432b8 Mon Sep 17 00:00:00 2001 From: Arik Hadas Date: Thu, 19 Oct 2023 10:39:55 +0300 Subject: [PATCH] fix error handling in DeletePVCs To return an error when we can't get a prime PVC rather than continue to the next PVC. Also refactored the code a bit to reduce cognitive complexity. Signed-off-by: Arik Hadas --- pkg/controller/plan/kubevirt.go | 15 ++++++--------- pkg/controller/plan/migration.go | 12 ++++++------ 2 files changed, 12 insertions(+), 15 deletions(-) diff --git a/pkg/controller/plan/kubevirt.go b/pkg/controller/plan/kubevirt.go index 4dfc6704a..0283afe1f 100644 --- a/pkg/controller/plan/kubevirt.go +++ b/pkg/controller/plan/kubevirt.go @@ -937,25 +937,22 @@ func (r *KubeVirt) DeletePVCs(vm *plan.VMStatus) (err error) { 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 { - if k8serr.IsNotFound(err) { - err = nil - } - continue + if err != nil && k8serr.IsNotFound(err) { + err = nil + } else if err != nil { + return } // Best effort deletion err = r.DeleteObject(&primePVC, vm, "Deleted prime PVC.", "pvc") if err != nil && k8serr.IsNotFound(err) { err = nil - } - if err != nil { + } else if err != nil { return } err = r.DeleteObject(&pvc, vm, "Deleted PVC.", "pvc") if err != nil && k8serr.IsNotFound(err) { err = nil - } - if err != nil { + } else if err != nil { return } pvcCopy := pvc.DeepCopy() diff --git a/pkg/controller/plan/migration.go b/pkg/controller/plan/migration.go index f674433f0..33bae589b 100644 --- a/pkg/controller/plan/migration.go +++ b/pkg/controller/plan/migration.go @@ -424,7 +424,9 @@ func (r *Migration) Cancel() (err error) { } func (r *Migration) cleanUpPopulatorPVCs(vm *plan.VMStatus) (err error) { - err = r.kubevirt.DeletePVCs(vm) + if r.builder.SupportsVolumePopulators() { + err = r.kubevirt.DeletePVCs(vm) + } return } @@ -435,11 +437,9 @@ 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.cleanUpPopulatorPVCs(vm) + if err != nil { + return } } err = r.deleteImporterPods(vm)