Skip to content

Commit

Permalink
Fixed pod's cpu & mem resource setters helpers. (#608)
Browse files Browse the repository at this point in the history
The original implementation was overwriting any req/limit that was
previosly set for the other resources. For instance, test cases
doing this didn't work properly:

  pod.RedefineWithCPUResources(testPod, "1", "1")
  pod.RedefineWithMemoryResources(testPod, "512Mi", "512Mi")

The call to pod.RedefineWithMemoryResources() was zero-ing the cpu
req/limits that was just set before ("1", "1") as it was creating a new
empty container.Resources field (corev1.ResourceRequirements{}).

The new implementations don't overwrite other reqs/limits values, it
justs sets/overwrite the corresponding ones (cpu/mem).

The affected test cases were passing until now because a bug in the cert
suite code in the function AreCPUResourcesWholeUnits() that was wrongly
returning "true" when the cpu reqs/limit were not set. It was fixed
here:
redhat-best-practices-for-k8s/certsuite#1631
  • Loading branch information
greyerof authored Nov 17, 2023
1 parent 1e408d5 commit 9b8506e
Showing 1 changed file with 22 additions and 14 deletions.
36 changes: 22 additions & 14 deletions tests/utils/pod/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,27 +102,35 @@ func RedefineWithPVC(pod *corev1.Pod, volumeName string, claimName string) {

func RedefineWithCPUResources(pod *corev1.Pod, limit string, req string) {
for i := range pod.Spec.Containers {
pod.Spec.Containers[i].Resources = corev1.ResourceRequirements{
Limits: corev1.ResourceList{
corev1.ResourceCPU: resource.MustParse(limit),
},
Requests: corev1.ResourceList{
corev1.ResourceCPU: resource.MustParse(req),
},
containerResources := &pod.Spec.Containers[i].Resources

if containerResources.Requests == nil {
containerResources.Requests = corev1.ResourceList{}
}

if containerResources.Limits == nil {
containerResources.Limits = corev1.ResourceList{}
}

containerResources.Requests[corev1.ResourceCPU] = resource.MustParse(req)
containerResources.Limits[corev1.ResourceCPU] = resource.MustParse(limit)
}
}

func RedefineWithMemoryResources(pod *corev1.Pod, limit string, req string) {
for i := range pod.Spec.Containers {
pod.Spec.Containers[i].Resources = corev1.ResourceRequirements{
Limits: corev1.ResourceList{
corev1.ResourceMemory: resource.MustParse(limit),
},
Requests: corev1.ResourceList{
corev1.ResourceMemory: resource.MustParse(req),
},
containerResources := &pod.Spec.Containers[i].Resources

if containerResources.Requests == nil {
containerResources.Requests = corev1.ResourceList{}
}

if containerResources.Limits == nil {
containerResources.Limits = corev1.ResourceList{}
}

containerResources.Requests[corev1.ResourceMemory] = resource.MustParse(req)
containerResources.Limits[corev1.ResourceMemory] = resource.MustParse(limit)
}
}

Expand Down

0 comments on commit 9b8506e

Please sign in to comment.