From deccb0122ab0e6545e1292172fa1d59dabdb01a1 Mon Sep 17 00:00:00 2001 From: iwilltry42 Date: Mon, 1 Jul 2019 12:17:03 +0200 Subject: [PATCH] enable importing multiple images at once --- cli/commands.go | 8 +++++++- cli/image.go | 22 ++++++++++------------ main.go | 11 ++++------- 3 files changed, 21 insertions(+), 20 deletions(-) diff --git a/cli/commands.go b/cli/commands.go index 4fe0c04f..4dbe60e0 100644 --- a/cli/commands.go +++ b/cli/commands.go @@ -365,5 +365,11 @@ func Shell(c *cli.Context) error { // ImportImage saves an image locally and imports it into the k3d containers func ImportImage(c *cli.Context) error { - return importImage(c.String("name"), c.String("image")) + images := make([]string, 0) + if strings.Contains(c.Args().First(), ",") { + images = append(images, strings.Split(c.Args().First(), ",")...) + } else { + images = append(images, c.Args()...) + } + return importImage(c.String("name"), images) } diff --git a/cli/image.go b/cli/image.go index f80b2748..f34fb657 100644 --- a/cli/image.go +++ b/cli/image.go @@ -8,6 +8,7 @@ import ( "log" "os" "strings" + "time" "github.com/docker/docker/api/types" "github.com/docker/docker/client" @@ -15,7 +16,7 @@ import ( const imageBasePathRemote = "/images/" -func importImage(clusterName, image string) error { +func importImage(clusterName string, images []string) error { // get a docker client ctx := context.Background() docker, err := client.NewEnvClient() @@ -30,18 +31,15 @@ func importImage(clusterName, image string) error { return fmt.Errorf("ERROR: couldn't get cluster directory for cluster [%s]\n%+v", clusterName, err) } - // TODO: extend to enable importing a list of images - imageList := []string{image} - //*** first, save the images using the local docker daemon - log.Printf("INFO: Saving image [%s] from local docker daemon...", image) - imageReader, err := docker.ImageSave(ctx, imageList) + log.Printf("INFO: Saving images [%s] from local docker daemon...", images) + imageReader, err := docker.ImageSave(ctx, images) if err != nil { - return fmt.Errorf("ERROR: failed to save image [%s] locally\n%+v", image, err) + return fmt.Errorf("ERROR: failed to save images [%s] locally\n%+v", images, err) } // create tarball - imageTarName := strings.ReplaceAll(strings.ReplaceAll(image, ":", "_"), "/", "_") + ".tar" + imageTarName := "k3d-" + clusterName + "-images-" + time.Now().Format("20060102150405") + ".tar" imageTar, err := os.Create(imageBasePathLocal + imageTarName) if err != nil { return err @@ -50,10 +48,10 @@ func importImage(clusterName, image string) error { _, err = io.Copy(imageTar, imageReader) if err != nil { - return fmt.Errorf("ERROR: couldn't save image [%s] to file [%s]\n%+v", image, imageTar.Name(), err) + return fmt.Errorf("ERROR: couldn't save image [%s] to file [%s]\n%+v", images, imageTar.Name(), err) } - // TODO: get correct container ID by cluster name + // Get the container IDs for all containers in the cluster clusters, err := getClusters(false, clusterName) if err != nil { return fmt.Errorf("ERROR: couldn't get cluster by name [%s]\n%+v", clusterName, err) @@ -86,7 +84,7 @@ func importImage(clusterName, image string) error { for _, container := range containerList { containerName := container.Names[0][1:] // trimming the leading "/" from name - log.Printf("INFO: Importing image [%s] in container [%s]", image, containerName) + log.Printf("INFO: Importing image [%s] in container [%s]", images, containerName) // create exec configuration execResponse, err := docker.ContainerExecCreate(ctx, container.ID, execConfig) @@ -119,7 +117,7 @@ func importImage(clusterName, image string) error { } } - log.Printf("INFO: Successfully imported image [%s] in all nodes of cluster [%s]", image, clusterName) + log.Printf("INFO: Successfully imported image [%s] in all nodes of cluster [%s]", images, clusterName) log.Println("INFO: Cleaning up tarball...") if err := os.Remove(imageBasePathLocal + imageTarName); err != nil { diff --git a/main.go b/main.go index 30c79244..c01dedba 100644 --- a/main.go +++ b/main.go @@ -216,18 +216,15 @@ func main() { }, { // get-kubeconfig grabs the kubeconfig from the cluster and prints the path to it - Name: "import-image", - Usage: "Import a container image from your local docker daemon into the cluster", + Name: "import-images", + Aliases: []string{"i"}, + Usage: "Import a comma- or space-separated list of container images from your local docker daemon into the cluster", Flags: []cli.Flag{ cli.StringFlag{ - Name: "name, n", + Name: "name, n, cluster, c", Value: defaultK3sClusterName, Usage: "Name of the cluster", }, - cli.StringFlag{ - Name: "image, i", - Usage: "Name of the image that you want to import, e.g. `nginx:local`", - }, }, Action: run.ImportImage, },