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.

Resolve docker service alert resource IDs in Patrol scope resolution

rcourtman465ef1fda9f6
diff --git a/internal/ai/patrol_state.go b/internal/ai/patrol_state.go
index 797431d69e..822728ba0a 100644
--- a/internal/ai/patrol_state.go
+++ b/internal/ai/patrol_state.go
@@ -21,6 +21,24 @@ func dockerAlertScopeAlias(hostID, containerID string) string {
return alerts.DockerResourceID(hostID, containerID)
}
+// dockerServiceAlertScopeAliases mirrors the alert subsystem's Swarm service
+// resource IDs ("docker:<host>/service/<serviceID>") onto the owning Docker
+// host record. Patrol has no service-level analysis, and a service's tasks run
+// as containers on the host, so the host is the smallest unit Patrol can
+// investigate for a service alert (#1699). Hosts without an ID are skipped for
+// the same anti-aliasing reason as dockerAlertScopeAlias: the host-less
+// "docker-service:<name>" fallback could collide across hosts.
+func dockerServiceAlertScopeAliases(hostID string, services []models.DockerService) []string {
+ if strings.TrimSpace(hostID) == "" || len(services) == 0 {
+ return nil
+ }
+ aliases := make([]string, 0, len(services))
+ for _, service := range services {
+ aliases = append(aliases, alerts.DockerServiceResourceID(hostID, service.ID, service.Name))
+ }
+ return aliases
+}
+
type patrolRuntimeResourceKind string
const (
@@ -228,7 +246,9 @@ func patrolVisitRuntimeResources(s patrolRuntimeState, visit func(patrolRuntimeR
}
}
for _, dh := range rs.DockerHosts() {
- if !emit(patrolRuntimeResourceDockerHost, []string{dh.ID(), dh.HostSourceID()}, dh.Name(), dh.Hostname(), dockerAlertScopeAlias(dh.HostSourceID(), "")) {
+ aliases := append([]string{dh.Name(), dh.Hostname(), dockerAlertScopeAlias(dh.HostSourceID(), "")},
+ dockerServiceAlertScopeAliases(dh.HostSourceID(), dh.Services())...)
+ if !emit(patrolRuntimeResourceDockerHost, []string{dh.ID(), dh.HostSourceID()}, aliases...) {
return
}
}
@@ -279,7 +299,9 @@ func patrolVisitRuntimeResources(s patrolRuntimeState, visit func(patrolRuntimeR
}
}
for _, dh := range s.DockerHosts {
- if !emit(patrolRuntimeResourceDockerHost, []string{dh.ID}, dh.DisplayName, dh.CustomDisplayName, dh.Hostname, dockerAlertScopeAlias(dh.ID, "")) {
+ aliases := append([]string{dh.DisplayName, dh.CustomDisplayName, dh.Hostname, dockerAlertScopeAlias(dh.ID, "")},
+ dockerServiceAlertScopeAliases(dh.ID, dh.Services)...)
+ if !emit(patrolRuntimeResourceDockerHost, []string{dh.ID}, aliases...) {
return
}
for _, dc := range dh.Containers {
diff --git a/internal/ai/patrol_state_scope_alias_test.go b/internal/ai/patrol_state_scope_alias_test.go
new file mode 100644
index 0000000000..0889178589
--- /dev/null
+++ b/internal/ai/patrol_state_scope_alias_test.go
@@ -0,0 +1,93 @@
+package ai
+
+import (
+ "strings"
+ "testing"
+
+ "github.com/rcourtman/pulse-go-rewrite/internal/alerts"
+ "github.com/rcourtman/pulse-go-rewrite/internal/models"
+)
+
+// TestDockerServiceAlertScopeAliasesMatchAlertResourceIDs pins the contract that
+// makes #1699 work: the alias Patrol registers on a Docker host must be byte
+// identical to the resource ID the alert subsystem stamps on a Swarm service
+// alert. If either side changes its ID format independently, service alerts stop
+// resolving to a scope and Patrol silently declines to investigate them.
+func TestDockerServiceAlertScopeAliasesMatchAlertResourceIDs(t *testing.T) {
+ services := []models.DockerService{
+ {ID: "svc-1", Name: "web"},
+ {ID: "svc-2", Name: "api"},
+ }
+
+ got := dockerServiceAlertScopeAliases("host-1", services)
+
+ want := make([]string, 0, len(services))
+ for _, service := range services {
+ want = append(want, alerts.DockerServiceResourceID("host-1", service.ID, service.Name))
+ }
+
+ if len(got) != len(want) {
+ t.Fatalf("alias count = %d, want %d (got %v)", len(got), len(want), got)
+ }
+ for i := range want {
+ if got[i] != want[i] {
+ t.Errorf("alias[%d] = %q, want %q", i, got[i], want[i])
+ }
+ }
+
+ // Guard the format itself, so a coordinated rename on both sides that broke
+ // the stored alert IDs would still be caught here.
+ if got[0] != "docker:host-1/service/svc-1" {
+ t.Errorf("alias[0] = %q, want %q", got[0], "docker:host-1/service/svc-1")
+ }
+}
+
+// TestDockerServiceAlertScopeAliasesSkipHostlessServices covers the
+// anti-aliasing guard. Without a host ID the alert subsystem falls back to
+// "docker-service:<name>", which is not host qualified and would collide across
+// hosts, so Patrol must register nothing rather than a colliding alias.
+func TestDockerServiceAlertScopeAliasesSkipHostlessServices(t *testing.T) {
+ services := []models.DockerService{{ID: "svc-1", Name: "web"}}
+
+ for _, hostID := range []string{"", " ", "\t"} {
+ got := dockerServiceAlertScopeAliases(hostID, services)
+ if got != nil {
+ t.Errorf("hostID %q: aliases = %v, want nil", hostID, got)
+ }
+ for _, alias := range got {
+ if strings.HasPrefix(alias, "docker-service:") {
+ t.Errorf("hostID %q: registered host-less alias %q, which collides across hosts", hostID, alias)
+ }
+ }
+ }
+
+ // Sanity check that the collidable form is really what a host-less ID
+ // produces, so this test keeps its meaning if the fallback changes.
+ if fallback := alerts.DockerServiceResourceID("", "svc-1", "web"); !strings.HasPrefix(fallback, "docker-service:") {
+ t.Fatalf("host-less resource ID = %q, expected the un-host-qualified docker-service: form", fallback)
+ }
+}
+
+func TestDockerServiceAlertScopeAliasesWithoutServices(t *testing.T) {
+ if got := dockerServiceAlertScopeAliases("host-1", nil); got != nil {
+ t.Errorf("nil services: aliases = %v, want nil", got)
+ }
+ if got := dockerServiceAlertScopeAliases("host-1", []models.DockerService{}); got != nil {
+ t.Errorf("empty services: aliases = %v, want nil", got)
+ }
+}
+
+// TestDockerServiceAlertScopeAliasesDeriveIDFromName covers services that carry
+// no ID, where the canonical builder derives one from the service name. The
+// alias has to follow that derivation rather than formatting its own string.
+func TestDockerServiceAlertScopeAliasesDeriveIDFromName(t *testing.T) {
+ got := dockerServiceAlertScopeAliases("host-1", []models.DockerService{{Name: "Web API"}})
+
+ want := alerts.DockerServiceResourceID("host-1", "", "Web API")
+ if len(got) != 1 || got[0] != want {
+ t.Fatalf("aliases = %v, want [%q]", got, want)
+ }
+ if got[0] != "docker:host-1/service/web-api" {
+ t.Errorf("alias = %q, want %q", got[0], "docker:host-1/service/web-api")
+ }
+}
diff --git a/internal/alerts/alerts_test.go b/internal/alerts/alerts_test.go
index d270935b1c..cb1b1ed2c2 100644
--- a/internal/alerts/alerts_test.go
+++ b/internal/alerts/alerts_test.go
@@ -2573,7 +2573,7 @@ func TestDockerServiceReplicaAlerts(t *testing.T) {
m.CheckDockerHost(host)
- resourceID := dockerServiceResourceID(host.ID, "svc-1", "web")
+ resourceID := DockerServiceResourceID(host.ID, "svc-1", "web")
alertID := fmt.Sprintf("docker-service-health-%s", resourceID)
alert, exists := testLookupActiveAlert(t, m, alertID)
if !exists {
@@ -2670,7 +2670,7 @@ func TestDockerServiceAlertPreservesLastNotifiedWhenUnchanged(t *testing.T) {
m.CheckDockerHost(host)
- resourceID := dockerServiceResourceID(host.ID, "svc-1", "web")
+ resourceID := DockerServiceResourceID(host.ID, "svc-1", "web")
alertID := fmt.Sprintf("docker-service-health-%s", resourceID)
alert, exists := testLookupActiveAlert(t, m, alertID)
if !exists {
@@ -2780,7 +2780,7 @@ func TestDockerServiceUpdateStateAlert(t *testing.T) {
m.CheckDockerHost(host)
- resourceID := dockerServiceResourceID(host.ID, "svc-update", "api")
+ resourceID := DockerServiceResourceID(host.ID, "svc-update", "api")
alertID := fmt.Sprintf("docker-service-health-%s", resourceID)
alert, exists := testLookupActiveAlert(t, m, alertID)
if !exists {
@@ -3056,7 +3056,7 @@ func TestDockerServiceAlertUsesClampedCriticalGap(t *testing.T) {
m.CheckDockerHost(host)
- resourceID := dockerServiceResourceID(host.ID, "svc-123", "api")
+ resourceID := DockerServiceResourceID(host.ID, "svc-123", "api")
alertID := fmt.Sprintf("docker-service-health-%s", resourceID)
alert, exists := testLookupActiveAlert(t, m, alertID)
@@ -5894,9 +5894,9 @@ func TestDockerServiceResourceID(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// t.Parallel()
- result := dockerServiceResourceID(tt.hostID, tt.serviceID, tt.serviceName)
+ result := DockerServiceResourceID(tt.hostID, tt.serviceID, tt.serviceName)
if result != tt.expected {
- t.Errorf("dockerServiceResourceID(%q, %q, %q) = %q, want %q",
+ t.Errorf("DockerServiceResourceID(%q, %q, %q) = %q, want %q",
tt.hostID, tt.serviceID, tt.serviceName, result, tt.expected)
}
})
diff --git a/internal/alerts/config_runtime.go b/internal/alerts/config_runtime.go
index 507e438491..8cc9f53bff 100644
--- a/internal/alerts/config_runtime.go
+++ b/internal/alerts/config_runtime.go
@@ -268,7 +268,7 @@ func (m *Manager) applyGlobalOfflineSettingsLocked() {
// dockerAlertResourcePath returns the portion of the alert's resource ID after
// the "docker:" scheme, matching the IDs built by DockerResourceID and
-// dockerServiceResourceID. Canonical alerts are stored under
+// DockerServiceResourceID. Canonical alerts are stored under
// "<resourceID>::<specID>" state IDs, so the legacy "docker-container-" /
// "docker-service-" alert-ID prefixes never match them.
func dockerAlertResourcePath(alert *Alert) (string, bool) {
diff --git a/internal/alerts/docker.go b/internal/alerts/docker.go
index e02529e0fe..8451f88798 100644
--- a/internal/alerts/docker.go
+++ b/internal/alerts/docker.go
@@ -163,7 +163,10 @@ func dockerServiceDisplayName(service models.DockerService) string {
return serviceID
}
-func dockerServiceResourceID(hostID, serviceID, serviceName string) string {
+// DockerServiceResourceID builds a stable identifier for Docker Swarm service
+// alerts. Patrol scope resolution registers this form as a known alias on the
+// owning Docker host record so a service alert's resource ID resolves there.
+func DockerServiceResourceID(hostID, serviceID, serviceName string) string {
hostID = strings.TrimSpace(hostID)
normalizedServiceID := strings.TrimSpace(serviceID)
if normalizedServiceID == "" {
@@ -296,7 +299,7 @@ func (m *Manager) CheckDockerHost(host models.DockerHost) {
}
for _, service := range host.Services {
- resourceID := dockerServiceResourceID(host.ID, service.ID, service.Name)
+ resourceID := DockerServiceResourceID(host.ID, service.ID, service.Name)
seen[resourceID] = struct{}{}
m.evaluateDockerService(host, service, resourceID)
}
diff --git a/internal/alerts/unified_eval_test.go b/internal/alerts/unified_eval_test.go
index 2bcb22e7c9..c7d9561a72 100644
--- a/internal/alerts/unified_eval_test.go
+++ b/internal/alerts/unified_eval_test.go
@@ -1293,7 +1293,7 @@ func TestCheckDockerServiceAnnotatesCanonicalSpecMetadata(t *testing.T) {
m.CheckDockerHost(host)
- resourceID := dockerServiceResourceID(host.ID, "svc-1", "web")
+ resourceID := DockerServiceResourceID(host.ID, "svc-1", "web")
alert := activeAlert(t, m, "docker-service-health-"+resourceID)
if got := alert.Metadata["canonicalAlertKind"]; got != "service-gap" {
t.Fatalf("canonicalAlertKind = %v, want service-gap", got)
@@ -1333,7 +1333,7 @@ func TestCheckDockerServiceUpdateStateAnnotatesCanonicalSpecMetadata(t *testing.
m.CheckDockerHost(host)
- resourceID := dockerServiceResourceID(host.ID, "svc-update", "api")
+ resourceID := DockerServiceResourceID(host.ID, "svc-update", "api")
alert := activeAlert(t, m, "docker-service-health-"+resourceID)
if got := alert.Metadata["canonicalAlertKind"]; got != "discrete-state" {
t.Fatalf("canonicalAlertKind = %v, want discrete-state", got)