Skip to content

Commit 6eabaf9

Browse files
committed
refactor(pod): move scope-name resolution into containerCgroupPath
Container-level naming was split across two layers: cgroupfs appended containerID inside containerCgroupPath, while systemd appended the .scope unit in containerCgroupSuffix. Consolidate both in containerCgroupPath by carrying the optional scope on cgroupPath, so the suffix function only formats.
1 parent f5a103a commit 6eabaf9

1 file changed

Lines changed: 33 additions & 18 deletions

File tree

internal/pod/container_cgroup_path.go

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,12 @@ const (
2727
defaultNodeCgroupName = "kubepods"
2828
)
2929

30-
// {"kubepods", "burstable", "pod1234-abcd-5678-efgh"}
31-
type cgroupPath []string
30+
// slices: {"kubepods", "burstable", "pod1234-abcd-5678-efgh"}
31+
// scope: systemd scope unit appended after the slice hierarchy (systemd driver only).
32+
type cgroupPath struct {
33+
slices []string
34+
scope string
35+
}
3236

3337
func escapeSystemd(part string) string {
3438
return strings.ReplaceAll(part, "-", "_")
@@ -52,21 +56,29 @@ func expandSystemdSlice(slice string) string {
5256

5357
// {"kubepods", "burstable", "pod1234-abcd-5678-efgh"} becomes
5458
// "/kubepods.slice/kubepods-burstable.slice/kubepods-burstable-pod1234_abcd_5678_efgh.slice"
55-
func (paths cgroupPath) ToSystemd() string {
59+
// with the scope unit appended when present.
60+
func (p cgroupPath) ToSystemd() string {
5661
newparts := []string{}
57-
for _, part := range paths {
62+
for _, part := range p.slices {
5863
part = escapeSystemd(part)
5964
newparts = append(newparts, part)
6065
}
6166

62-
return expandSystemdSlice(strings.Join(newparts, "-") + defaultSystemdSuffix)
67+
slicePath := expandSystemdSlice(strings.Join(newparts, "-") + defaultSystemdSuffix)
68+
69+
if p.scope != "" {
70+
return slicePath + "/" + p.scope
71+
}
72+
73+
return slicePath
6374
}
6475

65-
func (paths cgroupPath) ToCgroupfs() string {
66-
return "/" + path.Join(paths...)
76+
func (p cgroupPath) ToCgroupfs() string {
77+
return "/" + path.Join(p.slices...)
6778
}
6879

69-
func containerCgroupPath(containerID string, pod *corev1.Pod) cgroupPath {
80+
// https://github.com/kubernetes/kubernetes/blob/master/pkg/kubelet/cm/cgroup_manager_linux.go#L81
81+
func containerCgroupPath(containerID string, pod *corev1.Pod) (cgroupPath, error) {
7082
paths := []string{defaultNodeCgroupName}
7183

7284
if pod.Status.QOSClass != corev1.PodQOSGuaranteed {
@@ -75,23 +87,26 @@ func containerCgroupPath(containerID string, pod *corev1.Pod) cgroupPath {
7587

7688
paths = append(paths, fmt.Sprintf("pod%s", pod.UID))
7789

78-
if kubeletPodCgroupDriver != "systemd" {
79-
paths = append(paths, containerID)
90+
if kubeletPodCgroupDriver == "systemd" {
91+
scope, err := containerScopeName(containerID)
92+
if err != nil {
93+
return cgroupPath{}, fmt.Errorf("container scope name: %w", err)
94+
}
95+
return cgroupPath{slices: paths, scope: scope}, nil
8096
}
8197

82-
return paths
98+
paths = append(paths, containerID)
99+
return cgroupPath{slices: paths}, nil
83100
}
84101

85-
// https://github.com/kubernetes/kubernetes/blob/master/pkg/kubelet/cm/cgroup_manager_linux.go#L81
86102
func containerCgroupSuffix(containerID string, pod *corev1.Pod) (string, error) {
87-
name := containerCgroupPath(containerID, pod)
103+
name, err := containerCgroupPath(containerID, pod)
104+
if err != nil {
105+
return "", err
106+
}
88107

89108
if kubeletPodCgroupDriver == "systemd" {
90-
scope, err := containerScopeName(containerID)
91-
if err != nil {
92-
return "", fmt.Errorf("container scope name: %w", err)
93-
}
94-
return name.ToSystemd() + "/" + scope, nil
109+
return name.ToSystemd(), nil
95110
}
96111

97112
return name.ToCgroupfs(), nil

0 commit comments

Comments
 (0)