-
Notifications
You must be signed in to change notification settings - Fork 3
feat(vmclass): add defaultCoreFraction field #1783
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 5 commits
a958594
0bbab48
620ec37
364306e
e785adb
5dbde72
c78a89a
88e1eba
8aaa8e9
a0a7e77
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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 |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| /* | ||
| Copyright 2025 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 defaulter | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
|
|
||
| "k8s.io/apimachinery/pkg/types" | ||
| "sigs.k8s.io/controller-runtime/pkg/client" | ||
|
|
||
| "github.com/deckhouse/virtualization/api/core/v1alpha2" | ||
| "github.com/deckhouse/virtualization/api/core/v1alpha3" | ||
| ) | ||
|
|
||
| type CoreFractionDefaulter struct { | ||
| client client.Client | ||
| } | ||
|
|
||
| func NewCoreFractionDefaulter(client client.Client) *CoreFractionDefaulter { | ||
| return &CoreFractionDefaulter{ | ||
| client: client, | ||
| } | ||
| } | ||
|
|
||
| func (d *CoreFractionDefaulter) Default(ctx context.Context, vm *v1alpha2.VirtualMachine) error { | ||
| // Skip if coreFraction is already set. | ||
| if vm.Spec.CPU.CoreFraction != "" { | ||
| return nil | ||
| } | ||
|
|
||
| // Skip if vmClassName is not set (will be handled by validation later). | ||
| if vm.Spec.VirtualMachineClassName == "" { | ||
| return nil | ||
| } | ||
|
|
||
| // Get the VMClass. | ||
| vmClass := &v1alpha3.VirtualMachineClass{} | ||
| err := d.client.Get(ctx, types.NamespacedName{Name: vm.Spec.VirtualMachineClassName}, vmClass) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to get VirtualMachineClass %q: %w", vm.Spec.VirtualMachineClassName, err) | ||
| } | ||
|
|
||
| // Find the matching sizing policy based on CPU cores. | ||
| defaultCoreFraction := d.getDefaultCoreFraction(vm, vmClass) | ||
| if defaultCoreFraction != "" { | ||
| vm.Spec.CPU.CoreFraction = defaultCoreFraction | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| // getDefaultCoreFraction finds the default core fraction from the VMClass sizing policy | ||
| // that matches the VM's CPU cores count. | ||
| func (d *CoreFractionDefaulter) getDefaultCoreFraction(vm *v1alpha2.VirtualMachine, vmClass *v1alpha3.VirtualMachineClass) string { | ||
| const defaultValue = "100%" | ||
|
|
||
| if vmClass == nil || len(vmClass.Spec.SizingPolicies) == 0 { | ||
| return defaultValue | ||
| } | ||
|
|
||
| for _, sp := range vmClass.Spec.SizingPolicies { | ||
| if sp.Cores == nil { | ||
| continue | ||
| } | ||
|
|
||
| // Check if VM's cores fall within this policy's range. | ||
| if vm.Spec.CPU.Cores >= sp.Cores.Min && vm.Spec.CPU.Cores <= sp.Cores.Max { | ||
| if sp.DefaultCoreFraction != nil { | ||
| return string(*sp.DefaultCoreFraction) | ||
| } | ||
| return defaultValue | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. defaultValue may not be included in .vmClass.coreFractions. It looks like we should return an error here. |
||
| } | ||
| } | ||
|
|
||
| return defaultValue | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| /* | ||
| 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" | ||
| "fmt" | ||
| "slices" | ||
|
|
||
| "sigs.k8s.io/controller-runtime/pkg/webhook/admission" | ||
|
|
||
| "github.com/deckhouse/virtualization/api/core/v1alpha2" | ||
| ) | ||
|
|
||
| type DefaultCoreFractionValidator struct{} | ||
|
|
||
| func NewDefaultCoreFractionValidator() *DefaultCoreFractionValidator { | ||
| return &DefaultCoreFractionValidator{} | ||
| } | ||
|
|
||
| func (v *DefaultCoreFractionValidator) ValidateCreate(_ context.Context, vmclass *v1alpha2.VirtualMachineClass) (admission.Warnings, error) { | ||
| return nil, v.validate(vmclass) | ||
| } | ||
|
|
||
| func (v *DefaultCoreFractionValidator) ValidateUpdate(_ context.Context, _, newVMClass *v1alpha2.VirtualMachineClass) (admission.Warnings, error) { | ||
| return nil, v.validate(newVMClass) | ||
| } | ||
|
|
||
| func (v *DefaultCoreFractionValidator) validate(vmclass *v1alpha2.VirtualMachineClass) error { | ||
| for i, policy := range vmclass.Spec.SizingPolicies { | ||
| if policy.DefaultCoreFraction == nil { | ||
| continue | ||
| } | ||
|
|
||
| if len(policy.CoreFractions) == 0 { | ||
| continue | ||
| } | ||
|
|
||
| if !slices.Contains(policy.CoreFractions, *policy.DefaultCoreFraction) { | ||
| return fmt.Errorf("vmclass %s sizingPolicy[%d]: defaultCoreFraction %d is not in the allowed coreFractions list %v", | ||
| vmclass.Name, i, *policy.DefaultCoreFraction, policy.CoreFractions) | ||
| } | ||
| } | ||
|
|
||
| return nil | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.