@@ -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,52 @@ 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
+ // This should be dropped in 4.21, because 4.21(=1.34) fix would be a carry pulled from 1.35
348
+ newExpectedCPUShares = ConvertCPUSharesToCgroupV2Value (v1expectedCPUShares )
344
349
}
345
350
346
351
if expectedMemLimitString != "0" {
347
352
errs = append (errs , VerifyCgroupValue (f , pod , ci .Name , cgroupMemLimit , expectedMemLimitString ))
348
353
}
349
354
errs = append (errs , VerifyCgroupValue (f , pod , ci .Name , cgroupCPULimit , expectedCPULimits ... ))
350
- errs = append (errs , VerifyCgroupValue (f , pod , ci .Name , cgroupCPURequest , strconv .FormatInt (expectedCPUShares , 10 )))
355
+ errs = append (errs , VerifyCgroupValue (f , pod , ci .Name , cgroupCPURequest , strconv .FormatInt (expectedCPUShares , 10 ), strconv . FormatInt ( newExpectedCPUShares , 10 ) ))
351
356
// TODO(vinaykul,InPlacePodVerticalScaling): Verify oom_score_adj when runc adds support for updating it
352
357
// See https://github.com/opencontainers/runc/pull/4669
353
358
}
354
359
}
355
360
return utilerrors .NewAggregate (errs )
356
361
}
357
362
363
+ // ConvertCPUSharesToCgroupV2Value converts CPU shares, used by cgroup v1,
364
+ // to CPU weight, used by cgroup v2.
365
+ //
366
+ // Cgroup v1 CPU shares has a range of [2^1...2^18], i.e. [2...262144],
367
+ // and the default value is 1024.
368
+ //
369
+ // Cgroup v2 CPU weight has a range of [10^0...10^4], i.e. [1...10000],
370
+ // and the default value is 100.
371
+ //
372
+ // This function is identical to https://github.com/opencontainers/cgroups/blob/a3e2ecd1f756a19cee15f85b96337a59c3b5337b/utils.go#L417-L441
373
+ func ConvertCPUSharesToCgroupV2Value (cpuShares int64 ) int64 {
374
+ // The value of 0 means "unset".
375
+ if cpuShares == 0 {
376
+ return 0
377
+ }
378
+ if cpuShares <= 2 {
379
+ return 1
380
+ }
381
+ if cpuShares >= 262144 {
382
+ return 10000
383
+ }
384
+ l := math .Log2 (float64 (cpuShares ))
385
+ // Quadratic function which fits min, max, and default.
386
+ exponent := (l * l + 125 * l )/ 612.0 - 7.0 / 34.0
387
+
388
+ return int64 (math .Ceil (math .Pow (10 , exponent )))
389
+ }
390
+
358
391
func verifyPodRestarts (f * framework.Framework , pod * v1.Pod , wantInfo []ResizableContainerInfo ) error {
359
392
ginkgo .GinkgoHelper ()
360
393
0 commit comments