From 8f1ee2673b4fde70d70bfa17a2ee0330739c0d68 Mon Sep 17 00:00:00 2001 From: Nikolay Sivko Date: Wed, 7 Aug 2024 13:55:32 +0300 Subject: [PATCH] replace escaped hyphens in container names (fixes coroot/coroot#306) --- cgroup/cgroup.go | 2 +- cgroup/cgroup_test.go | 2 +- cgroup/cpu.go | 44 +++++++++++++++++++++---------------------- cgroup/utils.go | 8 ++++---- 4 files changed, 27 insertions(+), 29 deletions(-) diff --git a/cgroup/cgroup.go b/cgroup/cgroup.go index d3804d1..65202ae 100644 --- a/cgroup/cgroup.go +++ b/cgroup/cgroup.go @@ -167,7 +167,7 @@ func containerByCgroup(path string) (ContainerType, string, error) { if matches == nil { return ContainerTypeUnknown, "", fmt.Errorf("invalid systemd cgroup %s", path) } - return ContainerTypeSystemdService, matches[1], nil + return ContainerTypeSystemdService, strings.Replace(matches[1], "\\x2d", "-", -1), nil } return ContainerTypeUnknown, "", fmt.Errorf("unknown container: %s", path) } diff --git a/cgroup/cgroup_test.go b/cgroup/cgroup_test.go index bff6f5f..d5a5f60 100644 --- a/cgroup/cgroup_test.go +++ b/cgroup/cgroup_test.go @@ -119,7 +119,7 @@ func TestContainerByCgroup(t *testing.T) { typ, id, err = containerByCgroup("/system.slice/system-serial\\x2dgetty.slice") as.Equal(typ, ContainerTypeSystemdService) - as.Equal("/system.slice/system-serial\\x2dgetty.slice", id) + as.Equal("/system.slice/system-serial-getty.slice", id) as.Nil(err) typ, id, err = containerByCgroup("/runtime.slice/kubelet.service") diff --git a/cgroup/cpu.go b/cgroup/cpu.go index 4f74cd1..7491391 100644 --- a/cgroup/cpu.go +++ b/cgroup/cpu.go @@ -2,7 +2,7 @@ package cgroup import ( "fmt" - "io/ioutil" + "os" "path" "strconv" "strings" @@ -57,28 +57,26 @@ func (cg Cgroup) cpuStatV2() (*CPUStat, error) { UsageSeconds: float64(vars["usage_usec"]) / 1e6, ThrottledTimeSeconds: float64(vars["throttled_usec"]) / 1e6, } - payload, err := ioutil.ReadFile(path.Join(cgRoot, cg.subsystems[""], "cpu.max")) - if err != nil { - return nil, err - } - data := strings.TrimSpace(string(payload)) - parts := strings.Fields(data) - if len(parts) != 2 { - return nil, fmt.Errorf("invalid cpu.max payload: %s", data) - } - if parts[0] == "max" { //no limit - return res, nil - } - quotaUs, err := strconv.ParseUint(parts[0], 10, 64) - if err != nil { - return nil, fmt.Errorf("invalid quota value in cpu.max: %s", parts[0]) - } - periodUs, err := strconv.ParseUint(parts[1], 10, 64) - if err != nil { - return nil, fmt.Errorf("invalid period value in cpu.max: %s", parts[1]) - } - if periodUs > 0 { - res.LimitCores = float64(quotaUs) / float64(periodUs) + if payload, err := os.ReadFile(path.Join(cgRoot, cg.subsystems[""], "cpu.max")); err == nil { + data := strings.TrimSpace(string(payload)) + parts := strings.Fields(data) + if len(parts) != 2 { + return nil, fmt.Errorf("invalid cpu.max payload: %s", data) + } + if parts[0] == "max" { //no limit + return res, nil + } + quotaUs, err := strconv.ParseUint(parts[0], 10, 64) + if err != nil { + return nil, fmt.Errorf("invalid quota value in cpu.max: %s", parts[0]) + } + periodUs, err := strconv.ParseUint(parts[1], 10, 64) + if err != nil { + return nil, fmt.Errorf("invalid period value in cpu.max: %s", parts[1]) + } + if periodUs > 0 { + res.LimitCores = float64(quotaUs) / float64(periodUs) + } } return res, nil } diff --git a/cgroup/utils.go b/cgroup/utils.go index de8b969..aaa14ab 100644 --- a/cgroup/utils.go +++ b/cgroup/utils.go @@ -1,7 +1,7 @@ package cgroup import ( - "io/ioutil" + "os" "strconv" "strings" @@ -9,7 +9,7 @@ import ( ) func readVariablesFromFile(filePath string) (map[string]uint64, error) { - data, err := ioutil.ReadFile(filePath) + data, err := os.ReadFile(filePath) if err != nil { return nil, err } @@ -29,7 +29,7 @@ func readVariablesFromFile(filePath string) (map[string]uint64, error) { } func readIntFromFile(filePath string) (int64, error) { - data, err := ioutil.ReadFile(filePath) + data, err := os.ReadFile(filePath) if err != nil { return 0, err } @@ -37,7 +37,7 @@ func readIntFromFile(filePath string) (int64, error) { } func readUintFromFile(filePath string) (uint64, error) { - data, err := ioutil.ReadFile(filePath) + data, err := os.ReadFile(filePath) if err != nil { return 0, err }