66-improve-graph-dot-tool

- Showing services a node provides
- Showing all meshes not just one
- Showing the default route
This commit is contained in:
Tim Beatham 2023-12-25 01:25:20 +00:00
parent 41899c5831
commit 255d3c8b39
3 changed files with 41 additions and 21 deletions

View File

@ -6,6 +6,8 @@ import (
"os"
"github.com/akamensky/argparse"
"github.com/tim-beatham/wgmesh/pkg/ctrlserver"
graph "github.com/tim-beatham/wgmesh/pkg/dot"
"github.com/tim-beatham/wgmesh/pkg/ipc"
logging "github.com/tim-beatham/wgmesh/pkg/log"
)
@ -91,17 +93,40 @@ func leaveMesh(client *ipcRpc.Client, meshId string) {
fmt.Println(reply)
}
func getGraph(client *ipcRpc.Client, meshId string) {
var reply string
func getGraph(client *ipcRpc.Client) {
listMeshesReply := new(ipc.ListMeshReply)
err := client.Call("IpcHandler.GetDOT", &meshId, &reply)
err := client.Call("IpcHandler.ListMeshes", "", &listMeshesReply)
if err != nil {
fmt.Println(err.Error())
return
}
fmt.Println(reply)
meshes := make(map[string][]ctrlserver.MeshNode)
for _, meshId := range listMeshesReply.Meshes {
var meshReply ipc.GetMeshReply
err := client.Call("IpcHandler.GetMesh", &meshId, &meshReply)
if err != nil {
fmt.Println(err.Error())
return
}
meshes[meshId] = meshReply.Nodes
}
dotGenerator := graph.NewMeshGraphConverter(meshes)
dot, err := dotGenerator.Generate()
if err != nil {
fmt.Println(err.Error())
return
}
fmt.Println(dot)
}
func queryMesh(client *ipcRpc.Client, meshId, query string) {
@ -258,11 +283,6 @@ func main() {
Help: "Advertise ::/0 into the mesh network",
})
var getGraphMeshId *string = getGraphCmd.String("m", "mesh", &argparse.Options{
Required: true,
Help: "MeshID of the graph to get",
})
var leaveMeshMeshId *string = leaveMeshCmd.String("m", "mesh", &argparse.Options{
Required: true,
Help: "MeshID of the mesh to leave",
@ -351,7 +371,7 @@ func main() {
}
if getGraphCmd.Happened() {
getGraph(client, *getGraphMeshId)
getGraph(client)
}
if leaveMeshCmd.Happened() {

View File

@ -18,9 +18,10 @@ const (
)
const (
CIRCLE Shape = "circle"
STAR Shape = "star"
HEXAGON Shape = "hexagon"
CIRCLE Shape = "circle"
STAR Shape = "star"
HEXAGON Shape = "hexagon"
PARALLELOGRAM Shape = "parallelogram"
)
type Graph interface {

View File

@ -34,7 +34,7 @@ func (c *MeshDOTConverter) Generate() (string, error) {
}
for destination := range c.destinations {
g.PutNode(destination, destination, 3, CIRCLE)
g.PutNode(destination, destination, 1, HEXAGON)
}
return g.GetDOT()
@ -67,7 +67,7 @@ func (c *MeshDOTConverter) graphNode(g *RootGraph, node ctrlserver.MeshNode, mes
g.PutNode(node.PublicKey, alias, 2, CIRCLE)
for _, route := range node.Routes {
if len(route.Path) == 0 && route.Destination == "::/0" {
if len(route.Path) == 0 {
g.AddEdge(route.Destination, "", node.PublicKey, route.Destination)
continue
}
@ -88,7 +88,7 @@ func (c *MeshDOTConverter) graphNode(g *RootGraph, node ctrlserver.MeshNode, mes
g.AddEdge(routeID, "", reversedPath[index], reversedPath[index+1])
}
if reversedPath[len(reversedPath)-1] == "::/0" {
if route.Destination == "::/0" {
c.destinations[route.Destination] = struct{}{}
lastMesh := reversedPath[len(reversedPath)-1]
routeID := fmt.Sprintf("%s to %s", lastMesh, route.Destination)
@ -97,15 +97,14 @@ func (c *MeshDOTConverter) graphNode(g *RootGraph, node ctrlserver.MeshNode, mes
}
for service := range node.Services {
c.putService(g, service, node)
c.putService(g, service, meshId, node)
}
}
// putService: construct a service node and a link between the nodes
func (c *MeshDOTConverter) putService(g *RootGraph, key string, node ctrlserver.MeshNode) {
serviceID := fmt.Sprintf("%s%s", key, node.PublicKey)
g.PutNode(serviceID, key, 1, CIRCLE)
func (c *MeshDOTConverter) putService(g *RootGraph, key, meshId string, node ctrlserver.MeshNode) {
serviceID := fmt.Sprintf("%s%s%s", key, node.PublicKey, meshId)
g.PutNode(serviceID, key, 1, PARALLELOGRAM)
g.AddEdge(fmt.Sprintf("%s to %s", node.PublicKey, serviceID), "", node.PublicKey, serviceID)
}