Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
Signed-off-by: yaroslavborbat <[email protected]>
  • Loading branch information
yaroslavborbat committed Jan 18, 2025
1 parent e30014e commit 9de8c9c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,6 @@ func (s AttachmentService) CanUnplug(kvvm *virtv1.VirtualMachine, diskName strin
}

for _, volume := range kvvm.Spec.Template.Spec.Volumes {
if volume.Name == diskName {
return true
}
if kvapi.VolumeExists(volume, diskName) {
return true
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package internal

import (
"context"
"log/slog"

"k8s.io/apimachinery/pkg/types"
virtv1 "kubevirt.io/api/core/v1"
Expand All @@ -40,6 +41,8 @@ type UnplugInterface interface {
type DeletionHandler struct {
unplug UnplugInterface
client client.Client

log *slog.Logger
}

func NewDeletionHandler(unplug UnplugInterface, client client.Client) *DeletionHandler {
Expand All @@ -49,13 +52,14 @@ func NewDeletionHandler(unplug UnplugInterface, client client.Client) *DeletionH
}
}

func (h DeletionHandler) Handle(ctx context.Context, vmbda *virtv2.VirtualMachineBlockDeviceAttachment) (reconcile.Result, error) {
log := logger.FromContext(ctx).With(logger.SlogHandler(deletionHandlerName))
func (h *DeletionHandler) Handle(ctx context.Context, vmbda *virtv2.VirtualMachineBlockDeviceAttachment) (reconcile.Result, error) {
h.log = logger.FromContext(ctx).With(logger.SlogHandler(deletionHandlerName))

if vmbda.DeletionTimestamp != nil {
if err := h.cleanUp(ctx, vmbda); err != nil {
return reconcile.Result{}, err
}
log.Info("Deletion observed: remove cleanup finalizer from VirtualMachineBlockDeviceAttachment")
h.log.Info("Deletion observed: remove cleanup finalizer from VirtualMachineBlockDeviceAttachment")
controllerutil.RemoveFinalizer(vmbda, virtv2.FinalizerVMBDACleanup)
return reconcile.Result{}, nil
}
Expand All @@ -64,7 +68,7 @@ func (h DeletionHandler) Handle(ctx context.Context, vmbda *virtv2.VirtualMachin
return reconcile.Result{}, nil
}

func (h DeletionHandler) cleanUp(ctx context.Context, vmbda *virtv2.VirtualMachineBlockDeviceAttachment) error {
func (h *DeletionHandler) cleanUp(ctx context.Context, vmbda *virtv2.VirtualMachineBlockDeviceAttachment) error {
if vmbda == nil {
return nil
}
Expand All @@ -85,6 +89,7 @@ func (h DeletionHandler) cleanUp(ctx context.Context, vmbda *virtv2.VirtualMachi
}

if h.unplug.CanUnplug(kvvm, diskName) {
h.log.Info("Unplug Virtual Disk", slog.String("diskName", diskName), slog.String("vm", kvvm.Name))
if err = h.unplug.UnplugDisk(ctx, kvvm, diskName); err != nil {
return err
}
Expand Down

0 comments on commit 9de8c9c

Please sign in to comment.