diff --git a/devices/systemd.go b/devices/systemd.go index 010f7f2..3f5fdce 100644 --- a/devices/systemd.go +++ b/devices/systemd.go @@ -166,7 +166,7 @@ func systemdProperties(r *cgroups.Resources, sdVer int) ([]systemdDbus.Property, return properties, nil } -func newProp(name string, units interface{}) systemdDbus.Property { +func newProp(name string, units any) systemdDbus.Property { return systemdDbus.Property{ Name: name, Value: dbus.MakeVariant(units), diff --git a/devices/systemd_test.go b/devices/systemd_test.go index 21b8a6d..8319fbd 100644 --- a/devices/systemd_test.go +++ b/devices/systemd_test.go @@ -95,7 +95,7 @@ func TestPodSkipDevicesUpdate(t *testing.T) { } // Now update the pod a few times. - for i := 0; i < 42; i++ { + for range 42 { podConfig.Resources.PidsLimit++ podConfig.Resources.Memory += 1024 * 1024 if err := pm.Set(podConfig.Resources); err != nil { diff --git a/file_test.go b/file_test.go index 3a9fac3..9c3bf18 100644 --- a/file_test.go +++ b/file_test.go @@ -32,7 +32,7 @@ func TestWriteCgroupFileHandlesInterrupt(t *testing.T) { t.Skip(err) } - for i := 0; i < 100000; i++ { + for i := range 100000 { limit := 1024*1024 + i if err := WriteFile(cgroupPath, memoryLimit, strconv.Itoa(limit)); err != nil { t.Fatalf("Failed to write %d on attempt %d: %+v", limit, i, err) diff --git a/fs/blkio_test.go b/fs/blkio_test.go index 31aafab..6ab1d77 100644 --- a/fs/blkio_test.go +++ b/fs/blkio_test.go @@ -1,6 +1,7 @@ package fs import ( + "maps" "strconv" "testing" @@ -465,10 +466,7 @@ func TestBlkioStatsNoFilesBFQDebug(t *testing.T) { for _, testCase := range testCases { path := tempDir(t, "cpuset") - tempBlkioTestFiles := map[string]string{} - for i, v := range blkioBFQDebugStatsTestFiles { - tempBlkioTestFiles[i] = v - } + tempBlkioTestFiles := maps.Clone(blkioBFQDebugStatsTestFiles) delete(tempBlkioTestFiles, testCase.filename) writeFileContents(t, path, tempBlkioTestFiles) @@ -579,10 +577,7 @@ func TestBlkioStatsNoFilesCFQ(t *testing.T) { for _, testCase := range testCases { path := tempDir(t, "cpuset") - tempBlkioTestFiles := map[string]string{} - for i, v := range blkioCFQStatsTestFiles { - tempBlkioTestFiles[i] = v - } + tempBlkioTestFiles := maps.Clone(blkioCFQStatsTestFiles) delete(tempBlkioTestFiles, testCase.filename) writeFileContents(t, path, tempBlkioTestFiles) diff --git a/fs/cpuset_test.go b/fs/cpuset_test.go index 58e571b..713447a 100644 --- a/fs/cpuset_test.go +++ b/fs/cpuset_test.go @@ -1,6 +1,7 @@ package fs import ( + "maps" "reflect" "testing" @@ -211,10 +212,7 @@ func TestCPUSetStatsMissingFiles(t *testing.T) { t.Run(testCase.desc, func(t *testing.T) { path := tempDir(t, "cpuset") - tempCpusetTestFiles := map[string]string{} - for i, v := range cpusetTestFiles { - tempCpusetTestFiles[i] = v - } + tempCpusetTestFiles := maps.Clone(cpusetTestFiles) if testCase.removeFile { delete(tempCpusetTestFiles, testCase.filename) diff --git a/fs/freezer.go b/fs/freezer.go index dae4a60..fe0f0dd 100644 --- a/fs/freezer.go +++ b/fs/freezer.go @@ -57,7 +57,7 @@ func (s *FreezerGroup) Set(path string, r *cgroups.Resources) (Err error) { // Alas, this is still a game of chances, since the real fix // belong to the kernel (cgroup v2 do not have this bug). - for i := 0; i < 1000; i++ { + for i := range 1000 { if i%50 == 49 { // Occasional thaw and sleep improves // the chances to succeed in freezing diff --git a/systemd/common.go b/systemd/common.go index b3077bd..ec5ff22 100644 --- a/systemd/common.go +++ b/systemd/common.go @@ -89,7 +89,7 @@ func ExpandSlice(slice string) (string, error) { return path, nil } -func newProp(name string, units interface{}) systemdDbus.Property { +func newProp(name string, units any) systemdDbus.Property { return systemdDbus.Property{ Name: name, Value: dbus.MakeVariant(units), @@ -266,7 +266,7 @@ func systemdVersionAtoi(str string) (int, error) { // Unconditionally remove the leading prefix ("v). str = strings.TrimLeft(str, `"v`) // Match on the first integer we can grab. - for i := 0; i < len(str); i++ { + for i := range len(str) { if str[i] < '0' || str[i] > '9' { // First non-digit: cut the tail. str = str[:i] diff --git a/systemd/systemd_test.go b/systemd/systemd_test.go index dae851c..436a009 100644 --- a/systemd/systemd_test.go +++ b/systemd/systemd_test.go @@ -90,7 +90,7 @@ func TestUnitExistsIgnored(t *testing.T) { pm := newManager(t, podConfig) // create twice to make sure "UnitExists" error is ignored. - for i := 0; i < 2; i++ { + for range 2 { if err := pm.Apply(-1); err != nil { t.Fatal(err) } diff --git a/utils.go b/utils.go index 9ef24b1..028b443 100644 --- a/utils.go +++ b/utils.go @@ -395,7 +395,7 @@ func WriteCgroupProc(dir string, pid int) error { } defer file.Close() - for i := 0; i < 5; i++ { + for range 5 { _, err = file.WriteString(strconv.Itoa(pid)) if err == nil { return nil diff --git a/utils_test.go b/utils_test.go index 58ac85a..2d30373 100644 --- a/utils_test.go +++ b/utils_test.go @@ -5,6 +5,7 @@ import ( "errors" "path/filepath" "reflect" + "slices" "strings" "testing" @@ -273,14 +274,7 @@ func TestGetCgroupMounts(t *testing.T) { if !strings.HasPrefix(m.Mountpoint, "/sys/fs/cgroup/") && !strings.Contains(m.Mountpoint, ss) { t.Fatalf("unexpected mountpoint for %s: %s", ss, m.Mountpoint) } - var ssFound bool - for _, mss := range m.Subsystems { - if mss == ss { - ssFound = true - break - } - } - if !ssFound { + if !slices.Contains(m.Subsystems, ss) { t.Fatalf("subsystem %s not found in Subsystems field %v", ss, m.Subsystems) } } diff --git a/v1_utils.go b/v1_utils.go index 81193e2..19b8af1 100644 --- a/v1_utils.go +++ b/v1_utils.go @@ -5,6 +5,7 @@ import ( "fmt" "os" "path/filepath" + "slices" "strings" "sync" "syscall" @@ -144,10 +145,8 @@ func FindCgroupMountpointAndRoot(cgroupPath, subsystem string) (string, string, func findCgroupMountpointAndRootFromMI(mounts []*mountinfo.Info, cgroupPath, subsystem string) (string, string, error) { for _, mi := range mounts { if strings.HasPrefix(mi.Mountpoint, cgroupPath) { - for _, opt := range strings.Split(mi.VFSOptions, ",") { - if opt == subsystem { - return mi.Mountpoint, mi.Root, nil - } + if slices.Contains(strings.Split(mi.VFSOptions, ","), subsystem) { + return mi.Mountpoint, mi.Root, nil } } }