Skip to content

Commit

Permalink
feat(vmclass): add validation for matching virtual machine sizing pol…
Browse files Browse the repository at this point in the history
…icies upon virtual machine class change (#389)

A validation has been added to ensure virtual machines match the sizing policies when their virtual machine class is changed. The result of this matching check is also added to the conditions field of the virtual machine.
  • Loading branch information
eofff authored Sep 25, 2024
1 parent 43b9074 commit 51f17b2
Show file tree
Hide file tree
Showing 10 changed files with 333 additions and 150 deletions.
6 changes: 6 additions & 0 deletions api/core/v1alpha2/vmcondition/condition.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const (
TypeConfigurationApplied Type = "ConfigurationApplied"
TypeAwaitingRestartToApplyConfiguration Type = "AwaitingRestartToApplyConfiguration"
TypeFilesystemReady Type = "FilesystemReady"
TypeSizingPolicyMatched Type = "SizingPolicyMatched"
)

type Reason string
Expand Down Expand Up @@ -86,4 +87,9 @@ const (
ReasonFilesystemReady Reason = "Ready"
ReasonFilesystemFrozen Reason = "Frozen"
ReasonFilesystemNotReady Reason = "NotReady"

ReasonSizingPolicyMatched Reason = "SizingPolicyMatched"
ReasonSizingPolicyNotMatched Reason = "SizingPolicyNotMatched"
ReasonVirtualMachineClassTerminating Reason = "VirtualMachineClassTerminating"
ReasonVirtualMachineClassNotExists Reason = "VirtalMachineClassNotExists"
)
3 changes: 3 additions & 0 deletions crds/virtualmachines.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -867,6 +867,9 @@ spec:
* EFIWithSecureBoot - use UEFI/EFI with SecureBoot support.
virtualMachineClassName:
type: string
x-kubernetes-validations:
- rule: "size(self) > 0"
message: "The virtualMachineClassName field must not be an empty string."
description: |
Name of the `VirtualMachineClass` resource describing the requirements for a virtual CPU, memory and the resource allocation policy and node placement policies for virtual machines.
cpu:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.21.8-bookworm@sha256:ac14cc827536ef1a124cd2f7a03178c3335c1db8ad3807e7fdd57f74096abfa0 AS builder
FROM golang:1.22.7-bookworm@sha256:027bd04b1d3b4529bf8ccebf62eb7eeeae7b7bef134a68bd419824e929ad93ad AS builder
ARG TARGETOS
ARG TARGETARCH

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,12 @@ limitations under the License.
package service

import (
"context"
"errors"
"fmt"
"strconv"
"strings"

"k8s.io/apimachinery/pkg/api/resource"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/deckhouse/virtualization/api/core/v1alpha2"
)
Expand All @@ -36,23 +33,13 @@ const (
greater = 1
)

type SizePolicyService struct {
client client.Client
}
type SizePolicyService struct{}

func NewSizePolicyService(client client.Client) *SizePolicyService {
return &SizePolicyService{client: client}
func NewSizePolicyService() *SizePolicyService {
return &SizePolicyService{}
}

func (s *SizePolicyService) CheckVMMatchedSizePolicy(ctx context.Context, vm *v1alpha2.VirtualMachine) error {
vmClass := &v1alpha2.VirtualMachineClass{}
err := s.client.Get(ctx, types.NamespacedName{
Name: vm.Spec.VirtualMachineClassName,
}, vmClass)
if err != nil {
return err
}

func (s *SizePolicyService) CheckVMMatchedSizePolicy(vm *v1alpha2.VirtualMachine, vmClass *v1alpha2.VirtualMachineClass) error {
sizePolicy := getVMSizePolicy(vm, vmClass)
if sizePolicy == nil {
return fmt.Errorf(
Expand Down
Loading

0 comments on commit 51f17b2

Please sign in to comment.