Skip to content

Commit

Permalink
Tear down networks when daemon exits
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom Wilkie committed Apr 14, 2015
1 parent aa61d35 commit 02488a3
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 21 deletions.
2 changes: 2 additions & 0 deletions daemon/daemon.go
Expand Up @@ -1046,6 +1046,8 @@ func (daemon *Daemon) shutdown() error {
}
group.Wait()

daemon.networks.Shutdown()

return nil
}

Expand Down
39 changes: 18 additions & 21 deletions daemon/network.go
Expand Up @@ -89,7 +89,7 @@ func (daemon *Daemon) NetworkDestroy(id string) error {
return fmt.Errorf("Network '%s' not found", id)
}

if err := net.DestroyNetwork(); err != nil {
if err := net.Destroy(); err != nil {
return err
}

Expand Down Expand Up @@ -209,6 +209,15 @@ func (reg *NetworkRegistry) Walk(f func(network *Network)) {
}
}

func (reg *NetworkRegistry) Shutdown() {
reg.Lock()
defer reg.Unlock()

for _, network := range reg.networks {
network.Destroy()
}
}

func NewNetwork(networkName, driverName string, labels map[string]string) (*Network, error) {
driver, okay := GetNetworkDriver(driverName)
if !okay {
Expand All @@ -228,7 +237,7 @@ func NewNetwork(networkName, driverName string, labels map[string]string) (*Netw
return network, nil
}

func (net *Network) DestroyNetwork() error {
func (net *Network) Destroy() error {
driver, okay := GetNetworkDriver(net.Driver)
if !okay {
return fmt.Errorf("Driver '%s' not found", net.Driver)
Expand Down Expand Up @@ -284,7 +293,6 @@ var drivers map[string]Driver

func init() {
drivers = make(map[string]Driver)
RegisterNetworkDriver("noop", noopDriver{})
RegisterNetworkDriver("simplebridge", simpleBridgeDriver{make(map[string]*simplebridge.Network)})
}

Expand All @@ -297,22 +305,6 @@ func RegisterNetworkDriver(name string, driver Driver) {
drivers[name] = driver
}

// Noop network driver just does what its told to; communication is done via labels
type noopDriver struct{}

func (n noopDriver) Create(network *Network) error { return nil }
func (n noopDriver) Destroy(network *Network) error { return nil }
func (n noopDriver) Plug(network *Network, endpoint *Endpoint) (*execdriver.NetworkInterface, error) {
return &execdriver.NetworkInterface{
Gateway: "",
IPAddress: "10.0.0.6",
IPPrefixLen: 16,
MacAddress: "02:42:0a:03:00:01",
Bridge: "docker0",
}, nil
}
func (n noopDriver) Unplug(network *Network, endpoint *Endpoint) error { return nil }

// SimpleBridge drivers
type simpleBridgeDriver struct {
networks map[string]*simplebridge.Network
Expand All @@ -334,8 +326,13 @@ func (s simpleBridgeDriver) Create(network *Network) error {
}

func (s simpleBridgeDriver) Destroy(network *Network) error {
// TODO, implement
return nil
net, found := s.networks[network.ID]
if !found {
return fmt.Errorf("Network '%s' not found", network.ID)
}

delete(s.networks, network.ID)
return net.Destroy()
}

func (s simpleBridgeDriver) Plug(network *Network, endpoint *Endpoint) (*execdriver.NetworkInterface, error) {
Expand Down
4 changes: 4 additions & 0 deletions daemon/networkdriver/simplebridge/driver.go
Expand Up @@ -387,6 +387,10 @@ func (n Network) Setup() error {
return nil
}

func (n Network) Destroy() error {
return netlink.NetworkLinkDel(n.bridgeIface)
}

func (n Network) setupIPTables() error {
// Enable NAT

Expand Down

0 comments on commit 02488a3

Please sign in to comment.