Skip to content

Commit aa0edf0

Browse files
BenTheElderbitoku
andcommitted
Update pod resize tests to accept new cpu.weight conversion
Adapted from kubernetes#132791 to release-1.33 Co-Authored-By: Ayato Tokubi <atokubi@redhat.com>
1 parent fc38c49 commit aa0edf0

File tree

3 files changed

+81
-14
lines changed

3 files changed

+81
-14
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
Copyright 2025 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package pod
18+
19+
import (
20+
"strconv"
21+
22+
libcontainercgroups "github.com/opencontainers/cgroups"
23+
v1 "k8s.io/api/core/v1"
24+
kubecm "k8s.io/kubernetes/pkg/kubelet/cm"
25+
)
26+
27+
func getExpectedCPUShares(rr *v1.ResourceRequirements, podOnCgroupv2 bool) []string {
28+
// This function is moved out from cgroups.go because opencontainers/cgroups can only be compiled in linux platforms.
29+
cpuRequest := rr.Requests.Cpu()
30+
cpuLimit := rr.Limits.Cpu()
31+
var shares int64
32+
if cpuRequest.IsZero() && !cpuLimit.IsZero() {
33+
shares = int64(kubecm.MilliCPUToShares(cpuLimit.MilliValue()))
34+
} else {
35+
shares = int64(kubecm.MilliCPUToShares(cpuRequest.MilliValue()))
36+
}
37+
if podOnCgroupv2 {
38+
// Because of https://github.com/kubernetes/kubernetes/issues/131216, the way of conversion has been changed.
39+
// runc: https://github.com/opencontainers/runc/pull/4785
40+
// crun: https://github.com/containers/crun/issues/1721
41+
// This is dependent on the container runtime version. In order not to break the tests when we upgrade the
42+
// container runtimes, we check if either the old or the new conversion matches the actual value for now.
43+
// TODO: Remove the old conversion once container runtimes are updated.
44+
oldConverted := 1 + ((shares-2)*9999)/262142
45+
converted := libcontainercgroups.ConvertCPUSharesToCgroupV2Value(uint64(shares))
46+
return []string{strconv.FormatInt(oldConverted, 10), strconv.FormatInt(int64(converted), 10)}
47+
} else {
48+
return []string{strconv.FormatInt(shares, 10)}
49+
}
50+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//go:build !linux
2+
3+
/*
4+
Copyright 2025 The Kubernetes Authors.
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
*/
18+
19+
package pod
20+
21+
import v1 "k8s.io/api/core/v1"
22+
23+
func getExpectedCPUShares(rr *v1.ResourceRequirements, podOnCgroupv2 bool) []string {
24+
// cgroup is only supported in linux.
25+
return []string{}
26+
}

test/e2e/framework/pod/resize.go

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ import (
3030
utilerrors "k8s.io/apimachinery/pkg/util/errors"
3131
helpers "k8s.io/component-helpers/resource"
3232
"k8s.io/kubectl/pkg/util/podutils"
33-
kubecm "k8s.io/kubernetes/pkg/kubelet/cm"
3433
kubeqos "k8s.io/kubernetes/pkg/kubelet/qos"
3534
"k8s.io/kubernetes/test/e2e/framework"
3635
imageutils "k8s.io/kubernetes/test/utils/image"
@@ -305,10 +304,11 @@ func VerifyPodContainersCgroupValues(ctx context.Context, f *framework.Framework
305304
value := IsPodOnCgroupv2Node(f, pod)
306305
podOnCgroupv2Node = &value
307306
}
307+
podOnCgroupV2 := *podOnCgroupv2Node
308308
cgroupMemLimit := Cgroupv2MemLimit
309309
cgroupCPULimit := Cgroupv2CPULimit
310310
cgroupCPURequest := Cgroupv2CPURequest
311-
if !*podOnCgroupv2Node {
311+
if !podOnCgroupV2 {
312312
cgroupMemLimit = CgroupMemLimit
313313
cgroupCPULimit = CgroupCPUQuota
314314
cgroupCPURequest = CgroupCPUShares
@@ -322,32 +322,23 @@ func VerifyPodContainersCgroupValues(ctx context.Context, f *framework.Framework
322322
tc := makeResizableContainer(ci)
323323
if tc.Resources.Limits != nil || tc.Resources.Requests != nil {
324324
var expectedCPUShares int64
325-
var expectedMemLimitString string
326325
expectedMemLimitInBytes := tc.Resources.Limits.Memory().Value()
327326
cpuRequest := tc.Resources.Requests.Cpu()
328327
cpuLimit := tc.Resources.Limits.Cpu()
329-
if cpuRequest.IsZero() && !cpuLimit.IsZero() {
330-
expectedCPUShares = int64(kubecm.MilliCPUToShares(cpuLimit.MilliValue()))
331-
} else {
332-
expectedCPUShares = int64(kubecm.MilliCPUToShares(cpuRequest.MilliValue()))
333-
}
334-
328+
expectedCPUShares := getExpectedCPUShares(tc.Resources, podOnCgroupV2)
335329
expectedCPULimits := GetCPULimitCgroupExpectations(cpuLimit)
336330
expectedMemLimitString = strconv.FormatInt(expectedMemLimitInBytes, 10)
337-
if *podOnCgroupv2Node {
331+
if podOnCgroupV2 {
338332
if expectedMemLimitString == "0" {
339333
expectedMemLimitString = "max"
340334
}
341-
// convert cgroup v1 cpu.shares value to cgroup v2 cpu.weight value
342-
// 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)
344335
}
345336

346337
if expectedMemLimitString != "0" {
347338
errs = append(errs, VerifyCgroupValue(f, pod, ci.Name, cgroupMemLimit, expectedMemLimitString))
348339
}
349340
errs = append(errs, VerifyCgroupValue(f, pod, ci.Name, cgroupCPULimit, expectedCPULimits...))
350-
errs = append(errs, VerifyCgroupValue(f, pod, ci.Name, cgroupCPURequest, strconv.FormatInt(expectedCPUShares, 10)))
341+
errs = append(errs, VerifyCgroupValue(f, pod, ci.Name, cgroupCPURequest, expectedCPUShares...))
351342
// TODO(vinaykul,InPlacePodVerticalScaling): Verify oom_score_adj when runc adds support for updating it
352343
// See https://github.com/opencontainers/runc/pull/4669
353344
}

0 commit comments

Comments
 (0)