|
| 1 | +/* |
| 2 | +Copyright 2025 Flant JSC |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package defaulter |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "fmt" |
| 22 | + |
| 23 | + "k8s.io/apimachinery/pkg/types" |
| 24 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 25 | + |
| 26 | + "github.com/deckhouse/virtualization/api/core/v1alpha2" |
| 27 | + "github.com/deckhouse/virtualization/api/core/v1alpha3" |
| 28 | +) |
| 29 | + |
| 30 | +type CoreFractionDefaulter struct { |
| 31 | + client client.Client |
| 32 | +} |
| 33 | + |
| 34 | +func NewCoreFractionDefaulter(client client.Client) *CoreFractionDefaulter { |
| 35 | + return &CoreFractionDefaulter{ |
| 36 | + client: client, |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +func (d *CoreFractionDefaulter) Default(ctx context.Context, vm *v1alpha2.VirtualMachine) error { |
| 41 | + // Skip if coreFraction is already set. |
| 42 | + if vm.Spec.CPU.CoreFraction != "" { |
| 43 | + return nil |
| 44 | + } |
| 45 | + |
| 46 | + // Skip if vmClassName is not set (will be handled by validation later). |
| 47 | + if vm.Spec.VirtualMachineClassName == "" { |
| 48 | + return nil |
| 49 | + } |
| 50 | + |
| 51 | + // Get the VMClass. |
| 52 | + vmClass := &v1alpha3.VirtualMachineClass{} |
| 53 | + err := d.client.Get(ctx, types.NamespacedName{Name: vm.Spec.VirtualMachineClassName}, vmClass) |
| 54 | + if err != nil { |
| 55 | + return fmt.Errorf("failed to get VirtualMachineClass %q: %w", vm.Spec.VirtualMachineClassName, err) |
| 56 | + } |
| 57 | + |
| 58 | + // Find the matching sizing policy based on CPU cores. |
| 59 | + defaultCoreFraction := d.getDefaultCoreFraction(vm, vmClass) |
| 60 | + if defaultCoreFraction != "" { |
| 61 | + vm.Spec.CPU.CoreFraction = defaultCoreFraction |
| 62 | + } |
| 63 | + |
| 64 | + return nil |
| 65 | +} |
| 66 | + |
| 67 | +// getDefaultCoreFraction finds the default core fraction from the VMClass sizing policy |
| 68 | +// that matches the VM's CPU cores count. |
| 69 | +func (d *CoreFractionDefaulter) getDefaultCoreFraction(vm *v1alpha2.VirtualMachine, vmClass *v1alpha3.VirtualMachineClass) string { |
| 70 | + const defaultValue = "100%" |
| 71 | + |
| 72 | + if vmClass == nil || len(vmClass.Spec.SizingPolicies) == 0 { |
| 73 | + return defaultValue |
| 74 | + } |
| 75 | + |
| 76 | + for _, sp := range vmClass.Spec.SizingPolicies { |
| 77 | + if sp.Cores == nil { |
| 78 | + continue |
| 79 | + } |
| 80 | + |
| 81 | + // Check if VM's cores fall within this policy's range. |
| 82 | + 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 | + } |
| 86 | + return defaultValue |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + return defaultValue |
| 91 | +} |
0 commit comments