pkg/mesh: add peers to graph

This commit is contained in:
Lucas Servén Marín 2019-09-23 17:54:16 +02:00
parent 887806c7ce
commit 676007938e
No known key found for this signature in database
GPG Key ID: 586FEAF680DA74AD
2 changed files with 49 additions and 2 deletions

View File

@ -34,6 +34,10 @@ func runGraph(_ *cobra.Command, _ []string) error {
if err != nil {
return fmt.Errorf("failed to list nodes: %v", err)
}
ps, err := opts.backend.Peers().List()
if err != nil {
return fmt.Errorf("failed to list peers: %v", err)
}
var hostname string
subnet := mesh.DefaultKiloSubnet
nodes := make(map[string]*mesh.Node)
@ -50,7 +54,13 @@ func runGraph(_ *cobra.Command, _ []string) error {
if len(nodes) == 0 {
return fmt.Errorf("did not find any valid Kilo nodes in the cluster")
}
t, err := mesh.NewTopology(nodes, nil, opts.granularity, hostname, 0, []byte{}, subnet)
peers := make(map[string]*mesh.Peer)
for _, p := range ps {
if p.Ready() {
peers[p.Name] = p
}
}
t, err := mesh.NewTopology(nodes, peers, opts.granularity, hostname, 0, []byte{}, subnet)
if err != nil {
return fmt.Errorf("failed to create topology: %v", err)
}

View File

@ -74,6 +74,26 @@ func (t *Topology) Dot() (string, error) {
leaders[i] = graphEscape(s.hostnames[s.leader])
}
meshGraph(g, leaders, nil)
if err := g.AddSubGraph("kilo", graphEscape("cluster_peers"), nil); err != nil {
return "", fmt.Errorf("failed to add peer subgraph")
}
if err := g.AddAttr(graphEscape("cluster_peers"), string(gographviz.Label), graphEscape("peers")); err != nil {
return "", fmt.Errorf("failed to add label to peer subgraph")
}
if err := g.AddAttr(graphEscape("cluster_peers"), string(gographviz.Style), `"dashed,rounded"`); err != nil {
return "", fmt.Errorf("failed to add style to peer subgraph")
}
for j := range t.peers {
if err := g.AddNode(graphEscape("cluster_peers"), graphEscape(t.peers[j].Name), nodeAttrs); err != nil {
return "", fmt.Errorf("failed to add peer node to peer subgraph")
}
if err := g.Nodes.Lookup[graphEscape(t.peers[j].Name)].Attrs.Add(string(gographviz.Label), peerLabel(t.peers[j])); err != nil {
return "", fmt.Errorf("failed to add label to peer node")
}
}
meshPeers(g, leaders, g.Relations.SortedChildren(graphEscape("cluster_peers")), nil)
return g.String(), nil
}
@ -105,12 +125,25 @@ func meshSubGraph(g *gographviz.Graph, nodes []string, leader int, attrs gograph
}
}
func meshPeers(g *gographviz.Graph, nodes, peers []string, attrs gographviz.Attrs) {
if attrs == nil {
attrs = make(gographviz.Attrs)
attrs[gographviz.Dir] = "both"
attrs[gographviz.Style] = "dashed"
}
for i := range nodes {
for j := range peers {
g.Edges.Add(&gographviz.Edge{Src: nodes[i], Dst: peers[j], Dir: true, Attrs: attrs})
}
}
}
func graphEscape(s string) string {
return fmt.Sprintf("\"%s\"", s)
}
func subGraphName(name string) string {
return graphEscape(fmt.Sprintf("cluster_%s", name))
return graphEscape(fmt.Sprintf("cluster_location_%s", name))
}
func nodeLabel(location, name string, cidr *net.IPNet, priv, wgIP net.IP) string {
@ -120,3 +153,7 @@ func nodeLabel(location, name string, cidr *net.IPNet, priv, wgIP net.IP) string
}
return graphEscape(fmt.Sprintf("%s\n%s\n%s\n%s\n%s", location, name, cidr.String(), priv.String(), wg))
}
func peerLabel(peer *Peer) string {
return graphEscape(fmt.Sprintf("%s\n%s\n", peer.Name, peer.Endpoint.IP.String()))
}