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.

Make K8s node identity enrichment fail safe

rcourtman96b77f5b60ef
diff --git a/docs/release-control/v6/internal/subsystems/unified-resources.md b/docs/release-control/v6/internal/subsystems/unified-resources.md
index c97cc6e749..9f9b9862ab 100644
--- a/docs/release-control/v6/internal/subsystems/unified-resources.md
+++ b/docs/release-control/v6/internal/subsystems/unified-resources.md
@@ -79,9 +79,9 @@ canonical resource identity, discovery normalization, and platform-runtime
coverage stay governed as a first-class Pulse product surface.
Kubernetes node identity enrichment remains intentionally hostname-based when
the API cannot supply machine-level identity signals; the code may borrow
-machine-id and MAC evidence from a matched host agent, but duplicate short
-hostnames across domains or clusters stay an unresolved ambiguity rather than
-being force-merged into a stronger canonical identity.
+machine-id and MAC evidence from a uniquely matched host agent, but duplicate
+short hostnames across domains or clusters stay an unresolved ambiguity rather
+than being force-merged into a stronger canonical identity.
The unified-resource runtime now also owns the durable change timeline for the
canonical resource view. `internal/unifiedresources/monitor_adapter.go` feeds
diff --git a/internal/unifiedresources/resolved_host_set.go b/internal/unifiedresources/resolved_host_set.go
index 175022c9d7..3e8e51a572 100644
--- a/internal/unifiedresources/resolved_host_set.go
+++ b/internal/unifiedresources/resolved_host_set.go
@@ -461,26 +461,49 @@ func k8sCandidates(cluster models.KubernetesCluster, hosts []models.Host) []Host
return candidates
}
-// enrichK8sNodeIdentity attempts to find a host agent with a matching hostname
-// and copies its machine-id and MAC addresses to strengthen the K8s node's identity.
+// enrichK8sNodeIdentity attempts to find a uniquely matched host agent with a
+// matching hostname and copies its machine-id and MAC addresses to strengthen
+// the K8s node's identity.
func enrichK8sNodeIdentity(identity *ResourceIdentity, nodeName string, hosts []models.Host) {
if nodeName == "" || len(hosts) == 0 {
return
}
+
+ for i := range hosts {
+ host := &hosts[i]
+ if strings.EqualFold(strings.TrimSpace(host.Hostname), nodeName) {
+ if host.MachineID != "" && identity.MachineID == "" {
+ identity.MachineID = strings.TrimSpace(host.MachineID)
+ }
+ _, macs := collectInterfaceIDs(host.NetworkInterfaces)
+ identity.MACAddresses = uniqueStrings(append(identity.MACAddresses, macs...))
+ return
+ }
+ }
+
normName := NormalizeHostname(nodeName)
if normName == "" {
return
}
- for _, h := range hosts {
- if NormalizeHostname(h.Hostname) == normName {
- if h.MachineID != "" && identity.MachineID == "" {
- identity.MachineID = strings.TrimSpace(h.MachineID)
+
+ var matched *models.Host
+ for i := range hosts {
+ host := &hosts[i]
+ if NormalizeHostname(host.Hostname) == normName {
+ if matched != nil {
+ return
}
- _, macs := collectInterfaceIDs(h.NetworkInterfaces)
- identity.MACAddresses = uniqueStrings(append(identity.MACAddresses, macs...))
- return
+ matched = host
}
}
+ if matched == nil {
+ return
+ }
+ if matched.MachineID != "" && identity.MachineID == "" {
+ identity.MachineID = strings.TrimSpace(matched.MachineID)
+ }
+ _, macs := collectInterfaceIDs(matched.NetworkInterfaces)
+ identity.MACAddresses = uniqueStrings(append(identity.MACAddresses, macs...))
}
// ---------------------------------------------------------------------------
diff --git a/internal/unifiedresources/resolved_host_set_test.go b/internal/unifiedresources/resolved_host_set_test.go
index 90f000f0f0..4884f52bc0 100644
--- a/internal/unifiedresources/resolved_host_set_test.go
+++ b/internal/unifiedresources/resolved_host_set_test.go
@@ -395,6 +395,35 @@ func TestEnrichK8sNodeIdentity_NormalizesHostname(t *testing.T) {
}
}
+func TestEnrichK8sNodeIdentity_ExactHostnameMatchWinsOverAmbiguity(t *testing.T) {
+ hosts := []models.Host{
+ {ID: "h1", Hostname: "k8s-node1.local", MachineID: "machine-abc"},
+ {ID: "h2", Hostname: "k8s-node1.other", MachineID: "machine-def"},
+ }
+ identity := ResourceIdentity{Hostnames: []string{"k8s-node1.local"}}
+ enrichK8sNodeIdentity(&identity, "k8s-node1.local", hosts)
+
+ if identity.MachineID != "machine-abc" {
+ t.Fatalf("expected exact hostname match to enrich identity, got %q", identity.MachineID)
+ }
+}
+
+func TestEnrichK8sNodeIdentity_AmbiguousNormalizedHostnameSkipsEnrichment(t *testing.T) {
+ hosts := []models.Host{
+ {ID: "h1", Hostname: "k8s-node1.local", MachineID: "machine-abc"},
+ {ID: "h2", Hostname: "k8s-node1.other", MachineID: "machine-def"},
+ }
+ identity := ResourceIdentity{Hostnames: []string{"k8s-node1"}}
+ enrichK8sNodeIdentity(&identity, "k8s-node1", hosts)
+
+ if identity.MachineID != "" {
+ t.Fatalf("expected ambiguous normalized hostname to skip enrichment, got %q", identity.MachineID)
+ }
+ if len(identity.MACAddresses) != 0 {
+ t.Fatalf("expected no MAC enrichment on ambiguous normalized hostname, got %v", identity.MACAddresses)
+ }
+}
+
// ---------------------------------------------------------------------------
// End-to-end dedup scenario
// ---------------------------------------------------------------------------