From 69d6e5c51dc63f98787fa364803d5c78d480a69d Mon Sep 17 00:00:00 2001 From: Ivan Mikheykin Date: Sat, 2 Nov 2024 13:16:01 +0300 Subject: [PATCH] refactor(api): unified deletion message when removing cleanup finalizer (#453) chore(api): unified deletion message when removing cleanup finalizer Signed-off-by: Ivan Mikheykin --- .../pkg/controller/cvi/internal/deletion.go | 6 ++++++ .../pkg/controller/vd/internal/deletion.go | 6 ++++++ .../pkg/controller/vdsnapshot/internal/deletion.go | 6 ++++++ .../pkg/controller/vi/internal/deletion.go | 6 ++++++ .../pkg/controller/vm/internal/deletion_handler.go | 9 +++++---- .../pkg/controller/vmbda/internal/deletion.go | 7 ++++++- .../pkg/controller/vmclass/internal/deletion.go | 2 +- .../pkg/controller/vmip/internal/protection_handler.go | 4 ++-- .../controller/vmiplease/internal/protection_handler.go | 3 +++ .../pkg/controller/vmop/internal/deletion.go | 7 +++++-- 10 files changed, 46 insertions(+), 10 deletions(-) diff --git a/images/virtualization-artifact/pkg/controller/cvi/internal/deletion.go b/images/virtualization-artifact/pkg/controller/cvi/internal/deletion.go index 7ffb166b6..52289df64 100644 --- a/images/virtualization-artifact/pkg/controller/cvi/internal/deletion.go +++ b/images/virtualization-artifact/pkg/controller/cvi/internal/deletion.go @@ -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 } @@ -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 { @@ -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 } diff --git a/images/virtualization-artifact/pkg/controller/vd/internal/deletion.go b/images/virtualization-artifact/pkg/controller/vd/internal/deletion.go index 060831fbf..e88fcb9a7 100644 --- a/images/virtualization-artifact/pkg/controller/vd/internal/deletion.go +++ b/images/virtualization-artifact/pkg/controller/vd/internal/deletion.go @@ -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 } @@ -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 { @@ -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 } diff --git a/images/virtualization-artifact/pkg/controller/vdsnapshot/internal/deletion.go b/images/virtualization-artifact/pkg/controller/vdsnapshot/internal/deletion.go index 6cbcb73bf..58b707512 100644 --- a/images/virtualization-artifact/pkg/controller/vdsnapshot/internal/deletion.go +++ b/images/virtualization-artifact/pkg/controller/vdsnapshot/internal/deletion.go @@ -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 } @@ -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 { @@ -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 } diff --git a/images/virtualization-artifact/pkg/controller/vi/internal/deletion.go b/images/virtualization-artifact/pkg/controller/vi/internal/deletion.go index 695214970..2932655a3 100644 --- a/images/virtualization-artifact/pkg/controller/vi/internal/deletion.go +++ b/images/virtualization-artifact/pkg/controller/vi/internal/deletion.go @@ -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 } @@ -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 { @@ -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 } diff --git a/images/virtualization-artifact/pkg/controller/vm/internal/deletion_handler.go b/images/virtualization-artifact/pkg/controller/vm/internal/deletion_handler.go index b957ce698..4d1b545f1 100644 --- a/images/virtualization-artifact/pkg/controller/vm/internal/deletion_handler.go +++ b/images/virtualization-artifact/pkg/controller/vm/internal/deletion_handler.go @@ -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{ @@ -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 @@ -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 @@ -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 } diff --git a/images/virtualization-artifact/pkg/controller/vmbda/internal/deletion.go b/images/virtualization-artifact/pkg/controller/vmbda/internal/deletion.go index 5830d4cfd..fec026765 100644 --- a/images/virtualization-artifact/pkg/controller/vmbda/internal/deletion.go +++ b/images/virtualization-artifact/pkg/controller/vmbda/internal/deletion.go @@ -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 } diff --git a/images/virtualization-artifact/pkg/controller/vmclass/internal/deletion.go b/images/virtualization-artifact/pkg/controller/vmclass/internal/deletion.go index 9acb1d957..9a05051ca 100644 --- a/images/virtualization-artifact/pkg/controller/vmclass/internal/deletion.go +++ b/images/virtualization-artifact/pkg/controller/vmclass/internal/deletion.go @@ -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 } diff --git a/images/virtualization-artifact/pkg/controller/vmip/internal/protection_handler.go b/images/virtualization-artifact/pkg/controller/vmip/internal/protection_handler.go index 641723f9f..26b53f9a0 100644 --- a/images/virtualization-artifact/pkg/controller/vmip/internal/protection_handler.go +++ b/images/virtualization-artifact/pkg/controller/vmip/internal/protection_handler.go @@ -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") @@ -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) diff --git a/images/virtualization-artifact/pkg/controller/vmiplease/internal/protection_handler.go b/images/virtualization-artifact/pkg/controller/vmiplease/internal/protection_handler.go index bf1cd2333..deaa6fef7 100644 --- a/images/virtualization-artifact/pkg/controller/vmiplease/internal/protection_handler.go +++ b/images/virtualization-artifact/pkg/controller/vmiplease/internal/protection_handler.go @@ -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" ) @@ -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) @@ -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) } diff --git a/images/virtualization-artifact/pkg/controller/vmop/internal/deletion.go b/images/virtualization-artifact/pkg/controller/vmop/internal/deletion.go index 9772e9b23..6559f1b34 100644 --- a/images/virtualization-artifact/pkg/controller/vmop/internal/deletion.go +++ b/images/virtualization-artifact/pkg/controller/vmop/internal/deletion.go @@ -18,7 +18,6 @@ package internal import ( "context" - "fmt" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" @@ -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 }