graph.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. return g.String(), nil
  73. }
  74. func meshGraph(g *gographviz.Graph, nodes []string, attrs gographviz.Attrs) {
  75. if attrs == nil {
  76. attrs = make(gographviz.Attrs)
  77. attrs[gographviz.Dir] = "both"
  78. }
  79. for i := range nodes {
  80. for j := i + 1; j < len(nodes); j++ {
  81. if i == j {
  82. continue
  83. }
  84. g.Edges.Add(&gographviz.Edge{Src: nodes[i], Dst: nodes[j], Dir: true, Attrs: attrs})
  85. }
  86. }
  87. }
  88. func meshSubGraph(g *gographviz.Graph, nodes []string, leader int, attrs gographviz.Attrs) {
  89. if attrs == nil {
  90. attrs = make(gographviz.Attrs)
  91. attrs[gographviz.Dir] = "both"
  92. }
  93. for i := range nodes {
  94. if i == leader {
  95. continue
  96. }
  97. g.Edges.Add(&gographviz.Edge{Src: nodes[leader], Dst: nodes[i], Dir: true, Attrs: attrs})
  98. }
  99. }
  100. func graphEscape(s string) string {
  101. return fmt.Sprintf("\"%s\"", s)
  102. }
  103. func subGraphName(name string) string {
  104. return graphEscape(fmt.Sprintf("cluster_%s", name))
  105. }
  106. func nodeLabel(location, name string, cidr *net.IPNet, priv, wgIP net.IP) string {
  107. var wg string
  108. if wgIP != nil {
  109. wg = wgIP.String()
  110. }
  111. return graphEscape(fmt.Sprintf("%s\n%s\n%s\n%s\n%s", location, name, cidr.String(), priv.String(), wg))
  112. }