feat: Domain-keyed service registration model

Rewrite the service registration model per the architecture plan:

Model changes:
- Domain is the unique key (removed Name field)
- Removed ExtraDomains — each domain is its own registration
- Removed SourceNode, BackendStatic, ReachOff
- Added Subdomains bool for wildcard matching control
- Files stored as <domain_with_underscores>.yaml
- API routes: /services/{domain:.+} (regex for dots in path)
- Added DeregisterBySource for cleanup before re-registration

Reconciliation changes:
- No ExtraDomains iteration — each service IS one domain
- reach:internal → sets InternalDomain (generates local=/)
- reach:public → sets Domain (no local=/)
- tcp-passthrough → DNS points to backend IP
- http → DNS points to Central IP

All 22 tests pass (9 manager + 8 handler + 5 config/cert).

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-07-09 20:23:11 +00:00
parent d009e095c0
commit 8874bc6281
7 changed files with 384 additions and 329 deletions

View File

@@ -1,9 +1,9 @@
// Package services manages service registrations from Wild Cloud, Wild Works,
// and manual entries. Services register with Central to get DNS, gateway
// routing, TLS certificates, and optional public exposure (tunnels or direct).
// routing, TLS certificates, and optional public exposure via DDNS.
//
// Registrations are stored as YAML files on disk and will later be backed by
// NATS JetStream KV for real-time coordination.
// Each registration is keyed by its domain — one registration per domain.
// Central's own services (its UI, VPN, firewall) come from config, not here.
package services
import (
@@ -11,6 +11,7 @@ import (
"log/slog"
"os"
"path/filepath"
"strings"
"sync"
"gopkg.in/yaml.v3"
@@ -20,9 +21,8 @@ import (
type Reach string
const (
ReachOff Reach = "off" // localhost only — no DNS, no proxy
ReachInternal Reach = "internal" // LAN-visible — DNS + proxy + TLS
ReachPublic Reach = "public" // internet-visible — + tunnel or direct exposure
ReachPublic Reach = "public" // internet-visible — + DDNS + external HAProxy
)
// BackendType describes how the gateway handles traffic for this service.
@@ -31,41 +31,38 @@ type BackendType string
const (
BackendTCPPassthrough BackendType = "tcp-passthrough" // L4 SNI passthrough (k8s)
BackendHTTP BackendType = "http" // L7 HTTP reverse proxy
BackendStatic BackendType = "static" // L7 static file serving
)
// TLSMode describes how TLS is handled for the service.
type TLSMode string
const (
TLSTerminate TLSMode = "terminate" // Central terminates TLS (wildcard cert)
TLSTerminate TLSMode = "terminate" // Central terminates TLS
TLSPassthrough TLSMode = "passthrough" // Backend handles TLS (k8s traefik)
)
// Backend describes the target for a service.
type Backend struct {
Address string `yaml:"address" json:"address"` // host:port (e.g., "192.168.8.60:9001")
Type BackendType `yaml:"type" json:"type"` // how to route
Health string `yaml:"health,omitempty" json:"health,omitempty"` // health check path (e.g., "/health")
Address string `yaml:"address" json:"address"` // host:port
Type BackendType `yaml:"type" json:"type"` // tcp-passthrough or http
Health string `yaml:"health,omitempty" json:"health,omitempty"`
}
// Service represents a registered service.
// Service represents a registered service. The Domain is the unique key.
type Service struct {
Name string `yaml:"name" json:"name"`
Source string `yaml:"source" json:"source"` // "wild-cloud", "wild-works", "manual"
SourceNode string `yaml:"sourceNode,omitempty" json:"sourceNode,omitempty"` // IP of the node running this service
Domain string `yaml:"domain" json:"domain"` // FQDN (e.g., "my-api.payne.io")
ExtraDomains []string `yaml:"extraDomains,omitempty" json:"extraDomains,omitempty"` // additional domains
Backend Backend `yaml:"backend" json:"backend"`
Reach Reach `yaml:"reach" json:"reach"`
TLS TLSMode `yaml:"tls,omitempty" json:"tls,omitempty"`
Domain string `yaml:"domain" json:"domain"` // FQDN — unique key
Source string `yaml:"source,omitempty" json:"source,omitempty"` // who registered: wild-cloud, wild-works, manual
Backend Backend `yaml:"backend" json:"backend"` // where to route
Subdomains bool `yaml:"subdomains,omitempty" json:"subdomains,omitempty"` // also match *.domain
Reach Reach `yaml:"reach" json:"reach"` // internal or public
TLS TLSMode `yaml:"tls,omitempty" json:"tls,omitempty"` // passthrough or terminate
}
// Manager handles service registration CRUD and triggers networking reconciliation.
type Manager struct {
dataDir string
mu sync.RWMutex
reconcileFn func() // called after service changes to update DNS/proxy/TLS
reconcileFn func()
}
// NewManager creates a new service registration manager.
@@ -86,8 +83,13 @@ func (m *Manager) servicesDir() string {
return filepath.Join(m.dataDir, "services")
}
func (m *Manager) servicePath(name string) string {
return filepath.Join(m.servicesDir(), name+".yaml")
// domainToFilename converts a domain to a filesystem-safe filename.
func domainToFilename(domain string) string {
return strings.ReplaceAll(domain, ".", "_") + ".yaml"
}
func (m *Manager) servicePath(domain string) string {
return filepath.Join(m.servicesDir(), domainToFilename(domain))
}
// Register creates or updates a service registration.
@@ -95,24 +97,19 @@ func (m *Manager) Register(svc Service) error {
m.mu.Lock()
defer m.mu.Unlock()
if svc.Name == "" {
return fmt.Errorf("service name is required")
}
if svc.Domain == "" {
return fmt.Errorf("service domain is required")
return fmt.Errorf("domain is required")
}
if svc.Backend.Address == "" {
return fmt.Errorf("backend address is required")
}
// Default reach to internal
if svc.Reach == "" {
svc.Reach = ReachInternal
}
// Default backend type to http
if svc.Backend.Type == "" {
svc.Backend.Type = BackendHTTP
return fmt.Errorf("backend type is required")
}
if svc.Reach == "" {
return fmt.Errorf("reach is required")
}
// Default TLS mode based on backend type
if svc.TLS == "" {
if svc.Backend.Type == BackendTCPPassthrough {
@@ -122,16 +119,21 @@ func (m *Manager) Register(svc Service) error {
}
}
// Default source
if svc.Source == "" {
svc.Source = "manual"
}
data, err := yaml.Marshal(svc)
if err != nil {
return fmt.Errorf("marshaling service: %w", err)
}
if err := os.WriteFile(m.servicePath(svc.Name), data, 0644); err != nil {
if err := os.WriteFile(m.servicePath(svc.Domain), data, 0644); err != nil {
return fmt.Errorf("writing service file: %w", err)
}
slog.Info("service registered", "name", svc.Name, "domain", svc.Domain, "reach", svc.Reach, "source", svc.Source)
slog.Info("service registered", "domain", svc.Domain, "reach", svc.Reach, "source", svc.Source, "subdomains", svc.Subdomains)
if m.reconcileFn != nil {
go m.reconcileFn()
@@ -140,21 +142,21 @@ func (m *Manager) Register(svc Service) error {
return nil
}
// Deregister removes a service registration.
func (m *Manager) Deregister(name string) error {
// Deregister removes a service registration by domain.
func (m *Manager) Deregister(domain string) error {
m.mu.Lock()
defer m.mu.Unlock()
path := m.servicePath(name)
path := m.servicePath(domain)
if _, err := os.Stat(path); os.IsNotExist(err) {
return fmt.Errorf("service %q not found", name)
return fmt.Errorf("service %q not found", domain)
}
if err := os.Remove(path); err != nil {
return fmt.Errorf("removing service file: %w", err)
}
slog.Info("service deregistered", "name", name)
slog.Info("service deregistered", "domain", domain)
if m.reconcileFn != nil {
go m.reconcileFn()
@@ -163,15 +165,15 @@ func (m *Manager) Deregister(name string) error {
return nil
}
// Get retrieves a service registration by name.
func (m *Manager) Get(name string) (*Service, error) {
// Get retrieves a service registration by domain.
func (m *Manager) Get(domain string) (*Service, error) {
m.mu.RLock()
defer m.mu.RUnlock()
data, err := os.ReadFile(m.servicePath(name))
data, err := os.ReadFile(m.servicePath(domain))
if err != nil {
if os.IsNotExist(err) {
return nil, fmt.Errorf("service %q not found", name)
return nil, fmt.Errorf("service %q not found", domain)
}
return nil, fmt.Errorf("reading service file: %w", err)
}
@@ -221,19 +223,37 @@ func (m *Manager) List() ([]Service, error) {
return services, nil
}
// Update applies partial updates to a service registration.
func (m *Manager) Update(name string, updates map[string]any) error {
svc, err := m.Get(name)
// DeregisterBySource removes all registrations from a given source that match
// a backend address. Used by consumers to clean up before re-registering.
func (m *Manager) DeregisterBySource(source, backendAddress string) error {
svcs, err := m.List()
if err != nil {
return err
}
for _, svc := range svcs {
if svc.Source == source && svc.Backend.Address == backendAddress {
if err := m.Deregister(svc.Domain); err != nil {
slog.Warn("failed to deregister service during cleanup", "domain", svc.Domain, "error", err)
}
}
}
return nil
}
// Update applies partial updates to a service registration.
func (m *Manager) Update(domain string, updates map[string]any) error {
svc, err := m.Get(domain)
if err != nil {
return err
}
// Apply known fields
if reach, ok := updates["reach"].(string); ok {
svc.Reach = Reach(reach)
}
if domain, ok := updates["domain"].(string); ok {
svc.Domain = domain
if sub, ok := updates["subdomains"].(bool); ok {
svc.Subdomains = sub
}
if backend, ok := updates["backend"].(map[string]any); ok {
if addr, ok := backend["address"].(string); ok {