Add the service registration abstraction — the key contract between Central and its consumers (Wild Cloud, Wild Works). Services register with Central to get DNS, gateway routing, TLS, and public exposure. - internal/services/manager.go: CRUD for service registrations stored as YAML files, with reach model (off/internal/public), backend types (tcp-passthrough/http/static), and TLS modes - internal/api/v1/handlers_services.go: HTTP endpoints POST/GET/PATCH/DELETE /api/v1/services - Full test coverage for registration, update, deregister, validation Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
252 lines
7.0 KiB
Go
252 lines
7.0 KiB
Go
// 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).
|
|
//
|
|
// Registrations are stored as YAML files on disk and will later be backed by
|
|
// NATS JetStream KV for real-time coordination.
|
|
package services
|
|
|
|
import (
|
|
"fmt"
|
|
"log/slog"
|
|
"os"
|
|
"path/filepath"
|
|
"sync"
|
|
|
|
"gopkg.in/yaml.v3"
|
|
)
|
|
|
|
// Reach describes how a service is exposed.
|
|
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
|
|
)
|
|
|
|
// BackendType describes how the gateway handles traffic for this service.
|
|
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)
|
|
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")
|
|
}
|
|
|
|
// Service represents a registered service.
|
|
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"`
|
|
}
|
|
|
|
// 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
|
|
}
|
|
|
|
// NewManager creates a new service registration manager.
|
|
func NewManager(dataDir string) *Manager {
|
|
servicesDir := filepath.Join(dataDir, "services")
|
|
if err := os.MkdirAll(servicesDir, 0755); err != nil {
|
|
slog.Warn("failed to create services directory", "path", servicesDir, "error", err)
|
|
}
|
|
return &Manager{dataDir: dataDir}
|
|
}
|
|
|
|
// SetReconcileFn sets the function called after service changes to update networking.
|
|
func (m *Manager) SetReconcileFn(fn func()) {
|
|
m.reconcileFn = fn
|
|
}
|
|
|
|
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")
|
|
}
|
|
|
|
// Register creates or updates a service registration.
|
|
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")
|
|
}
|
|
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
|
|
}
|
|
// Default TLS mode based on backend type
|
|
if svc.TLS == "" {
|
|
if svc.Backend.Type == BackendTCPPassthrough {
|
|
svc.TLS = TLSPassthrough
|
|
} else {
|
|
svc.TLS = TLSTerminate
|
|
}
|
|
}
|
|
|
|
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 {
|
|
return fmt.Errorf("writing service file: %w", err)
|
|
}
|
|
|
|
slog.Info("service registered", "name", svc.Name, "domain", svc.Domain, "reach", svc.Reach, "source", svc.Source)
|
|
|
|
if m.reconcileFn != nil {
|
|
go m.reconcileFn()
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// Deregister removes a service registration.
|
|
func (m *Manager) Deregister(name string) error {
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
|
|
path := m.servicePath(name)
|
|
if _, err := os.Stat(path); os.IsNotExist(err) {
|
|
return fmt.Errorf("service %q not found", name)
|
|
}
|
|
|
|
if err := os.Remove(path); err != nil {
|
|
return fmt.Errorf("removing service file: %w", err)
|
|
}
|
|
|
|
slog.Info("service deregistered", "name", name)
|
|
|
|
if m.reconcileFn != nil {
|
|
go m.reconcileFn()
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// Get retrieves a service registration by name.
|
|
func (m *Manager) Get(name string) (*Service, error) {
|
|
m.mu.RLock()
|
|
defer m.mu.RUnlock()
|
|
|
|
data, err := os.ReadFile(m.servicePath(name))
|
|
if err != nil {
|
|
if os.IsNotExist(err) {
|
|
return nil, fmt.Errorf("service %q not found", name)
|
|
}
|
|
return nil, fmt.Errorf("reading service file: %w", err)
|
|
}
|
|
|
|
var svc Service
|
|
if err := yaml.Unmarshal(data, &svc); err != nil {
|
|
return nil, fmt.Errorf("parsing service file: %w", err)
|
|
}
|
|
|
|
return &svc, nil
|
|
}
|
|
|
|
// List returns all registered services.
|
|
func (m *Manager) List() ([]Service, error) {
|
|
m.mu.RLock()
|
|
defer m.mu.RUnlock()
|
|
|
|
entries, err := os.ReadDir(m.servicesDir())
|
|
if err != nil {
|
|
if os.IsNotExist(err) {
|
|
return nil, nil
|
|
}
|
|
return nil, fmt.Errorf("reading services directory: %w", err)
|
|
}
|
|
|
|
var services []Service
|
|
for _, entry := range entries {
|
|
if entry.IsDir() || filepath.Ext(entry.Name()) != ".yaml" {
|
|
continue
|
|
}
|
|
|
|
data, err := os.ReadFile(filepath.Join(m.servicesDir(), entry.Name()))
|
|
if err != nil {
|
|
slog.Warn("failed to read service file", "file", entry.Name(), "error", err)
|
|
continue
|
|
}
|
|
|
|
var svc Service
|
|
if err := yaml.Unmarshal(data, &svc); err != nil {
|
|
slog.Warn("failed to parse service file", "file", entry.Name(), "error", err)
|
|
continue
|
|
}
|
|
|
|
services = append(services, svc)
|
|
}
|
|
|
|
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)
|
|
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 backend, ok := updates["backend"].(map[string]any); ok {
|
|
if addr, ok := backend["address"].(string); ok {
|
|
svc.Backend.Address = addr
|
|
}
|
|
if typ, ok := backend["type"].(string); ok {
|
|
svc.Backend.Type = BackendType(typ)
|
|
}
|
|
if health, ok := backend["health"].(string); ok {
|
|
svc.Backend.Health = health
|
|
}
|
|
}
|
|
|
|
return m.Register(*svc)
|
|
}
|