Skip to content
This repository was archived by the owner on Feb 1, 2021. It is now read-only.

Commit 7c91dc7

Browse files
author
Wayne Song
committed
Strip off node name network prefixes in endpoint configs in networking configs
Previously, if you specified something like: ``` docker run --rm \ --net=mynode/testnet --network-alias=foobar \ alpine ``` Swarm classic would fail because it would try to pass an endpoint config to the daemon with `mynode/testnet` as the network name Signed-off-by: Wayne Song <[email protected]>
1 parent b909146 commit 7c91dc7

5 files changed

Lines changed: 65 additions & 0 deletions

File tree

api/handlers.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
apitypes "github.com/docker/docker/api/types"
1919
containertypes "github.com/docker/docker/api/types/container"
2020
dockerfilters "github.com/docker/docker/api/types/filters"
21+
"github.com/docker/docker/api/types/network"
2122
typesversions "github.com/docker/docker/api/types/versions"
2223
volumetypes "github.com/docker/docker/api/types/volume"
2324
"github.com/docker/docker/pkg/parsers/kernel"
@@ -698,6 +699,7 @@ func postContainersCreate(c *context, w http.ResponseWriter, r *http.Request) {
698699
httpError(w, err.Error(), http.StatusInternalServerError)
699700
return
700701
}
702+
containerConfig.NetworkingConfig = stripNodeNamesFromNetworkingConfig(containerConfig.NetworkingConfig, c.cluster.EngineNames())
701703

702704
container, err := c.cluster.CreateContainer(containerConfig, name, authConfig)
703705
if err != nil {
@@ -1645,3 +1647,26 @@ func (h headerFlusher) WriteHeader(status int) {
16451647
func postContainersWait(c *context, w http.ResponseWriter, r *http.Request) {
16461648
proxyContainerAndForceRefresh(c, headerFlusher{w}, r)
16471649
}
1650+
1651+
// stripNodeNamesFromNetworkingConfig goes through a container's
1652+
// NetworkingConfig and, if a user specified a Swarm classic-style network
1653+
// name in the endpoints config (i.e. <node name>/<network name>), strips off
1654+
// the node name so that the daemon will recognize the network.
1655+
func stripNodeNamesFromNetworkingConfig(n network.NetworkingConfig, nodeNames []string) network.NetworkingConfig {
1656+
endpointsConfig := map[string]*network.EndpointSettings{}
1657+
nodeNamesMap := map[string]struct{}{}
1658+
for _, name := range nodeNames {
1659+
nodeNamesMap[name] = struct{}{}
1660+
}
1661+
for networkName, v := range n.EndpointsConfig {
1662+
parts := strings.SplitN(networkName, "/", 2)
1663+
if len(parts) > 1 {
1664+
if _, ok := nodeNamesMap[parts[0]]; ok {
1665+
networkName = parts[1]
1666+
}
1667+
}
1668+
endpointsConfig[networkName] = v
1669+
}
1670+
n.EndpointsConfig = endpointsConfig
1671+
return n
1672+
}

api/handlers_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package api
2+
3+
import (
4+
"testing"
5+
6+
"github.com/docker/docker/api/types/network"
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
func TestStripNodeNamesFromNetworkingConfig(t *testing.T) {
11+
networkingConfig := network.NetworkingConfig{
12+
EndpointsConfig: map[string]*network.EndpointSettings{
13+
"testnet1": {},
14+
"nodename/testnet2": {},
15+
"nodename/test/net3": {},
16+
"foo/test/net4": {},
17+
"nodename/nodename/net5": {},
18+
},
19+
}
20+
networkingConfig = stripNodeNamesFromNetworkingConfig(networkingConfig, []string{"nodename"})
21+
assert.Contains(t, networkingConfig.EndpointsConfig, "testnet1")
22+
assert.Contains(t, networkingConfig.EndpointsConfig, "testnet2")
23+
assert.Contains(t, networkingConfig.EndpointsConfig, "test/net3")
24+
assert.Contains(t, networkingConfig.EndpointsConfig, "foo/test/net4")
25+
assert.Contains(t, networkingConfig.EndpointsConfig, "nodename/net5")
26+
}

cluster/cluster.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ type Cluster interface {
7575
// TotalCpus returns the number of CPUs in the cluster.
7676
TotalCpus() int64
7777

78+
// EngineNames returns the names of all the engines in the cluster.
79+
EngineNames() []string
80+
7881
// RegisterEventHandler registers an event handler for cluster-wide events.
7982
RegisterEventHandler(h EventHandler) error
8083

cluster/swarm/cluster.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -965,6 +965,15 @@ func (c *Cluster) TotalCpus() int64 {
965965
return totalCpus
966966
}
967967

968+
// EngineNames returns the names of all the engines in the cluster.
969+
func (c *Cluster) EngineNames() []string {
970+
ret := make([]string, len(c.engines))
971+
for _, engine := range c.engines {
972+
ret = append(ret, engine.Name)
973+
}
974+
return ret
975+
}
976+
968977
// Info returns some info about the cluster, like nb or containers / images.
969978
func (c *Cluster) Info() [][2]string {
970979
info := [][2]string{

test/integration/api/network.bats

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,4 +222,6 @@ function teardown() {
222222

223223
run docker_swarm network inspect testn
224224
[[ "${output}" != *"\"Containers\": {}"* ]]
225+
226+
docker_swarm run -d --net node-1/testn --network-alias=foobar --name test_container2 busybox sleep 100
225227
}

0 commit comments

Comments
 (0)