feat(api): Auto-Reload HAProxy bei Domain/Backend/Routing-Mutation
Symmetrisch zur Firewall: Domains-, Backends- und RoutingRules-Handler bekommen einen Reloader-Hook injiziert, der nach jeder Mutation haproxy.cfg neu rendert + sudo systemctl reload haproxy fährt. Errors werden nur geloggt, nicht failed (Row ist committed; manuelle Re-Render via edgeguard-ctl render-config bleibt möglich). Vorher: nur Firewall-Regeln waren auto-applied — Domain/Backend- Änderungen sind in der DB gelandet, aber das laufende haproxy hat sie nicht gesehen bis zum nächsten render-config oder API-Restart. Version 1.0.8. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"log/slog"
|
||||
"strconv"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
@@ -13,13 +15,23 @@ import (
|
||||
)
|
||||
|
||||
type BackendsHandler struct {
|
||||
Repo *backends.Repo
|
||||
Audit *audit.Repo
|
||||
NodeID string
|
||||
Repo *backends.Repo
|
||||
Audit *audit.Repo
|
||||
NodeID string
|
||||
Reloader func(ctx context.Context) error
|
||||
}
|
||||
|
||||
func NewBackendsHandler(repo *backends.Repo, a *audit.Repo, nodeID string) *BackendsHandler {
|
||||
return &BackendsHandler{Repo: repo, Audit: a, NodeID: nodeID}
|
||||
func NewBackendsHandler(repo *backends.Repo, a *audit.Repo, nodeID string, reloader func(context.Context) error) *BackendsHandler {
|
||||
return &BackendsHandler{Repo: repo, Audit: a, NodeID: nodeID, Reloader: reloader}
|
||||
}
|
||||
|
||||
func (h *BackendsHandler) reload(ctx context.Context, op string) {
|
||||
if h.Reloader == nil {
|
||||
return
|
||||
}
|
||||
if err := h.Reloader(ctx); err != nil {
|
||||
slog.Warn("haproxy: reload after backend mutation failed", "op", op, "error", err)
|
||||
}
|
||||
}
|
||||
|
||||
func (h *BackendsHandler) Register(rg *gin.RouterGroup) {
|
||||
@@ -69,7 +81,7 @@ func (h *BackendsHandler) Create(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
_ = h.Audit.Log(c.Request.Context(), actorOf(c), "backend.create", req.Name, out, h.NodeID)
|
||||
response.Created(c, out)
|
||||
response.Created(c, out); h.reload(c.Request.Context(), "create")
|
||||
}
|
||||
|
||||
func (h *BackendsHandler) Update(c *gin.Context) {
|
||||
@@ -92,7 +104,7 @@ func (h *BackendsHandler) Update(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
_ = h.Audit.Log(c.Request.Context(), actorOf(c), "backend.update", out.Name, out, h.NodeID)
|
||||
response.OK(c, out)
|
||||
response.OK(c, out); h.reload(c.Request.Context(), "update")
|
||||
}
|
||||
|
||||
func (h *BackendsHandler) Delete(c *gin.Context) {
|
||||
@@ -110,5 +122,5 @@ func (h *BackendsHandler) Delete(c *gin.Context) {
|
||||
}
|
||||
_ = h.Audit.Log(c.Request.Context(), actorOf(c), "backend.delete",
|
||||
strconv.FormatInt(id, 10), gin.H{"id": id}, h.NodeID)
|
||||
response.NoContent(c)
|
||||
response.NoContent(c); h.reload(c.Request.Context(), "delete")
|
||||
}
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"log/slog"
|
||||
"strconv"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
@@ -14,14 +16,30 @@ import (
|
||||
)
|
||||
|
||||
type DomainsHandler struct {
|
||||
Repo *domains.Repo
|
||||
Routing *routingrules.Repo
|
||||
Audit *audit.Repo
|
||||
NodeID string
|
||||
Repo *domains.Repo
|
||||
Routing *routingrules.Repo
|
||||
Audit *audit.Repo
|
||||
NodeID string
|
||||
|
||||
// Reloader regenerates and applies the haproxy config. Symmetric
|
||||
// to FirewallHandler.Reloader; called after every mutation so the
|
||||
// running haproxy.cfg always matches the DB. Errors are logged
|
||||
// but don't fail the API call (the row is committed and the
|
||||
// operator can re-trigger via `edgeguard-ctl render-config`).
|
||||
Reloader func(ctx context.Context) error
|
||||
}
|
||||
|
||||
func NewDomainsHandler(repo *domains.Repo, routing *routingrules.Repo, a *audit.Repo, nodeID string) *DomainsHandler {
|
||||
return &DomainsHandler{Repo: repo, Routing: routing, Audit: a, NodeID: nodeID}
|
||||
func NewDomainsHandler(repo *domains.Repo, routing *routingrules.Repo, a *audit.Repo, nodeID string, reloader func(context.Context) error) *DomainsHandler {
|
||||
return &DomainsHandler{Repo: repo, Routing: routing, Audit: a, NodeID: nodeID, Reloader: reloader}
|
||||
}
|
||||
|
||||
func (h *DomainsHandler) reload(ctx context.Context, op string) {
|
||||
if h.Reloader == nil {
|
||||
return
|
||||
}
|
||||
if err := h.Reloader(ctx); err != nil {
|
||||
slog.Warn("haproxy: reload after domain mutation failed", "op", op, "error", err)
|
||||
}
|
||||
}
|
||||
|
||||
func (h *DomainsHandler) Register(rg *gin.RouterGroup) {
|
||||
@@ -72,7 +90,7 @@ func (h *DomainsHandler) Create(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
_ = h.Audit.Log(c.Request.Context(), actorOf(c), "domain.create", req.Name, out, h.NodeID)
|
||||
response.Created(c, out)
|
||||
response.Created(c, out); h.reload(c.Request.Context(), "create")
|
||||
}
|
||||
|
||||
func (h *DomainsHandler) Update(c *gin.Context) {
|
||||
@@ -95,7 +113,7 @@ func (h *DomainsHandler) Update(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
_ = h.Audit.Log(c.Request.Context(), actorOf(c), "domain.update", out.Name, out, h.NodeID)
|
||||
response.OK(c, out)
|
||||
response.OK(c, out); h.reload(c.Request.Context(), "update")
|
||||
}
|
||||
|
||||
func (h *DomainsHandler) Delete(c *gin.Context) {
|
||||
@@ -113,7 +131,7 @@ func (h *DomainsHandler) Delete(c *gin.Context) {
|
||||
}
|
||||
_ = h.Audit.Log(c.Request.Context(), actorOf(c), "domain.delete",
|
||||
strconv.FormatInt(id, 10), gin.H{"id": id}, h.NodeID)
|
||||
response.NoContent(c)
|
||||
response.NoContent(c); h.reload(c.Request.Context(), "delete")
|
||||
}
|
||||
|
||||
// ListRoutingRules narrows /routing-rules to one domain — UI uses this
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"log/slog"
|
||||
"strconv"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
@@ -13,13 +15,23 @@ import (
|
||||
)
|
||||
|
||||
type RoutingRulesHandler struct {
|
||||
Repo *routingrules.Repo
|
||||
Audit *audit.Repo
|
||||
NodeID string
|
||||
Repo *routingrules.Repo
|
||||
Audit *audit.Repo
|
||||
NodeID string
|
||||
Reloader func(ctx context.Context) error
|
||||
}
|
||||
|
||||
func NewRoutingRulesHandler(repo *routingrules.Repo, a *audit.Repo, nodeID string) *RoutingRulesHandler {
|
||||
return &RoutingRulesHandler{Repo: repo, Audit: a, NodeID: nodeID}
|
||||
func NewRoutingRulesHandler(repo *routingrules.Repo, a *audit.Repo, nodeID string, reloader func(context.Context) error) *RoutingRulesHandler {
|
||||
return &RoutingRulesHandler{Repo: repo, Audit: a, NodeID: nodeID, Reloader: reloader}
|
||||
}
|
||||
|
||||
func (h *RoutingRulesHandler) reload(ctx context.Context, op string) {
|
||||
if h.Reloader == nil {
|
||||
return
|
||||
}
|
||||
if err := h.Reloader(ctx); err != nil {
|
||||
slog.Warn("haproxy: reload after routing-rule mutation failed", "op", op, "error", err)
|
||||
}
|
||||
}
|
||||
|
||||
func (h *RoutingRulesHandler) Register(rg *gin.RouterGroup) {
|
||||
@@ -70,7 +82,7 @@ func (h *RoutingRulesHandler) Create(c *gin.Context) {
|
||||
}
|
||||
_ = h.Audit.Log(c.Request.Context(), actorOf(c), "routing_rule.create",
|
||||
strconv.FormatInt(out.ID, 10), out, h.NodeID)
|
||||
response.Created(c, out)
|
||||
response.Created(c, out); h.reload(c.Request.Context(), "create")
|
||||
}
|
||||
|
||||
func (h *RoutingRulesHandler) Update(c *gin.Context) {
|
||||
@@ -94,7 +106,7 @@ func (h *RoutingRulesHandler) Update(c *gin.Context) {
|
||||
}
|
||||
_ = h.Audit.Log(c.Request.Context(), actorOf(c), "routing_rule.update",
|
||||
strconv.FormatInt(id, 10), out, h.NodeID)
|
||||
response.OK(c, out)
|
||||
response.OK(c, out); h.reload(c.Request.Context(), "update")
|
||||
}
|
||||
|
||||
func (h *RoutingRulesHandler) Delete(c *gin.Context) {
|
||||
@@ -112,5 +124,5 @@ func (h *RoutingRulesHandler) Delete(c *gin.Context) {
|
||||
}
|
||||
_ = h.Audit.Log(c.Request.Context(), actorOf(c), "routing_rule.delete",
|
||||
strconv.FormatInt(id, 10), gin.H{"id": id}, h.NodeID)
|
||||
response.NoContent(c)
|
||||
response.NoContent(c); h.reload(c.Request.Context(), "delete")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user