Skip to content

Commit

Permalink
add events
Browse files Browse the repository at this point in the history
Signed-off-by: Valeriy Khorunzhin <[email protected]>
  • Loading branch information
Valeriy Khorunzhin committed Dec 24, 2024
1 parent b08ac10 commit 40ac6c9
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 11 deletions.
9 changes: 9 additions & 0 deletions api/core/v1alpha2/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,13 @@ const (

// ReasonVMClassInUse is event reason that VMClass is used by virtual machine.
ReasonVMClassInUse = "VirtualMachineClassInUse"

// ReasonVMClassSizingPoliciesWasChanged is event reason that VMClass sizing policies was changed.
ReasonVMClassSizingPoliciesWasChanged = "SizingPoliciesWasChanged"

// ReasonVMClassAvailableNodesListEmpty is event reason that VMClass has no available nodes.
ReasonVMClassAvailableNodesListEmpty = "AvailableNodesListEmpty"

// ReasonVMClassAvailableNodesWasUpdated is event reason that VMClass available nodes list was updated.
ReasonVMClassAvailableNodesWasUpdated = "AvailableNodesWasUpdated"
)
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,20 @@ import (
"time"

corev1 "k8s.io/api/core/v1"
"k8s.io/client-go/tools/record"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/reconcile"

"github.com/deckhouse/deckhouse/pkg/log"
"github.com/deckhouse/virtualization-controller/pkg/common/object"
"github.com/deckhouse/virtualization-controller/pkg/controller/vmclass/internal/state"
"github.com/deckhouse/virtualization-controller/pkg/eventrecord"
virtv2 "github.com/deckhouse/virtualization/api/core/v1alpha2"
)

const nameDeletionHandler = "DeletionHandler"

