-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
a958594
t
0bbab48
t1
620ec37
regenerate
364306e
fix doc
e785adb
resolve
5dbde72
Update crds/doc-ru-virtualmachineclasses.yaml
eofff c78a89a
Update crds/virtualmachineclasses.yaml
eofff 88e1eba
Update crds/virtualmachineclasses.yaml
eofff 8aaa8e9
Update crds/doc-ru-virtualmachines.yaml
eofff a0a7e77
Review
prismagod File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
images/virtualization-artifact/pkg/apiserver/api/generated/openapi/zz_generated.openapi.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
91 changes: 91 additions & 0 deletions
91
images/virtualization-artifact/pkg/controller/vm/internal/defaulter/core_fraction.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| } | ||
| } | ||
|
|
||
| return defaultValue | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
...on-artifact/pkg/controller/vmclass/internal/validators/default_core_fraction_validator.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The 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.