github.com/containerd/nerdctl/v2@v2.0.0-beta.5.0.20240520001846-b5758f54fa28/pkg/cmd/image/load.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 image 18 19 import ( 20 "context" 21 "errors" 22 "fmt" 23 "io" 24 "os" 25 26 "github.com/containerd/containerd" 27 "github.com/containerd/containerd/archive/compression" 28 "github.com/containerd/containerd/images" 29 "github.com/containerd/containerd/images/archive" 30 "github.com/containerd/nerdctl/v2/pkg/api/types" 31 "github.com/containerd/nerdctl/v2/pkg/imgutil" 32 "github.com/containerd/nerdctl/v2/pkg/platformutil" 33 "github.com/containerd/platforms" 34 ) 35 36 type readCounter struct { 37 io.Reader 38 N int 39 } 40 41 func (r *readCounter) Read(p []byte) (int, error) { 42 n, err := r.Reader.Read(p) 43 if n > 0 { 44 r.N += n 45 } 46 return n, err 47 } 48 49 func Load(ctx context.Context, client *containerd.Client, options types.ImageLoadOptions) error { 50 if options.Input != "" { 51 f, err := os.Open(options.Input) 52 if err != nil { 53 return err 54 } 55 defer f.Close() 56 options.Stdin = f 57 } else { 58 // check if stdin is empty. 59 stdinStat, err := os.Stdin.Stat() 60 if err != nil { 61 return err 62 } 63 if stdinStat.Size() == 0 && (stdinStat.Mode()&os.ModeNamedPipe) == 0 { 64 return errors.New("stdin is empty and input flag is not specified") 65 } 66 } 67 decompressor, err := compression.DecompressStream(options.Stdin) 68 if err != nil { 69 return err 70 } 71 platMC, err := platformutil.NewMatchComparer(options.AllPlatforms, options.Platform) 72 if err != nil { 73 return err 74 } 75 return loadImage(ctx, client, decompressor, platMC, false, options) 76 } 77 78 func loadImage(ctx context.Context, client *containerd.Client, in io.Reader, platMC platforms.MatchComparer, quiet bool, options types.ImageLoadOptions) error { 79 // In addition to passing WithImagePlatform() to client.Import(), we also need to pass WithDefaultPlatform() to NewClient(). 80 // Otherwise unpacking may fail. 81 r := &readCounter{Reader: in} 82 imgs, err := client.Import(ctx, r, containerd.WithDigestRef(archive.DigestTranslator(options.GOptions.Snapshotter)), containerd.WithSkipDigestRef(func(name string) bool { return name != "" }), containerd.WithImportPlatform(platMC)) 83 if err != nil { 84 if r.N == 0 { 85 // Avoid confusing "unrecognized image format" 86 return errors.New("no image was built") 87 } 88 if errors.Is(err, images.ErrEmptyWalk) { 89 err = fmt.Errorf("%w (Hint: set `--platform=PLATFORM` or `--all-platforms`)", err) 90 } 91 return err 92 } 93 for _, img := range imgs { 94 image := containerd.NewImageWithPlatform(client, img, platMC) 95 96 // TODO: Show unpack status 97 if !quiet { 98 fmt.Fprintf(options.Stdout, "unpacking %s (%s)...\n", img.Name, img.Target.Digest) 99 } 100 err = image.Unpack(ctx, options.GOptions.Snapshotter) 101 if err != nil { 102 return err 103 } 104 if quiet { 105 fmt.Fprintln(options.Stdout, img.Target.Digest) 106 } else { 107 repo, tag := imgutil.ParseRepoTag(img.Name) 108 fmt.Fprintf(options.Stdout, "Loaded image: %s:%s\n", repo, tag) 109 } 110 } 111 112 return nil 113 }