From d2ab53aa5f67cb65a75f2c36fb076c0debd961ca Mon Sep 17 00:00:00 2001 From: umohnani8 Date: Thu, 14 Dec 2017 12:49:22 -0500 Subject: Add support for dir transport to kpod save kpod save can now save images to directories using the dir transport. Manifest conversion is also possible. To save with the oci manifest type set --format to oci-dir and to save with the v2s2(docker) manifest type, set --format to docker-dir. The layers can be compressed as well when saving to a directory using the --compress flag. Added functionality to kpod load to be able to load image from a directory Signed-off-by: umohnani8 Closes: #137 Approved by: rhatdan --- cmd/kpod/load.go | 12 ++++++++---- cmd/kpod/save.go | 34 +++++++++++++++++++++++++++++++--- 2 files changed, 39 insertions(+), 7 deletions(-) (limited to 'cmd') diff --git a/cmd/kpod/load.go b/cmd/kpod/load.go index d29da0c06..5ae75a7a2 100644 --- a/cmd/kpod/load.go +++ b/cmd/kpod/load.go @@ -104,14 +104,18 @@ func loadCmd(c *cli.Context) error { src := libpod.DockerArchive + ":" + input imgName, err := runtime.PullImage(src, options) if err != nil { - src = libpod.OCIArchive + ":" + input // generate full src name with specified image:tag + fullSrc := libpod.OCIArchive + ":" + input if image != "" { - src = src + ":" + image + fullSrc = fullSrc + ":" + image } - imgName, err = runtime.PullImage(src, options) + imgName, err = runtime.PullImage(fullSrc, options) if err != nil { - return errors.Wrapf(err, "error pulling %q", src) + src = libpod.DirTransport + ":" + input + imgName, err = runtime.PullImage(src, options) + if err != nil { + return errors.Wrapf(err, "error pulling %q", src) + } } } fmt.Println("Loaded image: ", imgName) diff --git a/cmd/kpod/save.go b/cmd/kpod/save.go index 0f5fcfa4d..85a8c7930 100644 --- a/cmd/kpod/save.go +++ b/cmd/kpod/save.go @@ -3,15 +3,27 @@ package main import ( "io" "os" + "strings" + "github.com/containers/image/manifest" + imgspecv1 "github.com/opencontainers/image-spec/specs-go/v1" "github.com/pkg/errors" "github.com/projectatomic/libpod/libpod" "github.com/sirupsen/logrus" "github.com/urfave/cli" ) +const ( + ociManifestDir = "oci-dir" + v2s2ManifestDir = "docker-dir" +) + var ( saveFlags = []cli.Flag{ + cli.BoolFlag{ + Name: "compress", + Usage: "compress tarball image layers when saving to a directory using the 'dir' transport. (default is same compression type as source)", + }, cli.StringFlag{ Name: "output, o", Usage: "Write to a file, default is STDOUT", @@ -23,7 +35,7 @@ var ( }, cli.StringFlag{ Name: "format", - Usage: "Save image to oci-archive", + Usage: "Save image to oci-archive, oci-dir (directory with oci manifest type), docker-dir (directory with v2s2 manifest type)", }, } saveDescription = ` @@ -56,6 +68,10 @@ func saveCmd(c *cli.Context) error { } defer runtime.Shutdown(false) + if c.IsSet("compress") && (c.String("format") != ociManifestDir && c.String("format") != v2s2ManifestDir && c.String("format") == "") { + return errors.Errorf("--compress can only be set when --format is either 'oci-dir' or 'docker-dir'") + } + var writer io.Writer if !c.Bool("quiet") { writer = os.Stdout @@ -69,10 +85,16 @@ func saveCmd(c *cli.Context) error { } } - var dst string + var dst, manifestType string switch c.String("format") { case libpod.OCIArchive: dst = libpod.OCIArchive + ":" + output + case "oci-dir": + dst = libpod.DirTransport + ":" + output + manifestType = imgspecv1.MediaTypeImageManifest + case "docker-dir": + dst = libpod.DirTransport + ":" + output + manifestType = manifest.DockerV2Schema2MediaType case libpod.DockerArchive: fallthrough case "": @@ -84,12 +106,18 @@ func saveCmd(c *cli.Context) error { saveOpts := libpod.CopyOptions{ SignaturePolicyPath: "", Writer: writer, + ManifestMIMEType: manifestType, + ForceCompress: c.Bool("compress"), } // only one image is supported for now // future pull requests will fix this for _, image := range args { - dest := dst + ":" + image + dest := dst + // need dest to be in the format transport:path:reference for the following transports + if strings.Contains(dst, libpod.OCIArchive) || strings.Contains(dst, libpod.DockerArchive) { + dest = dst + ":" + image + } if err := runtime.PushImage(image, dest, saveOpts); err != nil { if err2 := os.Remove(output); err2 != nil { logrus.Errorf("error deleting %q: %v", output, err) -- cgit v1.2.3-54-g00ecf