From cbdeea3bfa55c86a919199233d5c69268c280b25 Mon Sep 17 00:00:00 2001 From: iwilltry42 Date: Tue, 2 Jul 2019 14:48:28 +0200 Subject: [PATCH] create named volume --- cli/commands.go | 18 ++++++++++++++++-- cli/container.go | 14 -------------- cli/image.go | 6 +++--- vendor/modules.txt | 2 +- 4 files changed, 20 insertions(+), 20 deletions(-) diff --git a/cli/commands.go b/cli/commands.go index 4dbe60e0..8fb042cb 100644 --- a/cli/commands.go +++ b/cli/commands.go @@ -135,6 +135,15 @@ func CreateCluster(c *cli.Context) error { log.Fatal(err) } + // create a docker volume for sharing image tarballs with the cluster + imageVolume, err := createImageVolume(c.String("name")) + log.Println("Created docker volume ", imageVolume.Name) + if err != nil { + return err + } + volumes := c.StringSlice("volume") + volumes = append(volumes, fmt.Sprintf("%s:/images", imageVolume.Name)) + clusterSpec := &ClusterSpec{ AgentArgs: []string{}, APIPort: *apiPort, @@ -146,7 +155,7 @@ func CreateCluster(c *cli.Context) error { PortAutoOffset: c.Int("port-auto-offset"), ServerArgs: k3sServerArgs, Verbose: c.GlobalBool("verbose"), - Volumes: c.StringSlice("volume"), + Volumes: volumes, } // create the server @@ -241,8 +250,8 @@ func DeleteCluster(c *cli.Context) error { } } } - log.Println("...Removing server") deleteClusterDir(cluster.name) + log.Println("...Removing server") if err := removeContainer(cluster.server.ID); err != nil { return fmt.Errorf("ERROR: Couldn't remove server for cluster %s\n%+v", cluster.name, err) } @@ -251,6 +260,11 @@ func DeleteCluster(c *cli.Context) error { log.Printf("WARNING: couldn't delete cluster network for cluster %s\n%+v", cluster.name, err) } + log.Println("...Removing docker image volume") + if err := deleteImageVolume(cluster.name); err != nil { + log.Printf("WARNING: couldn't delete image docker volume for cluster %s\n%+v", cluster.name, err) + } + log.Printf("SUCCESS: removed cluster [%s]", cluster.name) } diff --git a/cli/container.go b/cli/container.go index 5dc09763..275b42db 100644 --- a/cli/container.go +++ b/cli/container.go @@ -123,13 +123,6 @@ func createServer(spec *ClusterSpec) (string, error) { hostConfig.Binds = spec.Volumes } - // we need to mount the clusterDir subdirectory `clusterDir/images` to enable importing images without the need for `docker cp` - clusterDir, err := getClusterDir(spec.ClusterName) - if err != nil { - return "", fmt.Errorf("ERROR: couldn't get cluster dir for mounting\n%+v", err) - } - hostConfig.Binds = append(hostConfig.Binds, fmt.Sprintf("%s:/images", clusterDir+"/images")) - networkingConfig := &network.NetworkingConfig{ EndpointsConfig: map[string]*network.EndpointSettings{ k3dNetworkName(spec.ClusterName): { @@ -199,13 +192,6 @@ func createWorker(spec *ClusterSpec, postfix int) (string, error) { hostConfig.Binds = spec.Volumes } - // we need to mount the clusterDir subdirectory `clusterDir/images` to enable importing images without the need for `docker cp` - clusterDir, err := getClusterDir(spec.ClusterName) - if err != nil { - return "", fmt.Errorf("ERROR: couldn't get cluster dir for mounting\n%+v", err) - } - hostConfig.Binds = append(hostConfig.Binds, fmt.Sprintf("%s:/images", clusterDir+"/images")) - networkingConfig := &network.NetworkingConfig{ EndpointsConfig: map[string]*network.EndpointSettings{ k3dNetworkName(spec.ClusterName): { diff --git a/cli/image.go b/cli/image.go index f34fb657..37630625 100644 --- a/cli/image.go +++ b/cli/image.go @@ -25,11 +25,11 @@ func importImage(clusterName string, images []string) error { } // get cluster directory to temporarily save the image tarball there - imageBasePathLocal, err := getClusterDir(clusterName) - imageBasePathLocal = imageBasePathLocal + "/images/" + imageVolume, err := getImageVolume(clusterName) if err != nil { - return fmt.Errorf("ERROR: couldn't get cluster directory for cluster [%s]\n%+v", clusterName, err) + return fmt.Errorf("ERROR: couldn't get image volume for cluster [%s]\n%+v", clusterName, err) } + imageBasePathLocal := imageVolume.Mountpoint + "/" //*** first, save the images using the local docker daemon log.Printf("INFO: Saving images [%s] from local docker daemon...", images) diff --git a/vendor/modules.txt b/vendor/modules.txt index 9a44086b..395bfcee 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -8,6 +8,7 @@ github.com/docker/docker/api/types github.com/docker/docker/api/types/container github.com/docker/docker/api/types/filters github.com/docker/docker/api/types/network +github.com/docker/docker/api/types/volume github.com/docker/docker/client github.com/docker/docker/api/types/mount github.com/docker/docker/api/types/registry @@ -18,7 +19,6 @@ github.com/docker/docker/api/types/versions github.com/docker/docker/api/types/events github.com/docker/docker/api/types/reference github.com/docker/docker/api/types/time -github.com/docker/docker/api/types/volume github.com/docker/docker/pkg/tlsconfig # github.com/docker/go-connections v0.4.0 github.com/docker/go-connections/nat