Skip to content

armgit / Pulse

Public Imported from GitHub
0

Real-time monitoring dashboard for Proxmox VE, PBS, Docker, Kubernetes, TrueNAS and vSphere. Self-hosted, with alerts for failed backups, full disks, unhealthy containers and offline nodes.

Serve agent config from continuity state during reload windows

rcourtman506d2e5b7e31
diff --git a/internal/api/agent_ingest.go b/internal/api/agent_ingest.go
index 25f44623db..ac7e097b63 100644
--- a/internal/api/agent_ingest.go
+++ b/internal/api/agent_ingest.go
@@ -363,7 +363,8 @@ func (h *UnifiedAgentHandlers) canReadConfig(record *config.APITokenRecord) bool
func (h *UnifiedAgentHandlers) resolveConfigAgent(ctx context.Context, agentID string, record *config.APITokenRecord) (models.Host, bool) {
// Use the live host snapshot so agents can still fetch config while
// Pulse is running in mock/demo mode.
- hosts := h.getMonitor(ctx).GetLiveHostsSnapshot()
+ monitor := h.getMonitor(ctx)
+ hosts := monitor.GetLiveHostsSnapshot()
if record == nil || record.HasScope(config.ScopeSettingsWrite) || record.HasScope(config.ScopeAgentManage) {
for _, candidate := range hosts {
@@ -371,7 +372,10 @@ func (h *UnifiedAgentHandlers) resolveConfigAgent(ctx context.Context, agentID s
return candidate, true
}
}
- return models.Host{}, false
+ // Live state loses agent-reported hosts across monitor reloads and
+ // restarts until the next report lands; fall back to the persisted
+ // continuity store so config fetches in that window do not 404 (#1570).
+ return monitor.MatchHostConfigContinuity(agentID, "")
}
for _, candidate := range hosts {
@@ -380,7 +384,7 @@ func (h *UnifiedAgentHandlers) resolveConfigAgent(ctx context.Context, agentID s
}
}
- return models.Host{}, false
+ return monitor.MatchHostConfigContinuity(agentID, record.ID)
}
func (h *UnifiedAgentHandlers) signAgentConfig(agentID string, cfg monitoring.HostAgentConfig) (monitoring.HostAgentConfig, error) {
diff --git a/internal/monitoring/monitor_agents.go b/internal/monitoring/monitor_agents.go
index e9b7f6ea63..8961e8a3a8 100644
--- a/internal/monitoring/monitor_agents.go
+++ b/internal/monitoring/monitor_agents.go
@@ -852,6 +852,43 @@ func (m *Monitor) matchPersistedHostContinuity(
)
}
+// MatchHostConfigContinuity resolves a host identity for agent config fetches
+// from the persisted continuity store. Live state loses agent-reported hosts
+// across monitor reloads and restarts until the next report lands, and config
+// fetches in that window 404ed with a valid token (#1570). Mirrors the live
+// resolution semantics: a report-scoped token resolves by its binding, a
+// manage-scoped (or absent) token resolves by host ID.
+func (m *Monitor) MatchHostConfigContinuity(agentID, tokenID string) (models.Host, bool) {
+ if m == nil || m.hostContinuityStore == nil {
+ return models.Host{}, false
+ }
+ agentID = strings.TrimSpace(agentID)
+ tokenID = strings.TrimSpace(tokenID)
+
+ // RecentEntries is sorted newest-first, so the first match wins.
+ for _, entry := range m.hostContinuityStore.RecentEntries(m.hostContinuitySince(time.Now().UTC())) {
+ if tokenID != "" {
+ if strings.TrimSpace(entry.TokenID) != tokenID {
+ continue
+ }
+ } else if agentID == "" ||
+ (entry.HostID != agentID && entry.ReportHostID != agentID && entry.AgentReportedID != agentID) {
+ continue
+ }
+ return models.Host{
+ ID: entry.HostID,
+ Hostname: entry.Hostname,
+ DisplayName: entry.DisplayName,
+ MachineID: entry.MachineID,
+ TokenID: entry.TokenID,
+ Platform: entry.Platform,
+ IsLegacy: entry.IsLegacy,
+ }, true
+ }
+
+ return models.Host{}, false
+}
+
func (m *Monitor) persistHostContinuity(host models.Host, report agentshost.Report) {
if m == nil || m.hostContinuityStore == nil {
return
diff --git a/internal/monitoring/monitor_host_agents_test.go b/internal/monitoring/monitor_host_agents_test.go
index 142cddb9d4..20f068c1c8 100644
--- a/internal/monitoring/monitor_host_agents_test.go
+++ b/internal/monitoring/monitor_host_agents_test.go
@@ -3386,6 +3386,46 @@ func TestCleanupRemovedHostAgents_ExpiresPersistedEntries(t *testing.T) {
}
}
+// TestMatchHostConfigContinuity pins the #1570 config-fetch fallback: after a
+// monitor reload wipes live state, a report-scoped token still resolves its
+// bound host from the persisted continuity store, other tokens do not, and a
+// manage-scoped (empty token) lookup resolves by host ID.
+func TestMatchHostConfigContinuity(t *testing.T) {
+ dir := t.TempDir()
+ monitor := &Monitor{
+ hostContinuityStore: config.NewHostContinuityStore(dir, nil),
+ }
+
+ entry := config.HostContinuityEntry{
+ HostID: "host-cont-1",
+ Hostname: "cont.local",
+ MachineID: "machine-cont-1",
+ TokenID: "token-cont-1",
+ LastSeen: time.Now().UTC(),
+ }
+ if err := monitor.hostContinuityStore.Upsert(entry); err != nil {
+ t.Fatalf("upsert continuity entry: %v", err)
+ }
+
+ host, ok := monitor.MatchHostConfigContinuity("host-cont-1", "token-cont-1")
+ if !ok || host.ID != "host-cont-1" {
+ t.Fatalf("expected token-bound continuity match, got ok=%v host=%q", ok, host.ID)
+ }
+
+ if _, ok := monitor.MatchHostConfigContinuity("host-cont-1", "token-other"); ok {
+ t.Fatal("expected no match for a token the host is not bound to")
+ }
+
+ host, ok = monitor.MatchHostConfigContinuity("host-cont-1", "")
+ if !ok || host.ID != "host-cont-1" {
+ t.Fatalf("expected ID-based continuity match for manage scope, got ok=%v host=%q", ok, host.ID)
+ }
+
+ if _, ok := monitor.MatchHostConfigContinuity("host-unknown", ""); ok {
+ t.Fatal("expected no ID-based match for an unknown host")
+ }
+}
+
func TestApplyHostReport_MissingHostname(t *testing.T) {
monitor := &Monitor{
state: models.NewState(),