Skip to content

Commit 75f37b9

Browse files
committed
UPSTREAM: <carry>: Fix cpu.weight wrong conversion in InPlacePodVerticalScaling tests
Signed-off-by: Ayato Tokubi <[email protected]>
1 parent 24677bb commit 75f37b9

File tree

1 file changed

+39
-5
lines changed

1 file changed

+39
-5
lines changed

test/e2e/framework/pod/resize.go

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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,53 @@ 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+
// 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)
344350
}
345351

346352
if expectedMemLimitString != "0" {
347353
errs = append(errs, VerifyCgroupValue(f, pod, ci.Name, cgroupMemLimit, expectedMemLimitString))
348354
}
349355
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)))
351357
// TODO(vinaykul,InPlacePodVerticalScaling): Verify oom_score_adj when runc adds support for updating it
352358
// See https://github.com/opencontainers/runc/pull/4669
353359
}
354360
}
355361
return utilerrors.NewAggregate(errs)
356362
}
357363

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+
358392
func verifyPodRestarts(f *framework.Framework, pod *v1.Pod, wantInfo []ResizableContainerInfo) error {
359393
ginkgo.GinkgoHelper()
360394

0 commit comments

Comments
 (0)