@@ -18,12 +18,15 @@ package pod
18
18
19
19
import (
20
20
"fmt"
21
+ "strconv"
21
22
"strings"
22
23
23
24
"github.com/onsi/ginkgo/v2"
24
25
"github.com/onsi/gomega"
25
26
26
27
v1 "k8s.io/api/core/v1"
28
+ "k8s.io/apimachinery/pkg/api/resource"
29
+ kubecm "k8s.io/kubernetes/pkg/kubelet/cm"
27
30
"k8s.io/kubernetes/test/e2e/framework"
28
31
imageutils "k8s.io/kubernetes/test/utils/image"
29
32
psaapi "k8s.io/pod-security-admission/api"
@@ -292,20 +295,23 @@ func FindContainerStatusInPod(pod *v1.Pod, containerName string) *v1.ContainerSt
292
295
}
293
296
294
297
// VerifyCgroupValue verifies that the given cgroup path has the expected value in
295
- // the specified container of the pod. It execs into the container to retrive the
296
- // cgroup value and compares it against the expected value.
297
- func VerifyCgroupValue (f * framework.Framework , pod * v1.Pod , cName , cgPath , expectedCgValue string ) error {
298
+ // the specified container of the pod. It execs into the container to retrieve the
299
+ // cgroup value, and ensures that the retrieved cgroup value is equivalent to at
300
+ // least one of the values in expectedCgValues.
301
+ func VerifyCgroupValue (f * framework.Framework , pod * v1.Pod , cName , cgPath string , expectedCgValues ... string ) error {
298
302
cmd := fmt .Sprintf ("head -n 1 %s" , cgPath )
299
- framework .Logf ("Namespace %s Pod %s Container %s - looking for cgroup value %s in path %s" ,
300
- pod .Namespace , pod .Name , cName , expectedCgValue , cgPath )
303
+ framework .Logf ("Namespace %s Pod %s Container %s - looking for one of the expected cgroup values %s in path %s" ,
304
+ pod .Namespace , pod .Name , cName , expectedCgValues , cgPath )
301
305
cgValue , _ , err := ExecCommandInContainerWithFullOutput (f , pod .Name , cName , "/bin/sh" , "-c" , cmd )
302
306
if err != nil {
303
- return fmt .Errorf ("failed to find expected value %q in container cgroup %q" , expectedCgValue , cgPath )
307
+ return fmt .Errorf ("failed to find one of the expected cgroup values %q in container cgroup %q" , expectedCgValues , cgPath )
304
308
}
305
309
cgValue = strings .Trim (cgValue , "\n " )
306
- if cgValue != expectedCgValue {
307
- return fmt .Errorf ("cgroup value %q not equal to expected %q" , cgValue , expectedCgValue )
310
+
311
+ if err := framework .Gomega ().Expect (cgValue ).To (gomega .BeElementOf (expectedCgValues )); err != nil {
312
+ return fmt .Errorf ("value of cgroup %q for container %q should match one of the expectations: %w" , cgPath , cName , err )
308
313
}
314
+
309
315
return nil
310
316
}
311
317
@@ -338,3 +344,35 @@ func IsPodOnCgroupv2Node(f *framework.Framework, pod *v1.Pod) bool {
338
344
}
339
345
return len (out ) != 0
340
346
}
347
+
348
+ // TODO: Remove the rounded cpu limit values when https://github.com/opencontainers/runc/issues/4622
349
+ // is fixed.
350
+ func GetCPULimitCgroupExpectations (cpuLimit * resource.Quantity ) []string {
351
+ var expectedCPULimits []string
352
+ milliCPULimit := cpuLimit .MilliValue ()
353
+
354
+ cpuQuota := kubecm .MilliCPUToQuota (milliCPULimit , kubecm .QuotaPeriod )
355
+ if cpuLimit .IsZero () {
356
+ cpuQuota = - 1
357
+ }
358
+ expectedCPULimits = append (expectedCPULimits , getExpectedCPULimitFromCPUQuota (cpuQuota ))
359
+
360
+ if milliCPULimit % 10 != 0 && cpuQuota != - 1 {
361
+ roundedCPULimit := (milliCPULimit / 10 + 1 ) * 10
362
+ cpuQuotaRounded := kubecm .MilliCPUToQuota (roundedCPULimit , kubecm .QuotaPeriod )
363
+ expectedCPULimits = append (expectedCPULimits , getExpectedCPULimitFromCPUQuota (cpuQuotaRounded ))
364
+ }
365
+
366
+ return expectedCPULimits
367
+ }
368
+
369
+ func getExpectedCPULimitFromCPUQuota (cpuQuota int64 ) string {
370
+ expectedCPULimitString := strconv .FormatInt (cpuQuota , 10 )
371
+ if * podOnCgroupv2Node {
372
+ if expectedCPULimitString == "-1" {
373
+ expectedCPULimitString = "max"
374
+ }
375
+ expectedCPULimitString = fmt .Sprintf ("%s %s" , expectedCPULimitString , CPUPeriod )
376
+ }
377
+ return expectedCPULimitString
378
+ }
0 commit comments