Skip to content

Commit

Permalink
Merge pull request #120 from coroot/issue_306
Browse files Browse the repository at this point in the history
replace escaped hyphens in container names
  • Loading branch information
def authored Aug 7, 2024
2 parents 094c562 + 8f1ee26 commit 3f56517
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 29 deletions.
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

0 comments on commit 3f56517

Please sign in to comment.