Sync 'owned' data with existing containers at startup
This commit is contained in:
1 parent
f29716387f
commit
95cbc8940e
3 files changed
+42
No files matched your search
@@ -94,6 +94,18 @@ func (c *Client) AddObserver(ob ContainerObserver) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Client) AllContainerIDs() ([]string, error) {
|
||||
all, err := c.ListContainers(docker.ListContainersOptions{All: true})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var ids []string
|
||||
for _, c := range all {
|
||||
ids = append(ids, c.ID)
|
||||
}
|
||||
return ids, nil
|
||||
}
|
||||
|
||||
// IsContainerNotRunning returns true if we have checked with Docker that the ID is not running
|
||||
func (c *Client) IsContainerNotRunning(idStr string) bool {
|
||||
container, err := c.InspectContainer(idStr)
|
||||
|
||||
@@ -267,6 +267,16 @@ func (alloc *Allocator) ContainerStarted(ident string) {
|
||||
}
|
||||
}
|
||||
|
||||
func (alloc *Allocator) AllContainerIDs(ids []string) {
|
||||
alloc.actionChan <- func() {
|
||||
idmap := make(map[string]struct{}, len(ids))
|
||||
for _, id := range ids {
|
||||
idmap[id] = struct{}{}
|
||||
}
|
||||
alloc.syncOwned(idmap)
|
||||
}
|
||||
}
|
||||
|
||||
// Delete (Sync) - release all IP addresses for container with given name
|
||||
func (alloc *Allocator) Delete(ident string) error {
|
||||
errChan := make(chan error)
|
||||
@@ -871,6 +881,23 @@ func (alloc *Allocator) findOwner(addr address.Address) string {
|
||||
return ""
|
||||
}
|
||||
|
||||
// For each ID in the 'owned' map, remove the entry if it isn't in the map
|
||||
func (alloc *Allocator) syncOwned(ids map[string]struct{}) {
|
||||
changed := false
|
||||
for ident, addrs := range alloc.owned {
|
||||
if _, found := ids[ident]; !found {
|
||||
for _, addr := range addrs {
|
||||
alloc.space.Free(addr)
|
||||
}
|
||||
delete(alloc.owned, ident)
|
||||
changed = true
|
||||
}
|
||||
}
|
||||
if changed {
|
||||
alloc.persistOwned()
|
||||
}
|
||||
}
|
||||
|
||||
// Logging
|
||||
|
||||
func (alloc *Allocator) fatalf(fmt string, args ...interface{}) {
|
||||
|
||||
@@ -228,6 +228,9 @@ func main() {
|
||||
if iprangeCIDR != "" {
|
||||
allocator, defaultSubnet = createAllocator(router.Router, iprangeCIDR, ipsubnetCIDR, determineQuorum(peerCount, peers), db, isKnownPeer)
|
||||
observeContainers(allocator)
|
||||
ids, err := dockerCli.AllContainerIDs()
|
||||
checkFatal(err)
|
||||
allocator.AllContainerIDs(ids)
|
||||
} else if peerCount > 0 {
|
||||
Log.Fatal("--init-peer-count flag specified without --ipalloc-range")
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user