graph.go 5.7 KB

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