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:
parent
39f7cd73e7
commit
2432b54a56
@ -79,12 +79,16 @@ If foobar.tar contains an OCI ref named "latest" and anonymous ref "sha256:deadb
|
|||||||
},
|
},
|
||||||
cli.BoolFlag{
|
cli.BoolFlag{
|
||||||
Name: "no-unpack",
|
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{
|
cli.BoolFlag{
|
||||||
Name: "compress-blobs",
|
Name: "compress-blobs",
|
||||||
Usage: "compress uncompressed blobs when creating manifest (Docker format only)",
|
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...),
|
}, commands.SnapshotterFlags...),
|
||||||
|
|
||||||
Action: func(context *cli.Context) error {
|
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")))
|
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)
|
client, ctx, cancel, err := commands.NewClient(context)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
|
ctx, done, err := client.WithLease(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer done(ctx)
|
||||||
|
|
||||||
var r io.ReadCloser
|
var r io.ReadCloser
|
||||||
if in == "-" {
|
if in == "-" {
|
||||||
r = os.Stdin
|
r = os.Stdin
|
||||||
@ -147,6 +162,7 @@ If foobar.tar contains an OCI ref named "latest" and anonymous ref "sha256:deadb
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
imgs, err := client.Import(ctx, r, opts...)
|
imgs, err := client.Import(ctx, r, opts...)
|
||||||
closeErr := r.Close()
|
closeErr := r.Close()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
14
import.go
14
import.go
@ -38,6 +38,7 @@ type importOpts struct {
|
|||||||
allPlatforms bool
|
allPlatforms bool
|
||||||
platformMatcher platforms.MatchComparer
|
platformMatcher platforms.MatchComparer
|
||||||
compress bool
|
compress bool
|
||||||
|
discardLayers bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// ImportOpt allows the caller to specify import specific options
|
// ImportOpt allows the caller to specify import specific options
|
||||||
@ -105,6 +106,15 @@ func WithImportCompression() ImportOpt {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WithDiscardUnpackedLayers allows the garbage collector to clean up
|
||||||
|
// layers from content store after unpacking.
|
||||||
|
func WithDiscardUnpackedLayers(discard bool) ImportOpt {
|
||||||
|
return func(c *importOpts) error {
|
||||||
|
c.discardLayers = discard
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Import imports an image from a Tar stream using reader.
|
// Import imports an image from a Tar stream using reader.
|
||||||
// Caller needs to specify importer. Future version may use oci.v1 as the default.
|
// Caller needs to specify importer. Future version may use oci.v1 as the default.
|
||||||
// Note that unreferenced blobs may be imported to the content store as well.
|
// Note that unreferenced blobs may be imported to the content store as well.
|
||||||
@ -195,7 +205,11 @@ func (c *Client) Import(ctx context.Context, reader io.Reader, opts ...ImportOpt
|
|||||||
}
|
}
|
||||||
|
|
||||||
handler = images.FilterPlatforms(handler, platformMatcher)
|
handler = images.FilterPlatforms(handler, platformMatcher)
|
||||||
|
if iopts.discardLayers {
|
||||||
|
handler = images.SetChildrenMappedLabels(cs, handler, images.ChildGCLabelsFilterLayers)
|
||||||
|
} else {
|
||||||
handler = images.SetChildrenLabels(cs, handler)
|
handler = images.SetChildrenLabels(cs, handler)
|
||||||
|
}
|
||||||
if err := images.WalkNotEmpty(ctx, handler, index); err != nil {
|
if err := images.WalkNotEmpty(ctx, handler, index); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user