From 1392b29a5d3c260590ac2dcd5fa846756ba914d8 Mon Sep 17 00:00:00 2001 From: Daniel J Walsh Date: Sat, 9 Sep 2017 11:14:40 +0000 Subject: [PATCH] Switch kpod load/push/save to use libpod runtime Since this is the last use of libpod/images/copy.go, removing that code Signed-off-by: Daniel J Walsh --- cmd/kpod/load.go | 57 +++++++++++++++--------------------------------- cmd/kpod/push.go | 28 +++++++++++------------- cmd/kpod/save.go | 49 ++++++++++++----------------------------- cmd/kpod/tag.go | 2 +- 4 files changed, 45 insertions(+), 91 deletions(-) diff --git a/cmd/kpod/load.go b/cmd/kpod/load.go index 6061a16e..933fa997 100644 --- a/cmd/kpod/load.go +++ b/cmd/kpod/load.go @@ -2,22 +2,14 @@ package main import ( "io" + "io/ioutil" "os" - "io/ioutil" - - "github.com/containers/storage" "github.com/kubernetes-incubator/cri-o/libpod/images" "github.com/pkg/errors" "github.com/urfave/cli" ) -type loadOptions struct { - input string - quiet bool - image string -} - var ( loadFlags = []cli.Flag{ cli.StringFlag{ @@ -44,14 +36,6 @@ var ( // loadCmd gets the image/file to be loaded from the command line // and calls loadImage to load the image to containers-storage func loadCmd(c *cli.Context) error { - config, err := getConfig(c) - if err != nil { - return errors.Wrapf(err, "could not get config") - } - store, err := getStore(config) - if err != nil { - return err - } args := c.Args() var image string @@ -62,6 +46,11 @@ func loadCmd(c *cli.Context) error { return errors.New("too many arguments. Requires exactly 1") } + runtime, err := getRuntime(c) + if err != nil { + return errors.Wrapf(err, "could not get runtime") + } + input := c.String("input") if input == "/dev/stdin" { @@ -93,33 +82,21 @@ func loadCmd(c *cli.Context) error { } } - opts := loadOptions{ - input: input, - quiet: c.Bool("quiet"), - image: image, + var output io.Writer + if !c.Bool("quiet") { + output = os.Stdout } - - return loadImage(store, opts) -} - -// loadImage loads the image from docker-archive or oci to containers-storage -// using the pullImage function -func loadImage(store storage.Store, opts loadOptions) error { - loadOpts := images.CopyOptions{ - Quiet: opts.quiet, - Store: store, - } - - src := images.DockerArchive + ":" + opts.input - if err := images.PullImage(src, false, loadOpts); err != nil { - src = images.OCIArchive + ":" + opts.input + src := images.DockerArchive + ":" + input + if err := runtime.PullImage(src, false, output); err != nil { + src = images.OCIArchive + ":" + input // generate full src name with specified image:tag - if opts.image != "" { - src = src + ":" + opts.image + if image != "" { + src = src + ":" + image } - if err := images.PullImage(src, false, loadOpts); err != nil { - return errors.Wrapf(err, "error pulling from %q", opts.input) + if err := runtime.PullImage(src, false, output); err != nil { + return errors.Wrapf(err, "error pulling %q", src) } } + return nil } diff --git a/cmd/kpod/push.go b/cmd/kpod/push.go index be9d3a1e..f3968eda 100644 --- a/cmd/kpod/push.go +++ b/cmd/kpod/push.go @@ -2,12 +2,13 @@ package main import ( "fmt" + "io" "os" "github.com/containers/image/types" "github.com/containers/storage/pkg/archive" + "github.com/kubernetes-incubator/cri-o/libpod" "github.com/kubernetes-incubator/cri-o/libpod/common" - "github.com/kubernetes-incubator/cri-o/libpod/images" "github.com/pkg/errors" "github.com/urfave/cli" "golang.org/x/crypto/ssh/terminal" @@ -70,7 +71,6 @@ func pushCmd(c *cli.Context) error { srcName := c.Args().Get(0) destName := c.Args().Get(1) - signaturePolicy := c.String("signature-policy") registryCredsString := c.String("creds") certPath := c.String("cert-dir") skipVerify := !c.BoolT("tls-verify") @@ -94,19 +94,19 @@ func pushCmd(c *cli.Context) error { registryCreds = creds } - config, err := getConfig(c) + runtime, err := getRuntime(c) if err != nil { - return errors.Wrapf(err, "Could not get config") - } - store, err := getStore(config) - if err != nil { - return err + return errors.Wrapf(err, "could not create runtime") } - options := images.CopyOptions{ + var writer io.Writer + if !c.Bool("quiet") { + writer = os.Stdout + } + + options := libpod.CopyOptions{ Compression: archive.Uncompressed, - SignaturePolicyPath: signaturePolicy, - Store: store, + SignaturePolicyPath: c.String("signature-policy"), DockerRegistryOptions: common.DockerRegistryOptions{ DockerRegistryCreds: registryCreds, DockerCertPath: certPath, @@ -117,8 +117,6 @@ func pushCmd(c *cli.Context) error { SignBy: signBy, }, } - if !c.Bool("quiet") { - options.ReportWriter = os.Stderr - } - return images.PushImage(srcName, destName, options) + + return runtime.PushImage(srcName, destName, options, writer) } diff --git a/cmd/kpod/save.go b/cmd/kpod/save.go index 54680a2e..9a6929e0 100644 --- a/cmd/kpod/save.go +++ b/cmd/kpod/save.go @@ -1,22 +1,16 @@ package main import ( + "io" "os" - "github.com/containers/storage" + "github.com/kubernetes-incubator/cri-o/libpod" "github.com/kubernetes-incubator/cri-o/libpod/images" "github.com/pkg/errors" "github.com/sirupsen/logrus" "github.com/urfave/cli" ) -type saveOptions struct { - output string - quiet bool - format string - images []string -} - var ( saveFlags = []cli.Flag{ cli.StringFlag{ @@ -54,17 +48,16 @@ func saveCmd(c *cli.Context) error { return errors.Errorf("need at least 1 argument") } - config, err := getConfig(c) + runtime, err := getRuntime(c) if err != nil { - return errors.Wrapf(err, "could not get config") + return errors.Wrapf(err, "could not create runtime") } - store, err := getStore(config) - if err != nil { - return err + var writer io.Writer + if !c.Bool("quiet") { + writer = os.Stdout } output := c.String("output") - if output == "/dev/stdout" { fi := os.Stdout if logrus.IsTerminal(fi) { @@ -72,41 +65,27 @@ func saveCmd(c *cli.Context) error { } } - opts := saveOptions{ - output: output, - quiet: c.Bool("quiet"), - format: c.String("format"), - images: args, - } - - return saveImage(store, opts) -} - -// saveImage pushes the image to docker-archive or oci by -// calling pushImage -func saveImage(store storage.Store, opts saveOptions) error { var dst string - switch opts.format { + switch c.String("format") { case images.OCIArchive: - dst = images.OCIArchive + ":" + opts.output + dst = images.OCIArchive + ":" + output case images.DockerArchive: fallthrough case "": - dst = images.DockerArchive + ":" + opts.output + dst = images.DockerArchive + ":" + output default: - return errors.Errorf("unknown format option %q", opts.format) + return errors.Errorf("unknown format option %q", c.String("format")) } - saveOpts := images.CopyOptions{ + saveOpts := libpod.CopyOptions{ SignaturePolicyPath: "", - Store: store, } // only one image is supported for now // future pull requests will fix this - for _, image := range opts.images { + for _, image := range args { dest := dst + ":" + image - if err := images.PushImage(image, dest, saveOpts); err != nil { + if err := runtime.PushImage(image, dest, saveOpts, writer); err != nil { return errors.Wrapf(err, "unable to save %q", image) } } diff --git a/cmd/kpod/tag.go b/cmd/kpod/tag.go index 78978ebc..f0829d4e 100644 --- a/cmd/kpod/tag.go +++ b/cmd/kpod/tag.go @@ -50,7 +50,7 @@ func addImageNames(runtime *libpod.Runtime, image *storage.Image, addNames []str } for _, name := range names { if err := runtime.TagImage(image, name); err != nil { - return errors.Wrapf(err, "error adding names (%v) to image %q", name, image.ID) + return errors.Wrapf(err, "error adding name (%v) to image %q", name, image.ID) } } return nil