Add new ctr option for discarding unpacked layers

Add a new ctr cli option, allowing the garbage collector to discard any
unpacked layers after importing an image. This new option is
incompatible with the no-unpack ctr import option.

Signed-off-by: James Jenkins <James.Jenkins@ibm.com>
This commit is contained in:
James Jenkins
2022-09-21 13:21:28 -04:00
parent 39f7cd73e7
commit 2432b54a56
2 changed files with 32 additions and 2 deletions

View File

@@ -79,12 +79,16 @@ If foobar.tar contains an OCI ref named "latest" and anonymous ref "sha256:deadb
},
cli.BoolFlag{
Name: "no-unpack",
Usage: "skip unpacking the images, false by default",
Usage: "skip unpacking the images, cannot be used with --discard-unpacked-layers, false by default",
},
cli.BoolFlag{
Name: "compress-blobs",
Usage: "compress uncompressed blobs when creating manifest (Docker format only)",
},
cli.BoolFlag{
Name: "discard-unpacked-layers",
Usage: "allow the garbage collector to clean layers up from the content store after unpacking, cannot be used with --no-unpack, false by default",
},
}, commands.SnapshotterFlags...),
Action: func(context *cli.Context) error {
@@ -132,12 +136,23 @@ If foobar.tar contains an OCI ref named "latest" and anonymous ref "sha256:deadb
opts = append(opts, containerd.WithAllPlatforms(context.Bool("all-platforms")))
if context.Bool("discard-unpacked-layers") && context.Bool("no-unpack") {
return fmt.Errorf("--discard-unpacked-layers and --no-unpack are incompatible options")
}
opts = append(opts, containerd.WithDiscardUnpackedLayers(context.Bool("discard-unpacked-layers")))
client, ctx, cancel, err := commands.NewClient(context)
if err != nil {
return err
}
defer cancel()
ctx, done, err := client.WithLease(ctx)
if err != nil {
return err
}
defer done(ctx)
var r io.ReadCloser
if in == "-" {
r = os.Stdin
@@ -147,6 +162,7 @@ If foobar.tar contains an OCI ref named "latest" and anonymous ref "sha256:deadb
return err
}
}
imgs, err := client.Import(ctx, r, opts...)
closeErr := r.Close()
if err != nil {