Skip to content

Commit 5289d5d

Browse files
committed
leave idle networks so that they can be deleted
1 parent 8f1885c commit 5289d5d

File tree

3 files changed

+49
-16
lines changed

3 files changed

+49
-16
lines changed

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ RUN apt-get update \
77
&& apt-get clean \
88
&& rm -r /var/lib/apt/lists/*
99

10-
RUN wget https://github.com/codekitchen/dinghy-http-proxy/releases/download/join-networks-v1/join-networks.tar.gz \
10+
RUN wget https://github.com/codekitchen/dinghy-http-proxy/releases/download/join-networks-v2/join-networks.tar.gz \
1111
&& tar -C /app -xzvf join-networks.tar.gz \
1212
&& rm join-networks.tar.gz
1313

build.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/bin/bash
22
set -e
33

4-
docker run --rm -v "$PWD":/go/src/github.com/codekitchen/dinghy-http-proxy -w /go/src/github.com/codekitchen/dinghy-http-proxy golang:1.6 go build -v -o join-networks
4+
docker run --rm -v "$PWD":/go/src/github.com/codekitchen/dinghy-http-proxy -w /go/src/github.com/codekitchen/dinghy-http-proxy golang:1.8 go build -v -o join-networks
55
tar czvf join-networks.tar.gz join-networks
66
rm join-networks

join-networks.go

Lines changed: 47 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,26 @@ func main() {
2020
}
2121

2222
currentNetworks := getJoinedNetworks(client, *containerName)
23-
bridgeNetworks := getBridgeNetworks(client)
23+
bridgeNetworks := getActiveBridgeNetworks(client)
2424

25-
for _, id := range bridgeNetworks {
26-
if !currentNetworks[id] {
27-
err := client.ConnectNetwork(id, docker.NetworkConnectionOptions{
28-
Container: *containerName,
29-
})
30-
if err != nil {
31-
panic(err)
32-
}
25+
toJoin := getNetworksToJoin(currentNetworks, bridgeNetworks)
26+
toLeave := getNetworksToLeave(currentNetworks, bridgeNetworks)
27+
28+
for _, id := range toLeave {
29+
err := client.DisconnectNetwork(id, docker.NetworkConnectionOptions{
30+
Container: *containerName,
31+
})
32+
if err != nil {
33+
panic(err)
34+
}
35+
}
36+
37+
for _, id := range toJoin {
38+
err := client.ConnectNetwork(id, docker.NetworkConnectionOptions{
39+
Container: *containerName,
40+
})
41+
if err != nil {
42+
panic(err)
3343
}
3444
}
3545
}
@@ -49,16 +59,39 @@ func getJoinedNetworks(client *docker.Client, containerName string) (networks ma
4959
return
5060
}
5161

52-
func getBridgeNetworks(client *docker.Client) (ids []string) {
53-
networks, err := client.ListNetworks()
62+
func getActiveBridgeNetworks(client *docker.Client) (networks map[string]bool) {
63+
networks = make(map[string]bool)
64+
65+
allNetworks, err := client.ListNetworks()
5466
if err != nil {
5567
panic(err)
5668
}
5769

58-
for _, net := range networks {
59-
if net.Driver == "bridge" {
60-
ids = append(ids, net.ID)
70+
for _, net := range allNetworks {
71+
if net.Driver == "bridge" && len(net.Containers) > 0 {
72+
networks[net.ID] = true
6173
}
6274
}
75+
76+
return
77+
}
78+
79+
func getNetworksToJoin(currentNetworks map[string]bool, bridgeNetworks map[string]bool) (ids []string) {
80+
for id := range bridgeNetworks {
81+
if !currentNetworks[id] {
82+
ids = append(ids, id)
83+
}
84+
}
85+
86+
return
87+
}
88+
89+
func getNetworksToLeave(currentNetworks map[string]bool, bridgeNetworks map[string]bool) (ids []string) {
90+
for id := range currentNetworks {
91+
if !bridgeNetworks[id] {
92+
ids = append(ids, id)
93+
}
94+
}
95+
6396
return
6497
}

0 commit comments

Comments
 (0)