Add NetworkDB API to fetch the per network peer (gossip cluster) list

Signed-off-by: Santhosh Manohar <santhosh@docker.com>
This commit is contained in:
Santhosh Manohar 2016-10-25 14:52:36 -07:00
parent 1a8fe1208a
commit c52c8ca6eb
2 changed files with 44 additions and 0 deletions

View file

@ -17,6 +17,7 @@ import (
"github.com/docker/libnetwork/ipamapi"
"github.com/docker/libnetwork/netlabel"
"github.com/docker/libnetwork/netutils"
"github.com/docker/libnetwork/networkdb"
"github.com/docker/libnetwork/options"
"github.com/docker/libnetwork/types"
)
@ -67,6 +68,11 @@ type NetworkInfo interface {
Labels() map[string]string
Dynamic() bool
Created() time.Time
// Peers returns a slice of PeerInfo structures which has the information about the peer
// nodes participating in the same overlay network. This is currently the per-network
// gossip cluster. For non-dynamic overlay networks and bridge networks it returns an
// empty slice
Peers() []networkdb.PeerInfo
}
// EndpointWalker is a client provided function which will be used to walk the Endpoints.
@ -1459,6 +1465,24 @@ func (n *network) Info() NetworkInfo {
return n
}
func (n *network) Peers() []networkdb.PeerInfo {
if !n.Dynamic() {
return []networkdb.PeerInfo{}
}
var nDB *networkdb.NetworkDB
n.ctrlr.Lock()
if n.ctrlr.agentInitDone == nil && n.ctrlr.agent != nil {
nDB = n.ctrlr.agent.networkDB
}
n.ctrlr.Unlock()
if nDB != nil {
return n.ctrlr.agent.networkDB.Peers(n.id)
}
return []networkdb.PeerInfo{}
}
func (n *network) DriverOptions() map[string]string {
n.Lock()
defer n.Unlock()

View file

@ -91,6 +91,12 @@ type NetworkDB struct {
keyring *memberlist.Keyring
}
// PeerInfo represents the peer (gossip cluster) nodes of a network
type PeerInfo struct {
Name string
IP string
}
type node struct {
memberlist.Node
ltime serf.LamportTime
@ -200,6 +206,20 @@ func (nDB *NetworkDB) Close() {
}
}
// Peers returns the gossip peers for a given network.
func (nDB *NetworkDB) Peers(nid string) []PeerInfo {
nDB.RLock()
defer nDB.RUnlock()
peers := make([]PeerInfo, 0, len(nDB.networkNodes[nid]))
for _, nodeName := range nDB.networkNodes[nid] {
peers = append(peers, PeerInfo{
Name: nDB.nodes[nodeName].Name,
IP: nDB.nodes[nodeName].Addr.String(),
})
}
return peers
}
// GetEntry retrieves the value of a table entry in a given (network,
// table, key) tuple
func (nDB *NetworkDB) GetEntry(tname, nid, key string) ([]byte, error) {