diff --git a/blkio.go b/blkio.go index 0e0534fb85d7d..a837e19fbffa2 100644 --- a/blkio.go +++ b/blkio.go @@ -72,7 +72,7 @@ func (b *blkioController) Create(path string, resources *specs.LinuxResources) e for _, t := range createBlkioSettings(resources.BlockIO) { if t.value != nil { if err := retryingWriteFile( - filepath.Join(b.Path(path), fmt.Sprintf("blkio.%s", t.name)), + filepath.Join(b.Path(path), "blkio."+t.name), t.format(t.value), defaultFilePerm, ); err != nil { @@ -93,7 +93,7 @@ func (b *blkioController) Stat(path string, stats *v1.Metrics) error { var settings []blkioStatSettings // Try to read CFQ stats available on all CFQ enabled kernels first - if _, err := os.Lstat(filepath.Join(b.Path(path), fmt.Sprintf("blkio.io_serviced_recursive"))); err == nil { + if _, err := os.Lstat(filepath.Join(b.Path(path), "blkio.io_serviced_recursive")); err == nil { settings = []blkioStatSettings{ { name: "sectors_recursive", @@ -173,7 +173,7 @@ func (b *blkioController) Stat(path string, stats *v1.Metrics) error { } func (b *blkioController) readEntry(devices map[deviceKey]string, path, name string, entry *[]*v1.BlkIOEntry) error { - f, err := os.Open(filepath.Join(b.Path(path), fmt.Sprintf("blkio.%s", name))) + f, err := os.Open(filepath.Join(b.Path(path), "blkio."+name)) if err != nil { return err } @@ -187,7 +187,7 @@ func (b *blkioController) readEntry(devices map[deviceKey]string, path, name str // skip total line continue } else { - return fmt.Errorf("Invalid line found while parsing %s: %s", path, sc.Text()) + return fmt.Errorf("invalid line found while parsing %s: %s", path, sc.Text()) } } major, err := strconv.ParseUint(fields[0], 10, 64) @@ -356,11 +356,3 @@ func getDevices(r io.Reader) (map[deviceKey]string, error) { } return devices, s.Err() } - -func major(devNumber uint64) uint64 { - return (devNumber >> 8) & 0xfff -} - -func minor(devNumber uint64) uint64 { - return (devNumber & 0xff) | ((devNumber >> 12) & 0xfff00) -} diff --git a/cgroup.go b/cgroup.go index 6c19f96e89019..e0e014b282f4f 100644 --- a/cgroup.go +++ b/cgroup.go @@ -216,7 +216,7 @@ func (c *cgroup) Delete() error { if c.err != nil { return c.err } - var errors []string + var errs []string for _, s := range c.subsystems { if d, ok := s.(deleter); ok { sp, err := c.path(s.Name()) @@ -224,7 +224,7 @@ func (c *cgroup) Delete() error { return err } if err := d.Delete(sp); err != nil { - errors = append(errors, string(s.Name())) + errs = append(errs, string(s.Name())) } continue } @@ -235,12 +235,12 @@ func (c *cgroup) Delete() error { } path := p.Path(sp) if err := remove(path); err != nil { - errors = append(errors, path) + errs = append(errs, path) } } } - if len(errors) > 0 { - return fmt.Errorf("cgroups: unable to remove paths %s", strings.Join(errors, ", ")) + if len(errs) > 0 { + return fmt.Errorf("cgroups: unable to remove paths %s", strings.Join(errs, ", ")) } c.err = ErrCgroupDeleted return nil diff --git a/cgroup_test.go b/cgroup_test.go index 68df81d8b839c..b8971055c12cd 100644 --- a/cgroup_test.go +++ b/cgroup_test.go @@ -207,7 +207,7 @@ func checkPid(mock *mockCgroup, path string, expected int) error { if err != nil { return err } - v, err := strconv.Atoi(string(data)) + v, err := strconv.Atoi(data) if err != nil { return err } @@ -222,7 +222,7 @@ func checkTaskid(mock *mockCgroup, path string, expected int) error { if err != nil { return err } - v, err := strconv.Atoi(string(data)) + v, err := strconv.Atoi(data) if err != nil { return err } diff --git a/cpu.go b/cpu.go index a7e530bf289c9..27024f17b826c 100644 --- a/cpu.go +++ b/cpu.go @@ -18,7 +18,6 @@ package cgroups import ( "bufio" - "fmt" "os" "path/filepath" "strconv" @@ -84,7 +83,7 @@ func (c *cpuController) Create(path string, resources *specs.LinuxResources) err } if value != nil { if err := retryingWriteFile( - filepath.Join(c.Path(path), fmt.Sprintf("cpu.%s", t.name)), + filepath.Join(c.Path(path), "cpu."+t.name), value, defaultFilePerm, ); err != nil { diff --git a/cpuset.go b/cpuset.go index 39573dcdb361e..3cae173bdd1b4 100644 --- a/cpuset.go +++ b/cpuset.go @@ -70,7 +70,7 @@ func (c *cpusetController) Create(path string, resources *specs.LinuxResources) } { if t.value != "" { if err := retryingWriteFile( - filepath.Join(c.Path(path), fmt.Sprintf("cpuset.%s", t.name)), + filepath.Join(c.Path(path), "cpuset."+t.name), []byte(t.value), defaultFilePerm, ); err != nil { diff --git a/memory.go b/memory.go index 3f5c3c0336d21..d3810c5ae8e5a 100644 --- a/memory.go +++ b/memory.go @@ -378,7 +378,7 @@ func (m *memoryController) set(path string, settings []memorySettings) error { for _, t := range settings { if t.value != nil { if err := retryingWriteFile( - filepath.Join(m.Path(path), fmt.Sprintf("memory.%s", t.name)), + filepath.Join(m.Path(path), "memory."+t.name), []byte(strconv.FormatInt(*t.value, 10)), defaultFilePerm, ); err != nil { diff --git a/opts.go b/opts.go index 7c5d9fb9c27c3..a1449e298d0ef 100644 --- a/opts.go +++ b/opts.go @@ -48,12 +48,12 @@ func newInitConfig() *InitConfig { type InitCheck func(Subsystem, Path, error) error // AllowAny allows any subsystem errors to be skipped -func AllowAny(s Subsystem, p Path, err error) error { +func AllowAny(_ Subsystem, _ Path, _ error) error { return ErrIgnoreSubsystem } // RequireDevices requires the device subsystem but no others -func RequireDevices(s Subsystem, p Path, err error) error { +func RequireDevices(s Subsystem, _ Path, _ error) error { if s.Name() == Devices { return ErrDevicesRequired } diff --git a/paths.go b/paths.go index f45fd425643e6..27197ecad795c 100644 --- a/paths.go +++ b/paths.go @@ -25,7 +25,7 @@ import ( type Path func(subsystem Name) (string, error) -func RootPath(subsysem Name) (string, error) { +func RootPath(subsystem Name) (string, error) { return "/", nil } @@ -63,7 +63,7 @@ var ErrControllerNotActive = errors.New("controller is not supported") func existingPath(paths map[string]string, suffix string) Path { // localize the paths based on the root mount dest for nested cgroups for n, p := range paths { - dest, err := getCgroupDestination(string(n)) + dest, err := getCgroupDestination(n) if err != nil { return errorPath(err) } @@ -79,7 +79,7 @@ func existingPath(paths map[string]string, suffix string) Path { return func(name Name) (string, error) { root, ok := paths[string(name)] if !ok { - if root, ok = paths[fmt.Sprintf("name=%s", name)]; !ok { + if root, ok = paths["name="+string(name)]; !ok { return "", ErrControllerNotActive } } diff --git a/systemd.go b/systemd.go index 4bae17f168ed4..c17f34a62607f 100644 --- a/systemd.go +++ b/systemd.go @@ -17,7 +17,6 @@ package cgroups import ( - "fmt" "path/filepath" "strings" "sync" @@ -78,7 +77,7 @@ func (s *SystemdController) Name() Name { return SystemdDbus } -func (s *SystemdController) Create(path string, resources *specs.LinuxResources) error { +func (s *SystemdController) Create(path string, _ *specs.LinuxResources) error { conn, err := systemdDbus.New() if err != nil { return err @@ -105,7 +104,7 @@ func (s *SystemdController) Create(path string, resources *specs.LinuxResources) } once.Do(checkDelegate) properties := []systemdDbus.Property{ - systemdDbus.PropDescription(fmt.Sprintf("cgroup %s", name)), + systemdDbus.PropDescription("cgroup " + name), systemdDbus.PropWants(slice), newProperty("DefaultDependencies", false), newProperty("MemoryAccounting", true), @@ -150,10 +149,6 @@ func newProperty(name string, units interface{}) systemdDbus.Property { } } -func unitName(name string) string { - return fmt.Sprintf("%s.slice", name) -} - func splitName(path string) (slice string, unit string) { slice, unit = filepath.Split(path) return strings.TrimSuffix(slice, "/"), unit diff --git a/utils.go b/utils.go index f52df59e1d303..fed9af02923b5 100644 --- a/utils.go +++ b/utils.go @@ -382,14 +382,14 @@ func cleanPath(path string) string { if !filepath.IsAbs(path) { path, _ = filepath.Rel(string(os.PathSeparator), filepath.Clean(string(os.PathSeparator)+path)) } - return filepath.Clean(path) + return path } func retryingWriteFile(path string, data []byte, mode os.FileMode) error { // Retry writes on EINTR; see: // https://github.com/golang/go/issues/38033 for { - err := ioutil.WriteFile(path, []byte(data), mode) + err := ioutil.WriteFile(path, data, mode) if err == nil { return nil } else if !errors.Is(err, syscall.EINTR) { diff --git a/v2/cpuv2_test.go b/v2/cpuv2_test.go index e5ace2f3c693e..4f75f7c4dd0f2 100644 --- a/v2/cpuv2_test.go +++ b/v2/cpuv2_test.go @@ -81,7 +81,7 @@ func TestExtractQuotaAndPeriod(t *testing.T) { assert.Equal(t, quota, tquota) assert.Equal(t, period, tPeriod) - //case with nil quota which makes it "max" - max int val + // case with nil quota which makes it "max" - max int val cpuMax2 := NewCPUMax(nil, &period) tquota2, tPeriod2 := cpuMax2.extractQuotaAndPeriod() diff --git a/v2/errors.go b/v2/errors.go index 46d2d9c2e18f4..dfab548e35ef8 100644 --- a/v2/errors.go +++ b/v2/errors.go @@ -44,7 +44,3 @@ func IgnoreNotExist(err error) error { } return err } - -func errPassthrough(err error) error { - return err -} diff --git a/v2/iov2_test.go b/v2/iov2_test.go index 2a6bd726f5772..e971fe391a684 100644 --- a/v2/iov2_test.go +++ b/v2/iov2_test.go @@ -28,7 +28,7 @@ func TestCgroupv2IOController(t *testing.T) { group := "/io-test-cg" groupPath := fmt.Sprintf("%s-%d", group, os.Getpid()) var ( - //weight uint16 = 100 + // weight uint16 = 100 maj int64 = 8 min int64 = 0 rate uint64 = 120 diff --git a/v2/manager.go b/v2/manager.go index ac1b06e037128..3bb546cb66e18 100644 --- a/v2/manager.go +++ b/v2/manager.go @@ -18,27 +18,22 @@ package v2 import ( "bufio" - stderrors "errors" - "fmt" "io/ioutil" "math" "os" "path/filepath" "strconv" "strings" - "sync" "syscall" "time" - "golang.org/x/sys/unix" - "github.com/containerd/cgroups/v2/stats" + systemdDbus "github.com/coreos/go-systemd/v22/dbus" "github.com/godbus/dbus/v5" "github.com/opencontainers/runtime-spec/specs-go" "github.com/pkg/errors" "github.com/sirupsen/logrus" - - systemdDbus "github.com/coreos/go-systemd/v22/dbus" + "golang.org/x/sys/unix" ) const ( @@ -50,13 +45,8 @@ const ( var ( canDelegate bool - once sync.Once ) -type cgValuer interface { - Values() []Value -} - type Event struct { Low uint64 High uint64 @@ -161,7 +151,7 @@ func (c *Value) write(path string, perm os.FileMode) error { ) if err == nil { return nil - } else if !stderrors.Is(err, syscall.EINTR) { + } else if !errors.Is(err, syscall.EINTR) { return err } } @@ -270,7 +260,7 @@ func (c *Manager) ToggleControllers(controllers []string, t ControllerToggle) er // Note that /sys/fs/cgroup/foo/bar/baz/cgroup.subtree_control does not need to be written. split := strings.Split(c.path, "/") var lastErr error - for i, _ := range split { + for i := range split { f := strings.Join(split[:i], "/") if !strings.HasPrefix(f, c.unifiedMountpoint) || f == c.path { continue @@ -373,8 +363,7 @@ func (c *Manager) Stat() (*stats.Metrics, error) { for _, controller := range controllers { switch controller { case "cpu", "memory": - filename := fmt.Sprintf("%s.stat", controller) - if err := readKVStatsFile(c.path, filename, out); err != nil { + if err := readKVStatsFile(c.path, controller+".stat", out); err != nil { if os.IsNotExist(err) { continue } @@ -681,7 +670,7 @@ func NewSystemd(slice, group string, pid int, resources *Resources) (*Manager, e defer conn.Close() properties := []systemdDbus.Property{ - systemdDbus.PropDescription(fmt.Sprintf("cgroup %s", group)), + systemdDbus.PropDescription("cgroup " + group), newSystemdProperty("DefaultDependencies", false), newSystemdProperty("MemoryAccounting", true), newSystemdProperty("CPUAccounting", true), diff --git a/v2/paths.go b/v2/paths.go index 6f2f5edb3d9fb..c4778c14244be 100644 --- a/v2/paths.go +++ b/v2/paths.go @@ -29,7 +29,7 @@ func NestedGroupPath(suffix string) (string, error) { if err != nil { return "", err } - return filepath.Join(string(path), suffix), nil + return filepath.Join(path, suffix), nil } // PidGroupPath will return the correct cgroup paths for an existing process running inside a cgroup diff --git a/v2/pidsv2_test.go b/v2/pidsv2_test.go index 86d0ffb023173..949fc8fdbd663 100644 --- a/v2/pidsv2_test.go +++ b/v2/pidsv2_test.go @@ -51,5 +51,5 @@ func TestSystemdCgroupPidsController(t *testing.T) { if err != nil { t.Fatal("failed to init new cgroup systemd manager: ", err) } - checkFileContent(t, c.path, "cgroup.procs", strconv.Itoa(int(pid))) + checkFileContent(t, c.path, "cgroup.procs", strconv.Itoa(pid)) } diff --git a/v2/testutils_test.go b/v2/testutils_test.go index 98d5472fdd00a..a329ce89fc5f8 100644 --- a/v2/testutils_test.go +++ b/v2/testutils_test.go @@ -24,7 +24,6 @@ import ( "testing" "github.com/stretchr/testify/assert" - "golang.org/x/sys/unix" ) diff --git a/v2/utils.go b/v2/utils.go index d20c15cd6e07b..8b8d654d6c9a1 100644 --- a/v2/utils.go +++ b/v2/utils.go @@ -28,9 +28,8 @@ import ( "strings" "time" - "github.com/godbus/dbus/v5" - "github.com/containerd/cgroups/v2/stats" + "github.com/godbus/dbus/v5" "github.com/opencontainers/runtime-spec/specs-go" "github.com/pkg/errors" "github.com/sirupsen/logrus" @@ -106,14 +105,6 @@ func parseKV(raw string) (string, interface{}, error) { } } -func readUint(path string) (uint64, error) { - v, err := ioutil.ReadFile(path) - if err != nil { - return 0, err - } - return parseUint(strings.TrimSpace(string(v)), 10, 64) -} - func parseUint(s string, base, bitSize int) (uint64, error) { v, err := strconv.ParseUint(s, base, bitSize) if err != nil { @@ -178,7 +169,7 @@ func ToResources(spec *specs.LinuxResources) *Resources { Mems: cpu.Mems, } if shares := cpu.Shares; shares != nil { - convertedWeight := (1 + ((*shares-2)*9999)/262142) + convertedWeight := 1 + ((*shares-2)*9999)/262142 resources.CPU.Weight = &convertedWeight } if period := cpu.Period; period != nil { @@ -301,8 +292,8 @@ func readIoStats(path string) []*stats.IOEntry { Major: major, Minor: minor, } - for _, stats := range parts { - keyPairValue := strings.Split(stats, "=") + for _, s := range parts { + keyPairValue := strings.Split(s, "=") if len(keyPairValue) != 2 { continue } diff --git a/v2/utils_test.go b/v2/utils_test.go index e5c074c753778..b5ecf91a7630b 100644 --- a/v2/utils_test.go +++ b/v2/utils_test.go @@ -33,8 +33,8 @@ func TestParseCgroupFromReader(t *testing.T) { for s, expected := range cases { g, err := parseCgroupFromReader(strings.NewReader(s)) if expected != "" { - if string(g) != expected { - t.Errorf("expected %q, got %q", expected, string(g)) + if g != expected { + t.Errorf("expected %q, got %q", expected, g) } if err != nil { t.Error(err) @@ -53,7 +53,7 @@ func TestToResources(t *testing.T) { period uint64 = 10000 shares uint64 = 5000 ) - weight := (1 + ((shares-2)*9999)/262142) + weight := 1 + ((shares-2)*9999)/262142 res := specs.LinuxResources{CPU: &specs.LinuxCPU{Quota: "a, Period: &period, Shares: &shares}} v2resources := ToResources(&res)