github.com/containerd/nerdctl/v2@v2.0.0-beta.5.0.20240520001846-b5758f54fa28/pkg/idutil/containerwalker/containerwalker.go (about) 1 /* 2 Copyright The containerd Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package containerwalker 18 19 import ( 20 "context" 21 "fmt" 22 "regexp" 23 "strings" 24 25 "github.com/containerd/containerd" 26 "github.com/containerd/nerdctl/v2/pkg/labels" 27 ) 28 29 type Found struct { 30 Container containerd.Container 31 Req string // The raw request string. name, short ID, or long ID. 32 MatchIndex int // Begins with 0, up to MatchCount - 1. 33 MatchCount int // 1 on exact match. > 1 on ambiguous match. Never be <= 0. 34 } 35 36 type OnFound func(ctx context.Context, found Found) error 37 38 type ContainerWalker struct { 39 Client *containerd.Client 40 OnFound OnFound 41 } 42 43 // Walk walks containers and calls w.OnFound . 44 // Req is name, short ID, or long ID. 45 // Returns the number of the found entries. 46 func (w *ContainerWalker) Walk(ctx context.Context, req string) (int, error) { 47 if strings.HasPrefix(req, "k8s://") { 48 return -1, fmt.Errorf("specifying \"k8s://...\" form is not supported (Hint: specify ID instead): %q", req) 49 } 50 filters := []string{ 51 fmt.Sprintf("labels.%q==%s", labels.Name, req), 52 fmt.Sprintf("id~=^%s.*$", regexp.QuoteMeta(req)), 53 } 54 55 containers, err := w.Client.Containers(ctx, filters...) 56 if err != nil { 57 return -1, err 58 } 59 60 matchCount := len(containers) 61 for i, c := range containers { 62 f := Found{ 63 Container: c, 64 Req: req, 65 MatchIndex: i, 66 MatchCount: matchCount, 67 } 68 if e := w.OnFound(ctx, f); e != nil { 69 return -1, e 70 } 71 } 72 return matchCount, nil 73 } 74 75 // WalkAll calls `Walk` for each req in `reqs`. 76 // 77 // It can be used when the matchCount is not important (e.g., only care if there 78 // is any error or if matchCount == 0 (not found error) when walking all reqs). 79 // If `forceAll`, it calls `Walk` on every req 80 // and return all errors joined by `\n`. If not `forceAll`, it returns the first error 81 // encountered while calling `Walk`. 82 func (w *ContainerWalker) WalkAll(ctx context.Context, reqs []string, forceAll bool) error { 83 var errs []string 84 for _, req := range reqs { 85 n, err := w.Walk(ctx, req) 86 if err == nil && n == 0 { 87 err = fmt.Errorf("no such container: %s", req) 88 } 89 if err != nil { 90 if !forceAll { 91 return err 92 } 93 errs = append(errs, err.Error()) 94 } 95 } 96 if len(errs) > 0 { 97 return fmt.Errorf("%d errors:\n%s", len(errs), strings.Join(errs, "\n")) 98 } 99 return nil 100 }