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.

feat: automatic subnet preference for cluster node discovery

rcourtmanbd1df9f9423f
diff --git a/internal/api/config_handlers.go b/internal/api/config_handlers.go
index 2d481578b5..7dd2cfd0d1 100644
--- a/internal/api/config_handlers.go
+++ b/internal/api/config_handlers.go
@@ -664,6 +664,81 @@ func findExistingGuestURL(nodeName string, existingEndpoints []config.ClusterEnd
return ""
}
+// findExistingIPOverride looks up the IPOverride for a node from existing endpoints
+func findExistingIPOverride(nodeName string, existingEndpoints []config.ClusterEndpoint) string {
+ for _, ep := range existingEndpoints {
+ if ep.NodeName == nodeName {
+ return ep.IPOverride
+ }
+ }
+ return ""
+}
+
+// extractSubnetFromHost extracts the network subnet from a host URL.
+// For example, "https://10.1.1.5:8006" returns a *net.IPNet for 10.1.1.0/24 (assumes /24 for IPv4).
+func extractSubnetFromHost(host string) *net.IPNet {
+ // Parse the URL to get the hostname/IP
+ parsed, err := url.Parse(host)
+ if err != nil {
+ return nil
+ }
+
+ hostname := parsed.Hostname()
+ if hostname == "" {
+ hostname = host
+ }
+
+ // Try to parse as IP
+ ip := net.ParseIP(hostname)
+ if ip == nil {
+ // Try resolving hostname
+ ips, err := net.LookupIP(hostname)
+ if err != nil || len(ips) == 0 {
+ return nil
+ }
+ ip = ips[0]
+ }
+
+ // Create subnet (assume /24 for IPv4, /64 for IPv6)
+ if ip4 := ip.To4(); ip4 != nil {
+ return &net.IPNet{
+ IP: ip4.Mask(net.CIDRMask(24, 32)),
+ Mask: net.CIDRMask(24, 32),
+ }
+ }
+ if ip6 := ip.To16(); ip6 != nil {
+ return &net.IPNet{
+ IP: ip6.Mask(net.CIDRMask(64, 128)),
+ Mask: net.CIDRMask(64, 128),
+ }
+ }
+ return nil
+}
+
+// findPreferredIP looks through a list of node network interfaces and returns
+// an IP that matches the preferred subnet. Returns empty string if no match found.
+func findPreferredIP(interfaces []proxmox.NodeNetworkInterface, preferredSubnet *net.IPNet) string {
+ if preferredSubnet == nil {
+ return ""
+ }
+
+ for _, iface := range interfaces {
+ // Skip inactive interfaces
+ if iface.Active != 1 {
+ continue
+ }
+
+ // Check IPv4 address
+ if iface.Address != "" {
+ ip := net.ParseIP(iface.Address)
+ if ip != nil && preferredSubnet.Contains(ip) {
+ return iface.Address
+ }
+ }
+ }
+ return ""
+}
+
var detectPVECluster = defaultDetectPVECluster
// detectPVECluster checks if a PVE node is part of a cluster and returns cluster information
@@ -737,6 +812,16 @@ func defaultDetectPVECluster(clientConfig proxmox.ClientConfig, nodeName string,
scheme, defaultPort := deriveSchemeAndPort(clientConfig.Host)
schemePrefix := scheme + "://"
+ // Extract the preferred subnet from the initial connection
+ // This allows us to prefer management network IPs over internal cluster IPs
+ preferredSubnet := extractSubnetFromHost(clientConfig.Host)
+ if preferredSubnet != nil {
+ log.Debug().
+ Str("subnet", preferredSubnet.String()).
+ Str("from_host", clientConfig.Host).
+ Msg("Extracted preferred subnet from initial connection")
+ }
+
var unvalidatedNodes []proxmox.ClusterStatus
for _, clusterNode := range clusterNodes {
@@ -759,7 +844,8 @@ func defaultDetectPVECluster(clientConfig proxmox.ClientConfig, nodeName string,
NodeID: clusterNode.ID,
NodeName: clusterNode.Name,
GuestURL: findExistingGuestURL(clusterNode.Name, existingEndpoints),
- Fingerprint: nodeFingerprint, // Store captured fingerprint for per-node TLS verification
+ IPOverride: findExistingIPOverride(clusterNode.Name, existingEndpoints), // Preserve user override
+ Fingerprint: nodeFingerprint, // Store captured fingerprint for per-node TLS verification
Online: clusterNode.Online == 1,
LastSeen: time.Now(),
}
@@ -770,11 +856,42 @@ func defaultDetectPVECluster(clientConfig proxmox.ClientConfig, nodeName string,
endpoint.Host = schemePrefix + nodeHost
}
- // Populate IP field separately for DNS-free connections
+ // Populate IP field with cluster-reported IP (may be internal network)
if clusterNode.IP != "" {
endpoint.IP = clusterNode.IP
}
+ // Try to find a better IP on the preferred subnet (management network)
+ // Only do this if no manual override is set
+ if endpoint.IPOverride == "" && preferredSubnet != nil && clusterNode.IP != "" {
+ // Check if cluster-reported IP is already on the preferred subnet
+ clusterIP := net.ParseIP(clusterNode.IP)
+ if clusterIP != nil && !preferredSubnet.Contains(clusterIP) {
+ // Cluster IP is on a different subnet, try to find one on preferred subnet
+ ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
+ nodeInterfaces, err := tempClient.GetNodeNetworkInterfaces(ctx, clusterNode.Name)
+ cancel()
+
+ if err == nil {
+ preferredIP := findPreferredIP(nodeInterfaces, preferredSubnet)
+ if preferredIP != "" && preferredIP != clusterNode.IP {
+ log.Info().
+ Str("node", clusterNode.Name).
+ Str("cluster_ip", clusterNode.IP).
+ Str("preferred_ip", preferredIP).
+ Str("subnet", preferredSubnet.String()).
+ Msg("Found preferred management IP for cluster node")
+ endpoint.IPOverride = preferredIP
+ }
+ } else {
+ log.Debug().
+ Err(err).
+ Str("node", clusterNode.Name).
+ Msg("Could not query node network interfaces for subnet preference")
+ }
+ }
+ }
+
clusterEndpoints = append(clusterEndpoints, endpoint)
}
diff --git a/pkg/proxmox/client.go b/pkg/proxmox/client.go
index 6e5d60c284..9b5576678b 100644
--- a/pkg/proxmox/client.go
+++ b/pkg/proxmox/client.go
@@ -841,6 +841,42 @@ type ContainerInterface struct {
IPAddresses []ContainerInterfaceAddress `json:"ip-addresses,omitempty"`
}
+// NodeNetworkInterface describes a network interface on a Proxmox node.
+type NodeNetworkInterface struct {
+ Iface string `json:"iface"` // Interface name (e.g., "eth0", "vmbr0")
+ Type string `json:"type"` // Type (e.g., "eth", "bridge", "bond")
+ Address string `json:"address,omitempty"` // IPv4 address
+ Address6 string `json:"address6,omitempty"` // IPv6 address
+ Netmask string `json:"netmask,omitempty"` // IPv4 netmask
+ CIDR string `json:"cidr,omitempty"` // CIDR notation (e.g., "10.1.1.5/24")
+ Active int `json:"active"` // 1 if active
+}
+
+// GetNodeNetworkInterfaces returns the network interfaces configured on a Proxmox node.
+// This can be used to find all IPs available on a node for connection purposes.
+func (c *Client) GetNodeNetworkInterfaces(ctx context.Context, node string) ([]NodeNetworkInterface, error) {
+ resp, err := c.get(ctx, fmt.Sprintf("/nodes/%s/network", node))
+ if err != nil {
+ return nil, err
+ }
+ defer resp.Body.Close()
+
+ if resp.StatusCode != http.StatusOK {
+ body, _ := io.ReadAll(resp.Body)
+ return nil, fmt.Errorf("failed to get node network interfaces (status %d): %s", resp.StatusCode, strings.TrimSpace(string(body)))
+ }
+
+ var result struct {
+ Data []NodeNetworkInterface `json:"data"`
+ }
+
+ if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
+ return nil, err
+ }
+
+ return result.Data, nil
+}
+
// GetContainerConfig returns the configuration of a specific container
func (c *Client) GetContainerConfig(ctx context.Context, node string, vmid int) (map[string]interface{}, error) {
resp, err := c.get(ctx, fmt.Sprintf("/nodes/%s/lxc/%d/config", node, vmid))