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.

Clear Docker alerts when removing unified hosts

[email protected]70469660b3e3
diff --git a/docs/release-control/v6/internal/subsystems/agent-lifecycle.md b/docs/release-control/v6/internal/subsystems/agent-lifecycle.md
index 507c5c229e..a1e572cdbb 100644
--- a/docs/release-control/v6/internal/subsystems/agent-lifecycle.md
+++ b/docs/release-control/v6/internal/subsystems/agent-lifecycle.md
@@ -167,6 +167,11 @@ same include list to collection. Includes may opt a matching filesystem back
in after Pulse's automatic pseudo-filesystem filtering, while an explicit
operator exclusion remains authoritative.
+Removing a unified host agent is also an ownership cleanup boundary for the
+Docker/Podman module attached to that host identity. The removal must delete
+the associated runtime report surfaces and clear their active alerts instead
+of leaving container alerts or inventory detached from a removed agent.
+
## Canonical Files
1. `internal/api/agent_install_command_shared.go`
diff --git a/docs/release-control/v6/internal/subsystems/monitoring.md b/docs/release-control/v6/internal/subsystems/monitoring.md
index 0816007cf1..8ded3613b1 100644
--- a/docs/release-control/v6/internal/subsystems/monitoring.md
+++ b/docs/release-control/v6/internal/subsystems/monitoring.md
@@ -392,6 +392,11 @@ for filesystems hidden by Pulse's automatic virtual/container filtering. The
include list is bounded to that automatic filter; explicit disk exclusions
still win and disk-I/O filtering retains its existing exclusion semantics.
+Unified host removal is authoritative for monitoring state owned by that
+agent, including Docker/Podman runtime reports and active alerts keyed to the
+removed host. Pulse must republish the remaining unified read state after the
+cleanup so readers cannot retain orphaned runtime or alert projections.
+
## Canonical Files
1. `internal/monitoring/monitor.go`
diff --git a/internal/monitoring/monitor_agents.go b/internal/monitoring/monitor_agents.go
index 32bfd8c24d..9887cb89cf 100644
--- a/internal/monitoring/monitor_agents.go
+++ b/internal/monitoring/monitor_agents.go
@@ -281,12 +281,30 @@ func (m *Monitor) RemoveDockerHost(hostID string) (models.DockerHost, error) {
}
}
- // Track removal to prevent resurrection from cached reports
- removedAt := time.Now()
+ // Track removal to prevent resurrection from cached reports.
+ m.finalizeDockerHostRemoval(host, time.Now().UTC())
+
+ log.Info().
+ Str("dockerHost", host.Hostname).
+ Str("dockerHostID", hostID).
+ Bool("removed", removed).
+ Msg("Docker host removed and alerts cleared")
+
+ return host, nil
+}
+
+func (m *Monitor) finalizeDockerHostRemoval(host models.DockerHost, removedAt time.Time) {
+ hostID := strings.TrimSpace(host.ID)
+ if hostID == "" {
+ return
+ }
m.mu.Lock()
+ if m.removedDockerHosts == nil {
+ m.removedDockerHosts = make(map[string]time.Time)
+ }
m.removedDockerHosts[hostID] = removedAt
- // Unbind the token so it can be reused with a different agent if needed
+ // Unbind the token so it can be reused with a different agent if needed.
if host.TokenID != "" {
delete(m.dockerTokenBindings, host.TokenID)
log.Debug().
@@ -314,14 +332,55 @@ func (m *Monitor) RemoveDockerHost(hostID string) (models.DockerHost, error) {
m.alertManager.HandleDockerHostRemoved(host)
m.SyncAlertState()
}
+}
- log.Info().
- Str("dockerHost", host.Hostname).
- Str("dockerHostID", hostID).
- Bool("removed", removed).
- Msg("Docker host removed and alerts cleared")
+func dockerHostBelongsToHostAgent(
+ dockerHost models.DockerHost,
+ hostID string,
+ host models.Host,
+ continuity config.HostContinuityEntry,
+) bool {
+ hostAliases := uniqueNonEmptyStrings(
+ hostID,
+ host.ID,
+ host.MachineID,
+ continuity.HostID,
+ continuity.ReportHostID,
+ continuity.AgentReportedID,
+ continuity.MachineID,
+ )
+ for _, dockerAlias := range []string{dockerHost.ID, dockerHost.AgentID} {
+ for _, hostAlias := range hostAliases {
+ if hostAgentIdentifiersMatch(dockerAlias, hostAlias) {
+ return true
+ }
+ }
+ }
- return host, nil
+ dockerMachineID := strings.TrimSpace(dockerHost.MachineID)
+ hostMachineID := firstNonEmpty(continuity.MachineID, host.MachineID)
+ return hostAgentIdentifiersMatch(dockerMachineID, hostMachineID)
+}
+
+func (m *Monitor) removeDockerHostsForHostAgent(
+ hostID string,
+ host models.Host,
+ continuity config.HostContinuityEntry,
+ removedAt time.Time,
+) int {
+ removedCount := 0
+ for _, dockerHost := range m.state.GetDockerHosts() {
+ if !dockerHostBelongsToHostAgent(dockerHost, hostID, host, continuity) {
+ continue
+ }
+ removedDockerHost, removed := m.state.RemoveDockerHost(dockerHost.ID)
+ if !removed {
+ continue
+ }
+ m.finalizeDockerHostRemoval(removedDockerHost, removedAt)
+ removedCount++
+ }
+ return removedCount
}
// RemoveHostAgent removes a host agent from monitoring state and clears related data.
@@ -378,6 +437,8 @@ func (m *Monitor) RemoveHostAgent(hostID string) (models.Host, error) {
m.mu.Unlock()
m.state.AddRemovedHostAgent(removedEntry)
+ removedDockerHosts := m.removeDockerHostsForHostAgent(hostID, host, tombstone, removedAt)
+
tokenID := strings.TrimSpace(host.TokenID)
hostname := strings.TrimSpace(host.Hostname)
@@ -484,6 +545,7 @@ func (m *Monitor) RemoveHostAgent(hostID string) (models.Host, error) {
Str("host", host.Hostname).
Str("hostID", hostID).
Bool("removed", removed).
+ Int("removedDockerHosts", removedDockerHosts).
Msg("Host agent removed from monitoring")
if m.alertManager != nil {
diff --git a/internal/monitoring/monitor_host_agent_removal_lifecycle_test.go b/internal/monitoring/monitor_host_agent_removal_lifecycle_test.go
index da65156347..f9ba67a7bf 100644
--- a/internal/monitoring/monitor_host_agent_removal_lifecycle_test.go
+++ b/internal/monitoring/monitor_host_agent_removal_lifecycle_test.go
@@ -18,7 +18,7 @@ func newHostRemovalLifecycleMonitor(t *testing.T, dataPath string) *Monitor {
t.Helper()
monitor := &Monitor{
state: models.NewState(),
- alertManager: alerts.NewManager(),
+ alertManager: alerts.NewManagerWithDataDir(dataPath),
hostTokenBindings: make(map[string]string),
removedHostAgents: make(map[string]time.Time),
rateTracker: NewRateTracker(),
@@ -221,6 +221,99 @@ func TestHostAgentRemovalLifecycleRepublishesUnifiedReadState(t *testing.T) {
}
}
+func TestHostAgentRemovalLifecycleRemovesAssociatedDockerSurfacesAndAlerts(t *testing.T) {
+ monitor := newHostRemovalLifecycleMonitor(t, t.TempDir())
+ now := time.Now().UTC()
+ report := hostRemovalLifecycleReport(
+ "host-machine-id",
+ "host-machine-id",
+ "host-agent-id",
+ "shared-name.local",
+ "linux",
+ now,
+ )
+ host, err := monitor.ApplyHostReport(report, &config.APITokenRecord{
+ ID: "host-token",
+ CreatedAt: now.Add(-time.Hour),
+ })
+ if err != nil {
+ t.Fatalf("ApplyHostReport: %v", err)
+ }
+
+ dockerHosts := []models.DockerHost{
+ {
+ ID: "docker-agent-alias",
+ AgentID: report.Agent.ID,
+ Hostname: report.Host.Hostname,
+ DisplayName: report.Host.Hostname,
+ Status: "offline",
+ TokenID: "docker-token-one",
+ },
+ {
+ ID: "docker-machine-alias",
+ MachineID: report.Host.MachineID,
+ Hostname: report.Host.Hostname,
+ DisplayName: report.Host.Hostname,
+ Status: "offline",
+ TokenID: "docker-token-two",
+ },
+ {
+ ID: "unrelated-docker-host",
+ AgentID: "unrelated-agent",
+ MachineID: "unrelated-machine",
+ Hostname: report.Host.Hostname,
+ DisplayName: report.Host.Hostname,
+ Status: "offline",
+ TokenID: "unrelated-token",
+ },
+ }
+ for _, dockerHost := range dockerHosts {
+ monitor.state.UpsertDockerHost(dockerHost)
+ for range 3 {
+ monitor.alertManager.HandleDockerHostOffline(dockerHost)
+ }
+ }
+ activeResources := make(map[string]bool)
+ for _, alert := range monitor.alertManager.GetActiveAlerts() {
+ activeResources[alert.ResourceID] = true
+ }
+ for _, dockerHost := range dockerHosts {
+ resourceID := "docker:" + dockerHost.ID
+ if !activeResources[resourceID] {
+ t.Fatalf("active alerts before host removal did not include %q", resourceID)
+ }
+ }
+
+ if _, err := monitor.RemoveHostAgent(host.ID); err != nil {
+ t.Fatalf("RemoveHostAgent: %v", err)
+ }
+ remaining := monitor.state.GetDockerHosts()
+ if len(remaining) != 1 || remaining[0].ID != "unrelated-docker-host" {
+ t.Fatalf("remaining Docker hosts = %+v, want only unrelated host", remaining)
+ }
+ activeResources = make(map[string]bool)
+ for _, alert := range monitor.alertManager.GetActiveAlerts() {
+ activeResources[alert.ResourceID] = true
+ }
+ if activeResources["docker:docker-agent-alias"] || activeResources["docker:docker-machine-alias"] {
+ t.Fatalf("associated Docker alerts remained active after host removal: %+v", activeResources)
+ }
+ if !activeResources["docker:unrelated-docker-host"] {
+ t.Fatalf("unrelated Docker alert was cleared after host removal: %+v", activeResources)
+ }
+
+ removedIDs := make(map[string]bool)
+ for _, removed := range monitor.state.GetRemovedDockerHosts() {
+ removedIDs[removed.ID] = true
+ }
+ if !removedIDs["docker-agent-alias"] || !removedIDs["docker-machine-alias"] {
+ t.Fatalf("removed Docker hosts = %+v, want both associated surfaces", removedIDs)
+ }
+ if removedIDs["unrelated-docker-host"] {
+ t.Fatal("same-hostname unrelated Docker host was removed")
+ }
+}
+
func TestHostAgentRemovalLifecycleSurvivesMonitorReconstruction(t *testing.T) {
dataPath := t.TempDir()
now := time.Now().UTC()