|
| 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 containerd |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "io" |
| 22 | + |
| 23 | + "github.com/containerd/containerd/errdefs" |
| 24 | + "github.com/containerd/containerd/images" |
| 25 | + ocispec "github.com/opencontainers/image-spec/specs-go/v1" |
| 26 | +) |
| 27 | + |
| 28 | +type importOpts struct { |
| 29 | +} |
| 30 | + |
| 31 | +// ImportOpt allows the caller to specify import specific options |
| 32 | +type ImportOpt func(c *importOpts) error |
| 33 | + |
| 34 | +func resolveImportOpt(opts ...ImportOpt) (importOpts, error) { |
| 35 | + var iopts importOpts |
| 36 | + for _, o := range opts { |
| 37 | + if err := o(&iopts); err != nil { |
| 38 | + return iopts, err |
| 39 | + } |
| 40 | + } |
| 41 | + return iopts, nil |
| 42 | +} |
| 43 | + |
| 44 | +// Import imports an image from a Tar stream using reader. |
| 45 | +// Caller needs to specify importer. Future version may use oci.v1 as the default. |
| 46 | +// Note that unreferrenced blobs may be imported to the content store as well. |
| 47 | +func (c *Client) Import(ctx context.Context, importer images.Importer, reader io.Reader, opts ...ImportOpt) ([]Image, error) { |
| 48 | + _, err := resolveImportOpt(opts...) // unused now |
| 49 | + if err != nil { |
| 50 | + return nil, err |
| 51 | + } |
| 52 | + |
| 53 | + ctx, done, err := c.WithLease(ctx) |
| 54 | + if err != nil { |
| 55 | + return nil, err |
| 56 | + } |
| 57 | + defer done(ctx) |
| 58 | + |
| 59 | + imgrecs, err := importer.Import(ctx, c.ContentStore(), reader) |
| 60 | + if err != nil { |
| 61 | + // is.Update() is not called on error |
| 62 | + return nil, err |
| 63 | + } |
| 64 | + |
| 65 | + is := c.ImageService() |
| 66 | + var images []Image |
| 67 | + for _, imgrec := range imgrecs { |
| 68 | + if updated, err := is.Update(ctx, imgrec, "target"); err != nil { |
| 69 | + if !errdefs.IsNotFound(err) { |
| 70 | + return nil, err |
| 71 | + } |
| 72 | + |
| 73 | + created, err := is.Create(ctx, imgrec) |
| 74 | + if err != nil { |
| 75 | + return nil, err |
| 76 | + } |
| 77 | + |
| 78 | + imgrec = created |
| 79 | + } else { |
| 80 | + imgrec = updated |
| 81 | + } |
| 82 | + |
| 83 | + images = append(images, &image{ |
| 84 | + client: c, |
| 85 | + i: imgrec, |
| 86 | + }) |
| 87 | + } |
| 88 | + return images, nil |
| 89 | +} |
| 90 | + |
| 91 | +type exportOpts struct { |
| 92 | +} |
| 93 | + |
| 94 | +// ExportOpt allows the caller to specify export-specific options |
| 95 | +type ExportOpt func(c *exportOpts) error |
| 96 | + |
| 97 | +func resolveExportOpt(opts ...ExportOpt) (exportOpts, error) { |
| 98 | + var eopts exportOpts |
| 99 | + for _, o := range opts { |
| 100 | + if err := o(&eopts); err != nil { |
| 101 | + return eopts, err |
| 102 | + } |
| 103 | + } |
| 104 | + return eopts, nil |
| 105 | +} |
| 106 | + |
| 107 | +// Export exports an image to a Tar stream. |
| 108 | +// OCI format is used by default. |
| 109 | +// It is up to caller to put "org.opencontainers.image.ref.name" annotation to desc. |
| 110 | +// TODO(AkihiroSuda): support exporting multiple descriptors at once to a single archive stream. |
| 111 | +func (c *Client) Export(ctx context.Context, exporter images.Exporter, desc ocispec.Descriptor, opts ...ExportOpt) (io.ReadCloser, error) { |
| 112 | + _, err := resolveExportOpt(opts...) // unused now |
| 113 | + if err != nil { |
| 114 | + return nil, err |
| 115 | + } |
| 116 | + pr, pw := io.Pipe() |
| 117 | + go func() { |
| 118 | + pw.CloseWithError(exporter.Export(ctx, c.ContentStore(), desc, pw)) |
| 119 | + }() |
| 120 | + return pr, nil |
| 121 | +} |
0 commit comments