Skip to content

Commit

Permalink
api(vmsnapshot): refactor vmsnapshot events
Browse files Browse the repository at this point in the history
Signed-off-by: Roman Sysoev <[email protected]>
  • Loading branch information
Roman Sysoev committed Jan 15, 2025
1 parent a066a9d commit d138ffd
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 15 deletions.
12 changes: 12 additions & 0 deletions api/core/v1alpha2/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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,
}
Expand Down Expand Up @@ -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
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,15 @@ 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"
"github.com/deckhouse/virtualization/api/core/v1alpha2/vmscondition"
)

var _ = Describe("LifeCycle handler", func() {
var recorder eventrecord.EventRecorderLogger
var snapshotter *SnapshotterMock
var storer *StorerMock
var vd *virtv2.VirtualDisk
Expand Down Expand Up @@ -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() {
Expand All @@ -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())
Expand All @@ -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())
Expand All @@ -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())
Expand All @@ -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())
Expand All @@ -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())
Expand All @@ -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())
Expand All @@ -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())
Expand All @@ -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())
Expand All @@ -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())
Expand All @@ -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())
Expand All @@ -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())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down

0 comments on commit d138ffd

Please sign in to comment.