Skip to content

Commit 3ae8cbe

Browse files
jacobseebertinatto
authored andcommitted
UPSTREAM: 132791: Update pod resize test to accept new cpu.weight conversion.
Backported from 799312a upstream - drop once we rebase onto a version that has it.
1 parent c3af123 commit 3ae8cbe

File tree

3 files changed

+78
-19
lines changed

3 files changed

+78
-19
lines changed

test/e2e/common/node/framework/cgroups/cgroups.go

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -207,23 +207,6 @@ func getExpectedCPULimitFromCPUQuota(cpuQuota int64, podOnCgroupV2 bool) string
207207
return expectedCPULimitString
208208
}
209209

210-
func getExpectedCPUShares(rr *v1.ResourceRequirements, podOnCgroupv2 bool) int64 {
211-
cpuRequest := rr.Requests.Cpu()
212-
cpuLimit := rr.Limits.Cpu()
213-
var shares int64
214-
if cpuRequest.IsZero() && !cpuLimit.IsZero() {
215-
shares = int64(kubecm.MilliCPUToShares(cpuLimit.MilliValue()))
216-
} else {
217-
shares = int64(kubecm.MilliCPUToShares(cpuRequest.MilliValue()))
218-
}
219-
if podOnCgroupv2 {
220-
// TODO: This fomula should be a shared function.
221-
return 1 + ((shares-2)*9999)/262142
222-
} else {
223-
return shares
224-
}
225-
}
226-
227210
func getExpectedMemLimitString(rr *v1.ResourceRequirements, podOnCgroupv2 bool) string {
228211
expectedMemLimitInBytes := rr.Limits.Memory().Value()
229212
expectedMemLimitString := strconv.FormatInt(expectedMemLimitInBytes, 10)
@@ -236,7 +219,7 @@ func getExpectedMemLimitString(rr *v1.ResourceRequirements, podOnCgroupv2 bool)
236219
func verifyContainerCPUWeight(f *framework.Framework, pod *v1.Pod, containerName string, expectedResources *v1.ResourceRequirements, podOnCgroupv2 bool) error {
237220
cpuWeightCgPath := getCgroupCPURequestPath(cgroupFsPath, podOnCgroupv2)
238221
expectedCPUShares := getExpectedCPUShares(expectedResources, podOnCgroupv2)
239-
if err := VerifyCgroupValue(f, pod, containerName, cpuWeightCgPath, strconv.FormatInt(expectedCPUShares, 10)); err != nil {
222+
if err := VerifyCgroupValue(f, pod, containerName, cpuWeightCgPath, expectedCPUShares...); err != nil {
240223
return fmt.Errorf("failed to verify cpu request cgroup value: %w", err)
241224
}
242225
return nil
@@ -286,7 +269,7 @@ func verifyPodCPUWeight(f *framework.Framework, pod *v1.Pod, expectedResources *
286269
cpuWeightCgPath = fmt.Sprintf("%s/%s", podCgPath, cgroupCPUSharesFile)
287270
}
288271
expectedCPUShares := getExpectedCPUShares(expectedResources, podOnCgroupv2)
289-
if err := VerifyCgroupValue(f, pod, pod.Spec.Containers[0].Name, cpuWeightCgPath, strconv.FormatInt(expectedCPUShares, 10)); err != nil {
272+
if err := VerifyCgroupValue(f, pod, pod.Spec.Containers[0].Name, cpuWeightCgPath, expectedCPUShares...); err != nil {
290273
return fmt.Errorf("pod cgroup cpu weight verification failed: %w", err)
291274
}
292275
return nil
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 cgroups
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 cgroups
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+
}

0 commit comments

Comments
 (0)