Add SafeApply lifecycle, health tracking, convergence loop, and startup checks

SafeApply pattern (validate → backup → write → reload → verify → rollback):
- HAProxy: SafeApply wraps existing validate+write+reload with backup and
  post-reload health check; rolls back to .bak on failure
- dnsmasq: SafeApply validates via dnsmasq --test, backs up, atomic writes,
  restarts, verifies daemon is active; rolls back on failure
- nftables: SafeApply validates via nft -c, backs up, atomic writes, applies
  to kernel, verifies table loaded; rolls back on failure

Health tracking:
- Add SubsystemHealth and Health structs to reconciler
- Track per-subsystem status (ok/degraded/error) after each reconcile
- Detect recovery: previous error → current ok broadcasts recovery event
- GET /api/v1/health/reconcile endpoint exposes health state
- HAProxy tracks excluded domains as "degraded" state

SSE error events:
- Broadcast haproxy:error, dnsmasq:error on SafeApply failure
- Broadcast haproxy:recovered, dnsmasq:recovered on recovery from error

Convergence loop:
- 5-minute periodic reconcile drives system toward desired state
- Catches config drift, daemon crashes, transient failures
- Serialized by reconcile mutex — no race with event-driven reconciles

Startup:
- CheckPrerequisites verifies required (dnsmasq, haproxy) and optional
  (wg, authelia, cscli, cloudflared, nft) binaries before first reconcile
This commit is contained in:
2026-07-14 11:44:44 +00:00
parent 88acd437a1
commit a0ab8faa1c
7 changed files with 276 additions and 20 deletions

View File

@@ -13,6 +13,7 @@ import (
"time"
"github.com/wild-cloud/wild-central/internal/domains"
"github.com/wild-cloud/wild-central/internal/storage"
)
// L4Route represents an L4 TCP passthrough route.
@@ -550,6 +551,51 @@ func (m *Manager) WriteConfig(content string) error {
return nil
}
// SafeApply validates, backs up, writes, reloads, and verifies the HAProxy config.
// On reload or verification failure, rolls back to the previous config.
func (m *Manager) SafeApply(content string) error {
// Backup current config
if storage.FileExists(m.configPath) {
_ = storage.CopyFile(m.configPath, m.configPath+".bak")
}
// Validate + atomic write
if err := m.WriteConfig(content); err != nil {
return fmt.Errorf("validation failed: %w", err)
}
// Reload daemon
if err := m.ReloadService(); err != nil {
m.rollback()
return fmt.Errorf("reload failed (rolled back): %w", err)
}
// Verify daemon is running
if err := m.verify(); err != nil {
m.rollback()
return fmt.Errorf("health check failed (rolled back): %w", err)
}
return nil
}
func (m *Manager) rollback() {
bakPath := m.configPath + ".bak"
if storage.FileExists(bakPath) {
_ = os.Rename(bakPath, m.configPath)
_ = m.ReloadService()
slog.Warn("rolled back to previous config", "component", "haproxy", "path", m.configPath)
}
}
func (m *Manager) verify() error {
cmd := exec.Command("systemctl", "is-active", "--quiet", "haproxy.service")
if err := cmd.Run(); err != nil {
return fmt.Errorf("haproxy not active after reload")
}
return nil
}
// ReloadService gracefully reloads HAProxy (starts it if not running)
func (m *Manager) ReloadService() error {
cmd := exec.Command("systemctl", "reload-or-restart", "haproxy.service")