Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

koordlet: fix bug that cpu eviction does not take effect #1932

Merged
merged 1 commit into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions pkg/koordlet/util/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,23 @@ func GetBECgroupCurCPUSet() ([]int32, error) {
if err != nil {
return nil, err
}
if len(containerPaths) != 0 {
targetCgroupDir = containerPaths[0]
}
containerPaths = append(containerPaths, targetCgroupDir)

cpus, err := resourceexecutor.NewCgroupReader().ReadCPUSet(targetCgroupDir)
if err != nil {
return nil, err
// find the minimal length of container's cpuset to avoid the interference of sandbox's cpuset
// since the cpuset's value of sandbox could always be set to all cpu ids of the machine by kubelet
var targetCpus []int32
for _, path := range containerPaths {
cpuStr, err := resourceexecutor.NewCgroupReader().ReadCPUSet(path)
if err != nil {
return nil, err
}
curCpus := cpuset.ParseCPUSet(cpuStr)
if targetCpus == nil || len(curCpus) < len(targetCpus) {
targetCpus = curCpus
}
}
return cpuset.ParseCPUSet(cpus), nil

return targetCpus, nil
}

// GetBECPUSetPathsByMaxDepth gets all the be cpuset groups' paths recursively from upper to lower
Expand Down
47 changes: 47 additions & 0 deletions pkg/koordlet/util/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,3 +269,50 @@ func TestGetCgroupPathsByTargetDepth(t *testing.T) {
})
}
}

func TestGetBECgroupCurCPUSet(t *testing.T) {
type fields struct {
prepareFn func(helper *system.FileTestUtil)
}
tests := []struct {
name string
fields fields
want []int32
wantErr bool
}{
{
name: "get the cpuset value of be containers fail",
wantErr: true,
},
{
name: "get the cpuset value of be containers successcully",
fields: fields{
prepareFn: func(helper *system.FileTestUtil) {
helper.SetCgroupsV2(false)
cpuset, _ := system.GetCgroupResource(system.CPUSetCPUSName)
qosCgroupDir := GetPodQoSRelativePath(corev1.PodQOSBestEffort)
helper.WriteCgroupFileContents(qosCgroupDir, cpuset, "0-63")
containerCgroupDir := filepath.Join(qosCgroupDir, "/kubepods-test-besteffort-pod-0.slice/cri-containerd-xxx.scope")
sandboxCgroupDir := filepath.Join(qosCgroupDir, "/kubepods-test-besteffort-pod-0.slice/cri-containerd-yyy.scope")
helper.WriteCgroupFileContents(containerCgroupDir, cpuset, "0-63")
helper.WriteCgroupFileContents(sandboxCgroupDir, cpuset, "0-1")
},
},
wantErr: false,
want: []int32{0, 1},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
helper := system.NewFileTestUtil(t)
defer helper.Cleanup()
if tt.fields.prepareFn != nil {
tt.fields.prepareFn(helper)
}

got, gotErr := GetBECgroupCurCPUSet()
assert.Equal(t, tt.wantErr, gotErr != nil, gotErr)
assert.Equal(t, tt.want, got)
})
}
}
Loading