| 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 commands |
| 18 |
|
| 19 |
import ( |
| 20 |
"compress/gzip" |
| 21 |
gocontext "context" |
| 22 |
"encoding/json" |
| 23 |
"errors" |
| 24 |
"fmt" |
| 25 |
"os" |
| 26 |
"os/signal" |
| 27 |
|
| 28 |
"github.com/containerd/containerd/cmd/ctr/commands" |
| 29 |
"github.com/containerd/containerd/content" |
| 30 |
"github.com/containerd/containerd/images" |
| 31 |
"github.com/containerd/containerd/images/converter" |
| 32 |
"github.com/containerd/containerd/images/converter/uncompress" |
| 33 |
"github.com/containerd/containerd/platforms" |
| 34 |
"github.com/containerd/stargz-snapshotter/estargz" |
| 35 |
estargzconvert "github.com/containerd/stargz-snapshotter/nativeconverter/estargz" |
| 36 |
esgzexternaltocconvert "github.com/containerd/stargz-snapshotter/nativeconverter/estargz/externaltoc" |
| 37 |
zstdchunkedconvert "github.com/containerd/stargz-snapshotter/nativeconverter/zstdchunked" |
| 38 |
"github.com/containerd/stargz-snapshotter/recorder" |
| 39 |
"github.com/klauspost/compress/zstd" |
| 40 |
ocispec "github.com/opencontainers/image-spec/specs-go/v1" |
| 41 |
"github.com/sirupsen/logrus" |
| 42 |
"github.com/urfave/cli" |
| 43 |
) |
| 44 |
|
| 45 |
// ConvertCommand converts an image |
| 46 |
var ConvertCommand = cli.Command{ |
| 47 |
Name: "convert", |
| 48 |
Usage: "convert an image", |
| 49 |
ArgsUsage: "[flags] <source_ref> <target_ref>...", |
| 50 |
Description: `Convert an image format. |
| 51 |
|
| 52 |
e.g., 'ctr-remote convert --estargz --oci example.com/foo:orig example.com/foo:esgz' |
| 53 |
|
| 54 |
Use '--platform' to define the output platform. |
| 55 |
When '--all-platforms' is given all images in a manifest list must be available. |
| 56 |
`, |
| 57 |
Flags: []cli.Flag{ |
| 58 |
// estargz flags |
| 59 |
cli.BoolFlag{ |
| 60 |
Name: "estargz", |
| 61 |
Usage: "convert legacy tar(.gz) layers to eStargz for lazy pulling. Should be used in conjunction with '--oci'", |
| 62 |
}, |
| 63 |
cli.StringFlag{ |
| 64 |
Name: "estargz-record-in", |
| 65 |
Usage: "Read 'ctr-remote optimize --record-out=<FILE>' record file", |
| 66 |
}, |
| 67 |
cli.IntFlag{ |
| 68 |
Name: "estargz-compression-level", |
| 69 |
Usage: "eStargz compression level", |
| 70 |
Value: gzip.BestCompression, |
| 71 |
}, |
| 72 |
cli.IntFlag{ |
| 73 |
Name: "estargz-chunk-size", |
| 74 |
Usage: "eStargz chunk size", |
| 75 |
Value: 0, |
| 76 |
}, |
| 77 |
cli.IntFlag{ |
| 78 |
Name: "estargz-min-chunk-size", |
| 79 |
Usage: "The minimal number of bytes of data must be written in one gzip stream. Note that this adds a TOC property that old reader doesn't understand.", |
| 80 |
Value: 0, |
| 81 |
}, |
| 82 |
cli.BoolFlag{ |
| 83 |
Name: "estargz-external-toc", |
| 84 |
Usage: "Separate TOC JSON into another image (called \"TOC image\"). The name of TOC image is the original + \"-esgztoc\" suffix. Both eStargz and the TOC image should be pushed to the same registry. stargz-snapshotter refers to the TOC image when it pulls the result eStargz image.", |
| 85 |
}, |
| 86 |
cli.BoolFlag{ |
| 87 |
Name: "estargz-keep-diff-id", |
| 88 |
Usage: "convert to esgz without changing diffID (cannot be used in conjunction with '--estargz-record-in'. must be specified with '--estargz-external-toc')", |
| 89 |
}, |
| 90 |
// zstd:chunked flags |
| 91 |
cli.BoolFlag{ |
| 92 |
Name: "zstdchunked", |
| 93 |
Usage: "use zstd compression instead of gzip (a.k.a zstd:chunked). Must be used in conjunction with '--oci'.", |
| 94 |
}, |
| 95 |
cli.StringFlag{ |
| 96 |
Name: "zstdchunked-record-in", |
| 97 |
Usage: "Read 'ctr-remote optimize --record-out=<FILE>' record file", |
| 98 |
}, |
| 99 |
cli.IntFlag{ |
| 100 |
Name: "zstdchunked-compression-level", |
| 101 |
Usage: "zstd:chunked compression level", |
| 102 |
Value: 3, // SpeedDefault; see also https://pkg.go.dev/github.com/klauspost/compress/zstd#EncoderLevel |
| 103 |
}, |
| 104 |
cli.IntFlag{ |
| 105 |
Name: "zstdchunked-chunk-size", |
| 106 |
Usage: "zstd:chunked chunk size", |
| 107 |
Value: 0, |
| 108 |
}, |
| 109 |
// generic flags |
| 110 |
cli.BoolFlag{ |
| 111 |
Name: "uncompress", |
| 112 |
Usage: "convert tar.gz layers to uncompressed tar layers", |
| 113 |
}, |
| 114 |
cli.BoolFlag{ |
| 115 |
Name: "oci", |
| 116 |
Usage: "convert Docker media types to OCI media types", |
| 117 |
}, |
| 118 |
// platform flags |
| 119 |
cli.StringSliceFlag{ |
| 120 |
Name: "platform", |
| 121 |
Usage: "Convert content for a specific platform", |
| 122 |
Value: &cli.StringSlice{}, |
| 123 |
}, |
| 124 |
cli.BoolFlag{ |
| 125 |
Name: "all-platforms", |
| 126 |
Usage: "Convert content for all platforms", |
| 127 |
}, |
| 128 |
}, |
| 129 |
Action: func(context *cli.Context) error { |
| 130 |
var ( |
| 131 |
convertOpts = []converter.Opt{} |
| 132 |
) |
| 133 |
srcRef := context.Args().Get(0) |
| 134 |
targetRef := context.Args().Get(1) |
| 135 |
if srcRef == "" || targetRef == "" { |
| 136 |
return errors.New("src and target image need to be specified") |
| 137 |
} |
| 138 |
|
| 139 |
var platformMC platforms.MatchComparer |
| 140 |
if context.Bool("all-platforms") { |
| 141 |
platformMC = platforms.All |
| 142 |
} else { |
| 143 |
if pss := context.StringSlice("platform"); len(pss) > 0 { |
| 144 |
var all []ocispec.Platform |
| 145 |
for _, ps := range pss { |
| 146 |
p, err := platforms.Parse(ps) |
| 147 |
if err != nil { |
| 148 |
return fmt.Errorf("invalid platform %q: %w", ps, err) |
| 149 |
} |
| 150 |
all = append(all, p) |
| 151 |
} |
| 152 |
platformMC = platforms.Ordered(all...) |
| 153 |
} else { |
| 154 |
platformMC = platforms.DefaultStrict() |
| 155 |
} |
| 156 |
} |
| 157 |
convertOpts = append(convertOpts, converter.WithPlatform(platformMC)) |
| 158 |
|
| 159 |
var layerConvertFunc converter.ConvertFunc |
| 160 |
var finalize func(ctx gocontext.Context, cs content.Store, ref string, desc *ocispec.Descriptor) (*images.Image, error) |
| 161 |
if context.Bool("estargz") { |
| 162 |
esgzOpts, err := getESGZConvertOpts(context) |
| 163 |
if err != nil { |
| 164 |
return err |
| 165 |
} |
| 166 |
if context.Bool("estargz-external-toc") { |
| 167 |
if !context.Bool("estargz-keep-diff-id") { |
| 168 |
layerConvertFunc, finalize = esgzexternaltocconvert.LayerConvertFunc(esgzOpts, context.Int("estargz-compression-level")) |
| 169 |
} else { |
| 170 |
if context.String("estargz-record-in") != "" { |
| 171 |
return fmt.Errorf("option --estargz-keep-diff-id conflicts with --estargz-record-in") |
| 172 |
} |
| 173 |
layerConvertFunc, finalize = esgzexternaltocconvert.LayerConvertLossLessFunc(esgzexternaltocconvert.LayerConvertLossLessConfig{ |
| 174 |
CompressionLevel: context.Int("estargz-compression-level"), |
| 175 |
ChunkSize: context.Int("estargz-chunk-size"), |
| 176 |
MinChunkSize: context.Int("estargz-min-chunk-size"), |
| 177 |
}) |
| 178 |
} |
| 179 |
} else { |
| 180 |
if context.Bool("estargz-keep-diff-id") { |
| 181 |
return fmt.Errorf("option --estargz-keep-diff-id must be used with --estargz-external-toc") |
| 182 |
} |
| 183 |
layerConvertFunc = estargzconvert.LayerConvertFunc(esgzOpts...) |
| 184 |
} |
| 185 |
if !context.Bool("oci") { |
| 186 |
logrus.Warn("option --estargz should be used in conjunction with --oci") |
| 187 |
} |
| 188 |
if context.Bool("uncompress") { |
| 189 |
return errors.New("option --estargz conflicts with --uncompress") |
| 190 |
} |
| 191 |
if context.Bool("zstdchunked") { |
| 192 |
return errors.New("option --estargz conflicts with --zstdchunked") |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
if context.Bool("zstdchunked") { |
| 197 |
esgzOpts, err := getZstdchunkedConvertOpts(context) |
| 198 |
if err != nil { |
| 199 |
return err |
| 200 |
} |
| 201 |
layerConvertFunc = zstdchunkedconvert.LayerConvertFuncWithCompressionLevel( |
| 202 |
zstd.EncoderLevelFromZstd(context.Int("zstdchunked-compression-level")), esgzOpts...) |
| 203 |
if !context.Bool("oci") { |
| 204 |
return errors.New("option --zstdchunked must be used in conjunction with --oci") |
| 205 |
} |
| 206 |
if context.Bool("uncompress") { |
| 207 |
return errors.New("option --zstdchunked conflicts with --uncompress") |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
if context.Bool("uncompress") { |
| 212 |
layerConvertFunc = uncompress.LayerConvertFunc |
| 213 |
} |
| 214 |
|
| 215 |
if layerConvertFunc == nil { |
| 216 |
return errors.New("specify layer converter") |
| 217 |
} |
| 218 |
convertOpts = append(convertOpts, converter.WithLayerConvertFunc(layerConvertFunc)) |
| 219 |
|
| 220 |
if context.Bool("oci") { |
| 221 |
convertOpts = append(convertOpts, converter.WithDockerToOCI(true)) |
| 222 |
} |
| 223 |
|
| 224 |
client, ctx, cancel, err := commands.NewClient(context) |
| 225 |
if err != nil { |
| 226 |
return err |
| 227 |
} |
| 228 |
defer cancel() |
| 229 |
|
| 230 |
ctx, done, err := client.WithLease(ctx) |
| 231 |
if err != nil { |
| 232 |
return err |
| 233 |
} |
| 234 |
defer done(ctx) |
| 235 |
|
| 236 |
sigCh := make(chan os.Signal, 1) |
| 237 |
signal.Notify(sigCh, os.Interrupt) |
| 238 |
go func() { |
| 239 |
// Cleanly cancel conversion |
| 240 |
select { |
| 241 |
case s := <-sigCh: |
| 242 |
logrus.Infof("Got %v", s) |
| 243 |
cancel() |
| 244 |
case <-ctx.Done(): |
| 245 |
} |
| 246 |
}() |
| 247 |
newImg, err := converter.Convert(ctx, client, targetRef, srcRef, convertOpts...) |
| 248 |
if err != nil { |
| 249 |
return err |
| 250 |
} |
| 251 |
if finalize != nil { |
| 252 |
newI, err := finalize(ctx, client.ContentStore(), targetRef, &newImg.Target) |
| 253 |
if err != nil { |
| 254 |
return err |
| 255 |
} |
| 256 |
is := client.ImageService() |
| 257 |
_ = is.Delete(ctx, newI.Name) |
| 258 |
finimg, err := is.Create(ctx, *newI) |
| 259 |
if err != nil { |
| 260 |
return err |
| 261 |
} |
| 262 |
fmt.Fprintln(context.App.Writer, "extra image:", finimg.Name) |
| 263 |
} |
| 264 |
fmt.Fprintln(context.App.Writer, newImg.Target.Digest.String()) |
| 265 |
return nil |
| 266 |
}, |
| 267 |
} |
| 268 |
|
| 269 |
func getESGZConvertOpts(context *cli.Context) ([]estargz.Option, error) { |
| 270 |
esgzOpts := []estargz.Option{ |
| 271 |
estargz.WithCompressionLevel(context.Int("estargz-compression-level")), |
| 272 |
estargz.WithChunkSize(context.Int("estargz-chunk-size")), |
| 273 |
estargz.WithMinChunkSize(context.Int("estargz-min-chunk-size")), |
| 274 |
} |
| 275 |
if estargzRecordIn := context.String("estargz-record-in"); estargzRecordIn != "" { |
| 276 |
paths, err := readPathsFromRecordFile(estargzRecordIn) |
| 277 |
if err != nil { |
| 278 |
return nil, err |
| 279 |
} |
| 280 |
esgzOpts = append(esgzOpts, estargz.WithPrioritizedFiles(paths)) |
| 281 |
var ignored []string |
| 282 |
esgzOpts = append(esgzOpts, estargz.WithAllowPrioritizeNotFound(&ignored)) |
| 283 |
} |
| 284 |
return esgzOpts, nil |
| 285 |
} |
| 286 |
|
| 287 |
func getZstdchunkedConvertOpts(context *cli.Context) ([]estargz.Option, error) { |
| 288 |
esgzOpts := []estargz.Option{ |
| 289 |
estargz.WithChunkSize(context.Int("zstdchunked-chunk-size")), |
| 290 |
} |
| 291 |
if zstdchunkedRecordIn := context.String("zstdchunked-record-in"); zstdchunkedRecordIn != "" { |
| 292 |
paths, err := readPathsFromRecordFile(zstdchunkedRecordIn) |
| 293 |
if err != nil { |
| 294 |
return nil, err |
| 295 |
} |
| 296 |
esgzOpts = append(esgzOpts, estargz.WithPrioritizedFiles(paths)) |
| 297 |
var ignored []string |
| 298 |
esgzOpts = append(esgzOpts, estargz.WithAllowPrioritizeNotFound(&ignored)) |
| 299 |
} |
| 300 |
return esgzOpts, nil |
| 301 |
} |
| 302 |
|
| 303 |
func readPathsFromRecordFile(filename string) ([]string, error) { |
| 304 |
r, err := os.Open(filename) |
| 305 |
if err != nil { |
| 306 |
return nil, err |
| 307 |
} |
| 308 |
defer r.Close() |
| 309 |
dec := json.NewDecoder(r) |
| 310 |
var paths []string |
| 311 |
added := make(map[string]struct{}) |
| 312 |
for dec.More() { |
| 313 |
var e recorder.Entry |
| 314 |
if err := dec.Decode(&e); err != nil { |
| 315 |
return nil, err |
| 316 |
} |
| 317 |
if _, ok := added[e.Path]; !ok { |
| 318 |
paths = append(paths, e.Path) |
| 319 |
added[e.Path] = struct{}{} |
| 320 |
} |
| 321 |
} |
| 322 |
return paths, nil |
| 323 |
} |