Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
Signed-off-by: yaroslavborbat <[email protected]>
  • Loading branch information
yaroslavborbat committed Nov 12, 2024
1 parent abb4690 commit dbdd41a
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 17 deletions.
51 changes: 35 additions & 16 deletions images/virtualization-artifact/pkg/controller/kvbuilder/kvvm.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,10 @@ func (b *KVVM) SetKVVMIAnnotation(annoKey, annoValue string) {
}

func (b *KVVM) SetCPUModel(class *virtv2.VirtualMachineClass) error {
var cpu virtv1.CPU
cpu := b.Resource.Spec.Template.Spec.Domain.CPU
if cpu == nil {
cpu = &virtv1.CPU{}
}

switch class.Spec.CPU.Type {
case virtv2.CPUTypeHost:
Expand All @@ -105,19 +108,17 @@ func (b *KVVM) SetCPUModel(class *virtv2.VirtualMachineClass) error {
case virtv2.CPUTypeModel:
cpu.Model = class.Spec.CPU.Model
case virtv2.CPUTypeFeatures, virtv2.CPUTypeDiscovery:
cpu.Features = make([]virtv1.CPUFeature, len(class.Status.CpuFeatures.Enabled))
features := make([]virtv1.CPUFeature, len(class.Status.CpuFeatures.Enabled))
for i, feature := range class.Status.CpuFeatures.Enabled {
cpu.Features[i] = virtv1.CPUFeature{
features[i] = virtv1.CPUFeature{
Name: feature,
Policy: "require",
}
}
cpu.Features = features
default:
return fmt.Errorf("unexpected cpu type: %q", class.Spec.CPU.Type)
}

b.Resource.Spec.Template.Spec.Domain.CPU = &cpu

return nil
}

Expand Down Expand Up @@ -206,24 +207,42 @@ func (b *KVVM) SetTopologySpreadConstraint(topology []corev1.TopologySpreadConst
b.Resource.Spec.Template.Spec.TopologySpreadConstraints = topology
}

func (b *KVVM) SetResourceRequirements(cores int, coreFraction string, memorySize resource.Quantity) error {
func (b *KVVM) SetCpu(cores int, coreFraction string) error {
domainSpec := &b.Resource.Spec.Template.Spec.Domain
if domainSpec.CPU == nil {
domainSpec.CPU = &virtv1.CPU{}
}
cpuRequest, err := GetCPURequest(cores, coreFraction)
if err != nil {
return err
}
b.Resource.Spec.Template.Spec.Domain.Resources = virtv1.ResourceRequirements{
Requests: corev1.ResourceList{
corev1.ResourceCPU: *cpuRequest,
corev1.ResourceMemory: memorySize,
},
Limits: corev1.ResourceList{
corev1.ResourceCPU: *GetCPULimit(cores),
corev1.ResourceMemory: memorySize,
},
cpuLimit := GetCPULimit(cores)
if domainSpec.Resources.Requests == nil {
domainSpec.Resources.Requests = make(map[corev1.ResourceName]resource.Quantity)
}
if domainSpec.Resources.Limits == nil {
domainSpec.Resources.Limits = make(map[corev1.ResourceName]resource.Quantity)
}
domainSpec.Resources.Requests[corev1.ResourceCPU] = *cpuRequest
domainSpec.Resources.Limits[corev1.ResourceCPU] = *cpuLimit
domainSpec.CPU.Cores = uint32(cores)
domainSpec.CPU.Sockets = uint32(cpuRequest.Value())
domainSpec.CPU.MaxSockets = uint32(cpuLimit.Value())
return nil
}

func (b *KVVM) SetMemory(memorySize resource.Quantity) {
res := &b.Resource.Spec.Template.Spec.Domain.Resources
if res.Requests == nil {
res.Requests = make(map[corev1.ResourceName]resource.Quantity)
}
if res.Limits == nil {
res.Limits = make(map[corev1.ResourceName]resource.Quantity)
}
res.Requests[corev1.ResourceMemory] = memorySize
res.Limits[corev1.ResourceMemory] = memorySize
}

func GetCPURequest(cores int, coreFraction string) (*resource.Quantity, error) {
if coreFraction == "" {
return GetCPULimit(cores), nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ func ApplyVirtualMachineSpec(
kvvm.SetPriorityClassName(vm.Spec.PriorityClassName)
kvvm.SetTerminationGracePeriod(vm.Spec.TerminationGracePeriodSeconds)
kvvm.SetTopologySpreadConstraint(vm.Spec.TopologySpreadConstraints)
if err := kvvm.SetResourceRequirements(vm.Spec.CPU.Cores, vm.Spec.CPU.CoreFraction, vm.Spec.Memory.Size); err != nil {
kvvm.SetMemory(vm.Spec.Memory.Size)
if err := kvvm.SetCpu(vm.Spec.CPU.Cores, vm.Spec.CPU.CoreFraction); err != nil {
return err
}

Expand Down
1 change: 1 addition & 0 deletions templates/kubevirt/kubevirt.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ spec:
family: Deckhouse
product: DeckhouseVirtualizationPlatform
evictionStrategy: LiveMigrate
vmRolloutStrategy: LiveUpdate
developerConfiguration:
featureGates:
- HotplugVolumes
Expand Down

0 comments on commit dbdd41a

Please sign in to comment.