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.

Supersede stale Docker host records after agent re-enrollment

rcourtman49516bf3676b
diff --git a/internal/monitoring/docker_host_supersede_test.go b/internal/monitoring/docker_host_supersede_test.go
new file mode 100644
index 0000000000..b3a9d7c43b
--- /dev/null
+++ b/internal/monitoring/docker_host_supersede_test.go
@@ -0,0 +1,106 @@
+package monitoring
+
+import (
+ "testing"
+ "time"
+
+ "github.com/rcourtman/pulse-go-rewrite/internal/config"
+ agentsdocker "github.com/rcourtman/pulse-go-rewrite/pkg/agents/docker"
+)
+
+func supersedeTestReport(agentID, version string, at time.Time) agentsdocker.Report {
+ return agentsdocker.Report{
+ Agent: agentsdocker.AgentInfo{
+ ID: agentID,
+ Version: version,
+ Type: "unified",
+ IntervalSeconds: 30,
+ },
+ Host: agentsdocker.HostInfo{
+ Hostname: "nuc",
+ MachineID: "machine-nuc",
+ TotalCPU: 4,
+ },
+ Containers: []agentsdocker.Container{
+ {ID: "container-" + agentID, Name: "heimdall"},
+ },
+ Timestamp: at,
+ }
+}
+
+// A wiped agent state dir regenerates the agent ID, and the fresh install
+// command mints a fresh token, so identity resolution refuses to adopt the old
+// record and creates a new one. The old record must be superseded rather than
+// left behind with a stale agent version and stale containers (#1586, #1564).
+func TestApplyDockerReportSupersedesStaleReenrolledHost(t *testing.T) {
+ monitor := newTestMonitor(t)
+ base := time.Now().UTC()
+
+ oldToken := &config.APITokenRecord{ID: "token-old", CreatedAt: base.Add(-24 * time.Hour)}
+ oldHost, err := monitor.ApplyDockerReport(supersedeTestReport("agent-old", "6.1.0-rc.2", base), oldToken)
+ if err != nil {
+ t.Fatalf("old report: %v", err)
+ }
+
+ // The new token is minted after the old record's last report: explicit
+ // re-enroll intent, so the corpse is reaped.
+ newToken := &config.APITokenRecord{ID: "token-new", CreatedAt: time.Now().UTC().Add(time.Hour)}
+ newHost, err := monitor.ApplyDockerReport(supersedeTestReport("agent-new", "6.1.0-rc.3", base.Add(time.Hour)), newToken)
+ if err != nil {
+ t.Fatalf("new report: %v", err)
+ }
+ if newHost.ID == oldHost.ID {
+ t.Fatalf("expected re-enrollment under a new token to mint a new identity, both got %q", newHost.ID)
+ }
+
+ hosts := monitor.state.GetDockerHosts()
+ if len(hosts) != 1 {
+ ids := make([]string, 0, len(hosts))
+ for _, h := range hosts {
+ ids = append(ids, h.ID+"@"+h.AgentVersion)
+ }
+ t.Fatalf("expected the stale record to be superseded, got %d hosts: %v", len(hosts), ids)
+ }
+ if hosts[0].ID != newHost.ID {
+ t.Fatalf("expected the surviving record to be the new identity %q, got %q", newHost.ID, hosts[0].ID)
+ }
+ if hosts[0].AgentVersion != "v6.1.0-rc.3" {
+ t.Fatalf("expected surviving record on the new agent version, got %q", hosts[0].AgentVersion)
+ }
+
+ monitor.mu.RLock()
+ _, oldTokenStillBound := monitor.dockerTokenBindings["token-old"]
+ _, oldStillBlocked := monitor.removedDockerHosts[oldHost.ID]
+ monitor.mu.RUnlock()
+ if oldTokenStillBound {
+ t.Fatal("expected the orphaned token binding to be released")
+ }
+ if oldStillBlocked {
+ t.Fatal("supersession must not set the deliberate-removal resurrection block")
+ }
+}
+
+// A record that reported after the new token was minted is a live host (for
+// example two genuinely different machines behind mismatched machine-id
+// hygiene, or an old agent the user has not uninstalled yet). It must never be
+// reaped by another agent's report.
+func TestApplyDockerReportKeepsLiveHostDespiteMatchingIdentity(t *testing.T) {
+ monitor := newTestMonitor(t)
+ base := time.Now().UTC()
+
+ oldToken := &config.APITokenRecord{ID: "token-old", CreatedAt: base.Add(-24 * time.Hour)}
+ if _, err := monitor.ApplyDockerReport(supersedeTestReport("agent-old", "6.1.0-rc.2", base), oldToken); err != nil {
+ t.Fatalf("old report: %v", err)
+ }
+
+ // Token minted before the existing record's last report: no proof the old
+ // record is a corpse, so both stay.
+ staleToken := &config.APITokenRecord{ID: "token-new", CreatedAt: base.Add(-time.Hour)}
+ if _, err := monitor.ApplyDockerReport(supersedeTestReport("agent-new", "6.1.0-rc.3", base.Add(time.Minute)), staleToken); err != nil {
+ t.Fatalf("new report: %v", err)
+ }
+
+ if hosts := monitor.state.GetDockerHosts(); len(hosts) != 2 {
+ t.Fatalf("expected both records to survive without corpse proof, got %d hosts", len(hosts))
+ }
+}
diff --git a/internal/monitoring/monitor_agents.go b/internal/monitoring/monitor_agents.go
index c0b4277b4e..14087bf8ce 100644
--- a/internal/monitoring/monitor_agents.go
+++ b/internal/monitoring/monitor_agents.go
@@ -1108,6 +1108,78 @@ func (m *Monitor) AcknowledgeDockerHostCommand(commandID, hostID, status, messag
return m.acknowledgeDockerCommand(commandID, hostID, status, message)
}
+// supersedeStaleDockerHostDuplicates reaps leftover Docker host records for
+// the same physical machine after an agent re-enrolls under a fresh identity.
+// Wiping the agent state dir regenerates the agent ID, and a fresh install
+// command mints a fresh token, so identity resolution correctly refuses to
+// adopt the old record (a foreign token must never take over a live host,
+// #1008) and creates a new one — leaving the old record behind forever with a
+// stale agent version, stale containers, and stale image digests (#1586,
+// #1564). A record qualifies as a corpse only when it stopped reporting
+// before the superseding token was even minted: generating a fresh install
+// command for the same machine is explicit replace intent (the same rule the
+// removal block uses for re-enrollment, #1581), while a record that is still
+// reporting keeps advancing LastSeen and is never touched. Unlike a
+// user-initiated removal this does not set the resurrection block and does
+// not revoke the orphaned token.
+func (m *Monitor) supersedeStaleDockerHostDuplicates(current models.DockerHost, tokenRecord *config.APITokenRecord, hosts []*unifiedresources.DockerHostView) {
+ if tokenRecord == nil || tokenRecord.CreatedAt.IsZero() {
+ return
+ }
+ machineID := strings.TrimSpace(current.MachineID)
+ hostname := strings.TrimSpace(current.Hostname)
+ if machineID == "" || hostname == "" {
+ return
+ }
+
+ for _, stale := range hosts {
+ if stale == nil {
+ continue
+ }
+ staleID := dockerHostStableID(stale)
+ if staleID == "" || staleID == strings.TrimSpace(current.ID) {
+ continue
+ }
+ if strings.TrimSpace(stale.MachineID()) != machineID {
+ continue
+ }
+ if !unifiedresources.HostnamesEquivalent(stale.Hostname(), hostname) {
+ continue
+ }
+ if !stale.LastSeen().Before(tokenRecord.CreatedAt) {
+ continue
+ }
+
+ removed, ok := m.state.RemoveDockerHost(staleID)
+ if !ok {
+ continue
+ }
+ m.state.RemoveConnectionHealth(dockerConnectionPrefix + staleID)
+
+ m.mu.Lock()
+ if removed.TokenID != "" && removed.TokenID != current.TokenID {
+ delete(m.dockerTokenBindings, removed.TokenID)
+ }
+ if cmd, ok := m.dockerCommands[staleID]; ok {
+ delete(m.dockerCommandIndex, cmd.status.ID)
+ }
+ delete(m.dockerCommands, staleID)
+ m.clearDockerHostIdentityTrackingLocked(staleID)
+ m.mu.Unlock()
+
+ if m.alertManager != nil {
+ m.alertManager.HandleDockerHostRemoved(removed)
+ m.SyncAlertState()
+ }
+
+ log.Info().
+ Str("dockerHost", removed.Hostname).
+ Str("staleDockerHostID", staleID).
+ Str("replacementDockerHostID", current.ID).
+ Msg("Superseded stale Docker host record after agent re-enrollment")
+ }
+}
+
// ApplyDockerReport ingests a Docker / Podman module report into the shared state.
func (m *Monitor) ApplyDockerReport(report agentsdocker.Report, tokenRecord *config.APITokenRecord) (models.DockerHost, error) {
readState := m.snapshotBackedUnifiedReadState()
@@ -1541,6 +1613,8 @@ func (m *Monitor) ApplyDockerReport(report agentsdocker.Report, tokenRecord *con
m.state.UpsertDockerHost(host)
m.state.SetConnectionHealth(dockerConnectionPrefix+host.ID, true)
+ m.supersedeStaleDockerHostDuplicates(host, tokenRecord, dockerHosts)
+
// Check if the host was previously hidden and is now visible again
if hasPrevious && previous.Hidden() && !host.Hidden {
log.Info().