enable importing multiple images at once
This commit is contained in:
parent
ab2c54ca1d
commit
deccb0122a
@ -365,5 +365,11 @@ func Shell(c *cli.Context) error {
|
|||||||
|
|
||||||
// ImportImage saves an image locally and imports it into the k3d containers
|
// ImportImage saves an image locally and imports it into the k3d containers
|
||||||
func ImportImage(c *cli.Context) error {
|
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)
|
||||||
}
|
}
|
||||||
|
22
cli/image.go
22
cli/image.go
@ -8,6 +8,7 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/docker/docker/api/types"
|
"github.com/docker/docker/api/types"
|
||||||
"github.com/docker/docker/client"
|
"github.com/docker/docker/client"
|
||||||
@ -15,7 +16,7 @@ import (
|
|||||||
|
|
||||||
const imageBasePathRemote = "/images/"
|
const imageBasePathRemote = "/images/"
|
||||||
|
|
||||||
func importImage(clusterName, image string) error {
|
func importImage(clusterName string, images []string) error {
|
||||||
// get a docker client
|
// get a docker client
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
docker, err := client.NewEnvClient()
|
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)
|
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
|
//*** first, save the images using the local docker daemon
|
||||||
log.Printf("INFO: Saving image [%s] from local docker daemon...", image)
|
log.Printf("INFO: Saving images [%s] from local docker daemon...", images)
|
||||||
imageReader, err := docker.ImageSave(ctx, imageList)
|
imageReader, err := docker.ImageSave(ctx, images)
|
||||||
if err != nil {
|
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
|
// create tarball
|
||||||
imageTarName := strings.ReplaceAll(strings.ReplaceAll(image, ":", "_"), "/", "_") + ".tar"
|
imageTarName := "k3d-" + clusterName + "-images-" + time.Now().Format("20060102150405") + ".tar"
|
||||||
imageTar, err := os.Create(imageBasePathLocal + imageTarName)
|
imageTar, err := os.Create(imageBasePathLocal + imageTarName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -50,10 +48,10 @@ func importImage(clusterName, image string) error {
|
|||||||
|
|
||||||
_, err = io.Copy(imageTar, imageReader)
|
_, err = io.Copy(imageTar, imageReader)
|
||||||
if err != nil {
|
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)
|
clusters, err := getClusters(false, clusterName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("ERROR: couldn't get cluster by name [%s]\n%+v", clusterName, err)
|
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 {
|
for _, container := range containerList {
|
||||||
|
|
||||||
containerName := container.Names[0][1:] // trimming the leading "/" from name
|
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
|
// create exec configuration
|
||||||
execResponse, err := docker.ContainerExecCreate(ctx, container.ID, execConfig)
|
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...")
|
log.Println("INFO: Cleaning up tarball...")
|
||||||
if err := os.Remove(imageBasePathLocal + imageTarName); err != nil {
|
if err := os.Remove(imageBasePathLocal + imageTarName); err != nil {
|
||||||
|
11
main.go
11
main.go
@ -216,18 +216,15 @@ func main() {
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
// get-kubeconfig grabs the kubeconfig from the cluster and prints the path to it
|
// get-kubeconfig grabs the kubeconfig from the cluster and prints the path to it
|
||||||
Name: "import-image",
|
Name: "import-images",
|
||||||
Usage: "Import a container image from your local docker daemon into the cluster",
|
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{
|
Flags: []cli.Flag{
|
||||||
cli.StringFlag{
|
cli.StringFlag{
|
||||||
Name: "name, n",
|
Name: "name, n, cluster, c",
|
||||||
Value: defaultK3sClusterName,
|
Value: defaultK3sClusterName,
|
||||||
Usage: "Name of the cluster",
|
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,
|
Action: run.ImportImage,
|
||||||
},
|
},
|
||||||
|
Loading…
Reference in New Issue
Block a user