graph.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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/kilo-io/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((&net.IPNet{IP: t.wireGuardCIDR.IP.Mask(t.wireGuardCIDR.Mask), Mask: t.wireGuardCIDR.Mask}).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. var priv net.IP
  69. if s.privateIPs != nil {
  70. priv = s.privateIPs[j]
  71. }
  72. if err := g.Nodes.Lookup[graphEscape(s.hostnames[j])].Attrs.Add(string(gographviz.Label), nodeLabel(s.location, s.hostnames[j], s.cidrs[j], priv, wg, endpoint)); err != nil {
  73. return "", fmt.Errorf("failed to add label to node")
  74. }
  75. }
  76. meshSubGraph(g, g.Relations.SortedChildren(subGraphName(s.location)), s.leader, nil)
  77. leaders[i] = graphEscape(s.hostnames[s.leader])
  78. }
  79. meshGraph(g, leaders, nil)
  80. if err := g.AddSubGraph("kilo", graphEscape("cluster_peers"), nil); err != nil {
  81. return "", fmt.Errorf("failed to add peer subgraph")
  82. }
  83. if err := g.AddAttr(graphEscape("cluster_peers"), string(gographviz.Label), graphEscape("peers")); err != nil {
  84. return "", fmt.Errorf("failed to add label to peer subgraph")
  85. }
  86. if err := g.AddAttr(graphEscape("cluster_peers"), string(gographviz.Style), `"dashed,rounded"`); err != nil {
  87. return "", fmt.Errorf("failed to add style to peer subgraph")
  88. }
  89. for j := range t.peers {
  90. if err := g.AddNode(graphEscape("cluster_peers"), graphEscape(t.peers[j].Name), nodeAttrs); err != nil {
  91. return "", fmt.Errorf("failed to add peer node to peer subgraph")
  92. }
  93. if err := g.Nodes.Lookup[graphEscape(t.peers[j].Name)].Attrs.Add(string(gographviz.Label), peerLabel(t.peers[j])); err != nil {
  94. return "", fmt.Errorf("failed to add label to peer node")
  95. }
  96. }
  97. meshPeers(g, leaders, g.Relations.SortedChildren(graphEscape("cluster_peers")), nil)
  98. return g.String(), nil
  99. }
  100. func meshGraph(g *gographviz.Graph, nodes []string, attrs gographviz.Attrs) {
  101. if attrs == nil {
  102. attrs = make(gographviz.Attrs)
  103. attrs[gographviz.Dir] = "both"
  104. }
  105. for i := range nodes {
  106. for j := i + 1; j < len(nodes); j++ {
  107. if i == j {
  108. continue
  109. }
  110. g.Edges.Add(&gographviz.Edge{Src: nodes[i], Dst: nodes[j], Dir: true, Attrs: attrs})
  111. }
  112. }
  113. }
  114. func meshSubGraph(g *gographviz.Graph, nodes []string, leader int, attrs gographviz.Attrs) {
  115. if attrs == nil {
  116. attrs = make(gographviz.Attrs)
  117. attrs[gographviz.Dir] = "both"
  118. }
  119. for i := range nodes {
  120. if i == leader {
  121. continue
  122. }
  123. g.Edges.Add(&gographviz.Edge{Src: nodes[leader], Dst: nodes[i], Dir: true, Attrs: attrs})
  124. }
  125. }
  126. func meshPeers(g *gographviz.Graph, nodes, peers []string, attrs gographviz.Attrs) {
  127. if attrs == nil {
  128. attrs = make(gographviz.Attrs)
  129. attrs[gographviz.Dir] = "both"
  130. attrs[gographviz.Style] = "dashed"
  131. }
  132. for i := range nodes {
  133. for j := range peers {
  134. g.Edges.Add(&gographviz.Edge{Src: nodes[i], Dst: peers[j], Dir: true, Attrs: attrs})
  135. }
  136. }
  137. }
  138. func graphEscape(s string) string {
  139. return fmt.Sprintf("\"%s\"", s)
  140. }
  141. func subGraphName(name string) string {
  142. return graphEscape(fmt.Sprintf("cluster_location_%s", name))
  143. }
  144. func nodeLabel(location, name string, cidr *net.IPNet, priv, wgIP net.IP, endpoint *wireguard.Endpoint) string {
  145. label := []string{
  146. location,
  147. name,
  148. cidr.String(),
  149. }
  150. if priv != nil {
  151. label = append(label, priv.String())
  152. }
  153. if wgIP != nil {
  154. label = append(label, wgIP.String())
  155. }
  156. if endpoint != nil {
  157. label = append(label, endpoint.String())
  158. }
  159. return graphEscape(strings.Join(label, "\\n"))
  160. }
  161. func peerLabel(peer *Peer) string {
  162. return graphEscape(fmt.Sprintf("%s\\n%s\n", peer.Name, peer.Endpoint.String()))
  163. }