Maps: a site takes the name its hosts share #66

Merged
jcoffey-dev merged 1 commits from feature/site-names into main 2026-11-16 07:45:10 +00:00
3 changed files with 67 additions and 1 deletions

No files matched your search

+1 -1
View File
@@ -56,7 +56,7 @@ everything at once, and that stops being readable at about forty boxes.
| What | Where it comes from |
| --- | --- |
| Hosts | Every joined agent, as now. |
| Networks and sites | Each agent reports its interfaces: name, kind (physical, bridge, VLAN, WireGuard, …), addresses with prefixes, and the default gateway. Hosts that share a private subnet (RFC 1918, ULA or CGNAT) are on one network. A **site** is the hosts joined by private networks, so a provider's private network becomes one site with no setup. Container bridges and Docker's own networks don't count. |
| Networks and sites | Each agent reports its interfaces: name, kind (physical, bridge, VLAN, WireGuard, …), addresses with prefixes, and the default gateway. Hosts that share a private subnet (RFC 1918, ULA or CGNAT) are on one network. A **site** is the hosts joined by private networks, so a provider's private network becomes one site with no setup. It's named after its network, or after the prefix most of its hosts' names share (`phx1` for phx1-app05, phx1-db01, …: at least three hosts, four in five, and no other site with it), until it's named by hand. Container bridges and Docker's own networks don't count. |
| WireGuard links | The root helper reads `wg show all dump`: each interface's public key, and each peer's public key, endpoint, allowed IPs, last handshake and byte counters. obs-beacond matches a peer's key to another host's interface key, so a mesh link is a fact, not a guess, and is "up" while the handshake is under three minutes old. A peer that isn't one of our hosts is an outside peer (someone's laptop, another office), counted on the host and listed in its detail. |
| Other links between hosts | The socket sample (below): host A has connections to host B. Drawn as a plain line with its ports, separately from WireGuard. |
| The way in from the Internet | Listeners on public addresses that have connections from public addresses: "Internet → node2 :443, :25". With Cloudflare set up, the Cloudflare box and its pools come from Cloudflare's API as today. |
+28
View File
@@ -319,3 +319,31 @@ func TestContainerLoopbackStaysInside(t *testing.T) {
t.Errorf("edges %+v, want none", g.Edges)
}
}
// A site whose hosts are named for it takes that name; a prefix two sites
// share, a mixed site and a small one keep their network's (#59).
func TestSiteNamedByHosts(t *testing.T) {
sites := []Site{
{Name: "10.100.0.0/24", Hosts: []string{"phx1-app01", "phx1-app02", "phx1-db01", "phx1-lb01", "build-box"}},
{Name: "10.100.1.0/24", Hosts: []string{"iad1-app01", "iad1-db01", "iad1-gw01"}},
{Name: "10.100.2.0/24", Hosts: []string{"fleet-0001", "fleet-0002", "fleet-0003"}},
{Name: "10.100.3.0/24", Hosts: []string{"fleet-0101", "fleet-0102", "fleet-0103"}},
{Name: "10.100.4.0/24", Hosts: []string{"ams1-app01", "ams1-db01", "web-1", "web-2"}},
{Name: "10.100.5.0/24", Hosts: []string{"fra1-app01", "fra1-db01"}},
{Name: "10.100.6.0/24", Hosts: []string{"10-0-0-1", "10-0-0-2", "10-0-0-3"}},
}
nameByHosts(sites)
var got []string
for _, s := range sites {
got = append(got, s.Name)
}
want := []string{"phx1", "iad1", "10.100.2.0/24", "10.100.3.0/24", "10.100.4.0/24", "10.100.5.0/24", "10.100.6.0/24"}
if !slices.Equal(got, want) {
t.Errorf("names %v, want %v", got, want)
}
// A name set by hand still wins.
hosts, cor := world()
if g := Build(hosts, cor, t0); g.Sites[0].Name != "Lisbon" {
t.Errorf("by hand: %s", g.Sites[0].Name)
}
}
+38
View File
@@ -6,6 +6,7 @@ import (
"slices"
"strings"
"time"
"unicode"
"github.com/cairnobs/cairnobs/internal/netmap"
)
@@ -105,6 +106,7 @@ func (b *builder) sites() ([]Site, []HostNode) {
}
sites = append(sites, s)
}
nameByHosts(sites)
if b.cor.SiteName != "" {
best, most := -1, 0
for i, s := range sites {
@@ -477,3 +479,39 @@ func (u *union) size(x string) int {
}
return 1
}
// nameByHosts names a site after the prefix most of its hosts' names share
// before the first "-", as fleets named by site do (phx1-app05,
// phx1-db01): at least three hosts, four in five of them, and no other
// site with the same prefix. A name from topology.yaml or the map's
// corrections still wins; otherwise a site is named after its network.
func nameByHosts(sites []Site) {
prefix := func(hs []string) string {
if len(hs) < 3 {
return ""
}
count := map[string]int{}
for _, h := range hs {
if p, _, ok := strings.Cut(h, "-"); ok && p != "" {
count[p]++
}
}
for p, n := range count {
if n*5 >= len(hs)*4 && strings.ContainsFunc(p, unicode.IsLetter) {
return p
}
}
return ""
}
names := make([]string, len(sites))
seen := map[string]int{}
for i, s := range sites {
names[i] = prefix(s.Hosts)
seen[names[i]]++
}
for i, n := range names {
if n != "" && seen[n] == 1 {
sites[i].Name = n
}
}
}