The seat set was a Go slice compiled into the controller and referenced by name everywhere, so changing it meant a rebuild and a freeze-prone deploy. It is now a table: catalogue keeps the shipped set as defaultSeats (the seed and the fallback) and a loadable working set; inventory adds the seat table (migration 0034), Seats to read it, and SeedSeats to fill it idempotently without overwriting an operator's edit; migrate seeds it; openInventory loads it, and an empty or unreadable table leaves the compiled defaults in force so it can never brick the control plane's boot. Behaviour-neutral: the seeded table equals the defaults. Phase 2 (reference by a stable id so a rename touches no manifest or code, and the builder reads the set from the mesh) follows.
225 lines
9.6 KiB
Go
225 lines
9.6 KiB
Go
package catalogue
|
|
|
|
import (
|
|
"fmt"
|
|
"sort"
|
|
"strings"
|
|
)
|
|
|
|
// The seats a mesh can have (novox/hq ADR 0110).
|
|
//
|
|
// **A closed set, defined here rather than by whoever claims one.** Until this, a well-formed name
|
|
// became a seat by being claimed, so nothing could say which seats a mesh has or who fills them:
|
|
// `the-showcase` and `the-build-machine` were each invented by the module claiming it. The set is
|
|
// what a person reads to learn what a mesh can have, so an entry nobody argued for is an entry
|
|
// nobody can explain — the same reason every shape in the host's vocabulary names its decision.
|
|
//
|
|
// A seat is held by a module assignment. What the mesh knows about a holder is what it knows about
|
|
// that assignment; nothing about holders is kept here or anywhere else.
|
|
|
|
// Seat is one role the mesh defines.
|
|
type Seat struct {
|
|
// Name is what a manifest claims.
|
|
Name string
|
|
// Scope is where there may be only one holder.
|
|
Scope string
|
|
// Delivers is the provision the seat's holder answers for, or empty. A seat that delivers a
|
|
// provision may only be held by a module providing it at the seat's scope, and its holder is
|
|
// what a requirement for that provision resolves to when several modules provide it.
|
|
Delivers string
|
|
// Decision is the record that made it a seat.
|
|
Decision string
|
|
}
|
|
|
|
// defaultSeats is the set the mesh ships with — the seed for the control plane's seat table and the
|
|
// fallback when it has none (novox/hq ADR 0122). It is the one place the closed set 0110 defines is
|
|
// written; the store's table is seeded from it and thereafter is the live, editable copy.
|
|
//
|
|
// In the order a person reads it: the mesh's own, then a node's.
|
|
var defaultSeats = []Seat{
|
|
{Name: "mesh-controller", Scope: ScopeMesh, Decision: "novox/hq ADR 0079"},
|
|
{Name: "mesh-store", Scope: ScopeMesh, Delivers: "postgres-database", Decision: "novox/hq ADR 0079"},
|
|
{Name: "mesh-broker", Scope: ScopeMesh, Delivers: "amqp", Decision: "novox/hq ADR 0079"},
|
|
{Name: "the-artifact-store", Scope: ScopeMesh, Delivers: "artifact-store", Decision: "novox/hq ADR 0075"},
|
|
{Name: "mesh-catalog", Scope: ScopeMesh, Decision: "novox/hq ADR 0121"},
|
|
// Deferred renames (novox/hq ADR 0121): these deliver a provision, so renaming them is a
|
|
// delivering-seat migration with a mesh-wide cascade if a holder stops resolving mid-flight.
|
|
// They keep their names until that migration is done deliberately, apart from the node-* pass.
|
|
{Name: "npm-package-registry", Scope: ScopeMesh, Delivers: "npm-package-registry", Decision: "novox/hq ADR 0109"},
|
|
{Name: "git", Scope: ScopeMesh, Delivers: "git", Decision: "novox/hq ADR 0111"},
|
|
{Name: "mesh-build-machine", Scope: ScopeMesh, Decision: "novox/hq ADR 0121"},
|
|
{Name: "node-dns-resolver", Scope: ScopeNode, Decision: "novox/hq ADR 0121"},
|
|
{Name: "node-intrusion-prevention", Scope: ScopeNode, Decision: "novox/hq ADR 0121"},
|
|
{Name: "node-packet-filter", Scope: ScopeNode, Decision: "novox/hq ADR 0121"},
|
|
// Deferred (novox/hq ADR 0121): renaming to mesh-private-network is a scope + server/client
|
|
// model change, not a rename, so it stays until that is built.
|
|
{Name: "the-private-network", Scope: ScopeNode, Decision: "novox/hq ADR 0110"},
|
|
{Name: "node-resolver-config", Scope: ScopeNode, Decision: "novox/hq ADR 0121"},
|
|
// The program that manages the machine's own network. It delivers nothing: its holder only
|
|
// keeps the manager and the mesh from contradicting each other — the resolver file left to the
|
|
// mesh, the private network's interface left alone — and never declares a link, an address or
|
|
// a wireless network, because the link is the only channel a fix could arrive on. A seat
|
|
// rather than a condition in the resolver's module, so a machine running two managers is
|
|
// refused at assignment instead of found by the resolver being rewritten (novox/hq ADR 0117).
|
|
{Name: "node-uplink", Scope: ScopeNode, Decision: "novox/hq ADR 0117"},
|
|
}
|
|
|
|
// A system seat name is the control plane's namespace: `mesh-*` for a mesh-wide role, `node-*` for
|
|
// a per-node one (novox/hq ADR 0121). A claim to a system name the mesh does not define is refused;
|
|
// any other name is a module's own to define and claim. Some of the mesh's own seats predate this
|
|
// convention and are not yet renamed (git, npm-package-registry, the-artifact-store,
|
|
// the-private-network) — those are in the set, so they resolve by name, not by prefix.
|
|
func isSystemSeatName(name string) bool {
|
|
return strings.HasPrefix(name, "mesh-") || strings.HasPrefix(name, "node-")
|
|
}
|
|
|
|
// seats is the working set the lookups read. It starts as the compiled defaults and is replaced by
|
|
// what the control plane loaded from its store (novox/hq ADR 0122), so a change to the set is a
|
|
// change to data, not to this code.
|
|
var seats = defaultSeats
|
|
|
|
// DefaultSeats is the set the mesh ships with, for seeding the store's seat table.
|
|
func DefaultSeats() []Seat { return append([]Seat(nil), defaultSeats...) }
|
|
|
|
// UseSeats replaces the working set with the one the control plane read from its store.
|
|
//
|
|
// **Empty is ignored on purpose.** A store that has not been seeded yet — or one that could not be
|
|
// read — must leave the compiled defaults in force rather than emptying the set: an empty set would
|
|
// refuse every claim and could stop the control plane composing at all, which is a far worse failure
|
|
// than running on the set the binary shipped with. So the store can only ever *replace* the set with
|
|
// a non-empty one, never erase it.
|
|
func UseSeats(s []Seat) {
|
|
if len(s) > 0 {
|
|
seats = s
|
|
}
|
|
}
|
|
|
|
// Seats is every seat the mesh defines, in reading order.
|
|
func Seats() []Seat {
|
|
return append([]Seat(nil), seats...)
|
|
}
|
|
|
|
// SeatNamed is the seat a claim names, if the mesh defines one.
|
|
func SeatNamed(name string) (Seat, bool) {
|
|
for _, s := range seats {
|
|
if s.Name == name {
|
|
return s, true
|
|
}
|
|
}
|
|
return Seat{}, false
|
|
}
|
|
|
|
// SeatDelivering is the seat whose holder answers for a provision, if there is one.
|
|
func SeatDelivering(provision string) (Seat, bool) {
|
|
if provision == "" {
|
|
return Seat{}, false
|
|
}
|
|
for _, s := range seats {
|
|
if s.Delivers == provision {
|
|
return s, true
|
|
}
|
|
}
|
|
return Seat{}, false
|
|
}
|
|
|
|
// claimProblems is what is wrong with a manifest's claims and the seats it defines.
|
|
//
|
|
// A claim is one of three things (novox/hq ADR 0121): a **system seat** the control plane defines —
|
|
// checked for scope and, if it delivers a provision, that the claimant provides it; a **system name
|
|
// the mesh does not define** (`mesh-*`/`node-*`) — refused, because that namespace is the control
|
|
// plane's; or a **module-defined seat** — valid only when this manifest also declares it, since a
|
|
// module may coordinate its own instances through a seat of its own but may not invent one by
|
|
// claiming it. A module's own seat declaration may not sit in the system namespace or shadow a
|
|
// system seat.
|
|
func claimProblems(m Manifest) []string {
|
|
var problems []string
|
|
|
|
defined := map[string]Claim{}
|
|
for _, d := range m.DefinesSeats {
|
|
if _, isSystem := SeatNamed(d.Name); isSystem || isSystemSeatName(d.Name) {
|
|
problems = append(problems, fmt.Sprintf(
|
|
"%s defines a seat %q in the mesh's own namespace; a module's seat is named outside "+
|
|
"mesh-*/node-* (novox/hq ADR 0121)", m.Module, d.Name))
|
|
continue
|
|
}
|
|
defined[d.Name] = d
|
|
}
|
|
|
|
for _, c := range m.Claims {
|
|
if seat, known := SeatNamed(c.Name); known {
|
|
if c.At() != seat.Scope {
|
|
problems = append(problems, fmt.Sprintf(
|
|
"%s claims %s at scope %q, and %s is a %s seat",
|
|
m.Module, c.Name, c.At(), c.Name, seat.Scope))
|
|
}
|
|
if seat.Delivers != "" && !providesAt(m, seat.Delivers, seat.Scope) {
|
|
problems = append(problems, fmt.Sprintf(
|
|
"%s claims %s, whose holder answers for %q, and %s does not provide %q at %s scope",
|
|
m.Module, c.Name, seat.Delivers, m.Module, seat.Delivers, seat.Scope))
|
|
}
|
|
continue
|
|
}
|
|
if isSystemSeatName(c.Name) {
|
|
problems = append(problems, fmt.Sprintf(
|
|
"%s claims %q, which is a seat in the mesh's own namespace (mesh-*/node-*) that it "+
|
|
"does not define (novox/hq ADR 0121) — the seats are: %s", m.Module, c.Name, seatNames()))
|
|
continue
|
|
}
|
|
d, ours := defined[c.Name]
|
|
if !ours {
|
|
problems = append(problems, fmt.Sprintf(
|
|
"%s claims %q, which is not a seat this mesh defines and not one %s declares itself "+
|
|
"(novox/hq ADR 0121) — the seats are: %s", m.Module, c.Name, m.Module, seatNames()))
|
|
continue
|
|
}
|
|
if c.At() != d.At() {
|
|
problems = append(problems, fmt.Sprintf(
|
|
"%s claims its own seat %s at scope %q, having declared it at %q",
|
|
m.Module, c.Name, c.At(), d.At()))
|
|
}
|
|
}
|
|
return problems
|
|
}
|
|
|
|
func providesAt(m Manifest, provision, scope string) bool {
|
|
for _, o := range m.Provides {
|
|
if o.Name == provision && o.At() == scope {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
func seatNames() string {
|
|
names := make([]string, 0, len(seats))
|
|
for _, s := range seats {
|
|
names = append(names, s.Name)
|
|
}
|
|
sort.Strings(names)
|
|
return strings.Join(names, ", ")
|
|
}
|
|
|
|
// HolderAmong is which of several providers of a provision holds the seat that delivers it.
|
|
//
|
|
// Found by the (node, module) pair, because a provider is identified by both (novox/hq to-be 23):
|
|
// two modules on one node could both provide a provision, and only the one holding the seat
|
|
// answers for it. Nothing when no seat delivers the provision, when nobody holds
|
|
// it, or when the holder is not among the providers offered.
|
|
func HolderAmong(provision string, providers []Provider, held []Held) (Provider, bool) {
|
|
seat, delivered := SeatDelivering(provision)
|
|
if !delivered {
|
|
return Provider{}, false
|
|
}
|
|
for _, h := range held {
|
|
if h.Claim != seat.Name || h.Scope != seat.Scope {
|
|
continue
|
|
}
|
|
for _, p := range providers {
|
|
if p.Node == h.Node && p.Module == h.Module {
|
|
return p, true
|
|
}
|
|
}
|
|
}
|
|
return Provider{}, false
|
|
}
|