From 2c75374a2fc2d52e32ac73c8221bc8bfabbf6abb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Gronowski?= Date: Wed, 2 Sep 2026 17:25:42 +0200 Subject: [PATCH] all: Use slices.Backward for reverse iteration MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Paweł Gronowski --- cmd/containerd/server/server.go | 4 ++-- core/diff/stream.go | 5 +++-- core/mount/manager/manager.go | 9 +++++---- core/mount/mount.go | 4 ++-- core/remotes/docker/fetcher.go | 5 +++-- core/remotes/handlers.go | 5 +++-- plugins/cri/cri.go | 5 +++-- 7 files changed, 21 insertions(+), 16 deletions(-) diff --git a/cmd/containerd/server/server.go b/cmd/containerd/server/server.go index 35cb027d26..d6eec3c05b 100644 --- a/cmd/containerd/server/server.go +++ b/cmd/containerd/server/server.go @@ -24,6 +24,7 @@ import ( "os" "path/filepath" "runtime" + "slices" "sync" "sync/atomic" "time" @@ -280,8 +281,7 @@ func (s *Server) Start(ctx context.Context) error { // Stop the containerd server canceling any open connections func (s *Server) Stop() { - for i := len(s.plugins) - 1; i >= 0; i-- { - p := s.plugins[i] + for _, p := range slices.Backward(s.plugins) { instance, err := p.Instance() if err != nil { log.L.WithFields(log.Fields{"error": err, "id": p.Registration.URI()}).Error("could not get plugin instance") diff --git a/core/diff/stream.go b/core/diff/stream.go index f44f8fa9ca..440b1197f0 100644 --- a/core/diff/stream.go +++ b/core/diff/stream.go @@ -21,6 +21,7 @@ import ( "errors" "io" "os" + "slices" "github.com/containerd/containerd/v2/core/images" "github.com/containerd/containerd/v2/pkg/archive/compression" @@ -48,8 +49,8 @@ func RegisterProcessor(handler Handler) { // GetProcessor returns the processor for a media-type func GetProcessor(ctx context.Context, stream StreamProcessor, payloads map[string]typeurl.Any) (StreamProcessor, error) { // reverse this list so that user configured handlers come up first - for i := len(handlers) - 1; i >= 0; i-- { - processor, ok := handlers[i](ctx, stream.MediaType()) + for _, handler := range slices.Backward(handlers) { + processor, ok := handler(ctx, stream.MediaType()) if ok { return processor(ctx, stream, payloads) } diff --git a/core/mount/manager/manager.go b/core/mount/manager/manager.go index bf826d7b4a..9588a27296 100644 --- a/core/mount/manager/manager.go +++ b/core/mount/manager/manager.go @@ -24,6 +24,7 @@ import ( "fmt" "os" "path/filepath" + "slices" "strconv" "strings" "sync" @@ -720,12 +721,12 @@ func (mm *mountManager) Deactivate(ctx context.Context, name string) error { // TODO: Should this also be backgrounded, no much can do on failure to unmount var mountErrors error - for i := len(allActive) - 1; i >= 0; i-- { + for _, active := range slices.Backward(allActive) { var err error - if h := mm.handlers[allActive[i].Type]; h != nil { - err = h.Unmount(ctx, allActive[i].MountPoint) + if h := mm.handlers[active.Type]; h != nil { + err = h.Unmount(ctx, active.MountPoint) } else { - err = mount.Unmount(allActive[i].MountPoint, 0) + err = mount.Unmount(active.MountPoint, 0) } if err != nil { mountErrors = errors.Join(mountErrors, err) diff --git a/core/mount/mount.go b/core/mount/mount.go index 4caecfc451..7f2013232c 100644 --- a/core/mount/mount.go +++ b/core/mount/mount.go @@ -66,8 +66,8 @@ func All(mounts []Mount, target string) error { // UnmountMounts unmounts all the mounts under a target in the reverse order of // the mounts array provided. func UnmountMounts(mounts []Mount, target string, flags int) error { - for i := len(mounts) - 1; i >= 0; i-- { - mountpoint, err := fs.RootPath(target, mounts[i].Target) + for i, m := range slices.Backward(mounts) { + mountpoint, err := fs.RootPath(target, m.Target) if err != nil { return err } diff --git a/core/remotes/docker/fetcher.go b/core/remotes/docker/fetcher.go index 3bd184f10e..4a340e24f3 100644 --- a/core/remotes/docker/fetcher.go +++ b/core/remotes/docker/fetcher.go @@ -26,6 +26,7 @@ import ( "io" "net/http" "net/url" + "slices" "strings" "sync" @@ -637,8 +638,8 @@ func (r dockerFetcher) open(ctx context.Context, req *request, mediatype string, } } - for i := len(encoding) - 1; i >= 0; i-- { - algorithm := strings.ToLower(encoding[i]) + for _, value := range slices.Backward(encoding) { + algorithm := strings.ToLower(value) switch algorithm { case "zstd": r, err := zstd.NewReader(body.ReadCloser, diff --git a/core/remotes/handlers.go b/core/remotes/handlers.go index 27fde2b8cd..aa69fc15f7 100644 --- a/core/remotes/handlers.go +++ b/core/remotes/handlers.go @@ -23,6 +23,7 @@ import ( "fmt" "io" "maps" + "slices" "strings" "sync" @@ -286,8 +287,8 @@ func PushContent(ctx context.Context, pusher Pusher, desc ocispec.Descriptor, st } // Iterate in reverse order as seen, parent always uploaded after child - for i := len(indexStack) - 1; i >= 0; i-- { - err := images.Dispatch(ctx, pushHandler, limiter, indexStack[i]) + for _, index := range slices.Backward(indexStack) { + err := images.Dispatch(ctx, pushHandler, limiter, index) if err != nil { // TODO(estesp): until we have a more complete method for index push, we need to report // missing dependencies in an index/manifest list by sensing the "400 Bad Request" diff --git a/plugins/cri/cri.go b/plugins/cri/cri.go index e2872d4b4f..302aa8da29 100644 --- a/plugins/cri/cri.go +++ b/plugins/cri/cri.go @@ -20,6 +20,7 @@ import ( "context" "fmt" "io" + "slices" "strings" "github.com/containerd/log" @@ -141,9 +142,9 @@ func initCRIService(ic *plugin.InitContext) (any, error) { } if hasEnv, ok := shimPlugin.(interface{ Env() []string }); ok { env := hasEnv.Env() - for i := len(env) - 1; i >= 0; i-- { + for _, value := range slices.Backward(env) { // iterate backwards to grab the last PATH= - if path, ok := strings.CutPrefix(env[i], "PATH="); ok { + if path, ok := strings.CutPrefix(value, "PATH="); ok { shimPath = path break }