graph.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // Copyright 2019 the Kilo authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package mesh
  15. import (
  16. "fmt"
  17. "net"
  18. "github.com/awalterschulze/gographviz"
  19. )
  20. // Dot generates a Graphviz graph of the Topology in DOT fomat.
  21. func (t *Topology) Dot() (string, error) {
  22. g := gographviz.NewGraph()
  23. g.Name = "kilo"
  24. if err := g.AddAttr("kilo", string(gographviz.Label), graphEscape(t.subnet.String())); err != nil {
  25. return "", fmt.Errorf("failed to add label to graph")
  26. }
  27. if err := g.AddAttr("kilo", string(gographviz.LabelLOC), "t"); err != nil {
  28. return "", fmt.Errorf("failed to add label location to graph")
  29. }
  30. if err := g.AddAttr("kilo", string(gographviz.Overlap), "false"); err != nil {
  31. return "", fmt.Errorf("failed to disable graph overlap")
  32. }
  33. if err := g.SetDir(true); err != nil {
  34. return "", fmt.Errorf("failed to set direction")
  35. }
  36. leaders := make([]string, len(t.Segments))
  37. nodeAttrs := map[string]string{
  38. string(gographviz.Shape): "ellipse",
  39. }
  40. for i, s := range t.Segments {
  41. if err := g.AddSubGraph("kilo", subGraphName(s.Location), nil); err != nil {
  42. return "", fmt.Errorf("failed to add subgraph")
  43. }
  44. if err := g.AddAttr(subGraphName(s.Location), string(gographviz.Label), graphEscape(s.Location)); err != nil {
  45. return "", fmt.Errorf("failed to add label to subgraph")
  46. }
  47. if err := g.AddAttr(subGraphName(s.Location), string(gographviz.Style), `"dashed,rounded"`); err != nil {
  48. return "", fmt.Errorf("failed to add style to subgraph")
  49. }
  50. for j := range s.cidrs {
  51. if err := g.AddNode(subGraphName(s.Location), graphEscape(s.hostnames[j]), nodeAttrs); err != nil {
  52. return "", fmt.Errorf("failed to add node to subgraph")
  53. }
  54. var wg net.IP
  55. if j == s.leader {
  56. wg = s.wireGuardIP
  57. if err := g.Nodes.Lookup[graphEscape(s.hostnames[j])].Attrs.Add(string(gographviz.Rank), "1"); err != nil {
  58. return "", fmt.Errorf("failed to add rank to node")
  59. }
  60. }
  61. if err := g.Nodes.Lookup[graphEscape(s.hostnames[j])].Attrs.Add(string(gographviz.Label), nodeLabel(s.Location, s.hostnames[j], s.cidrs[j], s.privateIPs[j], wg)); err != nil {
  62. return "", fmt.Errorf("failed to add label to node")
  63. }
  64. }
  65. meshSubGraph(g, g.Relations.SortedChildren(subGraphName(s.Location)), s.leader)
  66. leaders[i] = graphEscape(s.hostnames[s.leader])
  67. }
  68. meshSubGraph(g, leaders, 0)
  69. return g.String(), nil
  70. }
  71. func meshSubGraph(g *gographviz.Graph, nodes []string, leader int) {
  72. for i := range nodes {
  73. if i == leader {
  74. continue
  75. }
  76. a := make(gographviz.Attrs)
  77. a[gographviz.Dir] = "both"
  78. g.Edges.Add(&gographviz.Edge{Src: nodes[leader], Dst: nodes[i], Dir: true, Attrs: a})
  79. }
  80. }
  81. func graphEscape(s string) string {
  82. return fmt.Sprintf("\"%s\"", s)
  83. }
  84. func subGraphName(name string) string {
  85. return graphEscape(fmt.Sprintf("cluster_%s", name))
  86. }
  87. func nodeLabel(location, name string, cidr *net.IPNet, priv, wgIP net.IP) string {
  88. var wg string
  89. if wgIP != nil {
  90. wg = wgIP.String()
  91. }
  92. return graphEscape(fmt.Sprintf("%s\n%s\n%s\n%s\n%s", location, name, cidr.String(), priv.String(), wg))
  93. }