Support routes.

This commit is contained in:
2026-07-10 05:21:43 +00:00
parent 5c26c7530a
commit 43253ca120
8 changed files with 597 additions and 85 deletions

View File

@@ -40,14 +40,66 @@ type Backend struct {
Health string `yaml:"health,omitempty" json:"health,omitempty"`
}
// HeaderConfig describes custom request/response headers for L7 HTTP services.
type HeaderConfig struct {
Request map[string]string `yaml:"request,omitempty" json:"request,omitempty"`
Response map[string]string `yaml:"response,omitempty" json:"response,omitempty"`
}
// Route describes one path→backend mapping within a service.
// Routes are evaluated in order — first match wins.
// A route with empty Paths is a catch-all (should be last).
type Route struct {
Paths []string `yaml:"paths,omitempty" json:"paths,omitempty"` // path prefixes (empty = catch-all)
Backend Backend `yaml:"backend" json:"backend"` // where to route
Headers *HeaderConfig `yaml:"headers,omitempty" json:"headers,omitempty"` // per-route headers
IPAllow []string `yaml:"ipAllow,omitempty" json:"ipAllow,omitempty"` // per-route CIDR whitelist
}
// Service represents a registered service. The Domain is the unique key.
//
// Simple services use Backend directly. Multi-backend services use Routes
// for path-based splitting (Backend is ignored when Routes is present).
type Service struct {
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
Backend Backend `yaml:"backend,omitempty" json:"backend,omitempty"` // single backend (simple case)
Subdomains bool `yaml:"subdomains,omitempty" json:"subdomains,omitempty"` // also match *.domain
Public bool `yaml:"public,omitempty" json:"public,omitempty"` // true = internet-visible (DDNS + external), false = LAN only
TLS TLSMode `yaml:"tls,omitempty" json:"tls,omitempty"` // passthrough or terminate
// Multi-backend path routing. When present, Backend is ignored.
// Each route maps path prefixes to a specific backend with its own L7 options.
Routes []Route `yaml:"routes,omitempty" json:"routes,omitempty"`
}
// EffectiveRoutes returns the service's routing as a normalized []Route.
// Simple services (Backend, no Routes) are wrapped in a single-element slice.
func (s *Service) EffectiveRoutes() []Route {
if len(s.Routes) > 0 {
return s.Routes
}
if s.Backend.Address == "" {
return nil
}
return []Route{{Backend: s.Backend}}
}
// EffectiveBackendAddress returns the primary backend address for DNS/DDNS purposes.
// For multi-route services, returns the first route's backend.
func (s *Service) EffectiveBackendAddress() string {
if len(s.Routes) > 0 {
return s.Routes[0].Backend.Address
}
return s.Backend.Address
}
// EffectiveBackendType returns the backend type for routing decisions.
func (s *Service) EffectiveBackendType() BackendType {
if len(s.Routes) > 0 {
return s.Routes[0].Backend.Type
}
return s.Backend.Type
}
// Manager handles service registration CRUD and triggers networking reconciliation.
@@ -92,17 +144,36 @@ func (m *Manager) Register(svc Service) error {
if svc.Domain == "" {
return fmt.Errorf("domain is required")
}
if svc.Backend.Address == "" {
return fmt.Errorf("backend address is required")
hasBackend := svc.Backend.Address != ""
hasRoutes := len(svc.Routes) > 0
if hasBackend && hasRoutes {
return fmt.Errorf("service must use backend or routes, not both")
}
if svc.Backend.Type == "" {
return fmt.Errorf("backend type is required")
if !hasBackend && !hasRoutes {
return fmt.Errorf("backend address or routes required")
}
if hasRoutes {
for i, r := range svc.Routes {
if r.Backend.Address == "" {
return fmt.Errorf("route %d: backend address is required", i)
}
if r.Backend.Type == "" {
return fmt.Errorf("route %d: backend type is required", i)
}
}
} else {
if svc.Backend.Type == "" {
return fmt.Errorf("backend type is required")
}
}
// Public field is a bool — no validation needed (defaults to false)
// Default TLS mode based on backend type
if svc.TLS == "" {
if svc.Backend.Type == BackendTCPPassthrough {
bt := svc.EffectiveBackendType()
if bt == BackendTCPPassthrough {
svc.TLS = TLSPassthrough
} else {
svc.TLS = TLSTerminate
@@ -126,7 +197,14 @@ func (m *Manager) Register(svc Service) error {
slog.Info("service registered", "domain", svc.Domain, "public", svc.Public, "source", svc.Source, "subdomains", svc.Subdomains)
if m.reconcileFn != nil {
go m.reconcileFn()
go func() {
defer func() {
if r := recover(); r != nil {
slog.Error("reconcile panicked", "error", r)
}
}()
m.reconcileFn()
}()
}
return nil
@@ -222,7 +300,7 @@ func (m *Manager) DeregisterBySource(source, backendAddress string) error {
}
for _, svc := range svcs {
if svc.Source == source && svc.Backend.Address == backendAddress {
if svc.Source == source && svc.EffectiveBackendAddress() == backendAddress {
if err := m.Deregister(svc.Domain); err != nil {
slog.Warn("failed to deregister service during cleanup", "domain", svc.Domain, "error", err)
}
@@ -256,6 +334,8 @@ func (m *Manager) Update(domain string, updates map[string]any) error {
svc.Backend.Health = health
}
}
// Routes is a full replacement — partial route updates are too complex
// to express via map[string]any. Re-register the full service instead.
return m.Register(*svc)
}

View File

@@ -463,6 +463,94 @@ func TestList_EmptyDir(t *testing.T) {
}
}
func TestRegisterWithL7Fields(t *testing.T) {
mgr := NewManager(t.TempDir())
svc := Service{
Domain: "keila.cloud.payne.io",
Source: "wild-cloud",
Routes: []Route{{
Paths: []string{"/.well-known/matrix", "/_matrix"},
Backend: Backend{
Address: "192.168.8.80:80",
Type: BackendHTTP,
},
Headers: &HeaderConfig{
Response: map[string]string{
"Access-Control-Allow-Origin": "https://payne.io",
},
Request: map[string]string{
"X-Forwarded-Proto": "https",
},
},
IPAllow: []string{"10.0.0.0/8", "192.168.0.0/16"},
}},
}
if err := mgr.Register(svc); err != nil {
t.Fatalf("Register failed: %v", err)
}
got, err := mgr.Get("keila.cloud.payne.io")
if err != nil {
t.Fatalf("Get failed: %v", err)
}
if len(got.Routes) != 1 {
t.Fatalf("expected 1 route, got %d", len(got.Routes))
}
r := got.Routes[0]
if len(r.Paths) != 2 || r.Paths[0] != "/.well-known/matrix" {
t.Errorf("expected 2 paths, got %v", r.Paths)
}
if r.Headers == nil {
t.Fatal("expected headers, got nil")
}
if r.Headers.Response["Access-Control-Allow-Origin"] != "https://payne.io" {
t.Errorf("expected response header, got %v", r.Headers.Response)
}
if r.Headers.Request["X-Forwarded-Proto"] != "https" {
t.Errorf("expected request header, got %v", r.Headers.Request)
}
if len(r.IPAllow) != 2 || r.IPAllow[0] != "10.0.0.0/8" {
t.Errorf("expected 2 ipAllow CIDRs, got %v", r.IPAllow)
}
}
func TestUpdateL7Fields(t *testing.T) {
mgr := NewManager(t.TempDir())
mgr.Register(Service{
Domain: "updatable.example.com",
Source: "test",
Routes: []Route{{
Paths: []string{"/api", "/webhook"},
Backend: Backend{Address: "127.0.0.1:8080", Type: BackendHTTP},
IPAllow: []string{"10.0.0.0/8"},
Headers: &HeaderConfig{
Response: map[string]string{
"X-Custom": "value",
},
},
}},
})
got, _ := mgr.Get("updatable.example.com")
if len(got.Routes) != 1 {
t.Fatalf("expected 1 route, got %d", len(got.Routes))
}
r := got.Routes[0]
if len(r.Paths) != 2 || r.Paths[0] != "/api" {
t.Errorf("expected paths [/api /webhook], got %v", r.Paths)
}
if len(r.IPAllow) != 1 || r.IPAllow[0] != "10.0.0.0/8" {
t.Errorf("expected ipAllow [10.0.0.0/8], got %v", r.IPAllow)
}
if r.Headers == nil || r.Headers.Response["X-Custom"] != "value" {
t.Errorf("expected response header X-Custom=value, got %v", r.Headers)
}
}
func TestList_IgnoresNonYAML(t *testing.T) {
dir := t.TempDir()
mgr := NewManager(dir)