graph.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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.OutputOrder), "nodesfirst"); err != nil {
  31. return "", fmt.Errorf("failed to set output ordering")
  32. }
  33. if err := g.AddAttr("kilo", string(gographviz.Overlap), "false"); err != nil {
  34. return "", fmt.Errorf("failed to disable graph overlap")
  35. }
  36. if err := g.SetDir(true); err != nil {
  37. return "", fmt.Errorf("failed to set direction")
  38. }
  39. leaders := make([]string, len(t.segments))
  40. nodeAttrs := map[string]string{
  41. string(gographviz.Shape): "ellipse",
  42. }
  43. for i, s := range t.segments {
  44. if err := g.AddSubGraph("kilo", subGraphName(s.location), nil); err != nil {
  45. return "", fmt.Errorf("failed to add subgraph")
  46. }
  47. if err := g.AddAttr(subGraphName(s.location), string(gographviz.Label), graphEscape(s.location)); err != nil {
  48. return "", fmt.Errorf("failed to add label to subgraph")
  49. }
  50. if err := g.AddAttr(subGraphName(s.location), string(gographviz.Style), `"dashed,rounded"`); err != nil {
  51. return "", fmt.Errorf("failed to add style to subgraph")
  52. }
  53. for j := range s.cidrs {
  54. if err := g.AddNode(subGraphName(s.location), graphEscape(s.hostnames[j]), nodeAttrs); err != nil {
  55. return "", fmt.Errorf("failed to add node to subgraph")
  56. }
  57. var wg net.IP
  58. if j == s.leader {
  59. wg = s.wireGuardIP
  60. if err := g.Nodes.Lookup[graphEscape(s.hostnames[j])].Attrs.Add(string(gographviz.Rank), "1"); err != nil {
  61. return "", fmt.Errorf("failed to add rank to node")
  62. }
  63. }
  64. 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 {
  65. return "", fmt.Errorf("failed to add label to node")
  66. }
  67. }
  68. meshSubGraph(g, g.Relations.SortedChildren(subGraphName(s.location)), s.leader, nil)
  69. leaders[i] = graphEscape(s.hostnames[s.leader])
  70. }
  71. meshGraph(g, leaders, nil)
  72. if err := g.AddSubGraph("kilo", graphEscape("cluster_peers"), nil); err != nil {
  73. return "", fmt.Errorf("failed to add peer subgraph")
  74. }
  75. if err := g.AddAttr(graphEscape("cluster_peers"), string(gographviz.Label), graphEscape("peers")); err != nil {
  76. return "", fmt.Errorf("failed to add label to peer subgraph")
  77. }
  78. if err := g.AddAttr(graphEscape("cluster_peers"), string(gographviz.Style), `"dashed,rounded"`); err != nil {
  79. return "", fmt.Errorf("failed to add style to peer subgraph")
  80. }
  81. for j := range t.peers {
  82. if err := g.AddNode(graphEscape("cluster_peers"), graphEscape(t.peers[j].Name), nodeAttrs); err != nil {
  83. return "", fmt.Errorf("failed to add peer node to peer subgraph")
  84. }
  85. if err := g.Nodes.Lookup[graphEscape(t.peers[j].Name)].Attrs.Add(string(gographviz.Label), peerLabel(t.peers[j])); err != nil {
  86. return "", fmt.Errorf("failed to add label to peer node")
  87. }
  88. }
  89. meshPeers(g, leaders, g.Relations.SortedChildren(graphEscape("cluster_peers")), nil)
  90. return g.String(), nil
  91. }
  92. func meshGraph(g *gographviz.Graph, nodes []string, attrs gographviz.Attrs) {
  93. if attrs == nil {
  94. attrs = make(gographviz.Attrs)
  95. attrs[gographviz.Dir] = "both"
  96. }
  97. for i := range nodes {
  98. for j := i + 1; j < len(nodes); j++ {
  99. if i == j {
  100. continue
  101. }
  102. g.Edges.Add(&gographviz.Edge{Src: nodes[i], Dst: nodes[j], Dir: true, Attrs: attrs})
  103. }
  104. }
  105. }
  106. func meshSubGraph(g *gographviz.Graph, nodes []string, leader int, attrs gographviz.Attrs) {
  107. if attrs == nil {
  108. attrs = make(gographviz.Attrs)
  109. attrs[gographviz.Dir] = "both"
  110. }
  111. for i := range nodes {
  112. if i == leader {
  113. continue
  114. }
  115. g.Edges.Add(&gographviz.Edge{Src: nodes[leader], Dst: nodes[i], Dir: true, Attrs: attrs})
  116. }
  117. }
  118. func meshPeers(g *gographviz.Graph, nodes, peers []string, attrs gographviz.Attrs) {
  119. if attrs == nil {
  120. attrs = make(gographviz.Attrs)
  121. attrs[gographviz.Dir] = "both"
  122. attrs[gographviz.Style] = "dashed"
  123. }
  124. for i := range nodes {
  125. for j := range peers {
  126. g.Edges.Add(&gographviz.Edge{Src: nodes[i], Dst: peers[j], Dir: true, Attrs: attrs})
  127. }
  128. }
  129. }
  130. func graphEscape(s string) string {
  131. return fmt.Sprintf("\"%s\"", s)
  132. }
  133. func subGraphName(name string) string {
  134. return graphEscape(fmt.Sprintf("cluster_location_%s", name))
  135. }
  136. func nodeLabel(location, name string, cidr *net.IPNet, priv, wgIP net.IP) string {
  137. var wg string
  138. if wgIP != nil {
  139. wg = wgIP.String()
  140. }
  141. return graphEscape(fmt.Sprintf("%s\n%s\n%s\n%s\n%s", location, name, cidr.String(), priv.String(), wg))
  142. }
  143. func peerLabel(peer *Peer) string {
  144. return graphEscape(fmt.Sprintf("%s\n%s\n", peer.Name, peer.Endpoint.IP.String()))
  145. }