2015-05-15 18:23:59 -04:00
|
|
|
package hostdiscovery
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
log "github.com/Sirupsen/logrus"
|
|
|
|
|
|
|
|
mapset "github.com/deckarep/golang-set"
|
2015-09-18 15:54:08 -04:00
|
|
|
"github.com/docker/docker/pkg/discovery"
|
|
|
|
// Including KV
|
|
|
|
_ "github.com/docker/docker/pkg/discovery/kv"
|
|
|
|
"github.com/docker/libkv/store/consul"
|
|
|
|
"github.com/docker/libkv/store/etcd"
|
|
|
|
"github.com/docker/libkv/store/zookeeper"
|
|
|
|
"github.com/docker/libnetwork/types"
|
2015-05-15 18:23:59 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type hostDiscovery struct {
|
2015-09-18 15:54:08 -04:00
|
|
|
watcher discovery.Watcher
|
|
|
|
nodes mapset.Set
|
|
|
|
stopChan chan struct{}
|
2015-05-15 18:23:59 -04:00
|
|
|
sync.Mutex
|
|
|
|
}
|
|
|
|
|
2015-09-18 15:54:08 -04:00
|
|
|
func init() {
|
|
|
|
consul.Register()
|
|
|
|
etcd.Register()
|
|
|
|
zookeeper.Register()
|
2015-05-15 18:23:59 -04:00
|
|
|
}
|
|
|
|
|
2015-09-18 15:54:08 -04:00
|
|
|
// NewHostDiscovery function creates a host discovery object
|
|
|
|
func NewHostDiscovery(watcher discovery.Watcher) HostDiscovery {
|
|
|
|
return &hostDiscovery{watcher: watcher, nodes: mapset.NewSet(), stopChan: make(chan struct{})}
|
|
|
|
}
|
2015-05-15 18:23:59 -04:00
|
|
|
|
2015-10-22 00:31:43 -04:00
|
|
|
func (h *hostDiscovery) Watch(activeCallback ActiveCallback, joinCallback JoinCallback, leaveCallback LeaveCallback) error {
|
2015-05-15 18:23:59 -04:00
|
|
|
h.Lock()
|
2015-09-18 15:54:08 -04:00
|
|
|
d := h.watcher
|
2015-05-15 18:23:59 -04:00
|
|
|
h.Unlock()
|
2015-09-18 15:54:08 -04:00
|
|
|
if d == nil {
|
|
|
|
return types.BadRequestErrorf("invalid discovery watcher")
|
|
|
|
}
|
2015-05-29 23:42:23 -04:00
|
|
|
discoveryCh, errCh := d.Watch(h.stopChan)
|
2015-10-22 00:31:43 -04:00
|
|
|
go h.monitorDiscovery(discoveryCh, errCh, activeCallback, joinCallback, leaveCallback)
|
2015-05-15 18:23:59 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-10-22 00:31:43 -04:00
|
|
|
func (h *hostDiscovery) monitorDiscovery(ch <-chan discovery.Entries, errCh <-chan error,
|
|
|
|
activeCallback ActiveCallback, joinCallback JoinCallback, leaveCallback LeaveCallback) {
|
2015-05-29 23:42:23 -04:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case entries := <-ch:
|
2015-10-22 00:31:43 -04:00
|
|
|
h.processCallback(entries, activeCallback, joinCallback, leaveCallback)
|
2015-05-29 23:42:23 -04:00
|
|
|
case err := <-errCh:
|
2015-09-18 15:54:08 -04:00
|
|
|
if err != nil {
|
|
|
|
log.Errorf("discovery error: %v", err)
|
|
|
|
}
|
2015-05-29 23:42:23 -04:00
|
|
|
case <-h.stopChan:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-15 18:23:59 -04:00
|
|
|
func (h *hostDiscovery) StopDiscovery() error {
|
|
|
|
h.Lock()
|
|
|
|
stopChan := h.stopChan
|
2015-09-18 15:54:08 -04:00
|
|
|
h.watcher = nil
|
2015-05-15 18:23:59 -04:00
|
|
|
h.Unlock()
|
|
|
|
|
|
|
|
close(stopChan)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-10-22 00:31:43 -04:00
|
|
|
func (h *hostDiscovery) processCallback(entries discovery.Entries,
|
|
|
|
activeCallback ActiveCallback, joinCallback JoinCallback, leaveCallback LeaveCallback) {
|
2015-05-15 18:23:59 -04:00
|
|
|
updated := hosts(entries)
|
|
|
|
h.Lock()
|
|
|
|
existing := h.nodes
|
|
|
|
added, removed := diff(existing, updated)
|
|
|
|
h.nodes = updated
|
|
|
|
h.Unlock()
|
|
|
|
|
2015-10-22 00:31:43 -04:00
|
|
|
activeCallback()
|
2015-05-15 18:23:59 -04:00
|
|
|
if len(added) > 0 {
|
|
|
|
joinCallback(added)
|
|
|
|
}
|
|
|
|
if len(removed) > 0 {
|
|
|
|
leaveCallback(removed)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func diff(existing mapset.Set, updated mapset.Set) (added []net.IP, removed []net.IP) {
|
|
|
|
addSlice := updated.Difference(existing).ToSlice()
|
|
|
|
removeSlice := existing.Difference(updated).ToSlice()
|
|
|
|
for _, ip := range addSlice {
|
|
|
|
added = append(added, net.ParseIP(ip.(string)))
|
|
|
|
}
|
|
|
|
for _, ip := range removeSlice {
|
|
|
|
removed = append(removed, net.ParseIP(ip.(string)))
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-09-18 15:54:08 -04:00
|
|
|
func (h *hostDiscovery) Fetch() []net.IP {
|
2015-05-15 18:23:59 -04:00
|
|
|
h.Lock()
|
2015-05-29 23:42:23 -04:00
|
|
|
defer h.Unlock()
|
2015-05-15 18:23:59 -04:00
|
|
|
ips := []net.IP{}
|
2015-05-29 23:42:23 -04:00
|
|
|
for _, ipstr := range h.nodes.ToSlice() {
|
|
|
|
ips = append(ips, net.ParseIP(ipstr.(string)))
|
2015-05-15 18:23:59 -04:00
|
|
|
}
|
2015-09-18 15:54:08 -04:00
|
|
|
return ips
|
2015-05-15 18:23:59 -04:00
|
|
|
}
|
|
|
|
|
2015-05-29 23:42:23 -04:00
|
|
|
func hosts(entries discovery.Entries) mapset.Set {
|
2015-05-15 18:23:59 -04:00
|
|
|
hosts := mapset.NewSet()
|
|
|
|
for _, entry := range entries {
|
|
|
|
hosts.Add(entry.Host)
|
|
|
|
}
|
|
|
|
return hosts
|
|
|
|
}
|