-
Notifications
You must be signed in to change notification settings - Fork 3.3k
/
export.go
168 lines (145 loc) · 4.69 KB
/
export.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
Copyright The containerd Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package images
import (
"errors"
"fmt"
"io"
"os"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/urfave/cli"
"github.com/containerd/containerd/cmd/ctr/commands"
"github.com/containerd/containerd/images/archive"
"github.com/containerd/containerd/pkg/transfer"
tarchive "github.com/containerd/containerd/pkg/transfer/archive"
"github.com/containerd/containerd/pkg/transfer/image"
"github.com/containerd/containerd/platforms"
)
var exportCommand = cli.Command{
Name: "export",
Usage: "Export images",
ArgsUsage: "[flags] <out> <image> ...",
Description: `Export images to an OCI tar archive.
Tar output is formatted as an OCI archive, a Docker manifest is provided for the platform.
Use '--skip-manifest-json' to avoid including the Docker manifest.json file.
Use '--platform' to define the output platform.
When '--all-platforms' is given all images in a manifest list must be available.
`,
Flags: []cli.Flag{
cli.BoolFlag{
Name: "skip-manifest-json",
Usage: "Do not add Docker compatible manifest.json to archive",
},
cli.BoolFlag{
Name: "skip-non-distributable",
Usage: "Do not add non-distributable blobs such as Windows layers to archive",
},
cli.StringSliceFlag{
Name: "platform",
Usage: "Pull content from a specific platform",
Value: &cli.StringSlice{},
},
cli.BoolFlag{
Name: "all-platforms",
Usage: "Exports content from all platforms",
},
cli.BoolTFlag{
Name: "local",
Usage: "Run export locally rather than through transfer API",
},
},
Action: func(context *cli.Context) error {
var (
out = context.Args().First()
images = context.Args().Tail()
exportOpts = []archive.ExportOpt{}
)
if out == "" || len(images) == 0 {
return errors.New("please provide both an output filename and an image reference to export")
}
client, ctx, cancel, err := commands.NewClient(context)
if err != nil {
return err
}
defer cancel()
var w io.WriteCloser
if out == "-" {
w = os.Stdout
} else {
w, err = os.Create(out)
if err != nil {
return err
}
}
defer w.Close()
if !context.BoolT("local") {
pf, done := ProgressHandler(ctx, os.Stdout)
defer done()
exportOpts := []tarchive.ExportOpt{}
if pss := context.StringSlice("platform"); len(pss) > 0 {
for _, ps := range pss {
p, err := platforms.Parse(ps)
if err != nil {
return fmt.Errorf("invalid platform %q: %w", ps, err)
}
exportOpts = append(exportOpts, tarchive.WithPlatform(p))
}
}
if context.Bool("all-platforms") {
exportOpts = append(exportOpts, tarchive.WithAllPlatforms)
}
if context.Bool("skip-manifest-json") {
exportOpts = append(exportOpts, tarchive.WithSkipCompatibilityManifest)
}
if context.Bool("skip-non-distributable") {
exportOpts = append(exportOpts, tarchive.WithSkipNonDistributableBlobs)
}
storeOpts := []image.StoreOpt{}
for _, img := range images {
storeOpts = append(storeOpts, image.WithExtraReference(img))
}
return client.Transfer(ctx,
image.NewStore("", storeOpts...),
tarchive.NewImageExportStream(w, "", exportOpts...),
transfer.WithProgress(pf),
)
}
if pss := context.StringSlice("platform"); len(pss) > 0 {
var all []ocispec.Platform
for _, ps := range pss {
p, err := platforms.Parse(ps)
if err != nil {
return fmt.Errorf("invalid platform %q: %w", ps, err)
}
all = append(all, p)
}
exportOpts = append(exportOpts, archive.WithPlatform(platforms.Ordered(all...)))
} else {
exportOpts = append(exportOpts, archive.WithPlatform(platforms.DefaultStrict()))
}
if context.Bool("all-platforms") {
exportOpts = append(exportOpts, archive.WithAllPlatforms())
}
if context.Bool("skip-manifest-json") {
exportOpts = append(exportOpts, archive.WithSkipDockerManifest())
}
if context.Bool("skip-non-distributable") {
exportOpts = append(exportOpts, archive.WithSkipNonDistributableBlobs())
}
is := client.ImageService()
for _, img := range images {
exportOpts = append(exportOpts, archive.WithImage(is, img))
}
return client.Export(ctx, w, exportOpts...)
},
}