@@ -21,6 +21,7 @@ import (
2121 "encoding/json"
2222 "errors"
2323 "fmt"
24+ "math"
2425 "strconv"
2526 "strings"
2627
@@ -321,16 +322,17 @@ func VerifyPodContainersCgroupValues(ctx context.Context, f *framework.Framework
321322 }
322323 tc := makeResizableContainer (ci )
323324 if tc .Resources .Limits != nil || tc .Resources .Requests != nil {
324- var expectedCPUShares int64
325+ var expectedCPUShares , v1expectedCPUShares , newExpectedCPUShares int64
325326 var expectedMemLimitString string
326327 expectedMemLimitInBytes := tc .Resources .Limits .Memory ().Value ()
327328 cpuRequest := tc .Resources .Requests .Cpu ()
328329 cpuLimit := tc .Resources .Limits .Cpu ()
329330 if cpuRequest .IsZero () && ! cpuLimit .IsZero () {
330- expectedCPUShares = int64 (kubecm .MilliCPUToShares (cpuLimit .MilliValue ()))
331+ v1expectedCPUShares = int64 (kubecm .MilliCPUToShares (cpuLimit .MilliValue ()))
331332 } else {
332- expectedCPUShares = int64 (kubecm .MilliCPUToShares (cpuRequest .MilliValue ()))
333+ v1expectedCPUShares = int64 (kubecm .MilliCPUToShares (cpuRequest .MilliValue ()))
333334 }
335+ expectedCPUShares = v1expectedCPUShares
334336
335337 expectedCPULimits := GetCPULimitCgroupExpectations (cpuLimit )
336338 expectedMemLimitString = strconv .FormatInt (expectedMemLimitInBytes , 10 )
@@ -340,21 +342,52 @@ func VerifyPodContainersCgroupValues(ctx context.Context, f *framework.Framework
340342 }
341343 // convert cgroup v1 cpu.shares value to cgroup v2 cpu.weight value
342344 // 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 )
344349 }
345350
346351 if expectedMemLimitString != "0" {
347352 errs = append (errs , VerifyCgroupValue (f , pod , ci .Name , cgroupMemLimit , expectedMemLimitString ))
348353 }
349354 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 ) ))
351356 // TODO(vinaykul,InPlacePodVerticalScaling): Verify oom_score_adj when runc adds support for updating it
352357 // See https://github.com/opencontainers/runc/pull/4669
353358 }
354359 }
355360 return utilerrors .NewAggregate (errs )
356361}
357362
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+
358391func verifyPodRestarts (f * framework.Framework , pod * v1.Pod , wantInfo []ResizableContainerInfo ) error {
359392 ginkgo .GinkgoHelper ()
360393
0 commit comments