Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
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 sizingpolicy

import (
"fmt"

"github.com/deckhouse/virtualization/api/core/v1alpha2"
)

func FormatCoreFractionValues(cf []v1alpha2.CoreFractionValue) []string {
result := make([]string, len(cf))
for i, v := range cf {
result[i] = fmt.Sprintf("%d%%", v)
}
return result
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"k8s.io/apimachinery/pkg/api/resource"

"github.com/deckhouse/virtualization-controller/pkg/common"
sizingpolicy "github.com/deckhouse/virtualization-controller/pkg/common/sizing_policy"
"github.com/deckhouse/virtualization/api/core/v1alpha2"
)

Expand Down Expand Up @@ -103,7 +104,7 @@ func validateCoreFraction(vm *v1alpha2.VirtualMachine, sp *v1alpha2.SizingPolicy
}

if !hasFractionValueInPolicy {
formattedCoreFractions := formatCoreFractionValues(sp.CoreFractions)
formattedCoreFractions := sizingpolicy.FormatCoreFractionValues(sp.CoreFractions)
return fmt.Errorf("VM core fraction value %s is not within the allowed values: %v", vm.Spec.CPU.CoreFraction, formattedCoreFractions)
}

Expand Down Expand Up @@ -224,11 +225,3 @@ func generateValidGrid(min, max, step resource.Quantity) []resource.Quantity {

return grid
}

func formatCoreFractionValues(cf []v1alpha2.CoreFractionValue) []string {
result := make([]string, len(cf))
for i, v := range cf {
result[i] = fmt.Sprintf("%d%%", v)
}
return result
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2024 Flant JSC
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.
Expand All @@ -23,6 +23,7 @@ import (

"sigs.k8s.io/controller-runtime/pkg/webhook/admission"

sizingpolicy "github.com/deckhouse/virtualization-controller/pkg/common/sizing_policy"
"github.com/deckhouse/virtualization/api/core/v1alpha2"
)

Expand Down Expand Up @@ -51,8 +52,8 @@ func (v *DefaultCoreFractionValidator) validate(vmclass *v1alpha2.VirtualMachine
}

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 fmt.Errorf("vmclass %s sizingPolicy[%d]: defaultCoreFraction %d%% is not in the allowed coreFractions list %s",
vmclass.Name, i, *policy.DefaultCoreFraction, sizingpolicy.FormatCoreFractionValues(policy.CoreFractions))
}
}

Expand Down
Loading