Skip to content

Commit 8ee52bf

Browse files
committed
Move import/export code to separate client file
Signed-off-by: Michael Crosby <[email protected]>
1 parent e22c827 commit 8ee52bf

2 files changed

Lines changed: 121 additions & 96 deletions

File tree

client.go

Lines changed: 0 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ package containerd
1919
import (
2020
"context"
2121
"fmt"
22-
"io"
2322
"net/http"
2423
"runtime"
2524
"strconv"
@@ -551,98 +550,3 @@ func (c *Client) Version(ctx context.Context) (Version, error) {
551550
Revision: response.Revision,
552551
}, nil
553552
}
554-
555-
type importOpts struct {
556-
}
557-
558-
// ImportOpt allows the caller to specify import specific options
559-
type ImportOpt func(c *importOpts) error
560-
561-
func resolveImportOpt(opts ...ImportOpt) (importOpts, error) {
562-
var iopts importOpts
563-
for _, o := range opts {
564-
if err := o(&iopts); err != nil {
565-
return iopts, err
566-
}
567-
}
568-
return iopts, nil
569-
}
570-
571-
// Import imports an image from a Tar stream using reader.
572-
// Caller needs to specify importer. Future version may use oci.v1 as the default.
573-
// Note that unreferrenced blobs may be imported to the content store as well.
574-
func (c *Client) Import(ctx context.Context, importer images.Importer, reader io.Reader, opts ...ImportOpt) ([]Image, error) {
575-
_, err := resolveImportOpt(opts...) // unused now
576-
if err != nil {
577-
return nil, err
578-
}
579-
580-
ctx, done, err := c.WithLease(ctx)
581-
if err != nil {
582-
return nil, err
583-
}
584-
defer done(ctx)
585-
586-
imgrecs, err := importer.Import(ctx, c.ContentStore(), reader)
587-
if err != nil {
588-
// is.Update() is not called on error
589-
return nil, err
590-
}
591-
592-
is := c.ImageService()
593-
var images []Image
594-
for _, imgrec := range imgrecs {
595-
if updated, err := is.Update(ctx, imgrec, "target"); err != nil {
596-
if !errdefs.IsNotFound(err) {
597-
return nil, err
598-
}
599-
600-
created, err := is.Create(ctx, imgrec)
601-
if err != nil {
602-
return nil, err
603-
}
604-
605-
imgrec = created
606-
} else {
607-
imgrec = updated
608-
}
609-
610-
images = append(images, &image{
611-
client: c,
612-
i: imgrec,
613-
})
614-
}
615-
return images, nil
616-
}
617-
618-
type exportOpts struct {
619-
}
620-
621-
// ExportOpt allows the caller to specify export-specific options
622-
type ExportOpt func(c *exportOpts) error
623-
624-
func resolveExportOpt(opts ...ExportOpt) (exportOpts, error) {
625-
var eopts exportOpts
626-
for _, o := range opts {
627-
if err := o(&eopts); err != nil {
628-
return eopts, err
629-
}
630-
}
631-
return eopts, nil
632-
}
633-
634-
// Export exports an image to a Tar stream.
635-
// OCI format is used by default.
636-
// It is up to caller to put "org.opencontainers.image.ref.name" annotation to desc.
637-
// TODO(AkihiroSuda): support exporting multiple descriptors at once to a single archive stream.
638-
func (c *Client) Export(ctx context.Context, exporter images.Exporter, desc ocispec.Descriptor, opts ...ExportOpt) (io.ReadCloser, error) {
639-
_, err := resolveExportOpt(opts...) // unused now
640-
if err != nil {
641-
return nil, err
642-
}
643-
pr, pw := io.Pipe()
644-
go func() {
645-
pw.CloseWithError(exporter.Export(ctx, c.ContentStore(), desc, pw))
646-
}()
647-
return pr, nil
648-
}

import.go

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
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

Comments
 (0)