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.

fix: detect duplicate nodes by IP resolution during agent auto-register. Related to #924

rcourtman1dff90817fc4
diff --git a/internal/api/config_handlers.go b/internal/api/config_handlers.go
index d89314d5c6..a975f511c5 100644
--- a/internal/api/config_handlers.go
+++ b/internal/api/config_handlers.go
@@ -1175,6 +1175,58 @@ func normalizeNodeHost(rawHost, nodeType string) (string, error) {
return parsed.String(), nil
}
+// extractHostIP extracts the IP address from a host URL if it's an IP-based URL.
+// Returns empty string if the URL uses a hostname instead of an IP.
+func extractHostIP(hostURL string) string {
+ parsed, err := url.Parse(hostURL)
+ if err != nil {
+ return ""
+ }
+ hostname := parsed.Hostname()
+ if hostname == "" {
+ return ""
+ }
+ // Check if hostname is an IP address
+ if ip := net.ParseIP(hostname); ip != nil {
+ return ip.String()
+ }
+ return ""
+}
+
+// resolveHostnameToIP attempts to resolve a hostname URL to its first IP address.
+// Returns empty string if resolution fails or times out.
+func resolveHostnameToIP(hostURL string) string {
+ parsed, err := url.Parse(hostURL)
+ if err != nil {
+ return ""
+ }
+ hostname := parsed.Hostname()
+ if hostname == "" {
+ return ""
+ }
+
+ // Don't try to resolve if it's already an IP
+ if ip := net.ParseIP(hostname); ip != nil {
+ return ip.String()
+ }
+
+ // Resolve with a short timeout to avoid blocking
+ ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
+ defer cancel()
+
+ var resolver net.Resolver
+ addrs, err := resolver.LookupHost(ctx, hostname)
+ if err != nil || len(addrs) == 0 {
+ log.Debug().
+ Str("hostname", hostname).
+ Err(err).
+ Msg("Failed to resolve hostname for duplicate detection")
+ return ""
+ }
+
+ return addrs[0]
+}
+
// disambiguateNodeName ensures a node name is unique by appending the host IP if needed.
// This handles cases where multiple Proxmox hosts have the same hostname (e.g., "px1" on different networks).
// Returns the original name if unique, or "name (ip)" if duplicates exist.
@@ -5958,8 +6010,12 @@ func (h *ConfigHandlers) HandleAutoRegister(w http.ResponseWriter, r *http.Reque
// Also match by name+tokenID for DHCP scenarios where IP changed but it's the same host.
// Different physical hosts can have the same hostname (e.g., "px1" on different networks)
// but they'll have different tokens, so we only merge if BOTH name AND token match.
- // See: Issue #891, #104, and multiple fix attempts in Dec 2025.
+ // See: Issue #891, #104, #924, and multiple fix attempts in Dec 2025.
existingIndex := -1
+
+ // Extract IP from the new host URL for DNS comparison
+ newHostIP := extractHostIP(host)
+
if req.Type == "pve" {
for i, node := range h.config.PVEInstances {
if node.Host == host {
@@ -5978,6 +6034,26 @@ func (h *ConfigHandlers) HandleAutoRegister(w http.ResponseWriter, r *http.Reque
Msg("Detected IP change for existing node - updating host")
break
}
+ // Agent registration: check if existing hostname resolves to the new IP
+ // This catches the case where a node was manually added by hostname and
+ // then the agent registers using the IP address. (Issue #924)
+ if req.Source == "agent" && newHostIP != "" {
+ existingHostIP := extractHostIP(node.Host)
+ if existingHostIP == "" {
+ // Existing config uses hostname, try to resolve it
+ existingHostIP = resolveHostnameToIP(node.Host)
+ }
+ if existingHostIP == newHostIP {
+ existingIndex = i
+ log.Info().
+ Str("existingHost", node.Host).
+ Str("newHost", host).
+ Str("resolvedIP", newHostIP).
+ Str("node", node.Name).
+ Msg("Agent registration detected existing node by IP resolution - updating instead of creating duplicate")
+ break
+ }
+ }
}
} else {
for i, node := range h.config.PBSInstances {
@@ -5995,6 +6071,23 @@ func (h *ConfigHandlers) HandleAutoRegister(w http.ResponseWriter, r *http.Reque
Msg("Detected IP change for existing node - updating host")
break
}
+ // Agent registration: check if existing hostname resolves to the new IP
+ if req.Source == "agent" && newHostIP != "" {
+ existingHostIP := extractHostIP(node.Host)
+ if existingHostIP == "" {
+ existingHostIP = resolveHostnameToIP(node.Host)
+ }
+ if existingHostIP == newHostIP {
+ existingIndex = i
+ log.Info().
+ Str("existingHost", node.Host).
+ Str("newHost", host).
+ Str("resolvedIP", newHostIP).
+ Str("node", node.Name).
+ Msg("Agent registration detected existing node by IP resolution - updating instead of creating duplicate")
+ break
+ }
+ }
}
}
diff --git a/internal/api/config_handlers_auto_register_test.go b/internal/api/config_handlers_auto_register_test.go
index e055c39b2e..41fd7d8226 100644
--- a/internal/api/config_handlers_auto_register_test.go
+++ b/internal/api/config_handlers_auto_register_test.go
@@ -264,3 +264,47 @@ func TestAutoRegisterDuplicateHostnameSeparateNodes(t *testing.T) {
t.Errorf("second node name = %q, want %q (should be disambiguated)", node2.Name, "px1 (10.0.2.224)")
}
}
+
+// TestExtractHostIP verifies the IP extraction from host URLs.
+func TestExtractHostIP(t *testing.T) {
+ tests := []struct {
+ name string
+ hostURL string
+ expected string
+ }{
+ {
+ name: "IP-based URL",
+ hostURL: "https://192.168.1.100:8006",
+ expected: "192.168.1.100",
+ },
+ {
+ name: "hostname URL returns empty",
+ hostURL: "https://pve.local:8006",
+ expected: "",
+ },
+ {
+ name: "IPv6 URL",
+ hostURL: "https://[::1]:8006",
+ expected: "::1",
+ },
+ {
+ name: "empty URL",
+ hostURL: "",
+ expected: "",
+ },
+ {
+ name: "invalid URL",
+ hostURL: "not-a-url",
+ expected: "",
+ },
+ }
+
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ got := extractHostIP(tt.hostURL)
+ if got != tt.expected {
+ t.Errorf("extractHostIP(%q) = %q, want %q", tt.hostURL, got, tt.expected)
+ }
+ })
+ }
+}