Skip to content

Commit

Permalink
refactor(vd): events (#577)
Browse files Browse the repository at this point in the history
* feat(vd): add events

Signed-off-by: Daniil Antoshin <[email protected]>
  • Loading branch information
danilrwx authored Dec 26, 2024
1 parent bc389b6 commit 008c2ee
Show file tree
Hide file tree
Showing 18 changed files with 305 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,30 @@ import (
"errors"
"fmt"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/reconcile"

"github.com/deckhouse/virtualization-controller/pkg/controller/conditions"
"github.com/deckhouse/virtualization-controller/pkg/controller/service"
"github.com/deckhouse/virtualization-controller/pkg/controller/vd/internal/source"
"github.com/deckhouse/virtualization-controller/pkg/eventrecord"
"github.com/deckhouse/virtualization/api/core/v1alpha2"
virtv2 "github.com/deckhouse/virtualization/api/core/v1alpha2"
"github.com/deckhouse/virtualization/api/core/v1alpha2/vdcondition"
)

type DatasourceReadyHandler struct {
sources Sources
blank source.Handler
sources Sources
blank source.Handler
recorder eventrecord.EventRecorderLogger
}

func NewDatasourceReadyHandler(blank source.Handler, sources Sources) *DatasourceReadyHandler {
func NewDatasourceReadyHandler(recorder eventrecord.EventRecorderLogger, blank source.Handler, sources Sources) *DatasourceReadyHandler {
return &DatasourceReadyHandler{
blank: blank,
sources: sources,
blank: blank,
sources: sources,
recorder: recorder,
}
}

Expand Down Expand Up @@ -76,6 +81,13 @@ func (h DatasourceReadyHandler) Handle(ctx context.Context, vd *virtv2.VirtualDi
Message("")
return reconcile.Result{}, nil
case errors.Is(err, source.ErrSecretNotFound):
h.recorder.Event(
vd,
corev1.EventTypeNormal,
v1alpha2.ReasonVDContainerRegistrySecretNotFound,
"Container registry secret not found",
)

cb.
Status(metav1.ConditionFalse).
Reason(vdcondition.ContainerRegistrySecretNotFound).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@ import (

"github.com/stretchr/testify/require"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/deckhouse/virtualization-controller/pkg/controller/conditions"
"github.com/deckhouse/virtualization-controller/pkg/controller/vd/internal/source"
"github.com/deckhouse/virtualization-controller/pkg/eventrecord"
virtv2 "github.com/deckhouse/virtualization/api/core/v1alpha2"
"github.com/deckhouse/virtualization/api/core/v1alpha2/vdcondition"
)
Expand All @@ -41,6 +43,9 @@ func TestDatasourceReadyHandler_Handle(t *testing.T) {
return blank, true
},
}
recorder := &eventrecord.EventRecorderLoggerMock{
EventFunc: func(_ client.Object, _, _, _ string) {},
}

t.Run("VirtualDisk with DeletionTimestamp", func(t *testing.T) {
vd := virtv2.VirtualDisk{
Expand All @@ -49,7 +54,7 @@ func TestDatasourceReadyHandler_Handle(t *testing.T) {
},
}

handler := NewDatasourceReadyHandler(nil, nil)
handler := NewDatasourceReadyHandler(recorder, nil, nil)
_, err := handler.Handle(ctx, &vd)
require.NoError(t, err)

Expand All @@ -62,7 +67,7 @@ func TestDatasourceReadyHandler_Handle(t *testing.T) {
t.Run("VirtualDisk with Blank DataSource", func(t *testing.T) {
vd := virtv2.VirtualDisk{}

handler := NewDatasourceReadyHandler(blank, nil)
handler := NewDatasourceReadyHandler(recorder, blank, nil)
_, err := handler.Handle(ctx, &vd)
require.NoError(t, err)

Expand All @@ -81,7 +86,7 @@ func TestDatasourceReadyHandler_Handle(t *testing.T) {
},
}

handler := NewDatasourceReadyHandler(nil, sources)
handler := NewDatasourceReadyHandler(recorder, nil, sources)
_, err := handler.Handle(ctx, &vd)
require.NoError(t, err)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,34 +20,36 @@ import (
"context"
"fmt"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/reconcile"

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

type LifeCycleHandler struct {
client client.Client
blank source.Handler
sources Sources
client client.Client
blank source.Handler
sources Sources
recorder eventrecord.EventRecorderLogger
}

func NewLifeCycleHandler(blank source.Handler, sources Sources, client client.Client) *LifeCycleHandler {
func NewLifeCycleHandler(recorder eventrecord.EventRecorderLogger, blank source.Handler, sources Sources, client client.Client) *LifeCycleHandler {
return &LifeCycleHandler{
client: client,
blank: blank,
sources: sources,
client: client,
blank: blank,
sources: sources,
recorder: recorder,
}
}

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

readyCondition, ok := conditions.GetCondition(vdcondition.ReadyType, vd.Status.Conditions)
if !ok {
readyCondition = metav1.Condition{
Expand Down Expand Up @@ -81,7 +83,12 @@ func (h LifeCycleHandler) Handle(ctx context.Context, vd *virtv2.VirtualDisk) (r
}

if readyCondition.Status != metav1.ConditionTrue && readyCondition.Reason != vdcondition.Lost.String() && h.sources.Changed(ctx, vd) {
log.Info("Spec changes are detected: restart import process")
h.recorder.Event(
vd,
corev1.EventTypeNormal,
v1alpha2.ReasonVDStorageClassWasDeleted,
"Spec changes are detected: restart import process",
)

vd.Status = virtv2.VirtualDiskStatus{
Phase: virtv2.DiskPending,
Expand Down Expand Up @@ -114,7 +121,12 @@ func (h LifeCycleHandler) Handle(ctx context.Context, vd *virtv2.VirtualDisk) (r
}

if readyCondition.Status != metav1.ConditionTrue && storageClassReadyCondition.Status != metav1.ConditionTrue && vd.Status.StorageClassName != "" {
log.Info("Storage class was deleted while population")
h.recorder.Event(
vd,
corev1.EventTypeNormal,
v1alpha2.ReasonVDStorageClassWasDeleted,
"Storage class was deleted while population",
)

vd.Status = virtv2.VirtualDiskStatus{
Phase: virtv2.DiskPending,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/reconcile"

"github.com/deckhouse/virtualization-controller/pkg/controller/conditions"
"github.com/deckhouse/virtualization-controller/pkg/controller/vd/internal/source"
"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 Down Expand Up @@ -80,7 +82,10 @@ var _ = Describe("LifeCycleHandler Run", func() {
return &handler, false
}

handler := NewLifeCycleHandler(nil, &sourcesMock, nil)
recorder := &eventrecord.EventRecorderLoggerMock{
EventFunc: func(_ client.Object, _, _, _ string) {},
}
handler := NewLifeCycleHandler(recorder, nil, &sourcesMock, nil)

ctx := logger.ToContext(context.TODO(), slog.Default())

Expand Down Expand Up @@ -179,7 +184,11 @@ var _ = Describe("LifeCycleHandler Run", func() {
return &handler, false
}

handler := NewLifeCycleHandler(nil, &sourcesMock, nil)
recorder := &eventrecord.EventRecorderLoggerMock{
EventFunc: func(_ client.Object, _, _, _ string) {},
}

handler := NewLifeCycleHandler(recorder, nil, &sourcesMock, nil)

ctx := logger.ToContext(context.TODO(), slog.Default())

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,22 @@ import (
"github.com/deckhouse/virtualization-controller/pkg/controller/conditions"
"github.com/deckhouse/virtualization-controller/pkg/controller/service"
"github.com/deckhouse/virtualization-controller/pkg/controller/supplements"
"github.com/deckhouse/virtualization-controller/pkg/eventrecord"
"github.com/deckhouse/virtualization-controller/pkg/logger"
"github.com/deckhouse/virtualization/api/core/v1alpha2"
virtv2 "github.com/deckhouse/virtualization/api/core/v1alpha2"
"github.com/deckhouse/virtualization/api/core/v1alpha2/vdcondition"
)

type ResizingHandler struct {
diskService DiskService
recorder eventrecord.EventRecorderLogger
}

func NewResizingHandler(diskService DiskService) *ResizingHandler {
func NewResizingHandler(recorder eventrecord.EventRecorderLogger, diskService DiskService) *ResizingHandler {
return &ResizingHandler{
diskService: diskService,
recorder: recorder,
}
}

Expand Down Expand Up @@ -119,6 +123,13 @@ func (h ResizingHandler) Handle(ctx context.Context, vd *virtv2.VirtualDisk) (re
if vdSpecSize != nil && vdSpecSize.Cmp(pvcSpecSize) == common.CmpGreater {
snapshotting, _ := conditions.GetCondition(vdcondition.SnapshottingType, vd.Status.Conditions)
if snapshotting.Status == metav1.ConditionTrue {
h.recorder.Event(
vd,
corev1.EventTypeNormal,
v1alpha2.ReasonVDResizingNotAvailable,
"The virtual disk cannot be selected for resizing as it is currently snapshotting.",
)

cb.
Status(metav1.ConditionFalse).
Reason(vdcondition.ResizingNotAvailable).
Expand All @@ -139,6 +150,14 @@ func (h ResizingHandler) Handle(ctx context.Context, vd *virtv2.VirtualDisk) (re
}
return reconcile.Result{}, err
}

h.recorder.Event(
vd,
corev1.EventTypeNormal,
v1alpha2.ReasonVDResizingStarted,
"The virtual disk resizing has started",
)

case metav1.ConditionFalse:
cb.
Status(metav1.ConditionFalse).
Expand All @@ -164,6 +183,13 @@ func (h ResizingHandler) Handle(ctx context.Context, vd *virtv2.VirtualDisk) (re
// Expected disk size is NOT GREATER THAN expected pvc size: no resize needed since downsizing is not possible, and resizing to the same value makes no sense.
switch condition.Reason {
case vdcondition.InProgress.String(), vdcondition.Resized.String():
h.recorder.Event(
vd,
corev1.EventTypeNormal,
v1alpha2.ReasonVDResizingCompleted,
"The virtual disk resizing has completed",
)

cb.
Status(metav1.ConditionFalse).
Reason(vdcondition.Resized).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@ import (
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/deckhouse/virtualization-controller/pkg/controller/conditions"
"github.com/deckhouse/virtualization-controller/pkg/controller/supplements"
"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 Down Expand Up @@ -88,6 +90,10 @@ var _ = Describe("Resizing handler Run", func() {
}
})

recorder := &eventrecord.EventRecorderLoggerMock{
EventFunc: func(_ client.Object, _, _, _ string) {},
}

It("Resizing is in progress", func() {
vd.Spec.PersistentVolumeClaim.Size = nil
diskService.GetPersistentVolumeClaimFunc = func(ctx context.Context, sup *supplements.Generator) (*corev1.PersistentVolumeClaim, error) {
Expand All @@ -100,7 +106,7 @@ var _ = Describe("Resizing handler Run", func() {
return pvc, nil
}

h := NewResizingHandler(diskService)
h := NewResizingHandler(recorder, diskService)

_, err := h.Handle(testContext(), vd)
Expect(err).To(BeNil())
Expand All @@ -112,7 +118,7 @@ var _ = Describe("Resizing handler Run", func() {
It("Resize is not requested (vd.spec.size == nil)", func() {
vd.Spec.PersistentVolumeClaim.Size = nil

h := NewResizingHandler(diskService)
h := NewResizingHandler(recorder, diskService)

_, err := h.Handle(testContext(), vd)
Expect(err).To(BeNil())
Expand All @@ -124,7 +130,7 @@ var _ = Describe("Resizing handler Run", func() {
It("Resize is not requested (vd.spec.size < pvc.spec.size)", func() {
vd.Spec.PersistentVolumeClaim.Size.Sub(resource.MustParse("1G"))

h := NewResizingHandler(diskService)
h := NewResizingHandler(recorder, diskService)

_, err := h.Handle(testContext(), vd)
Expect(err).To(BeNil())
Expand All @@ -134,7 +140,7 @@ var _ = Describe("Resizing handler Run", func() {
})

It("Resize is not requested (vd.spec.size == pvc.spec.size)", func() {
h := NewResizingHandler(diskService)
h := NewResizingHandler(recorder, diskService)

_, err := h.Handle(testContext(), vd)
Expect(err).To(BeNil())
Expand All @@ -146,7 +152,7 @@ var _ = Describe("Resizing handler Run", func() {
It("Resize has started (vd.spec.size > pvc.spec.size)", func() {
vd.Spec.PersistentVolumeClaim.Size.Add(size)

h := NewResizingHandler(diskService)
h := NewResizingHandler(recorder, diskService)

_, err := h.Handle(testContext(), vd)
Expect(err).To(BeNil())
Expand All @@ -162,7 +168,7 @@ var _ = Describe("Resizing handler Run", func() {
Reason: vdcondition.InProgress.String(),
})

h := NewResizingHandler(diskService)
h := NewResizingHandler(recorder, diskService)

_, err := h.Handle(testContext(), vd)
Expect(err).To(BeNil())
Expand Down
Loading

0 comments on commit 008c2ee

Please sign in to comment.