Refactor image importer
Allow customization of reference creation. Add option for digest references. Signed-off-by: Derek McGowan <derek@mcgstyle.net>
This commit is contained in:
@@ -20,9 +20,10 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/containerd/containerd"
|
||||
"github.com/containerd/containerd/cmd/ctr/commands"
|
||||
"github.com/containerd/containerd/images"
|
||||
oci "github.com/containerd/containerd/images/oci"
|
||||
"github.com/containerd/containerd/log"
|
||||
"github.com/urfave/cli"
|
||||
@@ -53,23 +54,34 @@ If foobar.tar contains an OCI ref named "latest" and anonymous ref "sha256:deadb
|
||||
Usage: "image format. See DESCRIPTION.",
|
||||
},
|
||||
cli.StringFlag{
|
||||
Name: "oci-name",
|
||||
Value: "unknown/unknown",
|
||||
Usage: "prefix added to either oci.v1 ref annotation or digest",
|
||||
Name: "prefix,oci-name",
|
||||
Value: "",
|
||||
Usage: "prefix image name for added images",
|
||||
},
|
||||
cli.BoolFlag{
|
||||
Name: "digests",
|
||||
Usage: "whether to create digest images",
|
||||
},
|
||||
// TODO(AkihiroSuda): support commands.LabelFlag (for all children objects)
|
||||
}, commands.SnapshotterFlags...),
|
||||
|
||||
Action: func(context *cli.Context) error {
|
||||
var (
|
||||
in = context.Args().First()
|
||||
imageImporter images.Importer
|
||||
in = context.Args().First()
|
||||
opts []containerd.ImportOpt
|
||||
)
|
||||
|
||||
switch format := context.String("format"); format {
|
||||
case "oci.v1":
|
||||
imageImporter = &oci.V1Importer{
|
||||
ImageName: context.String("oci-name"),
|
||||
opts = append(opts, containerd.WithImporter(&oci.V1Importer{}))
|
||||
|
||||
prefix := context.String("prefix")
|
||||
if prefix == "" {
|
||||
prefix = fmt.Sprintf("import-%s", time.Now().Format("2006-01-02"))
|
||||
}
|
||||
|
||||
opts = append(opts, containerd.WithImageRefTranslator(oci.RefTranslator(prefix)))
|
||||
if context.Bool("digests") {
|
||||
opts = append(opts, containerd.WithDigestRef(oci.DigestTranslator(prefix)))
|
||||
}
|
||||
default:
|
||||
return fmt.Errorf("unknown format %s", format)
|
||||
@@ -90,20 +102,24 @@ If foobar.tar contains an OCI ref named "latest" and anonymous ref "sha256:deadb
|
||||
return err
|
||||
}
|
||||
}
|
||||
imgs, err := client.Import(ctx, imageImporter, r)
|
||||
imgs, err := client.Import(ctx, r, opts...)
|
||||
closeErr := r.Close()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err = r.Close(); err != nil {
|
||||
return err
|
||||
if closeErr != nil {
|
||||
return closeErr
|
||||
}
|
||||
|
||||
log.G(ctx).Debugf("unpacking %d images", len(imgs))
|
||||
|
||||
for _, img := range imgs {
|
||||
// TODO: Allow configuration of the platform
|
||||
image := containerd.NewImage(client, img)
|
||||
|
||||
// TODO: Show unpack status
|
||||
fmt.Printf("unpacking %s (%s)...", img.Name(), img.Target().Digest)
|
||||
err = img.Unpack(ctx, context.String("snapshotter"))
|
||||
fmt.Printf("unpacking %s (%s)...", img.Name, img.Target.Digest)
|
||||
err = image.Unpack(ctx, context.String("snapshotter"))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
Reference in New Issue
Block a user