From 22fedf0db0c0cc4b111e975efe41d17fbc35437d Mon Sep 17 00:00:00 2001 From: Victor Vieux Date: Thu, 18 Feb 2016 16:05:28 -0800 Subject: [PATCH] support docker run --net / Signed-off-by: Victor Vieux --- cluster/config.go | 12 +++++++++++ cluster/swarm/cluster.go | 7 +++++++ test/integration/api/network.bats | 13 +++++++++++- test/integration/compose/up.bats | 20 +++++++++++++++++++ .../testdata/compose/simple_v2.yml | 13 ++++++++++++ 5 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 test/integration/testdata/compose/simple_v2.yml diff --git a/cluster/config.go b/cluster/config.go index 57822f99d9..0a22434746 100644 --- a/cluster/config.go +++ b/cluster/config.go @@ -196,6 +196,18 @@ func (c *ContainerConfig) RemoveAffinity(affinity string) error { return nil } +// AddConstraint to config +func (c *ContainerConfig) AddConstraint(constraint string) error { + constraints := c.extractExprs("constraints") + constraints = append(constraints, constraint) + labels, err := json.Marshal(constraints) + if err != nil { + return err + } + c.Labels[SwarmLabelNamespace+".constraints"] = string(labels) + return nil +} + // HaveNodeConstraint in config func (c *ContainerConfig) HaveNodeConstraint() bool { constraints := c.extractExprs("constraints") diff --git a/cluster/swarm/cluster.go b/cluster/swarm/cluster.go index 9d7e847cc3..089d42604d 100644 --- a/cluster/swarm/cluster.go +++ b/cluster/swarm/cluster.go @@ -170,6 +170,13 @@ func (c *Cluster) createContainer(config *cluster.ContainerConfig, name string, config.SetSwarmID(swarmID) } + if network := c.Networks().Get(config.HostConfig.NetworkMode); network != nil && network.Scope == "local" { + if !config.HaveNodeConstraint() { + config.AddConstraint("node==~" + network.Engine.Name) + } + config.HostConfig.NetworkMode = network.Name + } + if withImageAffinity { config.AddAffinity("image==" + config.Image) } diff --git a/test/integration/api/network.bats b/test/integration/api/network.bats index fa9ad0048d..b938288636 100644 --- a/test/integration/api/network.bats +++ b/test/integration/api/network.bats @@ -40,7 +40,6 @@ function teardown() { run docker_swarm network ls --filter type=custom [ "${#lines[@]}" -eq 2 ] - } @test "docker network inspect" { @@ -176,3 +175,15 @@ function teardown() { run docker_swarm network inspect testn [[ "${output}" != *"\"Containers\": {}"* ]] } + +@test "docker run --net /" { + start_docker_with_busybox 2 + swarm_manage + + docker_swarm network create -d bridge node-1/testn + + docker_swarm run -d --net node-1/testn --name test_container busybox sleep 100 + + run docker_swarm network inspect testn + [[ "${output}" != *"\"Containers\": {}"* ]] +} diff --git a/test/integration/compose/up.bats b/test/integration/compose/up.bats index 11b0a4de05..4d80e80a0a 100644 --- a/test/integration/compose/up.bats +++ b/test/integration/compose/up.bats @@ -48,6 +48,26 @@ function teardown() { [[ "${output}" == *"->80/tcp"* ]] } +@test "docker-compose up - check bridge network" { + # docker network connect --ip is introduced in docker 1.10, skip older version without --ip + run docker network connect --help + if [[ "${output}" != *"--ip"* ]]; then + skip + fi + + start_docker_with_busybox 2 + swarm_manage + FILE=$TESTDATA/compose/simple_v2.yml + + docker-compose_swarm -f $FILE up -d + + run docker_swarm ps -q + [ "${#lines[@]}" -eq 1 ] + + run docker_swarm inspect compose_service1_1 + [[ "${output}" == *"testn\""* ]] +} + function containerRunning() { local container="$1" local node="$2" diff --git a/test/integration/testdata/compose/simple_v2.yml b/test/integration/testdata/compose/simple_v2.yml new file mode 100644 index 0000000000..98c631c8f5 --- /dev/null +++ b/test/integration/testdata/compose/simple_v2.yml @@ -0,0 +1,13 @@ +version: "2" + +services: + + service1: + image: busybox + command: sleep 100 + networks: + - testn + +networks: + testn: + driver: bridge