diff --git a/libnetwork/network.go b/libnetwork/network.go index 8a068d22e9..9c23ad9011 100644 --- a/libnetwork/network.go +++ b/libnetwork/network.go @@ -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() diff --git a/libnetwork/networkdb/networkdb.go b/libnetwork/networkdb/networkdb.go index a79b4231d2..04ac498b5a 100644 --- a/libnetwork/networkdb/networkdb.go +++ b/libnetwork/networkdb/networkdb.go @@ -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) {