160 lines
6.0 KiB
Go
160 lines
6.0 KiB
Go
package catalogue
|
|
|
|
import (
|
|
"fmt"
|
|
"sort"
|
|
"strings"
|
|
)
|
|
|
|
// What only the mesh knows, written where a module asks for it.
|
|
//
|
|
// **The graph is the control plane's; using it is the module's.** The mesh knows which machines
|
|
// exist, what they are called, and where they are. Turning that into a name that resolves is
|
|
// somebody's software, and which software is a choice the mesh should not be making.
|
|
//
|
|
// This replaced three modules — names, a resolver's data, and the private network's own
|
|
// configuration — that existed only because computed output needed somewhere to live. They ran no
|
|
// software and could not be swapped for anything, which is the test of whether something is a
|
|
// module at all (novox/hq ADR 0040).
|
|
|
|
const (
|
|
// FactNodeNames is every machine's name and address, as a hosts file.
|
|
//
|
|
// Exact names only: `homer` and `homer.internal` resolve to homer. Anything *under* a machine
|
|
// is a wildcard, which a hosts file cannot express — that is FactNodeZones.
|
|
FactNodeNames = "node-names"
|
|
|
|
// FactNodeZones is every machine as a wildcard: `*.homer.internal` is homer.
|
|
//
|
|
// Written in the form a resolver reads. A machine's own name and everything under it are one
|
|
// fact — if homer is at an address, so is anything homer serves.
|
|
FactNodeZones = "node-zones"
|
|
)
|
|
|
|
// facts is every fact the mesh computes, and what writes it.
|
|
//
|
|
// **A closed list.** A module asking for a fact the mesh does not have is asking for a file nobody
|
|
// will write, and finding that out on a machine — as a daemon that starts, reads nothing, and
|
|
// answers no queries — is worse than being told where the manifest is.
|
|
var facts = map[string]func(Resolution, map[string]string) string{
|
|
FactNodeNames: nodeNames,
|
|
FactNodeZones: nodeZones,
|
|
}
|
|
|
|
// FactsInto renders the facts a module asked for, as files it will be given.
|
|
//
|
|
// The module owns everything after the file exists: loading it, restarting on it, what a resolver
|
|
// does with it. This only puts it there.
|
|
func FactsInto(m Manifest, r Resolution, addresses map[string]string) ([]map[string]any, error) {
|
|
if len(m.Facts) == 0 {
|
|
return nil, nil
|
|
}
|
|
names := make([]string, 0, len(m.Facts))
|
|
for name := range m.Facts {
|
|
names = append(names, name)
|
|
}
|
|
sort.Strings(names)
|
|
|
|
out := make([]map[string]any, 0, len(names))
|
|
for _, name := range names {
|
|
write, known := facts[name]
|
|
if !known {
|
|
return nil, fmt.Errorf(
|
|
"%s asks the mesh for %q, which it does not compute. It has %s",
|
|
m.Module, name, spokenFacts())
|
|
}
|
|
path := m.Facts[name]
|
|
if !strings.HasPrefix(path, "/") {
|
|
return nil, fmt.Errorf(
|
|
"%s asks for %q at %q, which is not an absolute path", m.Module, name, path)
|
|
}
|
|
out = append(out, map[string]any{
|
|
"id": "fact-" + name, "type": "file", "path": path, "mode": "0644",
|
|
"content": write(r, addresses),
|
|
})
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
// spokenFacts lists them, so a refusal says what would have worked.
|
|
func spokenFacts() string {
|
|
names := make([]string, 0, len(facts))
|
|
for name := range facts {
|
|
names = append(names, name)
|
|
}
|
|
sort.Strings(names)
|
|
return strings.Join(names, ", ")
|
|
}
|
|
|
|
// nodeNames is every machine's name and address, as a hosts file.
|
|
//
|
|
// **A machine with no address is left out.** The mesh has a record for it — somebody added it —
|
|
// and does not yet know where it is, which is the ordinary state between adding a machine and it
|
|
// joining. Writing the name anyway would give a name that resolves to nothing, and a connection to
|
|
// that hangs; leaving it out fails at once and says the name is unknown.
|
|
func nodeNames(r Resolution, addresses map[string]string) string {
|
|
var b strings.Builder
|
|
b.WriteString("# Generated by the mesh. Do not edit — this file is replaced whenever a machine\n")
|
|
b.WriteString("# joins or leaves, and an edit would survive until then and vanish.\n\n")
|
|
// The floor every Linux expects, and which removing would break things that have nothing to do
|
|
// with the mesh.
|
|
b.WriteString("127.0.0.1\tlocalhost\n")
|
|
b.WriteString("::1\t\tlocalhost ip6-localhost ip6-loopback\n")
|
|
if r.Node != "" {
|
|
fmt.Fprintf(&b, "127.0.1.1\t%s\n", r.Node)
|
|
}
|
|
b.WriteString("\n")
|
|
for _, name := range sortedNames(addresses) {
|
|
at := addresses[name]
|
|
internal, bare := meshName(name)
|
|
// Its mesh name resolves to its address on the private network rather than to loopback,
|
|
// so a service binding the name it was given stays reachable from everywhere else.
|
|
fmt.Fprintf(&b, "%s\t%s\t%s", at, internal, bare)
|
|
if bare == r.Node {
|
|
b.WriteString("\t# this machine")
|
|
}
|
|
b.WriteString("\n")
|
|
}
|
|
return b.String()
|
|
}
|
|
|
|
// nodeZones is every machine as a wildcard, in the form a resolver reads.
|
|
//
|
|
// `*.homer.internal` is homer, which is the whole rule: if homer is at an address, so is anything
|
|
// homer serves. A module wanting this runs the resolver; the mesh only says what is true.
|
|
func nodeZones(_ Resolution, addresses map[string]string) string {
|
|
var b strings.Builder
|
|
b.WriteString("# Generated by the mesh. Do not edit — this file is replaced whenever a machine\n")
|
|
b.WriteString("# joins or leaves, and an edit would survive until then and vanish.\n\n")
|
|
for _, name := range sortedNames(addresses) {
|
|
internal, _ := meshName(name)
|
|
fmt.Fprintf(&b, "address=/%s/%s\n", internal, addresses[name])
|
|
}
|
|
return b.String()
|
|
}
|
|
|
|
// meshName is a machine's internal name and its bare one, from either. The control plane keys
|
|
// the names it hands a resolution by the internal name (`homer.internal`), the same map a
|
|
// container gets as its hosts; a caller that keys by the bare name gets the same answer. Written
|
|
// once, because the alternative was `homer.internal.internal` on every machine.
|
|
func meshName(name string) (internal, bare string) {
|
|
const suffix = ".internal"
|
|
if strings.HasSuffix(name, suffix) {
|
|
return name, strings.TrimSuffix(name, suffix)
|
|
}
|
|
return name + suffix, name
|
|
}
|
|
|
|
func sortedNames(addresses map[string]string) []string {
|
|
out := make([]string, 0, len(addresses))
|
|
for name, at := range addresses {
|
|
// See nodeNames: a machine the mesh cannot place is left out rather than named at nothing.
|
|
if at == "" {
|
|
continue
|
|
}
|
|
out = append(out, name)
|
|
}
|
|
sort.Strings(out)
|
|
return out
|
|
}
|