loadImages: loop over all input images

pull/266/head
iwilltry42 4 years ago
parent 7dec0cc1be
commit 933ac38059
No known key found for this signature in database
GPG Key ID: 7BA57AD1CFF16110
  1. 2
      cmd/get/getCluster.go
  2. 3
      cmd/load/loadImage.go
  3. 29
      pkg/runtimes/containerd/image.go
  4. 54
      pkg/runtimes/docker/image.go
  5. 1
      pkg/runtimes/runtime.go
  6. 87
      pkg/tools/tools.go

@ -116,7 +116,7 @@ func PrintClusters(clusters []*k3d.Cluster, flags clusterFlags) {
for _, cluster := range clusters {
masterCount := cluster.MasterCount()
workerCount := cluster.WorkerCount()
if flags.token {
fmt.Fprintf(tabwriter, "%s\t%d\t%d\t%s\n", cluster.Name, masterCount, workerCount, cluster.Token)
} else {

@ -61,9 +61,6 @@ func NewCmdLoadImage() *cobra.Command {
*********/
cmd.Flags().StringArrayP("cluster", "c", []string{k3d.DefaultClusterName}, "Select clusters to load the image to.")
cmd.Flags().BoolVarP(&loadImageOpts.KeepTar, "keep-tarball", "k", false, "Do not delete the tarball containing the saved images from the shared volume")
if err := cmd.MarkFlagFilename("tar", ".tar"); err != nil {
log.Fatalln("Failed to mark --tar flag as filename")
}
/* Subcommands */

@ -0,0 +1,29 @@
/*
Copyright © 2020 The k3d Author(s)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
package containerd
import "context"
// GetImages returns a list of images present in the runtime
func (d Containerd) GetImages(ctx context.Context) ([]string, error) {
return nil, nil
}

@ -0,0 +1,54 @@
/*
Copyright © 2020 The k3d Author(s)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
package docker
import (
"context"
"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
log "github.com/sirupsen/logrus"
)
// GetImages returns a list of images present in the runtime
func (d Docker) GetImages(ctx context.Context) ([]string, error) {
// create docker client
docker, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
if err != nil {
log.Errorln("Failed to create docker client")
return nil, err
}
defer docker.Close()
imageSummary, err := docker.ImageList(ctx, types.ImageListOptions{All: true})
if err != nil {
log.Errorln("Failed to list available docker images")
return nil, err
}
var images []string
for _, image := range imageSummary {
images = append(images, image.RepoTags...)
}
return images, nil
}

@ -58,6 +58,7 @@ type Runtime interface {
GetRuntimePath() string // returns e.g. '/var/run/docker.sock' for a default docker setup
ExecInNode(context.Context, *k3d.Node, []string) error
GetNodeLogs(context.Context, *k3d.Node, time.Time) (io.ReadCloser, error)
GetImages(context.Context) ([]string, error)
}
// GetRuntime checks, if a given name is represented by an implemented k3d runtime and returns it

@ -24,6 +24,8 @@ package tools
import (
"context"
"fmt"
"os"
"strings"
"sync"
"time"
@ -36,7 +38,41 @@ import (
// LoadImagesIntoCluster starts up a k3d tools container for the selected cluster and uses it to export
// images from the runtime to import them into the nodes of the selected cluster
func LoadImagesIntoCluster(ctx context.Context, runtime runtimes.Runtime, images []string, cluster *k3d.Cluster, loadImageOpts k3d.LoadImageOpts) error {
cluster, err := k3dc.GetCluster(ctx, runtime, cluster)
var imagesFromRuntime []string
var imagesFromTar []string
runtimeImages, err := runtime.GetImages(ctx)
if err != nil {
log.Errorln("Failed to fetch list of exsiting images from runtime")
return err
}
for _, image := range images {
found := false
// Check if the current element is a file
if _, err := os.Stat(image); os.IsNotExist(err) {
// not a file? Check if such an image is present in the container runtime
for _, runtimeImage := range runtimeImages {
if image == runtimeImage {
found = true
imagesFromRuntime = append(imagesFromRuntime, image)
log.Debugf("Selected image '%s' found in runtime", image)
break
}
}
} else {
// file exists
found = true
imagesFromTar = append(imagesFromTar, image)
log.Debugf("Selected image '%s' is a file", image)
}
if !found {
log.Warnf("Image '%s' is not a file and couldn't be found in the container runtime", image)
}
}
cluster, err = k3dc.GetCluster(ctx, runtime, cluster)
if err != nil {
log.Errorf("Failed to find the specified cluster")
return err
@ -84,29 +120,36 @@ func LoadImagesIntoCluster(ctx context.Context, runtime runtimes.Runtime, images
* 3. From stdin: save to tar -> import
* Note: temporary storage location is always the shared image volume and actions are always executed by the tools node
*/
// save image to tarfile in shared volume
log.Infoln("Saving images...")
tarName := fmt.Sprintf("%s/k3d-%s-images-%s.tar", k3d.DefaultImageVolumeMountPath, cluster.Name, time.Now().Format("20060102150405")) // FIXME: change
if err := runtime.ExecInNode(ctx, toolsNode, append([]string{"./k3d-tools", "save-image", "-d", tarName}, images...)); err != nil {
log.Errorf("Failed to save images in tools container for cluster '%s'", cluster.Name)
return err
var importTarNames []string
if len(imagesFromRuntime) > 0 {
// save image to tarfile in shared volume
log.Infof("Saving %d image(s) from runtime...", len(imagesFromRuntime))
tarName := fmt.Sprintf("%s/k3d-%s-images-%s.tar", k3d.DefaultImageVolumeMountPath, cluster.Name, time.Now().Format("20060102150405"))
if err := runtime.ExecInNode(ctx, toolsNode, append([]string{"./k3d-tools", "save-image", "-d", tarName}, imagesFromRuntime...)); err != nil {
log.Errorf("Failed to save image(s) in tools container for cluster '%s'", cluster.Name)
return err
}
importTarNames = append(importTarNames, tarName)
}
// import image in each node
log.Infoln("Importing images into nodes...")
var importWaitgroup sync.WaitGroup
for _, node := range cluster.Nodes {
// only import image in master and worker nodes (i.e. ignoring auxiliary nodes like the master loadbalancer)
if node.Role == k3d.MasterRole || node.Role == k3d.WorkerRole {
importWaitgroup.Add(1)
go func(node *k3d.Node, wg *sync.WaitGroup) {
log.Infof("Importing images into node '%s'...", node.Name)
if err := runtime.ExecInNode(ctx, node, []string{"ctr", "image", "import", tarName}); err != nil {
log.Errorf("Failed to import images in node '%s'", node.Name)
log.Errorln(err)
}
wg.Done()
}(node, &importWaitgroup)
for _, tarName := range importTarNames {
for _, node := range cluster.Nodes {
// only import image in master and worker nodes (i.e. ignoring auxiliary nodes like the master loadbalancer)
if node.Role == k3d.MasterRole || node.Role == k3d.WorkerRole {
importWaitgroup.Add(1)
go func(node *k3d.Node, wg *sync.WaitGroup) {
log.Infof("Importing images into node '%s'...", node.Name)
if err := runtime.ExecInNode(ctx, node, []string{"ctr", "image", "import", tarName}); err != nil {
log.Errorf("Failed to import images in node '%s'", node.Name)
log.Errorln(err)
}
wg.Done()
}(node, &importWaitgroup)
}
}
}
importWaitgroup.Wait()
@ -114,8 +157,8 @@ func LoadImagesIntoCluster(ctx context.Context, runtime runtimes.Runtime, images
// remove tarball
if !loadImageOpts.KeepTar {
log.Infoln("Removing the tarball...")
if err := runtime.ExecInNode(ctx, cluster.Nodes[0], []string{"rm", "-f", tarName}); err != nil { // TODO: do this in tools node (requires rm)
log.Errorf("Failed to delete tarball '%s'", tarName)
if err := runtime.ExecInNode(ctx, cluster.Nodes[0], []string{"rm", "-f", strings.Join(importTarNames, " ")}); err != nil { // TODO: do this in tools node (requires rm)
log.Errorf("Failed to delete one or more tarballs from '%+v'", importTarNames)
log.Errorln(err)
}
}
@ -126,7 +169,7 @@ func LoadImagesIntoCluster(ctx context.Context, runtime runtimes.Runtime, images
log.Errorln("Failed to delete tools node '%s': Try to delete it manually", toolsNode.Name)
}
log.Infoln("...Done")
log.Infoln("Successfully imported image(s)")
return nil

Loading…
Cancel
Save