From e9c486b0463329a8a81ccc363adc3505d30bd00c Mon Sep 17 00:00:00 2001 From: Victor Vieux Date: Fri, 16 Oct 2015 17:13:41 -0700 Subject: [PATCH 1/4] refresh networks on whole cluster after create and rm Signed-off-by: Victor Vieux --- cluster/swarm/cluster.go | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/cluster/swarm/cluster.go b/cluster/swarm/cluster.go index 36dc4fd416..658f877741 100644 --- a/cluster/swarm/cluster.go +++ b/cluster/swarm/cluster.go @@ -176,7 +176,9 @@ func (c *Cluster) RemoveContainer(container *cluster.Container, force, volumes b // RemoveNetwork removes a network from the cluster func (c *Cluster) RemoveNetwork(network *cluster.Network) error { - return network.Engine.RemoveNetwork(network) + err := network.Engine.RemoveNetwork(network) + c.refreshNetworks() + return err } func (c *Cluster) getEngineByAddr(addr string) *cluster.Engine { @@ -335,6 +337,18 @@ func (c *Cluster) RemoveImages(name string, force bool) ([]*dockerclient.ImageDe return out, err } +func (c *Cluster) refreshNetworks() { + var wg sync.WaitGroup + for _, e := range c.engines { + wg.Add(1) + go func() { + e.RefreshNetworks() + wg.Done() + }() + } + wg.Wait() +} + // CreateNetwork creates a network in the cluster func (c *Cluster) CreateNetwork(request *dockerclient.NetworkCreate) (response *dockerclient.NetworkCreateResponse, err error) { var ( @@ -353,7 +367,9 @@ func (c *Cluster) CreateNetwork(request *dockerclient.NetworkCreate) (response * return nil, err } if nodes != nil { - return c.engines[nodes[0].ID].CreateNetwork(request) + resp, err := c.engines[nodes[0].ID].CreateNetwork(request) + c.refreshNetworks() + return resp, err } return nil, nil } From 4e1ae773e21aa0f9e29c9e74ee160e8beddefe41 Mon Sep 17 00:00:00 2001 From: Victor Vieux Date: Fri, 16 Oct 2015 17:33:19 -0700 Subject: [PATCH 2/4] improve docker network ls and rm Signed-off-by: Victor Vieux --- api/handlers.go | 8 +++++--- cluster/network.go | 13 +++++++++++++ 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/api/handlers.go b/api/handlers.go index f60ef32e58..1ad1d82b9d 100644 --- a/api/handlers.go +++ b/api/handlers.go @@ -172,9 +172,11 @@ func getImagesJSON(c *context, w http.ResponseWriter, r *http.Request) { // GET /networks func getNetworks(c *context, w http.ResponseWriter, r *http.Request) { out := []*dockerclient.NetworkResource{} - for _, network := range c.cluster.Networks() { + for _, network := range c.cluster.Networks().Uniq() { tmp := (*network).NetworkResource - tmp.Name = network.Engine.Name + "/" + network.Name + if tmp.Scope == "local" { + tmp.Name = network.Engine.Name + "/" + network.Name + } out = append(out, &tmp) } w.Header().Set("Content-Type", "application/json") @@ -687,7 +689,7 @@ func deleteNetworks(c *context, w http.ResponseWriter, r *http.Request) { var id = mux.Vars(r)["networkid"] - if network := c.cluster.Networks().Get(id); network != nil { + if network := c.cluster.Networks().Uniq().Get(id); network != nil { if err := c.cluster.RemoveNetwork(network); err != nil { httpError(w, err.Error(), http.StatusInternalServerError) return diff --git a/cluster/network.go b/cluster/network.go index 9cede7a2e4..6c4e535ae9 100644 --- a/cluster/network.go +++ b/cluster/network.go @@ -17,6 +17,19 @@ type Network struct { // Networks represents a map of networks type Networks []*Network +// Uniq returns all uniq networks +func (networks Networks) Uniq() Networks { + tmp := make(map[string]*Network) + for _, network := range networks { + tmp[network.ID] = network + } + uniq := Networks{} + for _, network := range tmp { + uniq = append(uniq, network) + } + return uniq +} + // Get returns a network using it's ID or Name func (networks Networks) Get(IDOrName string) *Network { // Abort immediately if the name is empty. From 07a102adc805d89496770ca88158e8d1f3d842e2 Mon Sep 17 00:00:00 2001 From: Victor Vieux Date: Mon, 19 Oct 2015 15:46:38 -0700 Subject: [PATCH 3/4] fix tests Signed-off-by: Victor Vieux --- test/integration/api/network.bats | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/test/integration/api/network.bats b/test/integration/api/network.bats index c6b797f363..f84834ba17 100644 --- a/test/integration/api/network.bats +++ b/test/integration/api/network.bats @@ -26,7 +26,7 @@ function teardown() { [ "$status" -ne 0 ] run docker_swarm network inspect node-0/bridge - [ "${#lines[@]}" -eq 23 ] + [[ "${output}" != *"\"containers\": {}"* ]] } @test "docker network create" { @@ -75,20 +75,20 @@ function teardown() { docker_swarm run -d --name test_container -e constraint:node==node-0 busybox sleep 100 run docker_swarm network inspect node-0/bridge - [ "${#lines[@]}" -eq 23 ] + [[ "${output}" != *"\"containers\": {}"* ]] docker_swarm network disconnect node-0/bridge test_container run docker_swarm network inspect node-0/bridge - [ "${#lines[@]}" -eq 16 ] + [[ "${output}" == *"\"containers\": {}"* ]] docker_swarm network connect node-0/bridge test_container run docker_swarm network inspect node-0/bridge - [ "${#lines[@]}" -eq 23 ] + [[ "${output}" != *"\"containers\": {}"* ]] docker_swarm rm -f test_container run docker_swarm network inspect node-0/bridge - [ "${#lines[@]}" -eq 16 ] + [[ "${output}" == *"\"containers\": {}"* ]] } From f9807f561c89fd424d21a5f60af0d5ec67ec2d61 Mon Sep 17 00:00:00 2001 From: Victor Vieux Date: Sat, 17 Oct 2015 01:32:44 -0700 Subject: [PATCH 4/4] fix golint Signed-off-by: Victor Vieux --- cluster/swarm/cluster.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cluster/swarm/cluster.go b/cluster/swarm/cluster.go index 658f877741..9d03e1efff 100644 --- a/cluster/swarm/cluster.go +++ b/cluster/swarm/cluster.go @@ -341,10 +341,10 @@ func (c *Cluster) refreshNetworks() { var wg sync.WaitGroup for _, e := range c.engines { wg.Add(1) - go func() { + go func(e *cluster.Engine) { e.RefreshNetworks() wg.Done() - }() + }(e) } wg.Wait() }