Przeglądaj źródła

pkg/mesh: add peers to graph

Lucas Servén Marín 6 lat temu
rodzic
commit
676007938e
2 zmienionych plików z 49 dodań i 2 usunięć
  1. 11 1
      cmd/kgctl/graph.go
  2. 38 1
      pkg/mesh/graph.go

+ 11 - 1
cmd/kgctl/graph.go

@@ -34,6 +34,10 @@ func runGraph(_ *cobra.Command, _ []string) error {
 	if err != nil {
 	if err != nil {
 		return fmt.Errorf("failed to list nodes: %v", err)
 		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
 	var hostname string
 	subnet := mesh.DefaultKiloSubnet
 	subnet := mesh.DefaultKiloSubnet
 	nodes := make(map[string]*mesh.Node)
 	nodes := make(map[string]*mesh.Node)
@@ -50,7 +54,13 @@ func runGraph(_ *cobra.Command, _ []string) error {
 	if len(nodes) == 0 {
 	if len(nodes) == 0 {
 		return fmt.Errorf("did not find any valid Kilo nodes in the cluster")
 		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 {
 	if err != nil {
 		return fmt.Errorf("failed to create topology: %v", err)
 		return fmt.Errorf("failed to create topology: %v", err)
 	}
 	}

+ 38 - 1
pkg/mesh/graph.go

@@ -74,6 +74,26 @@ func (t *Topology) Dot() (string, error) {
 		leaders[i] = graphEscape(s.hostnames[s.leader])
 		leaders[i] = graphEscape(s.hostnames[s.leader])
 	}
 	}
 	meshGraph(g, leaders, nil)
 	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
 	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 {
 func graphEscape(s string) string {
 	return fmt.Sprintf("\"%s\"", s)
 	return fmt.Sprintf("\"%s\"", s)
 }
 }
 
 
 func subGraphName(name string) string {
 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 {
 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))
 	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()))
+}