Skip to content
This repository was archived by the owner on Feb 1, 2021. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions api/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
apitypes "github.com/docker/docker/api/types"
containertypes "github.com/docker/docker/api/types/container"
dockerfilters "github.com/docker/docker/api/types/filters"
"github.com/docker/docker/api/types/network"
typesversions "github.com/docker/docker/api/types/versions"
volumetypes "github.com/docker/docker/api/types/volume"
"github.com/docker/docker/pkg/parsers/kernel"
Expand Down Expand Up @@ -698,6 +699,7 @@ func postContainersCreate(c *context, w http.ResponseWriter, r *http.Request) {
httpError(w, err.Error(), http.StatusInternalServerError)
return
}
containerConfig.NetworkingConfig = stripNodeNamesFromNetworkingConfig(containerConfig.NetworkingConfig, c.cluster.EngineNames())

container, err := c.cluster.CreateContainer(containerConfig, name, authConfig)
if err != nil {
Expand Down Expand Up @@ -1645,3 +1647,26 @@ func (h headerFlusher) WriteHeader(status int) {
func postContainersWait(c *context, w http.ResponseWriter, r *http.Request) {
proxyContainerAndForceRefresh(c, headerFlusher{w}, r)
}

// stripNodeNamesFromNetworkingConfig goes through a container's
// NetworkingConfig and, if a user specified a Swarm classic-style network
// name in the endpoints config (i.e. <node name>/<network name>), strips off
// the node name so that the daemon will recognize the network.
func stripNodeNamesFromNetworkingConfig(n network.NetworkingConfig, nodeNames []string) network.NetworkingConfig {
endpointsConfig := map[string]*network.EndpointSettings{}
nodeNamesMap := map[string]struct{}{}
for _, name := range nodeNames {
nodeNamesMap[name] = struct{}{}
}
for networkName, v := range n.EndpointsConfig {
parts := strings.SplitN(networkName, "/", 2)
if len(parts) > 1 {
if _, ok := nodeNamesMap[parts[0]]; ok {
networkName = parts[1]
}
}
endpointsConfig[networkName] = v
}
n.EndpointsConfig = endpointsConfig
return n
}
26 changes: 26 additions & 0 deletions api/handlers_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package api

import (
"testing"

"github.com/docker/docker/api/types/network"
"github.com/stretchr/testify/assert"
)

func TestStripNodeNamesFromNetworkingConfig(t *testing.T) {
networkingConfig := network.NetworkingConfig{
EndpointsConfig: map[string]*network.EndpointSettings{
"testnet1": {},
"nodename/testnet2": {},
"nodename/test/net3": {},
"foo/test/net4": {},
"nodename/nodename/net5": {},
},
}
networkingConfig = stripNodeNamesFromNetworkingConfig(networkingConfig, []string{"nodename"})
assert.Contains(t, networkingConfig.EndpointsConfig, "testnet1")
assert.Contains(t, networkingConfig.EndpointsConfig, "testnet2")
assert.Contains(t, networkingConfig.EndpointsConfig, "test/net3")
assert.Contains(t, networkingConfig.EndpointsConfig, "foo/test/net4")
assert.Contains(t, networkingConfig.EndpointsConfig, "nodename/net5")
}
3 changes: 3 additions & 0 deletions cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ type Cluster interface {
// TotalCpus returns the number of CPUs in the cluster.
TotalCpus() int64

// EngineNames returns the names of all the engines in the cluster.
EngineNames() []string

// RegisterEventHandler registers an event handler for cluster-wide events.
RegisterEventHandler(h EventHandler) error

Expand Down
9 changes: 9 additions & 0 deletions cluster/swarm/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -965,6 +965,15 @@ func (c *Cluster) TotalCpus() int64 {
return totalCpus
}

// EngineNames returns the names of all the engines in the cluster.
func (c *Cluster) EngineNames() []string {
ret := make([]string, len(c.engines))
for _, engine := range c.engines {
ret = append(ret, engine.Name)
}
return ret
}

// Info returns some info about the cluster, like nb or containers / images.
func (c *Cluster) Info() [][2]string {
info := [][2]string{
Expand Down
2 changes: 2 additions & 0 deletions test/integration/api/network.bats
Original file line number Diff line number Diff line change
Expand Up @@ -222,4 +222,6 @@ function teardown() {

run docker_swarm network inspect testn
[[ "${output}" != *"\"Containers\": {}"* ]]

docker_swarm run -d --net node-1/testn --network-alias=foobar --name test_container2 busybox sleep 100
}