loadImage: cleanup command and initiate enhancements

pull/266/head
iwilltry42 4 years ago
parent 03d651a502
commit 487393bbde
No known key found for this signature in database
GPG Key ID: 7BA57AD1CFF16110
  1. 27
      cmd/load/loadImage.go
  2. 12
      pkg/tools/tools.go
  3. 5
      pkg/types/types.go

@ -34,6 +34,8 @@ import (
// NewCmdLoadImage returns a new cobra command
func NewCmdLoadImage() *cobra.Command {
loadImageOpts := k3d.LoadImageOpts{}
// create new command
cmd := &cobra.Command{
Use: "image [IMAGE [IMAGE...]]",
@ -41,11 +43,11 @@ func NewCmdLoadImage() *cobra.Command {
Long: `Load an image from docker into a k3d cluster.`,
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
images, clusters, keepTarball := parseLoadImageCmd(cmd, args)
images, clusters := parseLoadImageCmd(cmd, args)
log.Debugf("Load images [%+v] from runtime [%s] into clusters [%+v]", images, runtimes.SelectedRuntime, clusters)
for _, cluster := range clusters {
log.Infof("Loading images into '%s'", cluster.Name)
if err := tools.LoadImagesIntoCluster(cmd.Context(), runtimes.SelectedRuntime, images, &cluster, keepTarball); err != nil {
if err := tools.LoadImagesIntoCluster(cmd.Context(), runtimes.SelectedRuntime, images, &cluster, loadImageOpts); err != nil {
log.Errorf("Failed to load images into cluster '%s'", cluster.Name)
log.Errorln(err)
}
@ -58,8 +60,7 @@ func NewCmdLoadImage() *cobra.Command {
* Flags *
*********/
cmd.Flags().StringArrayP("cluster", "c", []string{k3d.DefaultClusterName}, "Select clusters to load the image to.")
cmd.Flags().BoolP("keep-tarball", "k", false, "Do not delete the tarball which contains the saved images from the shared volume")
cmd.Flags().StringP("tar", "t", "", "Import image from local tarball")
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")
}
@ -71,21 +72,7 @@ func NewCmdLoadImage() *cobra.Command {
}
// parseLoadImageCmd parses the command input into variables required to create a cluster
func parseLoadImageCmd(cmd *cobra.Command, args []string) ([]string, []k3d.Cluster, bool) {
// --tar
localTarball, err := cmd.Flags().GetString("tar")
if err != nil {
log.Fatalln(err)
}
if cmd.Flags().Changed("tar") { // TODO: loadImage: implement import from local tarball
log.Fatalf("--tar flag not supported yet '%s'", localTarball)
}
// --keep-tarball
keepTarball, err := cmd.Flags().GetBool("keep-tarball")
if err != nil {
log.Fatalln(err)
}
func parseLoadImageCmd(cmd *cobra.Command, args []string) ([]string, []k3d.Cluster) {
// --cluster
clusterNames, err := cmd.Flags().GetStringArray("cluster")
@ -103,5 +90,5 @@ func parseLoadImageCmd(cmd *cobra.Command, args []string) ([]string, []k3d.Clust
log.Fatalln("No images specified!")
}
return images, clusters, keepTarball
return images, clusters
}

@ -35,7 +35,7 @@ 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, keepTarball bool) error {
func LoadImagesIntoCluster(ctx context.Context, runtime runtimes.Runtime, images []string, cluster *k3d.Cluster, loadImageOpts k3d.LoadImageOpts) error {
cluster, err := k3dc.GetCluster(ctx, runtime, cluster)
if err != nil {
log.Errorf("Failed to find the specified cluster")
@ -76,6 +76,14 @@ func LoadImagesIntoCluster(ctx context.Context, runtime runtimes.Runtime, images
log.Errorf("Failed to start tools container for cluster '%s'", cluster.Name)
}
/* TODO:
* Loop over list of images and check, whether they are files (tar archives) and sort them respectively
* Special case: '-' means "read from stdin"
* 1. From daemon: save images -> import
* 2. From file: copy file -> import
* 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
@ -104,7 +112,7 @@ func LoadImagesIntoCluster(ctx context.Context, runtime runtimes.Runtime, images
importWaitgroup.Wait()
// remove tarball
if !keepTarball {
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)

@ -142,6 +142,11 @@ type StartNodeOpts struct {
Timeout time.Duration
}
// LoadImageOpts describes a set of options one can set for loading image(s) into cluster(s)
type LoadImageOpts struct {
KeepTar bool
}
// ClusterNetwork describes a network which a cluster is running in
type ClusterNetwork struct {
Name string `yaml:"name" json:"name,omitempty"`

Loading…
Cancel
Save