Skip to content

Commit

Permalink
refactor(api): unified deletion message when removing cleanup finaliz…
Browse files Browse the repository at this point in the history
…er (#453)

chore(api): unified deletion message when removing cleanup finalizer

Signed-off-by: Ivan Mikheykin <ivan.mikheykin@flant.com>
  • Loading branch information
diafour authored Nov 2, 2024
1 parent 19d7a25 commit 69d6e5c
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,12 @@ import (
"sigs.k8s.io/controller-runtime/pkg/reconcile"

"github.com/deckhouse/virtualization-controller/pkg/controller/cvi/internal/source"
"github.com/deckhouse/virtualization-controller/pkg/logger"
virtv2 "github.com/deckhouse/virtualization/api/core/v1alpha2"
)

const deletionHandlerName = "DeletionHandler"

type DeletionHandler struct {
sources *source.Sources
}
Expand All @@ -37,6 +40,8 @@ func NewDeletionHandler(sources *source.Sources) *DeletionHandler {
}

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

if cvi.DeletionTimestamp != nil {
result, err := h.sources.CleanUp(ctx, cvi)
if err != nil {
Expand All @@ -47,6 +52,7 @@ func (h DeletionHandler) Handle(ctx context.Context, cvi *virtv2.ClusterVirtualI
return result, nil
}

log.Info("Deletion observed: remove cleanup finalizer from clusterVirtualImage")
controllerutil.RemoveFinalizer(cvi, virtv2.FinalizerCVICleanup)
return reconcile.Result{}, nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,12 @@ import (
"sigs.k8s.io/controller-runtime/pkg/reconcile"

"github.com/deckhouse/virtualization-controller/pkg/controller/vd/internal/source"
"github.com/deckhouse/virtualization-controller/pkg/logger"
virtv2 "github.com/deckhouse/virtualization/api/core/v1alpha2"
)

const deletionHandlerName = "DeletionHandler"

type DeletionHandler struct {
sources *source.Sources
}
Expand All @@ -37,6 +40,8 @@ func NewDeletionHandler(sources *source.Sources) *DeletionHandler {
}

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

if vd.DeletionTimestamp != nil {
requeue, err := h.sources.CleanUp(ctx, vd)
if err != nil {
Expand All @@ -47,6 +52,7 @@ func (h DeletionHandler) Handle(ctx context.Context, vd *virtv2.VirtualDisk) (re
return reconcile.Result{Requeue: true}, nil
}

log.Info("Deletion observed: remove cleanup finalizer from VirtualDisk")
controllerutil.RemoveFinalizer(vd, virtv2.FinalizerVDCleanup)
return reconcile.Result{}, nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,12 @@ import (
"sigs.k8s.io/controller-runtime/pkg/reconcile"

"github.com/deckhouse/virtualization-controller/pkg/controller/service"
"github.com/deckhouse/virtualization-controller/pkg/logger"
virtv2 "github.com/deckhouse/virtualization/api/core/v1alpha2"
)

const deletionHandlerName = "DeletionHandler"

type DeletionHandler struct {
snapshotter *service.SnapshotService
}
Expand All @@ -37,6 +40,8 @@ func NewDeletionHandler(snapshotter *service.SnapshotService) *DeletionHandler {
}

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

if vdSnapshot.DeletionTimestamp != nil {
vs, err := h.snapshotter.GetVolumeSnapshot(ctx, vdSnapshot.Name, vdSnapshot.Namespace)
if err != nil {
Expand Down Expand Up @@ -82,6 +87,7 @@ func (h DeletionHandler) Handle(ctx context.Context, vdSnapshot *virtv2.VirtualD
return reconcile.Result{Requeue: true}, nil
}

log.Info("Deletion observed: remove cleanup finalizer from VirtualDiskSnapshot")
controllerutil.RemoveFinalizer(vdSnapshot, virtv2.FinalizerVDSnapshotCleanup)
return reconcile.Result{}, nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,12 @@ import (
"sigs.k8s.io/controller-runtime/pkg/reconcile"

"github.com/deckhouse/virtualization-controller/pkg/controller/vi/internal/source"
"github.com/deckhouse/virtualization-controller/pkg/logger"
virtv2 "github.com/deckhouse/virtualization/api/core/v1alpha2"
)

const deletionHandlerName = "DeletionHandler"

type DeletionHandler struct {
sources *source.Sources
}
Expand All @@ -37,6 +40,8 @@ func NewDeletionHandler(sources *source.Sources) *DeletionHandler {
}

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

if vi.DeletionTimestamp != nil {
requeue, err := h.sources.CleanUp(ctx, vi)
if err != nil {
Expand All @@ -47,6 +52,7 @@ func (h DeletionHandler) Handle(ctx context.Context, vi *virtv2.VirtualImage) (r
return reconcile.Result{Requeue: true}, nil
}

log.Info("Deletion observed: remove cleanup finalizer from VirtualImage")
controllerutil.RemoveFinalizer(vi, virtv2.FinalizerVICleanup)
return reconcile.Result{}, nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import (
virtv2 "github.com/deckhouse/virtualization/api/core/v1alpha2"
)

const nameDeletionHandler = "DeletionHandler"
const deletionHandlerName = "DeletionHandler"

func NewDeletionHandler(client client.Client) *DeletionHandler {
return &DeletionHandler{
Expand All @@ -47,7 +47,7 @@ type DeletionHandler struct {
}

func (h *DeletionHandler) Handle(ctx context.Context, s state.VirtualMachineState) (reconcile.Result, error) {
log := logger.FromContext(ctx).With(logger.SlogHandler(nameDeletionHandler))
log := logger.FromContext(ctx).With(logger.SlogHandler(deletionHandlerName))

if s.VirtualMachine().IsEmpty() {
return reconcile.Result{}, nil
Expand All @@ -57,7 +57,7 @@ func (h *DeletionHandler) Handle(ctx context.Context, s state.VirtualMachineStat
controllerutil.AddFinalizer(changed, virtv2.FinalizerVMCleanup)
return reconcile.Result{}, nil
}
log.Info("Delete VM, remove protective finalizers")
log.Info("Deletion observed: remove protection from KVVM")
kvvm, err := s.KVVM(ctx)
if err != nil {
return reconcile.Result{}, err
Expand All @@ -81,10 +81,11 @@ func (h *DeletionHandler) Handle(ctx context.Context, s state.VirtualMachineStat
return reconcile.Result{RequeueAfter: requeueAfter}, nil
}

log.Info("Deletion observed: remove cleanup finalizer from VirtualMachine")
controllerutil.RemoveFinalizer(s.VirtualMachine().Changed(), virtv2.FinalizerVMCleanup)
return reconcile.Result{}, nil
}

func (h *DeletionHandler) Name() string {
return nameDeletionHandler
return deletionHandlerName
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,22 @@ import (
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/reconcile"

"github.com/deckhouse/virtualization-controller/pkg/logger"
virtv2 "github.com/deckhouse/virtualization/api/core/v1alpha2"
)

const deletionHandlerName = "DeletionHandler"

type DeletionHandler struct{}

func NewDeletionHandler() *DeletionHandler {
return &DeletionHandler{}
}

func (h DeletionHandler) Handle(_ context.Context, vd *virtv2.VirtualMachineBlockDeviceAttachment) (reconcile.Result, error) {
func (h DeletionHandler) Handle(ctx context.Context, vd *virtv2.VirtualMachineBlockDeviceAttachment) (reconcile.Result, error) {
log := logger.FromContext(ctx).With(logger.SlogHandler(deletionHandlerName))
if vd.DeletionTimestamp != nil {
log.Info("Deletion observed: remove cleanup finalizer from VirtualMachineBlockDeviceAttachment")
controllerutil.RemoveFinalizer(vd, virtv2.FinalizerVMBDACleanup)
return reconcile.Result{}, nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func (h *DeletionHandler) Handle(ctx context.Context, s state.VirtualMachineClas
h.recorder.Event(changed, corev1.EventTypeWarning, virtv2.ReasonVMClassInUse, msg)
return reconcile.Result{RequeueAfter: 60 * time.Second}, nil
}
h.logger.Info("Delete VmClass, remove protection finalizers")
h.logger.Info("Deletion observed: remove cleanup finalizer from VirtualMachineClass")
controllerutil.RemoveFinalizer(changed, virtv2.FinalizerVMCleanup)
return reconcile.Result{}, nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (h *ProtectionHandler) Handle(ctx context.Context, state state.VMIPState) (

switch {
case len(configuredVms) == 0:
log.Debug("Allow VirtualMachineIPAddress deletion")
log.Debug("Allow VirtualMachineIPAddress deletion: remove protection finalizer")
controllerutil.RemoveFinalizer(vmip, virtv2.FinalizerIPAddressProtection)
case vmip.DeletionTimestamp == nil:
log.Debug("Protect VirtualMachineIPAddress from deletion")
Expand All @@ -67,7 +67,7 @@ func (h *ProtectionHandler) Handle(ctx context.Context, state state.VMIPState) (
}

if vm == nil || vm.DeletionTimestamp != nil {
log.Info("VirtualMachineIP is no longer attached to any VM, proceeding with detachment", "VirtualMachineIPName", vmip.Name)
log.Info("VirtualMachineIP is no longer attached to any VM: remove cleanup finalizer", "VirtualMachineIPName", vmip.Name)
controllerutil.RemoveFinalizer(vmip, virtv2.FinalizerIPAddressCleanup)
} else if vmip.GetDeletionTimestamp() == nil {
controllerutil.AddFinalizer(vmip, virtv2.FinalizerIPAddressCleanup)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/reconcile"

"github.com/deckhouse/virtualization-controller/pkg/controller/vmiplease/internal/state"
"github.com/deckhouse/virtualization-controller/pkg/logger"
virtv2 "github.com/deckhouse/virtualization/api/core/v1alpha2"
)

Expand All @@ -35,6 +36,7 @@ func NewProtectionHandler() *ProtectionHandler {
}

func (h *ProtectionHandler) Handle(ctx context.Context, state state.VMIPLeaseState) (reconcile.Result, error) {
log := logger.FromContext(ctx).With(logger.SlogHandler(ProtectionHandlerName))
lease := state.VirtualMachineIPAddressLease()

vmip, err := state.VirtualMachineIPAddress(ctx)
Expand All @@ -45,6 +47,7 @@ func (h *ProtectionHandler) Handle(ctx context.Context, state state.VMIPLeaseSta
if vmip != nil {
controllerutil.AddFinalizer(lease, virtv2.FinalizerIPAddressLeaseCleanup)
} else if lease.GetDeletionTimestamp() == nil {
log.Info("Deletion observed: remove cleanup finalizer from VirtualMachineIPAddressLease")
controllerutil.RemoveFinalizer(lease, virtv2.FinalizerIPAddressLeaseCleanup)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package internal

import (
"context"
"fmt"

"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
Expand Down Expand Up @@ -59,7 +58,11 @@ func (h DeletionHandler) Handle(ctx context.Context, s state.VMOperationState) (
}

// Remove finalizer when VirtualMachineOperation is in deletion state or not in progress.
log.Debug(fmt.Sprintf("Remove cleanup finalier from VMOP: deletion %v, phase %s", changed.DeletionTimestamp != nil, changed.Status.Phase))
if changed.DeletionTimestamp != nil {
log.Info("Deletion observed: remove cleanup finalizer from VirtualMachineOperation", "phase", changed.Status.Phase)
} else {
log.Debug("Remove cleanup finalizer from VirtualMachineOperation: not InProgress state", "phase", changed.Status.Phase)
}
controllerutil.RemoveFinalizer(changed, virtv2.FinalizerVMOPCleanup)
return reconcile.Result{}, nil
}
Expand Down

0 comments on commit 69d6e5c

Please sign in to comment.