From d138ffde100dfff0a4cc78abb723ad0888a16b47 Mon Sep 17 00:00:00 2001 From: Roman Sysoev Date: Wed, 15 Jan 2025 20:04:33 +0300 Subject: [PATCH] api(vmsnapshot): refactor vmsnapshot events Signed-off-by: Roman Sysoev --- api/core/v1alpha2/events.go | 12 +++++++++ .../vmsnapshot/internal/life_cycle.go | 9 +++++-- .../vmsnapshot/internal/life_cycle_test.go | 26 +++++++++++-------- .../vmsnapshot/vmsnapshot_controller.go | 4 ++- .../vmsnapshot/vmsnapshot_reconciler.go | 2 +- 5 files changed, 38 insertions(+), 15 deletions(-) diff --git a/api/core/v1alpha2/events.go b/api/core/v1alpha2/events.go index 124da0509..0dfc17a6a 100644 --- a/api/core/v1alpha2/events.go +++ b/api/core/v1alpha2/events.go @@ -95,4 +95,16 @@ const ( // ReasonDataSourceDiskProvisioningFailed is event reason that DataSource disk provisioning is failed. ReasonDataSourceDiskProvisioningFailed = "DataSourceImportDiskProvisioningFailed" + + // ReasonVMSnapshottingStarted is event reason that VirtualMachine snapshotting is started. + ReasonVMSnapshottingStarted = "VirtualMachineSnapshottingStarted" + + // ReasonVMSnapshottingPending is event reason that VirtualMachine is not ready for snapshotting. + ReasonVMSnapshottingPending = "VirtualMachineSnapshottingPending" + + // ReasonVMSnapshottingCompleted is event reason that VirtualMachine snapshotting is completed. + ReasonVMSnapshottingCompleted = "VirtualMachineSnapshottingCompleted" + + // ReasonVMSnapshottingFailed is event reason that VirtualMachine snapshotting is failed. + ReasonVMSnapshottingFailed = "VirtualMachineSnapshottingFailed" ) diff --git a/images/virtualization-artifact/pkg/controller/vmsnapshot/internal/life_cycle.go b/images/virtualization-artifact/pkg/controller/vmsnapshot/internal/life_cycle.go index 853d0fb6e..341bd90cc 100644 --- a/images/virtualization-artifact/pkg/controller/vmsnapshot/internal/life_cycle.go +++ b/images/virtualization-artifact/pkg/controller/vmsnapshot/internal/life_cycle.go @@ -29,6 +29,7 @@ import ( "github.com/deckhouse/virtualization-controller/pkg/controller/conditions" "github.com/deckhouse/virtualization-controller/pkg/controller/service" + "github.com/deckhouse/virtualization-controller/pkg/eventrecord" "github.com/deckhouse/virtualization-controller/pkg/logger" virtv2 "github.com/deckhouse/virtualization/api/core/v1alpha2" "github.com/deckhouse/virtualization/api/core/v1alpha2/vdcondition" @@ -38,12 +39,14 @@ import ( ) type LifeCycleHandler struct { + recorder eventrecord.EventRecorderLogger snapshotter Snapshotter storer Storer } -func NewLifeCycleHandler(snapshotter Snapshotter, storer Storer) *LifeCycleHandler { +func NewLifeCycleHandler(recorder eventrecord.EventRecorderLogger, snapshotter Snapshotter, storer Storer) *LifeCycleHandler { return &LifeCycleHandler{ + recorder: recorder, snapshotter: snapshotter, storer: storer, } @@ -131,10 +134,12 @@ func (h LifeCycleHandler) Handle(ctx context.Context, vmSnapshot *virtv2.Virtual virtualMachineReadyCondition, _ := conditions.GetCondition(vmscondition.VirtualMachineReadyType, vmSnapshot.Status.Conditions) if vm == nil || virtualMachineReadyCondition.Status != metav1.ConditionTrue { vmSnapshot.Status.Phase = virtv2.VirtualMachineSnapshotPhasePending + msg := fmt.Sprintf("Waiting for the virtual machine %q to be ready for snapshotting.", vmSnapshot.Spec.VirtualMachineName) + h.recorder.Event(vmSnapshot, corev1.EventTypeWarning, virtv2.ReasonVMSnapshottingPending, msg) cb. Status(metav1.ConditionFalse). Reason(vmscondition.WaitingForTheVirtualMachine). - Message(fmt.Sprintf("Waiting for the virtual machine %q to be ready for snapshotting.", vmSnapshot.Spec.VirtualMachineName)) + Message(msg) return reconcile.Result{}, nil } diff --git a/images/virtualization-artifact/pkg/controller/vmsnapshot/internal/life_cycle_test.go b/images/virtualization-artifact/pkg/controller/vmsnapshot/internal/life_cycle_test.go index 1d8acebca..8fab656b7 100644 --- a/images/virtualization-artifact/pkg/controller/vmsnapshot/internal/life_cycle_test.go +++ b/images/virtualization-artifact/pkg/controller/vmsnapshot/internal/life_cycle_test.go @@ -27,6 +27,7 @@ import ( "k8s.io/utils/ptr" "github.com/deckhouse/virtualization-controller/pkg/controller/conditions" + "github.com/deckhouse/virtualization-controller/pkg/eventrecord" virtv2 "github.com/deckhouse/virtualization/api/core/v1alpha2" "github.com/deckhouse/virtualization/api/core/v1alpha2/vdcondition" "github.com/deckhouse/virtualization/api/core/v1alpha2/vmcondition" @@ -34,6 +35,7 @@ import ( ) var _ = Describe("LifeCycle handler", func() { + var recorder eventrecord.EventRecorderLogger var snapshotter *SnapshotterMock var storer *StorerMock var vd *virtv2.VirtualDisk @@ -143,6 +145,8 @@ var _ = Describe("LifeCycle handler", func() { return vdSnapshot, nil }, } + + recorder = &eventrecord.EventRecorderLoggerImpl{} }) Context("The block devices of the virtual machine are not in the consistent state", func() { @@ -154,7 +158,7 @@ var _ = Describe("LifeCycle handler", func() { conditions.SetCondition(cb, &vm.Status.Conditions) return vm, nil } - h := NewLifeCycleHandler(snapshotter, storer) + h := NewLifeCycleHandler(recorder, snapshotter, storer) _, err := h.Handle(testContext(), vmSnapshot) Expect(err).To(BeNil()) @@ -170,7 +174,7 @@ var _ = Describe("LifeCycle handler", func() { vd.Status.Phase = virtv2.DiskPending return vd, nil } - h := NewLifeCycleHandler(snapshotter, storer) + h := NewLifeCycleHandler(recorder, snapshotter, storer) _, err := h.Handle(testContext(), vmSnapshot) Expect(err).To(BeNil()) @@ -189,7 +193,7 @@ var _ = Describe("LifeCycle handler", func() { conditions.SetCondition(cb, &vd.Status.Conditions) return vd, nil } - h := NewLifeCycleHandler(snapshotter, storer) + h := NewLifeCycleHandler(recorder, snapshotter, storer) _, err := h.Handle(testContext(), vmSnapshot) Expect(err).To(BeNil()) @@ -209,7 +213,7 @@ var _ = Describe("LifeCycle handler", func() { conditions.SetCondition(cb, &vd.Status.Conditions) return vd, nil } - h := NewLifeCycleHandler(snapshotter, storer) + h := NewLifeCycleHandler(recorder, snapshotter, storer) _, err := h.Handle(testContext(), vmSnapshot) Expect(err).To(BeNil()) @@ -228,7 +232,7 @@ var _ = Describe("LifeCycle handler", func() { return vm, nil } - h := NewLifeCycleHandler(snapshotter, storer) + h := NewLifeCycleHandler(recorder, snapshotter, storer) _, err := h.Handle(testContext(), vmSnapshot) Expect(err).To(BeNil()) @@ -247,7 +251,7 @@ var _ = Describe("LifeCycle handler", func() { return false } - h := NewLifeCycleHandler(snapshotter, storer) + h := NewLifeCycleHandler(recorder, snapshotter, storer) _, err := h.Handle(testContext(), vmSnapshot) Expect(err).To(BeNil()) @@ -269,7 +273,7 @@ var _ = Describe("LifeCycle handler", func() { return nil } - h := NewLifeCycleHandler(snapshotter, storer) + h := NewLifeCycleHandler(recorder, snapshotter, storer) _, err := h.Handle(testContext(), vmSnapshot) Expect(err).To(BeNil()) @@ -287,7 +291,7 @@ var _ = Describe("LifeCycle handler", func() { }) It("The snapshot of virtual machine is Ready", func() { - h := NewLifeCycleHandler(snapshotter, storer) + h := NewLifeCycleHandler(recorder, snapshotter, storer) _, err := h.Handle(testContext(), vmSnapshot) Expect(err).To(BeNil()) @@ -301,7 +305,7 @@ var _ = Describe("LifeCycle handler", func() { }) It("The snapshot of running virtual machine is consistent", func() { - h := NewLifeCycleHandler(snapshotter, storer) + h := NewLifeCycleHandler(recorder, snapshotter, storer) _, err := h.Handle(testContext(), vmSnapshot) Expect(err).To(BeNil()) @@ -313,7 +317,7 @@ var _ = Describe("LifeCycle handler", func() { vm.Status.Phase = virtv2.MachineStopped return vm, nil } - h := NewLifeCycleHandler(snapshotter, storer) + h := NewLifeCycleHandler(recorder, snapshotter, storer) _, err := h.Handle(testContext(), vmSnapshot) Expect(err).To(BeNil()) @@ -326,7 +330,7 @@ var _ = Describe("LifeCycle handler", func() { vdSnapshot.Status.Consistent = nil return vdSnapshot, nil } - h := NewLifeCycleHandler(snapshotter, storer) + h := NewLifeCycleHandler(recorder, snapshotter, storer) _, err := h.Handle(testContext(), vmSnapshot) Expect(err).To(BeNil()) diff --git a/images/virtualization-artifact/pkg/controller/vmsnapshot/vmsnapshot_controller.go b/images/virtualization-artifact/pkg/controller/vmsnapshot/vmsnapshot_controller.go index 052ef353b..9ddaaea50 100644 --- a/images/virtualization-artifact/pkg/controller/vmsnapshot/vmsnapshot_controller.go +++ b/images/virtualization-artifact/pkg/controller/vmsnapshot/vmsnapshot_controller.go @@ -29,6 +29,7 @@ import ( "github.com/deckhouse/virtualization-controller/pkg/controller/service" "github.com/deckhouse/virtualization-controller/pkg/controller/service/restorer" "github.com/deckhouse/virtualization-controller/pkg/controller/vmsnapshot/internal" + "github.com/deckhouse/virtualization-controller/pkg/eventrecord" "github.com/deckhouse/virtualization-controller/pkg/logger" "github.com/deckhouse/virtualization/api/client/kubeclient" virtv2 "github.com/deckhouse/virtualization/api/core/v1alpha2" @@ -43,12 +44,13 @@ func NewController( virtClient kubeclient.Client, ) error { protection := service.NewProtectionService(mgr.GetClient(), virtv2.FinalizerVMSnapshotProtection) + recorder := eventrecord.NewEventRecorderLogger(mgr, ControllerName) snapshotter := service.NewSnapshotService(virtClient, mgr.GetClient(), protection) reconciler := NewReconciler( mgr.GetClient(), internal.NewVirtualMachineReadyHandler(snapshotter), - internal.NewLifeCycleHandler(snapshotter, restorer.NewSecretRestorer(mgr.GetClient())), + internal.NewLifeCycleHandler(recorder, snapshotter, restorer.NewSecretRestorer(mgr.GetClient())), ) vmSnapshotController, err := controller.New(ControllerName, mgr, controller.Options{ diff --git a/images/virtualization-artifact/pkg/controller/vmsnapshot/vmsnapshot_reconciler.go b/images/virtualization-artifact/pkg/controller/vmsnapshot/vmsnapshot_reconciler.go index 647091807..6a0f49078 100644 --- a/images/virtualization-artifact/pkg/controller/vmsnapshot/vmsnapshot_reconciler.go +++ b/images/virtualization-artifact/pkg/controller/vmsnapshot/vmsnapshot_reconciler.go @@ -105,7 +105,7 @@ func (r *Reconciler) SetupController(_ context.Context, mgr manager.Manager, ctr } { err := w.Watch(mgr, ctr) if err != nil { - return fmt.Errorf("faield to run watcher %s: %w", reflect.TypeOf(w).Elem().Name(), err) + return fmt.Errorf("failed to run watcher %s: %w", reflect.TypeOf(w).Elem().Name(), err) } }