Extract containerID from systemd-style cgroupPath in cri_stats_provider

And fix test to generate UUID without dash
This commit is contained in:
YanzhaoLi 2021-08-09 18:23:23 -07:00
parent 7ee94f2f4f
commit 545d898584
2 changed files with 46 additions and 3 deletions

View File

@ -826,7 +826,24 @@ func getCRICadvisorStats(infos map[string]cadvisorapiv2.ContainerInfo) map[strin
if !isPodManagedContainer(&info) {
continue
}
stats[path.Base(key)] = info
stats[extractIDFromCgroupPath(key)] = info
}
return stats
}
func extractIDFromCgroupPath(cgroupPath string) string {
// case0 == cgroupfs: "/kubepods/burstable/pod2fc932ce-fdcc-454b-97bd-aadfdeb4c340/9be25294016e2dc0340dd605ce1f57b492039b267a6a618a7ad2a7a58a740f32"
id := path.Base(cgroupPath)
// case1 == systemd: "/kubepods.slice/kubepods-burstable.slice/kubepods-burstable-pod2fc932ce_fdcc_454b_97bd_aadfdeb4c340.slice/cri-containerd-aaefb9d8feed2d453b543f6d928cede7a4dbefa6a0ae7c9b990dd234c56e93b9.scope"
// trim anything before the final '-' and suffix .scope
systemdSuffix := ".scope"
if strings.HasSuffix(id, systemdSuffix) {
id = strings.TrimSuffix(id, systemdSuffix)
components := strings.Split(id, "-")
if len(components) > 1 {
id = components[len(components)-1]
}
}
return id
}

View File

@ -21,6 +21,7 @@ import (
"os"
"path/filepath"
"runtime"
"strings"
"testing"
"time"
@ -696,7 +697,7 @@ func makeFakePodSandbox(name, uid, namespace string, terminated bool) *critest.F
if terminated {
p.PodSandboxStatus.State = runtimeapi.PodSandboxState_SANDBOX_NOTREADY
}
p.PodSandboxStatus.Id = string(uuid.NewUUID())
p.PodSandboxStatus.Id = strings.ReplaceAll(string(uuid.NewUUID()), "-", "")
return p
}
@ -722,7 +723,7 @@ func makeFakeContainer(sandbox *critest.FakePodSandbox, name string, attempt uin
} else {
c.ContainerStatus.State = runtimeapi.ContainerState_CONTAINER_RUNNING
}
c.ContainerStatus.Id = string(uuid.NewUUID())
c.ContainerStatus.Id = strings.ReplaceAll(string(uuid.NewUUID()), "-", "")
return c
}
@ -1072,3 +1073,28 @@ func TestGetContainerUsageNanoCores(t *testing.T) {
assert.Equal(t, test.expected, cached, test.desc)
}
}
func TestExtractIDFromCgroupPath(t *testing.T) {
tests := []struct {
cgroupPath string
expected string
}{
{
cgroupPath: "/kubepods/burstable/pod2fc932ce-fdcc-454b-97bd-aadfdeb4c340/9be25294016e2dc0340dd605ce1f57b492039b267a6a618a7ad2a7a58a740f32",
expected: "9be25294016e2dc0340dd605ce1f57b492039b267a6a618a7ad2a7a58a740f32",
},
{
cgroupPath: "/kubepods.slice/kubepods-burstable.slice/kubepods-burstable-pod2fc932ce_fdcc_454b_97bd_aadfdeb4c340.slice/cri-containerd-aaefb9d8feed2d453b543f6d928cede7a4dbefa6a0ae7c9b990dd234c56e93b9.scope",
expected: "aaefb9d8feed2d453b543f6d928cede7a4dbefa6a0ae7c9b990dd234c56e93b9",
},
{
cgroupPath: "/kubepods.slice/kubepods-burstable.slice/kubepods-burstable-pod2fc932ce_fdcc_454b_97bd_aadfdeb4c340.slice/cri-o-aaefb9d8feed2d453b543f6d928cede7a4dbefa6a0ae7c9b990dd234c56e93b9.scope",
expected: "aaefb9d8feed2d453b543f6d928cede7a4dbefa6a0ae7c9b990dd234c56e93b9",
},
}
for _, test := range tests {
id := extractIDFromCgroupPath(test.cgroupPath)
assert.Equal(t, test.expected, id)
}
}