@@ -20,10 +20,11 @@ import (
2020 "fmt"
2121 "io"
2222 "os"
23+ "time"
2324
25+ "github.com/containerd/containerd"
2426 "github.com/containerd/containerd/cmd/ctr/commands"
25- "github.com/containerd/containerd/images"
26- oci "github.com/containerd/containerd/images/oci"
27+ "github.com/containerd/containerd/images/archive"
2728 "github.com/containerd/containerd/log"
2829 "github.com/urfave/cli"
2930)
@@ -34,45 +35,58 @@ var importCommand = cli.Command{
3435 ArgsUsage : "[flags] <in>" ,
3536 Description : `Import images from a tar stream.
3637Implemented formats:
37- - oci.v1 (default)
38+ - oci.v1
39+ - docker.v1.1
40+ - docker.v1.2
3841
3942
40- For oci.v1 format, you need to specify --oci-name because an OCI archive contains image refs (tags)
41- but does not contain the base image name.
43+ For OCI v1, you may need to specify --base-name because an OCI archive may
44+ contain only partial image references (tags without the base image name).
45+ If no base image name is provided, a name will be generated as "import-%{yyyy-MM-dd}".
4246
4347e.g.
44- $ ctr images import --format oci.v1 --oci -name foo/bar foobar.tar
48+ $ ctr images import --base -name foo/bar foobar.tar
4549
4650If foobar.tar contains an OCI ref named "latest" and anonymous ref "sha256:deadbeef", the command will create
4751"foo/bar:latest" and "foo/bar@sha256:deadbeef" images in the containerd store.
4852` ,
4953 Flags : append ([]cli.Flag {
5054 cli.StringFlag {
51- Name : "format" ,
52- Value : "oci.v1" ,
53- Usage : "image format. See DESCRIPTION." ,
55+ Name : "base-name" ,
56+ Value : "" ,
57+ Usage : "base image name for added images, when provided only images with this name prefix are imported" ,
58+ },
59+ cli.BoolFlag {
60+ Name : "digests" ,
61+ Usage : "whether to create digest images (default: false)" ,
5462 },
5563 cli.StringFlag {
56- Name : "oci-name" ,
57- Value : "unknown/unknown" ,
58- Usage : "prefix added to either oci.v1 ref annotation or digest" ,
64+ Name : "index-name" ,
65+ Usage : "image name to keep index as, by default index is discarded" ,
5966 },
60- // TODO(AkihiroSuda): support commands.LabelFlag (for all children objects)
6167 }, commands .SnapshotterFlags ... ),
6268
6369 Action : func (context * cli.Context ) error {
6470 var (
65- in = context .Args ().First ()
66- imageImporter images. Importer
71+ in = context .Args ().First ()
72+ opts []containerd. ImportOpt
6773 )
6874
69- switch format := context .String ("format" ); format {
70- case "oci.v1" :
71- imageImporter = & oci.V1Importer {
72- ImageName : context .String ("oci-name" ),
73- }
74- default :
75- return fmt .Errorf ("unknown format %s" , format )
75+ prefix := context .String ("base-name" )
76+ if prefix == "" {
77+ prefix = fmt .Sprintf ("import-%s" , time .Now ().Format ("2006-01-02" ))
78+ opts = append (opts , containerd .WithImageRefTranslator (archive .AddRefPrefix (prefix )))
79+ } else {
80+ // When provided, filter out references which do not match
81+ opts = append (opts , containerd .WithImageRefTranslator (archive .FilterRefPrefix (prefix )))
82+ }
83+
84+ if context .Bool ("digests" ) {
85+ opts = append (opts , containerd .WithDigestRef (archive .DigestTranslator (prefix )))
86+ }
87+
88+ if idxName := context .String ("index-name" ); idxName != "" {
89+ opts = append (opts , containerd .WithIndexName (idxName ))
7690 }
7791
7892 client , ctx , cancel , err := commands .NewClient (context )
@@ -90,20 +104,24 @@ If foobar.tar contains an OCI ref named "latest" and anonymous ref "sha256:deadb
90104 return err
91105 }
92106 }
93- imgs , err := client .Import (ctx , imageImporter , r )
107+ imgs , err := client .Import (ctx , r , opts ... )
108+ closeErr := r .Close ()
94109 if err != nil {
95110 return err
96111 }
97- if err = r . Close (); err != nil {
98- return err
112+ if closeErr != nil {
113+ return closeErr
99114 }
100115
101116 log .G (ctx ).Debugf ("unpacking %d images" , len (imgs ))
102117
103118 for _ , img := range imgs {
119+ // TODO: Allow configuration of the platform
120+ image := containerd .NewImage (client , img )
121+
104122 // TODO: Show unpack status
105- fmt .Printf ("unpacking %s (%s)..." , img .Name () , img .Target () .Digest )
106- err = img .Unpack (ctx , context .String ("snapshotter" ))
123+ fmt .Printf ("unpacking %s (%s)..." , img .Name , img .Target .Digest )
124+ err = image .Unpack (ctx , context .String ("snapshotter" ))
107125 if err != nil {
108126 return err
109127 }
0 commit comments