func NewDeletionHandler(client client.Client, recorder record.EventRecorder, logger *log.Logger) *DeletionHandler {
func NewDeletionHandler(client client.Client, recorder eventrecord.EventRecorderLogger, logger *log.Logger) *DeletionHandler {
return &DeletionHandler{
client: client,
recorder: recorder,
Expand All @@ -45,7 +45,7 @@ func NewDeletionHandler(client client.Client, recorder record.EventRecorder, log

type DeletionHandler struct {
client client.Client
recorder record.EventRecorder
recorder eventrecord.EventRecorderLogger
logger *log.Logger
}

Expand All @@ -64,7 +64,6 @@ func (h *DeletionHandler) Handle(ctx context.Context, s state.VirtualMachineClas
}
if len(vms) > 0 {
msg := fmt.Sprintf("VirtualMachineClass cannot be deleted, there are VMs that use it. %s...", object.NamespacedName(&vms[0]))
h.logger.Info(msg)
h.recorder.Event(changed, corev1.EventTypeWarning, virtv2.ReasonVMClassInUse, msg)
return reconcile.Result{RequeueAfter: 60 * time.Second}, nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,22 @@ import (

"github.com/deckhouse/virtualization-controller/pkg/controller/conditions"
"github.com/deckhouse/virtualization-controller/pkg/controller/vmclass/internal/state"
"github.com/deckhouse/virtualization-controller/pkg/eventrecord"
virtv2 "github.com/deckhouse/virtualization/api/core/v1alpha2"
"github.com/deckhouse/virtualization/api/core/v1alpha2/vmclasscondition"
)

const nameDiscoveryHandler = "DiscoveryHandler"

func NewDiscoveryHandler() *DiscoveryHandler {
return &DiscoveryHandler{}
func NewDiscoveryHandler(recorder eventrecord.EventRecorderLogger) *DiscoveryHandler {
return &DiscoveryHandler{
recorder: recorder,
}
}

type DiscoveryHandler struct{}
type DiscoveryHandler struct {
recorder eventrecord.EventRecorderLogger
}

func (h *DiscoveryHandler) Handle(ctx context.Context, s state.VirtualMachineClassState) (reconcile.Result, error) {
if s.VirtualMachineClass().IsEmpty() {
Expand Down Expand Up @@ -120,6 +125,28 @@ func (h *DiscoveryHandler) Handle(ctx context.Context, s state.VirtualMachineCla
sort.Strings(featuresEnabled)
sort.Strings(featuresNotEnabled)

addedNodes, removedNodes := nodeNamesDiff(current.Status.AvailableNodes, availableNodeNames)
if len(addedNodes) > 0 || len(removedNodes) > 0 {
if len(availableNodes) > 0 {
h.recorder.Eventf(
changed,
corev1.EventTypeNormal,
virtv2.ReasonVMClassAvailableNodesWasUpdated,
"List of available nodes was updated, added nodes: %q, removed nodes: %q",
addedNodes,
removedNodes,
)
} else {
h.recorder.Eventf(
changed,
corev1.EventTypeWarning,
virtv2.ReasonVMClassAvailableNodesListEmpty,
"List of available nodes was updated, now it's empty, removed nodes: %q",
removedNodes,
)
}
}

changed.Status.AvailableNodes = availableNodeNames
changed.Status.MaxAllocatableResources = h.maxAllocatableResources(availableNodes)
changed.Status.CpuFeatures = virtv2.CpuFeatures{
Expand Down Expand Up @@ -175,3 +202,30 @@ func (h *DiscoveryHandler) maxAllocatableResources(nodes []corev1.Node) corev1.R
}
return resourceList
}

func nodeNamesDiff(prev, current []string) (added, removed []string) {
prevMap := make(map[string]struct{})
currentMap := make(map[string]struct{})

for _, n := range prev {
prevMap[n] = struct{}{}
}

for _, n := range current {
currentMap[n] = struct{}{}
}

for _, n := range prev {
if _, ok := currentMap[n]; !ok {
removed = append(removed, n)
}
}

for _, n := range current {
if _, ok := prevMap[n]; !ok {
added = append(added, n)
}
}

return added, removed
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
Copyright 2024 Flant JSC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package validators

import (
"context"
"reflect"

corev1 "k8s.io/api/core/v1"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"

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

type PolicyChangesValidator struct {
recorder eventrecord.EventRecorderLogger
}

func NewPolicyChangesValidator(recorder eventrecord.EventRecorderLogger) *PolicyChangesValidator {
return &PolicyChangesValidator{recorder: recorder}
}

func (v *PolicyChangesValidator) ValidateCreate(_ context.Context, _ *v1alpha2.VirtualMachineClass) (admission.Warnings, error) {
return nil, nil
}

func (v *PolicyChangesValidator) ValidateUpdate(_ context.Context, oldVMClass, newVMClass *v1alpha2.VirtualMachineClass) (admission.Warnings, error) {
if !reflect.DeepEqual(oldVMClass.Spec.SizingPolicies, newVMClass.Spec.SizingPolicies) {
v.recorder.Event(newVMClass, corev1.EventTypeNormal, v1alpha2.ReasonVMClassSizingPoliciesWasChanged, "Sizing policies was changed")
}

return nil, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (

"github.com/deckhouse/deckhouse/pkg/log"
"github.com/deckhouse/virtualization-controller/pkg/controller/vmclass/internal"
"github.com/deckhouse/virtualization-controller/pkg/eventrecord"
"github.com/deckhouse/virtualization-controller/pkg/logger"
"github.com/deckhouse/virtualization/api/core/v1alpha2"
)
Expand All @@ -41,11 +42,11 @@ func NewController(
controllerNamespace string,
log *log.Logger,
) (controller.Controller, error) {
recorder := mgr.GetEventRecorderFor(ControllerName)
recorder := eventrecord.NewEventRecorderLogger(mgr, ControllerName)
client := mgr.GetClient()
handlers := []Handler{
internal.NewDeletionHandler(client, recorder, log),
internal.NewDiscoveryHandler(),
internal.NewDiscoveryHandler(recorder),
internal.NewLifeCycleHandler(client),
}
r := NewReconciler(controllerNamespace, client, handlers...)
Expand All @@ -66,7 +67,7 @@ func NewController(

if err = builder.WebhookManagedBy(mgr).
For(&v1alpha2.VirtualMachineClass{}).
WithValidator(NewValidator(mgr.GetClient(), log)).
WithValidator(NewValidator(mgr.GetClient(), log, recorder)).
Complete(); err != nil {
return nil, err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

"github.com/deckhouse/deckhouse/pkg/log"
"github.com/deckhouse/virtualization-controller/pkg/controller/vmclass/internal/validators"
"github.com/deckhouse/virtualization-controller/pkg/eventrecord"
virtv2 "github.com/deckhouse/virtualization/api/core/v1alpha2"
)

Expand All @@ -36,10 +37,11 @@ type Validator struct {
log *log.Logger
}

func NewValidator(client client.Client, log *log.Logger) *Validator {
func NewValidator(client client.Client, log *log.Logger, recorder eventrecord.EventRecorderLogger) *Validator {
return &Validator{
validators: []VirtualMachineClassValidator{
validators.NewSizingPoliciesValidator(client),
validators.NewPolicyChangesValidator(recorder),
},
log: log.With("webhook", "validation"),
}
Expand Down

0 comments on commit 40ac6c9

Please sign in to comment.