diff --git a/cli/commands.go b/cli/commands.go index 27f9f773..0026506b 100644 --- a/cli/commands.go +++ b/cli/commands.go @@ -37,6 +37,13 @@ func CheckTools(c *cli.Context) error { // CreateCluster creates a new single-node cluster container and initializes the cluster directory func CreateCluster(c *cli.Context) error { + // create cluster network + networkID, err := createClusterNetwork(c.String("name")) + if err != nil { + return err + } + log.Printf("Created cluster network with ID %s", networkID) + if c.IsSet("timeout") && !c.IsSet("wait") { return errors.New("Cannot use --timeout flag without --wait flag") } @@ -180,6 +187,10 @@ func DeleteCluster(c *cli.Context) error { return fmt.Errorf("ERROR: Couldn't remove server for cluster %s\n%+v", cluster.name, err) } + if err := deleteClusterNetwork(cluster.name); err != nil { + log.Printf("WARNING: couldn't delete cluster network 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 a4d15c19..af77579b 100644 --- a/cli/container.go +++ b/cli/container.go @@ -13,6 +13,7 @@ import ( "github.com/docker/docker/api/types" "github.com/docker/docker/api/types/container" + "github.com/docker/docker/api/types/network" "github.com/docker/docker/client" ) @@ -65,6 +66,14 @@ func createServer(verbose bool, image string, port string, args []string, env [] hostConfig.Binds = volumes } + networkingConfig := &network.NetworkingConfig{ + EndpointsConfig: map[string]*network.EndpointSettings{ + name: &network.EndpointSettings{ + Aliases: []string{containerName}, + }, + }, + } + resp, err := docker.ContainerCreate(ctx, &container.Config{ Image: image, Cmd: append([]string{"server"}, args...), @@ -73,7 +82,7 @@ func createServer(verbose bool, image string, port string, args []string, env [] }, Env: env, Labels: containerLabels, - }, hostConfig, nil, containerName) + }, hostConfig, networkingConfig, containerName) if err != nil { return "", fmt.Errorf("ERROR: couldn't create container %s\n%+v", containerName, err) } @@ -126,11 +135,19 @@ func createWorker(verbose bool, image string, args []string, env []string, name hostConfig.Binds = volumes } + networkingConfig := &network.NetworkingConfig{ + EndpointsConfig: map[string]*network.EndpointSettings{ + name: &network.EndpointSettings{ + Aliases: []string{containerName}, + }, + }, + } + resp, err := docker.ContainerCreate(ctx, &container.Config{ Image: image, Env: env, Labels: containerLabels, - }, hostConfig, nil, containerName) + }, hostConfig, networkingConfig, containerName) if err != nil { return "", fmt.Errorf("ERROR: couldn't create container %s\n%+v", containerName, err) } diff --git a/cli/network.go b/cli/network.go new file mode 100644 index 00000000..148a0241 --- /dev/null +++ b/cli/network.go @@ -0,0 +1,59 @@ +package run + +import ( + "context" + "fmt" + "log" + + "github.com/docker/docker/api/types/filters" + + "github.com/docker/docker/api/types" + "github.com/docker/docker/client" +) + +func createClusterNetwork(clusterName string) (string, error) { + ctx := context.Background() + docker, err := client.NewEnvClient() + if err != nil { + return "", fmt.Errorf("ERROR: couldn't create docker client\n%+v", err) + } + + resp, err := docker.NetworkCreate(ctx, clusterName, types.NetworkCreate{ + Labels: map[string]string{ + "app": "k3d", + "cluster": clusterName, + }, + }) + if err != nil { + return "", fmt.Errorf("ERROR: couldn't create network\n%+v", err) + } + + return resp.ID, nil +} + +func deleteClusterNetwork(clusterName string) error { + ctx := context.Background() + docker, err := client.NewEnvClient() + if err != nil { + return fmt.Errorf("ERROR: couldn't create docker client\n%+v", err) + } + + filters := filters.NewArgs() + filters.Add("label", "app=k3d") + filters.Add("label", fmt.Sprintf("cluster=%s", clusterName)) + + networks, err := docker.NetworkList(ctx, types.NetworkListOptions{ + Filters: filters, + }) + if err != nil { + return fmt.Errorf("ERROR: couldn't find network for cluster %s\n%+v", clusterName, err) + } + + for _, network := range networks { + if err := docker.NetworkRemove(ctx, network.ID); err != nil { + log.Printf("WARNING: couldn't remove network for cluster %s\n%+v", clusterName, err) + continue + } + } + return nil +} diff --git a/vendor/modules.txt b/vendor/modules.txt index e46577e4..9a44086b 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -7,9 +7,9 @@ github.com/docker/distribution/digestset 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/client github.com/docker/docker/api/types/mount -github.com/docker/docker/api/types/network github.com/docker/docker/api/types/registry github.com/docker/docker/api/types/swarm github.com/docker/docker/api/types/blkiodev