@@ -21,6 +21,7 @@ import (
21
21
"encoding/json"
22
22
"errors"
23
23
"fmt"
24
+ "math"
24
25
"strconv"
25
26
"strings"
26
27
@@ -321,16 +322,17 @@ func VerifyPodContainersCgroupValues(ctx context.Context, f *framework.Framework
321
322
}
322
323
tc := makeResizableContainer (ci )
323
324
if tc .Resources .Limits != nil || tc .Resources .Requests != nil {
324
- var expectedCPUShares int64
325
+ var expectedCPUShares , v1expectedCPUShares , newExpectedCPUShares int64
325
326
var expectedMemLimitString string
326
327
expectedMemLimitInBytes := tc .Resources .Limits .Memory ().Value ()
327
328
cpuRequest := tc .Resources .Requests .Cpu ()
328
329
cpuLimit := tc .Resources .Limits .Cpu ()
329
330
if cpuRequest .IsZero () && ! cpuLimit .IsZero () {
330
- expectedCPUShares = int64 (kubecm .MilliCPUToShares (cpuLimit .MilliValue ()))
331
+ v1expectedCPUShares = int64 (kubecm .MilliCPUToShares (cpuLimit .MilliValue ()))
331
332
} else {
332
- expectedCPUShares = int64 (kubecm .MilliCPUToShares (cpuRequest .MilliValue ()))
333
+ v1expectedCPUShares = int64 (kubecm .MilliCPUToShares (cpuRequest .MilliValue ()))
333
334
}
335
+ expectedCPUShares = v1expectedCPUShares
334
336
335
337
expectedCPULimits := GetCPULimitCgroupExpectations (cpuLimit )
336
338
expectedMemLimitString = strconv .FormatInt (expectedMemLimitInBytes , 10 )
@@ -340,21 +342,53 @@ func VerifyPodContainersCgroupValues(ctx context.Context, f *framework.Framework
340
342
}
341
343
// convert cgroup v1 cpu.shares value to cgroup v2 cpu.weight value
342
344
// https://github.com/kubernetes/enhancements/tree/master/keps/sig-node/2254-cgroup-v2#phase-1-convert-from-cgroups-v1-settings-to-v2
343
- expectedCPUShares = int64 (1 + ((expectedCPUShares - 2 )* 9999 )/ 262142 )
345
+ expectedCPUShares = int64 (1 + ((v1expectedCPUShares - 2 )* 9999 )/ 262142 )
346
+ // TODO(atokubi): This is required to fix https://github.com/kubernetes/kubernetes/pull/132791
347
+ // Since the file structure changed in the newer releases, we may need to update the test again
348
+ // in later releases.
349
+ newExpectedCPUShares = ConvertCPUSharesToCgroupV2Value (v1expectedCPUShares )
344
350
}
345
351
346
352
if expectedMemLimitString != "0" {
347
353
errs = append (errs , VerifyCgroupValue (f , pod , ci .Name , cgroupMemLimit , expectedMemLimitString ))
348
354
}
349
355
errs = append (errs , VerifyCgroupValue (f , pod , ci .Name , cgroupCPULimit , expectedCPULimits ... ))
350
- errs = append (errs , VerifyCgroupValue (f , pod , ci .Name , cgroupCPURequest , strconv .FormatInt (expectedCPUShares , 10 )))
356
+ errs = append (errs , VerifyCgroupValue (f , pod , ci .Name , cgroupCPURequest , strconv .FormatInt (expectedCPUShares , 10 ), strconv . FormatInt ( newExpectedCPUShares , 10 ) ))
351
357
// TODO(vinaykul,InPlacePodVerticalScaling): Verify oom_score_adj when runc adds support for updating it
352
358
// See https://github.com/opencontainers/runc/pull/4669
353
359
}
354
360
}
355
361
return utilerrors .NewAggregate (errs )
356
362
}
357
363
364
+ // ConvertCPUSharesToCgroupV2Value converts CPU shares, used by cgroup v1,
365
+ // to CPU weight, used by cgroup v2.
366
+ //
367
+ // Cgroup v1 CPU shares has a range of [2^1...2^18], i.e. [2...262144],
368
+ // and the default value is 1024.
369
+ //
370
+ // Cgroup v2 CPU weight has a range of [10^0...10^4], i.e. [1...10000],
371
+ // and the default value is 100.
372
+ //
373
+ // This function is identical to https://github.com/opencontainers/cgroups/blob/a3e2ecd1f756a19cee15f85b96337a59c3b5337b/utils.go#L417-L441
374
+ func ConvertCPUSharesToCgroupV2Value (cpuShares int64 ) int64 {
375
+ // The value of 0 means "unset".
376
+ if cpuShares == 0 {
377
+ return 0
378
+ }
379
+ if cpuShares <= 2 {
380
+ return 1
381
+ }
382
+ if cpuShares >= 262144 {
383
+ return 10000
384
+ }
385
+ l := math .Log2 (float64 (cpuShares ))
386
+ // Quadratic function which fits min, max, and default.
387
+ exponent := (l * l + 125 * l )/ 612.0 - 7.0 / 34.0
388
+
389
+ return int64 (math .Ceil (math .Pow (10 , exponent )))
390
+ }
391
+
358
392
func verifyPodRestarts (f * framework.Framework , pod * v1.Pod , wantInfo []ResizableContainerInfo ) error {
359
393
ginkgo .GinkgoHelper ()
360
394
0 commit comments