Skip to content

Commit f1bf9be

Browse files
authored
Merge pull request kubernetes#70678 from dashpole/fix_cgroup_manager
Fix slice sharing bug in cgroup manager
2 parents ebcc2c7 + d4f6ae3 commit f1bf9be

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

pkg/kubelet/cm/cgroup_manager_linux.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,10 @@ func NewCgroupName(base CgroupName, components ...string) CgroupName {
6767
panic(fmt.Errorf("invalid character in component [%q] of CgroupName", component))
6868
}
6969
}
70-
return CgroupName(append(base, components...))
70+
// copy data from the base cgroup to eliminate cases where CgroupNames share underlying slices. See #68416
71+
baseCopy := make([]string, len(base))
72+
copy(baseCopy, base)
73+
return CgroupName(append(baseCopy, components...))
7174
}
7275

7376
func escapeSystemdCgroupName(part string) string {

pkg/kubelet/cm/cgroup_manager_linux_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,34 @@ package cm
2020

2121
import (
2222
"path"
23+
"reflect"
2324
"testing"
2425
)
2526

27+
// TestNewCgroupName tests confirms that #68416 is fixed
28+
func TestNewCgroupName(t *testing.T) {
29+
a := ParseCgroupfsToCgroupName("/a/")
30+
ab := NewCgroupName(a, "b")
31+
32+
expectedAB := CgroupName([]string{"a", "", "b"})
33+
if !reflect.DeepEqual(ab, expectedAB) {
34+
t.Errorf("Expected %d%+v; got %d%+v", len(expectedAB), expectedAB, len(ab), ab)
35+
}
36+
37+
abc := NewCgroupName(ab, "c")
38+
39+
expectedABC := CgroupName([]string{"a", "", "b", "c"})
40+
if !reflect.DeepEqual(abc, expectedABC) {
41+
t.Errorf("Expected %d%+v; got %d%+v", len(expectedABC), expectedABC, len(abc), abc)
42+
}
43+
44+
_ = NewCgroupName(ab, "d")
45+
46+
if !reflect.DeepEqual(abc, expectedABC) {
47+
t.Errorf("Expected %d%+v; got %d%+v", len(expectedABC), expectedABC, len(abc), abc)
48+
}
49+
}
50+
2651
func TestCgroupNameToSystemdBasename(t *testing.T) {
2752
testCases := []struct {
2853
input CgroupName

0 commit comments

Comments
 (0)