(ctx context.Context, client *containerd.Client, options types.ImageImportOptions)
| 49 | ) |
| 50 | |
| 51 | func Import(ctx context.Context, client *containerd.Client, options types.ImageImportOptions) (string, error) { |
| 52 | prefix := options.Reference |
| 53 | if prefix == "" { |
| 54 | prefix = fmt.Sprintf("import-%s", time.Now().Format("2006-01-02")) |
| 55 | } |
| 56 | |
| 57 | parsed, err := referenceutil.Parse(prefix) |
| 58 | if err != nil { |
| 59 | return "", err |
| 60 | } |
| 61 | imageName := parsed.String() |
| 62 | |
| 63 | platUnpack := platforms.DefaultSpec() |
| 64 | var opts []transferimage.StoreOpt |
| 65 | if options.Platform != "" { |
| 66 | p, err := platforms.Parse(options.Platform) |
| 67 | if err != nil { |
| 68 | return "", err |
| 69 | } |
| 70 | platUnpack = p |
| 71 | opts = append(opts, transferimage.WithPlatforms(platUnpack)) |
| 72 | } |
| 73 | |
| 74 | opts = append(opts, transferimage.WithUnpack(platUnpack, options.GOptions.Snapshotter)) |
| 75 | opts = append(opts, transferimage.WithDigestRef(imageName, true, true)) |
| 76 | |
| 77 | var r io.ReadCloser |
| 78 | if rc, ok := options.Stdin.(io.ReadCloser); ok { |
| 79 | r = rc |
| 80 | } else { |
| 81 | r = io.NopCloser(options.Stdin) |
| 82 | } |
| 83 | |
| 84 | converted, cleanup, err := ensureOCIArchive(ctx, client, r, options, prefix) |
| 85 | if err != nil { |
| 86 | return "", err |
| 87 | } |
| 88 | defer cleanup() |
| 89 | |
| 90 | iis := tarchive.NewImageImportStream(converted, "") |
| 91 | is := transferimage.NewStore("", opts...) |
| 92 | |
| 93 | pf, done := transferutil.ProgressHandler(ctx, os.Stderr) |
| 94 | defer done() |
| 95 | |
| 96 | if err := client.Transfer(ctx, iis, is, transfer.WithProgress(pf)); err != nil { |
| 97 | return "", err |
| 98 | } |
| 99 | |
| 100 | return imageName, nil |
| 101 | } |
| 102 | |
| 103 | func ensureOCIArchive(ctx context.Context, client *containerd.Client, r io.ReadCloser, options types.ImageImportOptions, prefix string) (io.ReadCloser, func(), error) { |
| 104 | buf := &bytes.Buffer{} |
no test coverage detected
searching dependent graphs…