Przeglądaj źródła

Merge pull request #1521 from sanimej/gstatus

Add NetworkDB API to fetch the per network peer (gossip cluster) list
Jana Radhakrishnan 8 lat temu
rodzic
commit
e052f27538
2 zmienionych plików z 44 dodań i 0 usunięć
  1. 24 0
      libnetwork/network.go
  2. 20 0
      libnetwork/networkdb/networkdb.go

+ 24 - 0
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.
@@ -1460,6 +1466,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()

+ 20 - 0
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) {