Skip to content

Commit fb3accb

Browse files
eofffIsteb4k
authored andcommitted
feat(vm): check default value (#1817)
Signed-off-by: Valeriy Khorunzhin <valeriy.khorunzhin@flant.com> (cherry picked from commit 18bb590)
1 parent 6f9cf3d commit fb3accb

File tree

1 file changed

+20
-9
lines changed
  • images/virtualization-artifact/pkg/controller/vm/internal/defaulter

1 file changed

+20
-9
lines changed

images/virtualization-artifact/pkg/controller/vm/internal/defaulter/core_fraction.go

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package defaulter
1919
import (
2020
"context"
2121
"fmt"
22+
"slices"
2223

2324
"k8s.io/apimachinery/pkg/types"
2425
"sigs.k8s.io/controller-runtime/pkg/client"
@@ -56,21 +57,22 @@ func (d *CoreFractionDefaulter) Default(ctx context.Context, vm *v1alpha2.Virtua
5657
}
5758

5859
// Find the matching sizing policy based on CPU cores.
59-
defaultCoreFraction := d.getDefaultCoreFraction(vm, vmClass)
60-
if defaultCoreFraction != "" {
61-
vm.Spec.CPU.CoreFraction = defaultCoreFraction
60+
defaultCoreFraction, err := d.getDefaultCoreFraction(vm, vmClass)
61+
if err != nil {
62+
return err
6263
}
64+
vm.Spec.CPU.CoreFraction = defaultCoreFraction
6365

6466
return nil
6567
}
6668

6769
// getDefaultCoreFraction finds the default core fraction from the VMClass sizing policy
6870
// that matches the VM's CPU cores count.
69-
func (d *CoreFractionDefaulter) getDefaultCoreFraction(vm *v1alpha2.VirtualMachine, vmClass *v1alpha3.VirtualMachineClass) string {
71+
func (d *CoreFractionDefaulter) getDefaultCoreFraction(vm *v1alpha2.VirtualMachine, vmClass *v1alpha3.VirtualMachineClass) (string, error) {
7072
const defaultValue = "100%"
7173

7274
if vmClass == nil || len(vmClass.Spec.SizingPolicies) == 0 {
73-
return defaultValue
75+
return defaultValue, nil
7476
}
7577

7678
for _, sp := range vmClass.Spec.SizingPolicies {
@@ -80,12 +82,21 @@ func (d *CoreFractionDefaulter) getDefaultCoreFraction(vm *v1alpha2.VirtualMachi
8082

8183
// Check if VM's cores fall within this policy's range.
8284
if vm.Spec.CPU.Cores >= sp.Cores.Min && vm.Spec.CPU.Cores <= sp.Cores.Max {
83-
if sp.DefaultCoreFraction != nil {
84-
return string(*sp.DefaultCoreFraction)
85+
switch {
86+
case sp.DefaultCoreFraction != nil:
87+
return string(*sp.DefaultCoreFraction), nil
88+
case len(sp.CoreFractions) > 0 && !slices.Contains(sp.CoreFractions, defaultValue):
89+
return "", fmt.Errorf(
90+
"the default value for core fraction is not defined. For the specified configuration \".spec.cpu.cores %d\", "+
91+
"the following core fractions are allowed: %v. Please specify the \".spec.core.coreFraction\" value and try again",
92+
vm.Spec.CPU.Cores,
93+
sp.CoreFractions,
94+
)
95+
default:
96+
return defaultValue, nil
8597
}
86-
return defaultValue
8798
}
8899
}
89100

90-
return defaultValue
101+
return "", fmt.Errorf("the specified \".spec.cpu.cores %d\" value is not among the sizing policies allowed for the virtual machine", vm.Spec.CPU.Cores)
91102
}

0 commit comments

Comments
 (0)