Browse code

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

Signed-off-by: Santhosh Manohar <santhosh@docker.com>

Santhosh Manohar authored on 2016/10/26 06:52:36
Showing 2 changed files
... ...
@@ -17,6 +17,7 @@ import (
17 17
 	"github.com/docker/libnetwork/ipamapi"
18 18
 	"github.com/docker/libnetwork/netlabel"
19 19
 	"github.com/docker/libnetwork/netutils"
20
+	"github.com/docker/libnetwork/networkdb"
20 21
 	"github.com/docker/libnetwork/options"
21 22
 	"github.com/docker/libnetwork/types"
22 23
 )
... ...
@@ -67,6 +68,11 @@ type NetworkInfo interface {
67 67
 	Labels() map[string]string
68 68
 	Dynamic() bool
69 69
 	Created() time.Time
70
+	// Peers returns a slice of PeerInfo structures which has the information about the peer
71
+	// nodes participating in the same overlay network. This is currently the per-network
72
+	// gossip cluster. For non-dynamic overlay networks and bridge networks it returns an
73
+	// empty slice
74
+	Peers() []networkdb.PeerInfo
70 75
 }
71 76
 
72 77
 // EndpointWalker is a client provided function which will be used to walk the Endpoints.
... ...
@@ -1459,6 +1465,24 @@ func (n *network) Info() NetworkInfo {
1459 1459
 	return n
1460 1460
 }
1461 1461
 
1462
+func (n *network) Peers() []networkdb.PeerInfo {
1463
+	if !n.Dynamic() {
1464
+		return []networkdb.PeerInfo{}
1465
+	}
1466
+
1467
+	var nDB *networkdb.NetworkDB
1468
+	n.ctrlr.Lock()
1469
+	if n.ctrlr.agentInitDone == nil && n.ctrlr.agent != nil {
1470
+		nDB = n.ctrlr.agent.networkDB
1471
+	}
1472
+	n.ctrlr.Unlock()
1473
+
1474
+	if nDB != nil {
1475
+		return n.ctrlr.agent.networkDB.Peers(n.id)
1476
+	}
1477
+	return []networkdb.PeerInfo{}
1478
+}
1479
+
1462 1480
 func (n *network) DriverOptions() map[string]string {
1463 1481
 	n.Lock()
1464 1482
 	defer n.Unlock()
... ...
@@ -91,6 +91,12 @@ type NetworkDB struct {
91 91
 	keyring *memberlist.Keyring
92 92
 }
93 93
 
94
+// PeerInfo represents the peer (gossip cluster) nodes of a network
95
+type PeerInfo struct {
96
+	Name string
97
+	IP   string
98
+}
99
+
94 100
 type node struct {
95 101
 	memberlist.Node
96 102
 	ltime serf.LamportTime
... ...
@@ -200,6 +206,20 @@ func (nDB *NetworkDB) Close() {
200 200
 	}
201 201
 }
202 202
 
203
+// Peers returns the gossip peers for a given network.
204
+func (nDB *NetworkDB) Peers(nid string) []PeerInfo {
205
+	nDB.RLock()
206
+	defer nDB.RUnlock()
207
+	peers := make([]PeerInfo, 0, len(nDB.networkNodes[nid]))
208
+	for _, nodeName := range nDB.networkNodes[nid] {
209
+		peers = append(peers, PeerInfo{
210
+			Name: nDB.nodes[nodeName].Name,
211
+			IP:   nDB.nodes[nodeName].Addr.String(),
212
+		})
213
+	}
214
+	return peers
215
+}
216
+
203 217
 // GetEntry retrieves the value of a table entry in a given (network,
204 218
 // table, key) tuple
205 219
 func (nDB *NetworkDB) GetEntry(tname, nid, key string) ([]byte, error) {