Skip to content

Commit

Permalink
delete condition add reason
Browse files Browse the repository at this point in the history
Signed-off-by: Valeriy Khorunzhin <valeriy.khorunzhin@flant.com>
  • Loading branch information
Valeriy Khorunzhin committed Jan 15, 2025
1 parent b3c5083 commit 37b7d4e
Show file tree
Hide file tree
Showing 8 changed files with 122 additions and 95 deletions.
4 changes: 1 addition & 3 deletions api/core/v1alpha2/vmcondition/condition.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ const (
TypeFilesystemReady Type = "FilesystemReady"
TypeSizingPolicyMatched Type = "SizingPolicyMatched"
TypeSnapshotting Type = "Snapshotting"
TypeDiskAttachmentCapacityAvailable Type = "DiskAttachmentCapacityAvailable"
)

type Reason string
Expand Down Expand Up @@ -102,8 +101,7 @@ const (
ReasonVirtualMachineClassTerminating Reason = "VirtualMachineClassTerminating"
ReasonVirtualMachineClassNotExists Reason = "VirtalMachineClassNotExists"

ReasonBlockDeviceCapacityAvailable Reason = "BlockDeviceCapacityAvailable"
ReasonBlockDeviceCapacityReached Reason = "BlockDeviceCapacityReached"
ReasonBlockDeviceCapacityReached Reason = "BlockDeviceCapacityReached"

ReasonPodTerminatingReason Reason = "PodTerminating"
ReasonPodNotExistsReason Reason = "PodNotExists"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,11 @@ import (

const nameBlockDeviceHandler = "BlockDeviceHandler"

func NewBlockDeviceHandler(cl client.Client, recorder eventrecord.EventRecorderLogger) *BlockDeviceHandler {
func NewBlockDeviceHandler(cl client.Client, recorder eventrecord.EventRecorderLogger, blockDeviceService IBlockDeviceService) *BlockDeviceHandler {
return &BlockDeviceHandler{
client: cl,
recorder: recorder,
service: blockDeviceService,

viProtection: service.NewProtectionService(cl, virtv2.FinalizerVIProtection),
cviProtection: service.NewProtectionService(cl, virtv2.FinalizerCVIProtection),
Expand All @@ -57,6 +58,7 @@ func NewBlockDeviceHandler(cl client.Client, recorder eventrecord.EventRecorderL
type BlockDeviceHandler struct {
client client.Client
recorder eventrecord.EventRecorderLogger
service IBlockDeviceService

viProtection *service.ProtectionService
cviProtection *service.ProtectionService
Expand Down Expand Up @@ -95,6 +97,20 @@ func (h *BlockDeviceHandler) Handle(ctx context.Context, s state.VirtualMachineS
return reconcile.Result{}, fmt.Errorf("unable to add block devices finalizers: %w", err)
}

// Get number of connected block devices
// If it greater limit then set condition to false
blockDeviceAttachedCount, err := h.service.CountBlockDevicesAttachedToVm(ctx, changed)
if err != nil {
return reconcile.Result{}, err
}

if blockDeviceAttachedCount > common.VmBlockDeviceAttachedLimit {
cb.
Status(metav1.ConditionFalse).
Reason(vmcondition.ReasonBlockDeviceCapacityReached).
Message(fmt.Sprintf("Can not attach %d block devices (%d is maximum) to `VirtualMachine` %q", blockDeviceAttachedCount, common.VmBlockDeviceAttachedLimit, changed.Name))
}

// Get hot plugged BlockDeviceRefs from vmbdas.
vmbdaStatusRefs, err := h.getBlockDeviceStatusRefsFromVMBDA(ctx, s)
if err != nil {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package internal

import (
"context"
"log/slog"
"testing"

Expand All @@ -41,10 +42,17 @@ var _ = Describe("func areVirtualDisksAllowedToUse", func() {
var vdFoo *virtv2.VirtualDisk
var vdBar *virtv2.VirtualDisk

blockDevicesConnectedCount := 3

blockDeviceHandlerMock := &IBlockDeviceServiceMock{}
blockDeviceHandlerMock.CountBlockDevicesAttachedToVmFunc = func(_ context.Context, vm *virtv2.VirtualMachine) (int, error) {
return blockDevicesConnectedCount, nil
}

BeforeEach(func() {
h = NewBlockDeviceHandler(nil, &eventrecord.EventRecorderLoggerMock{
EventFunc: func(_ client.Object, _, _, _ string) {},
})
}, blockDeviceHandlerMock)
vdFoo = &virtv2.VirtualDisk{
ObjectMeta: metav1.ObjectMeta{Name: "vd-foo"},
Status: virtv2.VirtualDiskStatus{
Expand Down Expand Up @@ -152,6 +160,13 @@ var _ = Describe("BlockDeviceHandler", func() {
var vdFoo *virtv2.VirtualDisk
var vdBar *virtv2.VirtualDisk

blockDevicesConnectedCount := 3

blockDeviceHandlerMock := &IBlockDeviceServiceMock{}
blockDeviceHandlerMock.CountBlockDevicesAttachedToVmFunc = func(_ context.Context, vm *virtv2.VirtualMachine) (int, error) {
return blockDevicesConnectedCount, nil
}

getBlockDevicesState := func(vi *virtv2.VirtualImage, cvi *virtv2.ClusterVirtualImage, vdFoo, vdBar *virtv2.VirtualDisk) BlockDevicesState {
return BlockDevicesState{
VIByName: map[string]*virtv2.VirtualImage{vi.Name: vi},
Expand All @@ -164,7 +179,7 @@ var _ = Describe("BlockDeviceHandler", func() {
logger = slog.Default()
h = NewBlockDeviceHandler(nil, &eventrecord.EventRecorderLoggerMock{
EventFunc: func(_ client.Object, _, _, _ string) {},
})
}, blockDeviceHandlerMock)
vi = &virtv2.VirtualImage{
ObjectMeta: metav1.ObjectMeta{Name: "vi-01"},
Status: virtv2.VirtualImageStatus{Phase: virtv2.ImageReady},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,18 @@ limitations under the License.

package internal

import "k8s.io/client-go/tools/record"
import (
"context"

//go:generate moq -rm -out mock.go . EventRecorder
"k8s.io/client-go/tools/record"

virtv2 "github.com/deckhouse/virtualization/api/core/v1alpha2"
)

//go:generate moq -rm -out mock.go . EventRecorder IBlockDeviceService

type EventRecorder = record.EventRecorder

type IBlockDeviceService interface {
CountBlockDevicesAttachedToVm(ctx context.Context, vm *virtv2.VirtualMachine) (int, error)
}
74 changes: 74 additions & 0 deletions images/virtualization-artifact/pkg/controller/vm/internal/mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -229,11 +229,6 @@ func (h *SyncKvvmHandler) isWaiting(vm *virtv2.VirtualMachine) bool {
return true
}

case vmcondition.TypeDiskAttachmentCapacityAvailable:
if c.Status != metav1.ConditionTrue {
return true
}

case vmcondition.TypeSizingPolicyMatched:
if c.Status != metav1.ConditionTrue {
return true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ func SetupController(
internal.NewDeletionHandler(client),
internal.NewClassHandler(client, recorder),
internal.NewIPAMHandler(ipam.New(), client, recorder),
internal.NewBlockDeviceHandler(client, recorder),
internal.NewBlockDeviceLimiterHandler(blockDeviceService),
internal.NewBlockDeviceHandler(client, recorder, blockDeviceService),
internal.NewProvisioningHandler(client),
internal.NewAgentHandler(),
internal.NewFilesystemHandler(),
Expand Down

0 comments on commit 37b7d4e

Please sign in to comment.