Skip to content

Commit 4754d2a

Browse files
committed
Update image export to support Docker format
Add manifest.json file which is used by Docker to import images. Signed-off-by: Derek McGowan <[email protected]>
1 parent f2a20ea commit 4754d2a

File tree

9 files changed

+643
-354
lines changed

9 files changed

+643
-354
lines changed

cmd/ctr/commands/images/export.go

Lines changed: 50 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -21,36 +21,33 @@ import (
2121
"os"
2222

2323
"github.com/containerd/containerd/cmd/ctr/commands"
24-
"github.com/containerd/containerd/images/oci"
25-
"github.com/containerd/containerd/reference"
26-
digest "github.com/opencontainers/go-digest"
24+
"github.com/containerd/containerd/images/archive"
25+
"github.com/containerd/containerd/platforms"
2726
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
2827
"github.com/pkg/errors"
2928
"github.com/urfave/cli"
3029
)
3130

3231
var exportCommand = cli.Command{
3332
Name: "export",
34-
Usage: "export an image",
35-
ArgsUsage: "[flags] <out> <image>",
36-
Description: `Export an image to a tar stream.
37-
Currently, only OCI format is supported.
33+
Usage: "export images",
34+
ArgsUsage: "[flags] <out> <image> ...",
35+
Description: `Export images to an OCI tar archive.
36+
37+
Tar output is formatted as an OCI archive, a Docker manifest is provided for the platform.
38+
Use '--skip-manifest-json' to avoid including the Docker manifest.json file.
39+
Use '--platform' to define the output platform.
40+
When '--all-platforms' is given all images in a manifest list must be available.
3841
`,
3942
Flags: []cli.Flag{
40-
// TODO(AkihiroSuda): make this map[string]string as in moby/moby#33355?
41-
cli.StringFlag{
42-
Name: "oci-ref-name",
43-
Value: "",
44-
Usage: "override org.opencontainers.image.ref.name annotation",
45-
},
46-
cli.StringFlag{
47-
Name: "manifest",
48-
Usage: "digest of manifest",
43+
cli.BoolFlag{
44+
Name: "skip-manifest-json",
45+
Usage: "do not add Docker compatible manifest.json to archive",
4946
},
50-
cli.StringFlag{
51-
Name: "manifest-type",
52-
Usage: "media type of manifest digest",
53-
Value: ocispec.MediaTypeImageManifest,
47+
cli.StringSliceFlag{
48+
Name: "platform",
49+
Usage: "Pull content from a specific platform",
50+
Value: &cli.StringSlice{},
5451
},
5552
cli.BoolFlag{
5653
Name: "all-platforms",
@@ -59,43 +56,47 @@ Currently, only OCI format is supported.
5956
},
6057
Action: func(context *cli.Context) error {
6158
var (
62-
out = context.Args().First()
63-
local = context.Args().Get(1)
64-
desc ocispec.Descriptor
59+
out = context.Args().First()
60+
images = context.Args().Tail()
61+
exportOpts = []archive.ExportOpt{}
6562
)
66-
if out == "" || local == "" {
63+
if out == "" || len(images) == 0 {
6764
return errors.New("please provide both an output filename and an image reference to export")
6865
}
66+
67+
if pss := context.StringSlice("platform"); len(pss) > 0 {
68+
var all []ocispec.Platform
69+
for _, ps := range pss {
70+
p, err := platforms.Parse(ps)
71+
if err != nil {
72+
return errors.Wrapf(err, "invalid platform %q", ps)
73+
}
74+
all = append(all, p)
75+
}
76+
exportOpts = append(exportOpts, archive.WithPlatform(platforms.Ordered(all...)))
77+
} else {
78+
exportOpts = append(exportOpts, archive.WithPlatform(platforms.Default()))
79+
}
80+
81+
if context.Bool("all-platforms") {
82+
exportOpts = append(exportOpts, archive.WithAllPlatforms())
83+
}
84+
85+
if context.Bool("skip-manifest-json") {
86+
exportOpts = append(exportOpts, archive.WithSkipDockerManifest())
87+
}
88+
6989
client, ctx, cancel, err := commands.NewClient(context)
7090
if err != nil {
7191
return err
7292
}
7393
defer cancel()
74-
if manifest := context.String("manifest"); manifest != "" {
75-
desc.Digest, err = digest.Parse(manifest)
76-
if err != nil {
77-
return errors.Wrap(err, "invalid manifest digest")
78-
}
79-
desc.MediaType = context.String("manifest-type")
80-
} else {
81-
img, err := client.ImageService().Get(ctx, local)
82-
if err != nil {
83-
return errors.Wrap(err, "unable to resolve image to manifest")
84-
}
85-
desc = img.Target
86-
}
8794

88-
if desc.Annotations == nil {
89-
desc.Annotations = make(map[string]string)
90-
}
91-
if s, ok := desc.Annotations[ocispec.AnnotationRefName]; !ok || s == "" {
92-
if ociRefName := determineOCIRefName(local); ociRefName != "" {
93-
desc.Annotations[ocispec.AnnotationRefName] = ociRefName
94-
}
95-
if ociRefName := context.String("oci-ref-name"); ociRefName != "" {
96-
desc.Annotations[ocispec.AnnotationRefName] = ociRefName
97-
}
95+
is := client.ImageService()
96+
for _, img := range images {
97+
exportOpts = append(exportOpts, archive.WithImage(is, img))
9898
}
99+
99100
var w io.WriteCloser
100101
if out == "-" {
101102
w = os.Stdout
@@ -105,32 +106,8 @@ Currently, only OCI format is supported.
105106
return nil
106107
}
107108
}
109+
defer w.Close()
108110

109-
var (
110-
exportOpts []oci.V1ExporterOpt
111-
)
112-
113-
exportOpts = append(exportOpts, oci.WithAllPlatforms(context.Bool("all-platforms")))
114-
115-
r, err := client.Export(ctx, desc, exportOpts...)
116-
if err != nil {
117-
return err
118-
}
119-
if _, err := io.Copy(w, r); err != nil {
120-
return err
121-
}
122-
if err := w.Close(); err != nil {
123-
return err
124-
}
125-
return r.Close()
111+
return client.Export(ctx, w, exportOpts...)
126112
},
127113
}
128-
129-
func determineOCIRefName(local string) string {
130-
refspec, err := reference.Parse(local)
131-
if err != nil {
132-
return ""
133-
}
134-
tag, _ := reference.SplitObject(refspec.Object)
135-
return tag
136-
}

export.go

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,26 +20,12 @@ import (
2020
"context"
2121
"io"
2222

23-
"github.com/containerd/containerd/images/oci"
24-
25-
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
26-
"github.com/pkg/errors"
23+
"github.com/containerd/containerd/images/archive"
2724
)
2825

29-
// Export exports an image to a Tar stream.
30-
// OCI format is used by default.
31-
// It is up to caller to put "org.opencontainers.image.ref.name" annotation to desc.
32-
// TODO(AkihiroSuda): support exporting multiple descriptors at once to a single archive stream.
33-
func (c *Client) Export(ctx context.Context, desc ocispec.Descriptor, opts ...oci.V1ExporterOpt) (io.ReadCloser, error) {
34-
35-
exporter, err := oci.ResolveV1ExportOpt(opts...)
36-
if err != nil {
37-
return nil, err
38-
}
39-
40-
pr, pw := io.Pipe()
41-
go func() {
42-
pw.CloseWithError(errors.Wrap(exporter.Export(ctx, c.ContentStore(), desc, pw), "export failed"))
43-
}()
44-
return pr, nil
26+
// Export exports images to a Tar stream.
27+
// The tar archive is in OCI format with a Docker compatible manifest
28+
// when a single target platform is given.
29+
func (c *Client) Export(ctx context.Context, w io.Writer, opts ...archive.ExportOpt) error {
30+
return archive.Export(ctx, c.ContentStore(), w, opts...)
4531
}

export_test.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,17 @@ package containerd
1818

1919
import (
2020
"archive/tar"
21+
"bytes"
2122
"io"
2223
"runtime"
2324
"testing"
25+
26+
"github.com/containerd/containerd/images/archive"
27+
"github.com/containerd/containerd/platforms"
2428
)
2529

26-
// TestOCIExport exports testImage as a tar stream
27-
func TestOCIExport(t *testing.T) {
30+
// TestExport exports testImage as a tar stream
31+
func TestExport(t *testing.T) {
2832
// TODO: support windows
2933
if testing.Short() || runtime.GOOS == "windows" {
3034
t.Skip()
@@ -38,15 +42,16 @@ func TestOCIExport(t *testing.T) {
3842
}
3943
defer client.Close()
4044

41-
pulled, err := client.Fetch(ctx, testImage)
45+
_, err = client.Fetch(ctx, testImage)
4246
if err != nil {
4347
t.Fatal(err)
4448
}
45-
exportedStream, err := client.Export(ctx, pulled.Target)
49+
wb := bytes.NewBuffer(nil)
50+
err = client.Export(ctx, wb, archive.WithPlatform(platforms.Default()), archive.WithImage(client.ImageService(), testImage))
4651
if err != nil {
4752
t.Fatal(err)
4853
}
49-
assertOCITar(t, exportedStream)
54+
assertOCITar(t, bytes.NewReader(wb.Bytes()))
5055
}
5156

5257
func assertOCITar(t *testing.T, r io.Reader) {

images/annotations.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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 images
18+
19+
const (
20+
// AnnotationImageName is an annotation on a Descriptor in an index.json
21+
// containing the `Name` value as used by an `Image` struct
22+
AnnotationImageName = "io.containerd.image.name"
23+
24+
// AnnotationImageNamePrefix is used the same way as AnnotationImageName
25+
// but may be used to refer to additional names in the annotation map
26+
// using user-defined suffixes (i.e. "extra.1")
27+
AnnotationImageNamePrefix = AnnotationImageName + "."
28+
)

0 commit comments

Comments
 (0)