Skip to content

Commit

Permalink
stop double encoding systemd style cgroup names
Browse files Browse the repository at this point in the history
  • Loading branch information
derekwaynecarr committed Feb 21, 2017
1 parent b201ac2 commit 7fe105e
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 7 deletions.
26 changes: 19 additions & 7 deletions pkg/kubelet/cm/cgroup_manager_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,25 +49,37 @@ func ConvertCgroupNameToSystemd(cgroupName CgroupName, outputToCgroupFs bool) st
name := string(cgroupName)
result := ""
if name != "" && name != "/" {
// systemd treats - as a step in the hierarchy, we convert all - to _
name = strings.Replace(name, "-", "_", -1)
parts := strings.Split(name, "/")
results := []string{}
for _, part := range parts {
// ignore leading stuff for now
// ignore leading stuff
if part == "" {
continue
}
if len(result) > 0 {
result = result + "-"
// detect if we are given a systemd style name.
// if so, we do not want to do double encoding.
if strings.HasSuffix(part, ".slice") {
part = strings.TrimSuffix(part, ".slice")
separatorIndex := strings.LastIndex(part, "-")
if separatorIndex >= 0 && separatorIndex < len(part) {
part = part[separatorIndex+1:]
}
} else {
// systemd treats - as a step in the hierarchy, we convert all - to _
part = strings.Replace(part, "-", "_", -1)
}
result = result + part
results = append(results, part)
}
// each part is appended with systemd style -
result = strings.Join(results, "-")
} else {
// root converts to -
result = "-"
}
// always have a .slice suffix
result = result + ".slice"
if !strings.HasSuffix(result, ".slice") {
result = result + ".slice"
}

// if the caller desired the result in cgroupfs format...
if outputToCgroupFs {
Expand Down
20 changes: 20 additions & 0 deletions pkg/kubelet/cm/cgroup_manager_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,26 @@ func TestLibcontainerAdapterAdaptToSystemd(t *testing.T) {
input: "/",
expected: "-.slice",
},
{
input: "/system.slice",
expected: "system.slice",
},
{
input: "/system.slice/Burstable",
expected: "system-Burstable.slice",
},
{
input: "/Burstable.slice/Burstable-pod_123.slice",
expected: "Burstable-pod_123.slice",
},
{
input: "/test.slice/test-a.slice/test-a-b.slice",
expected: "test-a-b.slice",
},
{
input: "/test.slice/test-a.slice/test-a-b.slice/Burstable",
expected: "test-a-b-Burstable.slice",
},
{
input: "/Burstable",
expected: "Burstable.slice",
Expand Down

0 comments on commit 7fe105e

Please sign in to comment.