This commit is contained in:
rcourtman
2026-07-11 19:02:14 +01:00
parent aefc631374
commit a23a8fad9a
2 changed files with 61 additions and 0 deletions
+3
View File
@@ -1361,6 +1361,9 @@ func (r *Router) SetMonitor(m *monitoring.Monitor) {
if r.unifiedAgentHandlers != nil {
r.unifiedAgentHandlers.SetMonitor(m)
}
if r.kubernetesAgentHandlers != nil {
r.kubernetesAgentHandlers.SetMonitor(m)
}
if r.systemSettingsHandler != nil {
r.systemSettingsHandler.SetMonitor(m)
}
@@ -0,0 +1,58 @@
package api
import (
"context"
"testing"
"github.com/rcourtman/pulse-go-rewrite/internal/config"
"github.com/rcourtman/pulse-go-rewrite/internal/monitoring"
)
// TestRouterSetMonitor_UpdatesAgentHandlers verifies the fix for issue #1558:
// Router.SetMonitor must repoint the kubernetes agent handlers at the new
// monitor, exactly as it does for the docker agent handlers. Without this,
// kubernetes agent reports resolved through the single-tenant fallback keep
// landing in the orphaned pre-reload monitor after a config reload, so the
// cluster flaps in the UI.
func TestRouterSetMonitor_UpdatesAgentHandlers(t *testing.T) {
tempDir := t.TempDir()
cfg := &config.Config{
DataPath: tempDir,
ConfigPath: tempDir,
}
monitor1, err := monitoring.New(cfg)
if err != nil {
t.Fatalf("monitoring.New (initial): %v", err)
}
t.Cleanup(func() { monitor1.Stop() })
monitor2, err := monitoring.New(cfg)
if err != nil {
t.Fatalf("monitoring.New (reload): %v", err)
}
t.Cleanup(func() { monitor2.Stop() })
// No multi-tenant monitor: getMonitor resolves via the defaultMonitor
// fallback, which is exactly the field SetMonitor must refresh.
kubernetesHandlers := NewKubernetesAgentHandlers(nil, monitor1, nil)
dockerHandlers := NewDockerAgentHandlers(nil, monitor1, nil, cfg)
// Minimal router, as in TestReloadSystemSettings_AppliesWebhookCIDRsToNewMonitor:
// SetMonitor only touches the handlers that are non-nil.
router := &Router{
config: cfg,
kubernetesAgentHandlers: kubernetesHandlers,
dockerAgentHandlers: dockerHandlers,
}
router.SetMonitor(monitor2)
ctx := context.Background()
if got := kubernetesHandlers.getMonitor(ctx); got != monitor2 {
t.Fatal("kubernetes agent handlers still resolve the pre-reload monitor after Router.SetMonitor")
}
if got := dockerHandlers.getMonitor(ctx); got != monitor2 {
t.Fatal("docker agent handlers still resolve the pre-reload monitor after Router.SetMonitor")
}
}