From 28a1de08ab0e380f182bc1cd0a6bc55ba22f2591 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roman=20Van=C3=AD=C4=8Dek?= Date: Mon, 31 Jul 2023 19:03:21 +0200 Subject: [PATCH] Fix container Id detection from cgroup as "12:rdma:/" is possible --- plugin.go | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/plugin.go b/plugin.go index 606fe21..5a0c502 100644 --- a/plugin.go +++ b/plugin.go @@ -168,17 +168,24 @@ func getContainerId() (string, error) { defer file.Close() scanner := bufio.NewScanner(file) - if !scanner.Scan() { - return "", errors.New("cgroup is empty") + for scanner.Scan() { + // The file name is the container identifier + // Example: 12:rdma:/docker/a33940bf95ec6c57b6d79a3edfa784a1364ce840fb92c9a7f3951b9f7b0ccccb/docker/330d9b057fdd54b224cd38daf370524f7e8c567ccf95f33a2388f02c0b854982 + line := scanner.Text() + pathOffset := strings.LastIndex(line, ":") + if pathOffset == -1 { + continue + } + + cgroup := line[pathOffset + 1:] + + result := path.Base(cgroup) + if result == "." || result == "/" { + continue + } + + return result, nil } - // The file name is the container identifier - // Example: 12:rdma:/docker/a33940bf95ec6c57b6d79a3edfa784a1364ce840fb92c9a7f3951b9f7b0ccccb/docker/330d9b057fdd54b224cd38daf370524f7e8c567ccf95f33a2388f02c0b854982 - cgroup := scanner.Text() - result := path.Base(cgroup) - if result == "." { - return "", errors.New("cgroup is unexpected") - } - - return result, nil + return "", errors.New("no suitable cgroup entry found") } \ No newline at end of file