Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

replace escaped hyphens in container names #120

Merged
merged 1 commit into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cgroup/cgroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
2 changes: 1 addition & 1 deletion cgroup/cgroup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
44 changes: 21 additions & 23 deletions cgroup/cpu.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package cgroup

import (
"fmt"
"io/ioutil"
"os"
"path"
"strconv"
"strings"
Expand Down Expand Up @@ -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
}
8 changes: 4 additions & 4 deletions cgroup/utils.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package cgroup

import (
"io/ioutil"
"os"
"strconv"
"strings"

"k8s.io/klog/v2"
)

func readVariablesFromFile(filePath string) (map[string]uint64, error) {
data, err := ioutil.ReadFile(filePath)
data, err := os.ReadFile(filePath)
if err != nil {
return nil, err
}
Expand All @@ -29,15 +29,15 @@ 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
}
return strconv.ParseInt(strings.TrimSpace(string(data)), 10, 64)
}

func readUintFromFile(filePath string) (uint64, error) {
data, err := ioutil.ReadFile(filePath)
data, err := os.ReadFile(filePath)
if err != nil {
return 0, err
}
Expand Down