Skip to content

Commit

Permalink
fix error handling in DeletePVCs
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
ahadas committed Oct 19, 2023
1 parent 12d417b commit d2f09cd
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 15 deletions.
15 changes: 6 additions & 9 deletions pkg/controller/plan/kubevirt.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

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

View workflow job for this annotation

GitHub Actions / lint

ineffectual assignment to err (ineffassign)
} 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

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

View workflow job for this annotation

GitHub Actions / lint

ineffectual assignment to err (ineffassign)
}
if err != nil {
} else if err != nil {
return
}
err = r.DeleteObject(&pvc, vm, "Deleted PVC.", "pvc")
if err != nil && k8serr.IsNotFound(err) {
err = nil

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

View workflow job for this annotation

GitHub Actions / lint

ineffectual assignment to err (ineffassign)
}
if err != nil {
} else if err != nil {
return
}
pvcCopy := pvc.DeepCopy()
Expand Down
12 changes: 6 additions & 6 deletions pkg/controller/plan/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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)
Expand Down

0 comments on commit d2f09cd

Please sign in to comment.