@@ -24,6 +24,7 @@ import (
2424
2525 "github.com/containerd/containerd"
2626 "github.com/containerd/containerd/cmd/ctr/commands"
27+ "github.com/containerd/containerd/images/docker"
2728 oci "github.com/containerd/containerd/images/oci"
2829 "github.com/containerd/containerd/log"
2930 "github.com/urfave/cli"
@@ -35,11 +36,14 @@ var importCommand = cli.Command{
3536 ArgsUsage : "[flags] <in>" ,
3637 Description : `Import images from a tar stream.
3738Implemented formats:
38- - oci.v1 (default)
39+ - oci.v1
40+ - docker.v1.1
41+ - docker.v1.2
3942
4043
41- For oci.v1 format, you need to specify --oci-name because an OCI archive contains image refs (tags)
42- but does not contain the base image name.
44+ For OCI v1, you may need to specify --base-name because an OCI archive
45+ contains only partial image references (tags without the base image name).
46+ If no base image name is provided, a name will be generated as "import-%{date}".
4347
4448e.g.
4549 $ ctr images import --format oci.v1 --oci-name foo/bar foobar.tar
@@ -50,18 +54,22 @@ If foobar.tar contains an OCI ref named "latest" and anonymous ref "sha256:deadb
5054 Flags : append ([]cli.Flag {
5155 cli.StringFlag {
5256 Name : "format" ,
53- Value : "oci.v1 " ,
54- Usage : "image format. See DESCRIPTION. " ,
57+ Value : "" ,
58+ Usage : "image format, by default supports OCI v1, Docker v1.1, Docker v1.2 " ,
5559 },
5660 cli.StringFlag {
57- Name : "prefix ,oci-name" ,
61+ Name : "base-name ,oci-name" ,
5862 Value : "" ,
59- Usage : "prefix image name for added images" ,
63+ Usage : "base image name for added images, when provided images without this name prefix are filtered out " ,
6064 },
6165 cli.BoolFlag {
6266 Name : "digests" ,
6367 Usage : "whether to create digest images" ,
6468 },
69+ cli.StringFlag {
70+ Name : "index-name" ,
71+ Usage : "image name to keep index as, by default index is discarded" ,
72+ },
6573 }, commands .SnapshotterFlags ... ),
6674
6775 Action : func (context * cli.Context ) error {
@@ -70,23 +78,30 @@ If foobar.tar contains an OCI ref named "latest" and anonymous ref "sha256:deadb
7078 opts []containerd.ImportOpt
7179 )
7280
81+ prefix := context .String ("base-name" )
82+ if prefix == "" {
83+ prefix = fmt .Sprintf ("import-%s" , time .Now ().Format ("2006-01-02" ))
84+ }
85+
7386 switch format := context .String ("format" ); format {
74- case "oci.v1" :
87+ case "" , "docker" , "docker.v1.1" , "docker.v1.2" :
88+ opts = append (opts , containerd .WithImporter (& docker.V1Importer {}))
89+ opts = append (opts , containerd .WithImageRefTranslator (docker .RefTranslator (prefix , context .String ("base-name" ) != "" )))
90+ case "oci" , "oci.v1" :
7591 opts = append (opts , containerd .WithImporter (& oci.V1Importer {}))
76-
77- prefix := context .String ("prefix" )
78- if prefix == "" {
79- prefix = fmt .Sprintf ("import-%s" , time .Now ().Format ("2006-01-02" ))
80- }
81-
8292 opts = append (opts , containerd .WithImageRefTranslator (oci .RefTranslator (prefix )))
83- if context .Bool ("digests" ) {
84- opts = append (opts , containerd .WithDigestRef (oci .DigestTranslator (prefix )))
85- }
8693 default :
8794 return fmt .Errorf ("unknown format %s" , format )
8895 }
8996
97+ if context .Bool ("digests" ) {
98+ opts = append (opts , containerd .WithDigestRef (oci .DigestTranslator (prefix )))
99+ }
100+
101+ if idxName := context .String ("index-name" ); idxName != "" {
102+ opts = append (opts , containerd .WithIndexName (idxName ))
103+ }
104+
90105 client , ctx , cancel , err := commands .NewClient (context )
91106 if err != nil {
92107 return err
0 commit comments