From fd28740108daeabd320eb4fdb430cfd4022a98e3 Mon Sep 17 00:00:00 2001 From: Daniel Middlecote Date: Sun, 5 Mar 2017 17:16:55 +0000 Subject: [PATCH 1/2] Only check running containers for existing addresses --- common/docker/client.go | 16 ++++++++++++++-- prog/weaver/main.go | 6 +++++- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/common/docker/client.go b/common/docker/client.go index 00100186..f85e53b1 100644 --- a/common/docker/client.go +++ b/common/docker/client.go @@ -165,13 +165,25 @@ func (pending pendingStarts) finish(id string) { } } +// AllContainerIDs returns all the IDs of Docker containers, +// whether they are running or not. func (c *Client) AllContainerIDs() ([]string, error) { - all, err := c.ListContainers(docker.ListContainersOptions{All: true}) + return c.containerIDs(true) +} + +// RunningContainerIDs returns all the IDs of the running +// Docker containers. +func (c *Client) RunningContainerIDs() ([]string, error) { + return c.containerIDs(false) +} + +func (c *Client) containerIDs(all bool) ([]string, error) { + containers, err := c.ListContainers(docker.ListContainersOptions{All: all}) if err != nil { return nil, err } var ids []string - for _, c := range all { + for _, c := range containers { ids = append(ids, c.ID) } return ids, nil diff --git a/prog/weaver/main.go b/prog/weaver/main.go index f2666188..231c157a 100644 --- a/prog/weaver/main.go +++ b/prog/weaver/main.go @@ -307,11 +307,15 @@ func main() { trackerName = "awsvpc" } var allContainerIDs []string + var runningContainerIDs []string if dockerCli != nil { allContainerIDs, err = dockerCli.AllContainerIDs() checkFatal(err) + + runningContainerIDs, err = dockerCli.RunningContainerIDs() + checkFatal(err) } - preClaims, err := findExistingAddresses(dockerCli, allContainerIDs, weavenet.WeaveBridgeName) + preClaims, err := findExistingAddresses(dockerCli, runningContainerIDs, weavenet.WeaveBridgeName) checkFatal(err) allocator, defaultSubnet = createAllocator(router, ipamConfig, preClaims, db, t, isKnownPeer) observeContainers(allocator) From 104d2240ec4f4c8e7ad54c6630dcc0bde3925f72 Mon Sep 17 00:00:00 2001 From: Daniel Middlecote Date: Wed, 8 Mar 2017 21:43:21 +0000 Subject: [PATCH 2/2] Move container lists to scope they are used in --- prog/weaver/main.go | 18 ++++++++---------- prog/weaver/reclaim.go | 7 ++++++- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/prog/weaver/main.go b/prog/weaver/main.go index 231c157a..371e8a35 100644 --- a/prog/weaver/main.go +++ b/prog/weaver/main.go @@ -306,20 +306,18 @@ func main() { } trackerName = "awsvpc" } - var allContainerIDs []string - var runningContainerIDs []string - if dockerCli != nil { - allContainerIDs, err = dockerCli.AllContainerIDs() - checkFatal(err) - runningContainerIDs, err = dockerCli.RunningContainerIDs() - checkFatal(err) - } - preClaims, err := findExistingAddresses(dockerCli, runningContainerIDs, weavenet.WeaveBridgeName) + preClaims, err := findExistingAddresses(dockerCli, weavenet.WeaveBridgeName) checkFatal(err) + allocator, defaultSubnet = createAllocator(router, ipamConfig, preClaims, db, t, isKnownPeer) observeContainers(allocator) - allocator.PruneOwned(allContainerIDs) + + if dockerCli != nil { + allContainerIDs, err := dockerCli.AllContainerIDs() + checkFatal(err) + allocator.PruneOwned(allContainerIDs) + } } var ( diff --git a/prog/weaver/reclaim.go b/prog/weaver/reclaim.go index 5ccae97b..a90edfe9 100644 --- a/prog/weaver/reclaim.go +++ b/prog/weaver/reclaim.go @@ -20,7 +20,7 @@ func a(cidr *net.IPNet) address.CIDR { // Get all the existing Weave IPs at startup, so we can stop IPAM // giving out any as duplicates -func findExistingAddresses(dockerCli *weavedocker.Client, containerIDs []string, bridgeName string) (addrs []ipam.PreClaim, err error) { +func findExistingAddresses(dockerCli *weavedocker.Client, bridgeName string) (addrs []ipam.PreClaim, err error) { Log.Infof("Checking for pre-existing addresses on %s bridge", bridgeName) // First get the address for the bridge bridgeNetDev, err := weavenet.GetBridgeNetDev(bridgeName) @@ -50,6 +50,11 @@ func findExistingAddresses(dockerCli *weavedocker.Client, containerIDs []string, // Now iterate over all containers to see if they have a network // namespace with an attached interface if dockerCli != nil { + containerIDs, err := dockerCli.RunningContainerIDs() + if err != nil { + return nil, err + } + for _, cid := range containerIDs { container, err := dockerCli.InspectContainer(cid) if err != nil {