Debian Codegraph @

e2e/router/hostmap.go

raw
1 //go:build e2e_testing
2 // +build e2e_testing
3
4 package router
5
6 import (
7 "fmt"
8 "net/netip"
9 "sort"
10 "strings"
11
12 "github.com/slackhq/nebula"
13 )
14
15 type edge struct {
16 from string
17 to string
18 dual bool
19 }
20
21 func renderHostmaps(controls ...*nebula.Control) string {
22 var lines []*edge
23 r := "graph TB\n"
24 for _, c := range controls {
25 sr, se := renderHostmap(c)
26 r += sr
27 for _, e := range se {
28 add := true
29
30 // Collapse duplicate edges into a bi-directionally connected edge
31 for _, ge := range lines {
32 if e.to == ge.from && e.from == ge.to {
33 add = false
34 ge.dual = true
35 break
36 }
37 }
38
39 if add {
40 lines = append(lines, e)
41 }
42 }
43 }
44
45 for _, line := range lines {
46 if line.dual {
47 r += fmt.Sprintf("\t%v <--> %v\n", line.from, line.to)
48 } else {
49 r += fmt.Sprintf("\t%v --> %v\n", line.from, line.to)
50 }
51
52 }
53
54 return r
55 }
56
57 func renderHostmap(c *nebula.Control) (string, []*edge) {
58 var lines []string
59 var globalLines []*edge
60
61 crt := c.GetCertState().GetDefaultCertificate()
62 clusterName := strings.Trim(crt.Name(), " ")
63 clusterVpnIp := crt.Networks()[0].Addr()
64 r := fmt.Sprintf("\tsubgraph %s[\"%s (%s)\"]\n", clusterName, clusterName, clusterVpnIp)
65
66 hm := c.GetHostmap()
67 hm.RLock()
68 defer hm.RUnlock()
69
70 // Draw the vpn to index nodes
71 r += fmt.Sprintf("\t\tsubgraph %s.hosts[\"Hosts (vpn ip to index)\"]\n", clusterName)
72 hosts := sortedHosts(hm.Hosts)
73 for _, vpnIp := range hosts {
74 hi := hm.Hosts[vpnIp]
75 r += fmt.Sprintf("\t\t\t%v.%v[\"%v\"]\n", clusterName, vpnIp, vpnIp)
76 lines = append(lines, fmt.Sprintf("%v.%v --> %v.%v", clusterName, vpnIp, clusterName, hi.GetLocalIndex()))
77
78 rs := hi.GetRelayState()
79 for _, relayIp := range rs.CopyRelayIps() {
80 lines = append(lines, fmt.Sprintf("%v.%v --> %v.%v", clusterName, vpnIp, clusterName, relayIp))
81 }
82
83 for _, relayIp := range rs.CopyRelayForIdxs() {
84 lines = append(lines, fmt.Sprintf("%v.%v --> %v.%v", clusterName, vpnIp, clusterName, relayIp))
85 }
86 }
87 r += "\t\tend\n"
88
89 // Draw the relay hostinfos
90 if len(hm.Relays) > 0 {
91 r += fmt.Sprintf("\t\tsubgraph %s.relays[\"Relays (relay index to hostinfo)\"]\n", clusterName)
92 for relayIndex, hi := range hm.Relays {
93 r += fmt.Sprintf("\t\t\t%v.%v[\"%v\"]\n", clusterName, relayIndex, relayIndex)
94 lines = append(lines, fmt.Sprintf("%v.%v --> %v.%v", clusterName, relayIndex, clusterName, hi.GetLocalIndex()))
95 }
96 r += "\t\tend\n"
97 }
98
99 // Draw the local index to relay or remote index nodes
100 r += fmt.Sprintf("\t\tsubgraph indexes.%s[\"Indexes (index to hostinfo)\"]\n", clusterName)
101 indexes := sortedIndexes(hm.Indexes)
102 for _, idx := range indexes {
103 hi, ok := hm.Indexes[idx]
104 if ok {
105 r += fmt.Sprintf("\t\t\t%v.%v[\"%v (%v)\"]\n", clusterName, idx, idx, hi.GetVpnAddrs())
106 remoteClusterName := strings.Trim(hi.GetCert().Certificate.Name(), " ")
107 globalLines = append(globalLines, &edge{from: fmt.Sprintf("%v.%v", clusterName, idx), to: fmt.Sprintf("%v.%v", remoteClusterName, hi.GetRemoteIndex())})
108 _ = hi
109 }
110 }
111 r += "\t\tend\n"
112
113 // Add the edges inside this host
114 for _, line := range lines {
115 r += fmt.Sprintf("\t\t%v\n", line)
116 }
117
118 r += "\tend\n"
119 return r, globalLines
120 }
121
122 func sortedHosts(hosts map[netip.Addr]*nebula.HostInfo) []netip.Addr {
123 keys := make([]netip.Addr, 0, len(hosts))
124 for key := range hosts {
125 keys = append(keys, key)
126 }
127
128 sort.SliceStable(keys, func(i, j int) bool {
129 return keys[i].Compare(keys[j]) > 0
130 })
131
132 return keys
133 }
134
135 func sortedIndexes(indexes map[uint32]*nebula.HostInfo) []uint32 {
136 keys := make([]uint32, 0, len(indexes))
137 for key := range indexes {
138 keys = append(keys, key)
139 }
140
141 sort.SliceStable(keys, func(i, j int) bool {
142 return keys[i] > keys[j]
143 })
144
145 return keys
146 }