mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-28 05:57:25 +00:00
stop double encoding systemd style cgroup names
This commit is contained in:
parent
b201ac2f8f
commit
7fe105ebc7
@ -49,25 +49,37 @@ func ConvertCgroupNameToSystemd(cgroupName CgroupName, outputToCgroupFs bool) st
|
|||||||
name := string(cgroupName)
|
name := string(cgroupName)
|
||||||
result := ""
|
result := ""
|
||||||
if name != "" && name != "/" {
|
if name != "" && name != "/" {
|
||||||
// systemd treats - as a step in the hierarchy, we convert all - to _
|
|
||||||
name = strings.Replace(name, "-", "_", -1)
|
|
||||||
parts := strings.Split(name, "/")
|
parts := strings.Split(name, "/")
|
||||||
|
results := []string{}
|
||||||
for _, part := range parts {
|
for _, part := range parts {
|
||||||
// ignore leading stuff for now
|
// ignore leading stuff
|
||||||
if part == "" {
|
if part == "" {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if len(result) > 0 {
|
// detect if we are given a systemd style name.
|
||||||
result = result + "-"
|
// 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 {
|
} else {
|
||||||
// root converts to -
|
// root converts to -
|
||||||
result = "-"
|
result = "-"
|
||||||
}
|
}
|
||||||
// always have a .slice suffix
|
// 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 the caller desired the result in cgroupfs format...
|
||||||
if outputToCgroupFs {
|
if outputToCgroupFs {
|
||||||
|
@ -29,6 +29,26 @@ func TestLibcontainerAdapterAdaptToSystemd(t *testing.T) {
|
|||||||
input: "/",
|
input: "/",
|
||||||
expected: "-.slice",
|
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",
|
input: "/Burstable",
|
||||||
expected: "Burstable.slice",
|
expected: "Burstable.slice",
|
||||||
|
Loading…
Reference in New Issue
Block a user