Skip to content

Commit

Permalink
Limit guest CPUs to two threads if NUMA passthrough is selected
Browse files Browse the repository at this point in the history
Signed-off-by: Roman Mohr <[email protected]>
  • Loading branch information
rmohr committed Sep 9, 2021
1 parent dd02a8c commit 8f507ab
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -981,6 +981,7 @@ func validateCpuPinning(field *k8sfield.Path, spec *v1.VirtualMachineInstanceSpe
causes = append(causes, validateRequestLimitOrCoresProvidedOnDedicatedCPUPlacement(field, spec)...)
causes = append(causes, validateRequestEqualsLimitOnDedicatedCPUPlacement(field, spec)...)
causes = append(causes, validateRequestOrLimitWithCoresProvidedOnDedicatedCPUPlacement(field, spec)...)
causes = append(causes, validateThreadCountOnDedicatedCPUPlacement(field, spec)...)
}
return causes
}
Expand Down Expand Up @@ -1019,6 +1020,20 @@ func validateNUMA(field *k8sfield.Path, spec *v1.VirtualMachineInstanceSpec, con
return causes
}

func validateThreadCountOnDedicatedCPUPlacement(field *k8sfield.Path, spec *v1.VirtualMachineInstanceSpec) (causes []metav1.StatusCause) {
if spec.Domain.CPU != nil && spec.Domain.CPU.Threads > 2 {
causes = append(causes, metav1.StatusCause{
Type: metav1.CauseTypeFieldValueInvalid,
Message: fmt.Sprintf("Not more than two threads must be provided at %v (got %v) when DedicatedCPUPlacement is true",
field.Child("domain", "cpu", "threads").String(),
spec.Domain.CPU.Threads,
),
Field: field.Child("domain", "cpu", "dedicatedCpuPlacement").String(),
})
}
return causes
}

func validateRequestOrLimitWithCoresProvidedOnDedicatedCPUPlacement(field *k8sfield.Path, spec *v1.VirtualMachineInstanceSpec) (causes []metav1.StatusCause) {
if (spec.Domain.Resources.Requests.Cpu().Value() > 0 || spec.Domain.Resources.Limits.Cpu().Value() > 0) && hwutil.GetNumberOfVCPUs(spec.Domain.CPU) > 0 &&
spec.Domain.Resources.Requests.Cpu().Value() != hwutil.GetNumberOfVCPUs(spec.Domain.CPU) && spec.Domain.Resources.Limits.Cpu().Value() != hwutil.GetNumberOfVCPUs(spec.Domain.CPU) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2276,6 +2276,18 @@ var _ = Describe("Validating VMICreate Admitter", func() {
causes := ValidateVirtualMachineInstanceSpec(k8sfield.NewPath("fake"), &vmi.Spec, config)
Expect(causes).To(BeEmpty())
})
It("should reject specs with more than two threads", func() {
vmi.Spec.Domain.Memory = &v1.Memory{Hugepages: &v1.Hugepages{PageSize: "2Mi"}}
vmi.Spec.Domain.CPU.Cores = 4
vmi.Spec.Domain.CPU.Threads = 3
vmi.Spec.Domain.CPU.NUMA = &v1.NUMA{GuestMappingPassthrough: &v1.NUMAGuestMappingPassthrough{}}
vmi.Spec.Domain.Resources.Limits = k8sv1.ResourceList{
k8sv1.ResourceCPU: resource.MustParse("12"),
}
causes := ValidateVirtualMachineInstanceSpec(k8sfield.NewPath("fake"), &vmi.Spec, config)
Expect(causes).To(HaveLen(1))
Expect(causes[0].Message).To(ContainSubstring("Not more than two threads must be provided at fake.domain.cpu.threads (got 3) when DedicatedCPUPlacement is true"))
})
It("should reject specs without cpu reqirements", func() {
causes := ValidateVirtualMachineInstanceSpec(k8sfield.NewPath("fake"), &vmi.Spec, config)
Expect(len(causes)).To(Equal(1))
Expand Down

0 comments on commit 8f507ab

Please sign in to comment.