Skip to content

Commit d1c2d34

Browse files
authored
fix(vd): improve virtual disk protection logic during deletion (#1285)
fix(vd): improve virtual disk protection logic during deletion Refactor the deletion and protection handlers to enhance the logic for managing virtual disk deletion. The deletion handler now checks for the presence of a finalizer before proceeding with cleanup. The protection handler now properly adds the vd-protection finalizer when a VirtualDisk is attached to any VM (including via VMBDA). This fixes protection logic for VMBDA attached disks and PVC deletion spam when deleting attached VDs. --------- Signed-off-by: Daniil Loktev <[email protected]>
1 parent e56c713 commit d1c2d34

File tree

3 files changed

+19
-18
lines changed

3 files changed

+19
-18
lines changed

images/virtualization-artifact/pkg/controller/vd/internal/deletion.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ func (h DeletionHandler) Handle(ctx context.Context, vd *virtv2.VirtualDisk) (re
4444
log := logger.FromContext(ctx).With(logger.SlogHandler(deletionHandlerName))
4545

4646
if vd.DeletionTimestamp != nil {
47+
if controllerutil.ContainsFinalizer(vd, virtv2.FinalizerVDProtection) {
48+
return reconcile.Result{}, nil
49+
}
50+
4751
requeue, err := h.sources.CleanUp(ctx, vd)
4852
if err != nil {
4953
return reconcile.Result{}, err

images/virtualization-artifact/pkg/controller/vd/internal/protection.go

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -39,27 +39,23 @@ func (h ProtectionHandler) Handle(ctx context.Context, vd *virtv2.VirtualDisk) (
3939
log.Debug("virtual disk connected to multiple virtual machines", "vms", len(vd.Status.AttachedToVirtualMachines))
4040
}
4141

42-
switch {
43-
case len(vd.Status.AttachedToVirtualMachines) == 0:
42+
unmounted := true
43+
for _, vm := range vd.Status.AttachedToVirtualMachines {
44+
if vm.Mounted {
45+
unmounted = false
46+
break
47+
}
48+
}
49+
50+
if unmounted {
4451
log.Debug("Allow virtual disk deletion")
4552
controllerutil.RemoveFinalizer(vd, virtv2.FinalizerVDProtection)
46-
case len(vd.Status.AttachedToVirtualMachines) != 0:
47-
unmounted := true
48-
for _, vm := range vd.Status.AttachedToVirtualMachines {
49-
if vm.Mounted {
50-
unmounted = false
51-
}
52-
}
53-
if unmounted {
54-
log.Debug("Allow virtual disk deletion")
55-
controllerutil.RemoveFinalizer(vd, virtv2.FinalizerVDProtection)
56-
}
57-
case vd.DeletionTimestamp == nil:
53+
return reconcile.Result{}, nil
54+
}
55+
56+
if vd.DeletionTimestamp == nil {
5857
log.Debug("Protect virtual disk from deletion")
5958
controllerutil.AddFinalizer(vd, virtv2.FinalizerVDProtection)
60-
default:
61-
log.Debug("Virtual disk deletion is delayed: it's protected by virtual machines")
6259
}
63-
6460
return reconcile.Result{}, nil
6561
}

images/virtualization-artifact/pkg/controller/vd/vd_reconciler.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package vd
1919
import (
2020
"context"
2121
"fmt"
22+
"reflect"
2223

2324
"k8s.io/apimachinery/pkg/types"
2425
cdiv1 "kubevirt.io/containerized-data-importer-api/pkg/apis/core/v1beta1"
@@ -91,7 +92,7 @@ func (r *Reconciler) SetupController(_ context.Context, mgr manager.Manager, ctr
9192
CreateFunc: func(e event.CreateEvent) bool { return true },
9293
DeleteFunc: func(e event.DeleteEvent) bool { return true },
9394
UpdateFunc: func(e event.UpdateEvent) bool {
94-
return e.ObjectOld.GetGeneration() != e.ObjectNew.GetGeneration()
95+
return !reflect.DeepEqual(e.ObjectOld.GetFinalizers(), e.ObjectNew.GetFinalizers()) || e.ObjectOld.GetGeneration() != e.ObjectNew.GetGeneration()
9596
},
9697
},
9798
); err != nil {

0 commit comments

Comments
 (0)