Skip to content
This repository was archived by the owner on Mar 9, 2022. It is now read-only.

Commit e1a37e8

Browse files
committed
Unpack image during import.
Signed-off-by: Lantao Liu <[email protected]>
1 parent a0cfc8c commit e1a37e8

3 files changed

Lines changed: 29 additions & 6 deletions

File tree

hack/verify-lint.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ for d in $(find . -type d -a \( -iwholename './pkg*' -o -iwholename './cmd*' \)
2929
--disable=aligncheck \
3030
--disable=gotype \
3131
--disable=gas \
32+
--disable=gosec \
3233
--cyclo-over=60 \
3334
--dupl-threshold=100 \
3435
--tests \

pkg/containerd/importer/importer.go

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,33 @@ type imageConfig struct {
8787
img ocispec.Image
8888
}
8989

90+
type importConfig struct {
91+
unpack bool
92+
snapshotter string
93+
}
94+
95+
// ImportOption configures import behavior.
96+
type ImportOption func(*importConfig)
97+
98+
// WithUnpack is used to unpack image after import.
99+
func WithUnpack(snapshotter string) ImportOption {
100+
return func(c *importConfig) {
101+
c.unpack = true
102+
c.snapshotter = snapshotter
103+
}
104+
}
105+
90106
// Import implements Docker Image Spec v1.1.
91107
// An image MUST have `manifest.json`.
92108
// `repositories` file in Docker Image Spec v1.0 is not supported (yet).
93109
// Also, the current implementation assumes the implicit file name convention,
94110
// which is not explicitly documented in the spec. (e.g. foobar/layer.tar)
95111
// It returns a group of image references successfully loaded.
96-
func Import(ctx context.Context, client *containerd.Client, reader io.Reader) (_ []string, retErr error) {
112+
func Import(ctx context.Context, client *containerd.Client, reader io.Reader, opts ...ImportOption) (_ []string, retErr error) {
113+
c := &importConfig{}
114+
for _, o := range opts {
115+
o(c)
116+
}
97117
ctx, done, err := client.WithLease(ctx)
98118
if err != nil {
99119
return nil, err
@@ -209,6 +229,12 @@ func Import(ctx context.Context, client *containerd.Client, reader io.Reader) (_
209229
Name: ref,
210230
Target: *desc,
211231
}
232+
if c.unpack {
233+
img := containerd.NewImage(client, imgrec)
234+
if err := img.Unpack(ctx, c.snapshotter); err != nil {
235+
return refs, errors.Wrapf(err, "unpack image %q", ref)
236+
}
237+
}
212238
if _, err := is.Create(ctx, imgrec); err != nil {
213239
if !errdefs.IsAlreadyExists(err) {
214240
return refs, errors.Wrapf(err, "create image ref %+v", imgrec)

pkg/server/image_load.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func (c *criService) LoadImage(ctx context.Context, r *api.LoadImageRequest) (*a
3939
if err != nil {
4040
return nil, errors.Wrap(err, "failed to open file")
4141
}
42-
repoTags, err := importer.Import(ctx, c.client, f)
42+
repoTags, err := importer.Import(ctx, c.client, f, importer.WithUnpack(c.config.ContainerdConfig.Snapshotter))
4343
if err != nil {
4444
return nil, errors.Wrap(err, "failed to import image")
4545
}
@@ -48,10 +48,6 @@ func (c *criService) LoadImage(ctx context.Context, r *api.LoadImageRequest) (*a
4848
if err != nil {
4949
return nil, errors.Wrapf(err, "failed to get image %q", repoTag)
5050
}
51-
if err := image.Unpack(ctx, c.config.ContainerdConfig.Snapshotter); err != nil {
52-
logrus.WithError(err).Warnf("Failed to unpack image %q", repoTag)
53-
// Do not fail image importing. Unpack will be retried when container creation.
54-
}
5551
info, err := getImageInfo(ctx, image)
5652
if err != nil {
5753
return nil, errors.Wrapf(err, "failed to get image %q info", repoTag)

0 commit comments

Comments
 (0)