connect workers and server via own docker network

pull/12/head
iwilltry42 6 years ago
parent 92039edbb1
commit 6c072fb6d9
  1. 11
      cli/commands.go
  2. 21
      cli/container.go
  3. 59
      cli/network.go
  4. 2
      vendor/modules.txt

@ -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)
}

@ -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)
}

@ -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
}

@ -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

Loading…
Cancel
Save