Selaa lähdekoodia

fix(topology): stop duplicated hypervisor CT chips (#1379)

- Slice aliasing in the #1374 absorption rewrote the shared backing array; the last distnode (dist-pve-default-citadel-02) reached the overview twice and the frontend drew that host's CT grid twice. Absorption now builds a fresh slice and exposes TopoSemantics.AbsorbedDists; live.go filters with a fresh slice.
- PVE inventory keys resolving to the same host can no longer emit the distnode twice.
- Hypervisor guests never pin by their own saved position; they always follow the host grid.

Closes #1378
Nacho 1 kuukausi sitten
vanhempi
sitoutus
518d618a78

+ 6 - 3
app/src/components/topology/model.ts

@@ -1225,9 +1225,12 @@ export function buildTopologyModel({ routers, devices, wan, wireguard, distribut
   // posición real del chip tras el override.
   if (seedPositions) {
     for (const c of chips) {
-      // Los CTs de un host hipervisores con semilla NO se pinnean: siguen la
-      // posición del host (el grid ya se re-derivó alrededor de él arriba).
-      if (c.isCt && hypervisorHosts.has(c.hubId) && seedPositions[c.hubId]) continue
+      // Los CTs de un host hipervisor NO se pinnean nunca por su propia
+      // semilla: siguen la posición del host (el grid se deriva de él).
+      // Con la condición antigua (solo si el host tenía semilla), un CT con
+      // posición guardada y un host sin fijar se pintaba dos veces: el grid
+      // bajo el host y el pin de su semilla vieja (#1378).
+      if (c.isCt && hypervisorHosts.has(c.hubId)) continue
       const p = seedPositions[c.id]
       if (p) {
         c.x = p.x

+ 16 - 0
server-go/internal/adapters/live.go

@@ -3425,6 +3425,22 @@ func (l *Live) buildOverview(ctx context.Context) (*Overview, error) {
 	usteerAvailable := l.usteerAvailableCached()
 	dawnDetected := dawnDeprecatedFromPolled(polled)
 	topo := BuildTopoSemantics(routerList, devices, wgStats, distNodes, wan.Gateway, fdbEvidence, lldpEvidence, overrideUplinks) // SPEC-65 D65-3 + #1042/#1051/#1279/#1321
+	// #1374: retirar de la lista que ve el frontend los distnodes absorbidos
+	// como sombra de un switch gestionado. Slice NUEVO: filtrar in situ
+	// reescribe el backing array compartido y duplica el último (#1378).
+	if len(topo.AbsorbedDists) > 0 {
+		absorbed := map[string]bool{}
+		for _, id := range topo.AbsorbedDists {
+			absorbed[id] = true
+		}
+		filtered := make([]DistributionNode, 0, len(distNodes))
+		for _, dn := range distNodes {
+			if !absorbed[dn.ID] {
+				filtered = append(filtered, dn)
+			}
+		}
+		distNodes = filtered
+	}
 	fillUplinkSpeeds(polled, distNodes, topo.Links)
 	return &Overview{
 		Health:  computeHealth(routerList, adguard),

+ 4 - 0
server-go/internal/adapters/proxmox.go

@@ -451,6 +451,10 @@ func applyPVEInfra(devices []Device, dists []DistributionNode, inv *pveInventory
 			HostDeviceID: id, Name: n.Node, MacCount: ctCountByHost[id],
 			Source: "proxmox", Instance: n.Instance,
 		})
+		// #1378: dos claves de inventario pueden resolver al mismo host (IP
+		// repetida o IP+nombre); sin marcarlo aquí el distnode salía duplicado
+		// y el frontend dibujaba los CTs dos veces.
+		existingHost[id] = true
 	}
 	// #1053: retirar los nodos inferred absorbidos por un host PVE.
 	kept := dists[:0]

+ 33 - 0
server-go/internal/adapters/proxmox_test.go

@@ -627,3 +627,36 @@ func TestPVENoAbsorbeInferredConDesconocidos(t *testing.T) {
 		t.Fatalf("host AttachTo=%q (want sin cambios)", devices[0].AttachTo)
 	}
 }
+
+// #1378: dos claves de inventario que resuelven al mismo host (p. ej. dos
+// entradas nodeIPs con la misma IP, o IP + nombre) no deben emitir el
+// distnode hypervisor dos veces: el frontend dibujaba los CTs duplicados.
+func TestPVEHypervisorDistNodesNoDuplicaHostRepetido(t *testing.T) {
+	inv := &pveInventory{
+		ctByMAC: map[string]pveVM{
+			"BC:24:11:A4:9E:BB": {Name: "webs", Node: "citadel-02", Type: "lxc", Instance: "default"},
+		},
+		nodes: map[string]pveNode{
+			"default|citadel-02": {Instance: "default", Node: "citadel-02"},
+			"default|pve2":       {Instance: "default", Node: "pve2"},
+		},
+		nodeIPs: map[string][]string{
+			"default|citadel-02": {"192.168.1.101"},
+			"default|pve2":       {"192.168.1.101"}, // misma IP: mismo host físico
+		},
+	}
+	devices := []Device{
+		{ID: "e8-ff-1e-dd-c7-ed", MAC: "E8:FF:1E:DD:C7:ED", Name: "beelink-nas", IP: "192.168.1.101", RouterID: "switch16", Port: "lan8"},
+		{ID: "bc-24-11-a4-9e-bb", MAC: "BC:24:11:A4:9E:BB", Name: "webs"},
+	}
+	dists := applyPVEInfra(devices, nil, inv)
+	count := 0
+	for _, dn := range dists {
+		if dn.Kind == "hypervisor" && dn.HostDeviceID == "e8-ff-1e-dd-c7-ed" {
+			count++
+		}
+	}
+	if count != 1 {
+		t.Fatalf("distnodes hypervisor para el mismo host: %d (want 1): %+v", count, dists)
+	}
+}

+ 9 - 1
server-go/internal/adapters/topo_semantics.go

@@ -466,7 +466,10 @@ func BuildTopoSemantics(routers []Router, devices []Device, wg WireGuardStats, d
 	}
 	if len(switchUnit) > 0 {
 		absorbed := map[string]string{} // distnode -> switch que lo absorbe
-		kept := dists[:0]
+		// kept es un slice NUEVO: filtrar in situ (dists[:0]) reescribe el
+		// backing array del caller, que conserva su len original y acaba con
+		// el último elemento duplicado (#1378).
+		kept := make([]DistributionNode, 0, len(dists))
 		for _, dn := range dists {
 			hub := ""
 			if dn.Kind == "inferred" {
@@ -485,6 +488,11 @@ func BuildTopoSemantics(routers []Router, devices []Device, wg WireGuardStats, d
 		}
 		if len(absorbed) > 0 {
 			dists = kept
+			sem.AbsorbedDists = make([]string, 0, len(absorbed))
+			for id := range absorbed {
+				sem.AbsorbedDists = append(sem.AbsorbedDists, id)
+			}
+			sort.Strings(sem.AbsorbedDists)
 			for i := range online {
 				// Re-anclar al switch SOLO los clientes atribuidos a él: los
 				// atribuidos a otro router (p. ej. equipos de un switch NO

+ 8 - 0
server-go/internal/adapters/topo_semantics_test.go

@@ -762,4 +762,12 @@ func TestTopoSemanticsAbsorbeDistnodeSombra(t *testing.T) {
 	if fromOf["pc"] != "gw" {
 		t.Fatalf("pc cuelga de %q, want gw (cliente del switch tonto, no del gestionado)", fromOf["pc"])
 	}
+	// #1378: la absorción no debe corromper el slice del caller (aliasing):
+	// dists conserva su len y contenido; los absorbidos se exponen aparte.
+	if len(dists) != 1 || dists[0].ID != "dist-gw-lan1" {
+		t.Fatalf("dists del caller corrupto: %+v", dists)
+	}
+	if len(sem.AbsorbedDists) != 1 || sem.AbsorbedDists[0] != "dist-gw-lan1" {
+		t.Fatalf("AbsorbedDists: %+v", sem.AbsorbedDists)
+	}
 }

+ 5 - 0
server-go/internal/adapters/types.go

@@ -414,6 +414,11 @@ type TopoSemantics struct {
 	// El mapa lo dibuja bajo el nodo Internet en vez de como cliente LAN.
 	// Vacío si no hay coincidencia.
 	WanPeer string `json:"wanPeer,omitempty"`
+	// AbsorbedDists (#1374): ids de distnodes inferred absorbidos como sombra
+	// de un switch gestionado. El caller filtra su lista con un slice NUEVO:
+	// reescribir el backing array compartido duplicaba el último elemento
+	// (#1378).
+	AbsorbedDists []string `json:"absorbedDists,omitempty"`
 }
 
 // TopoLink es un enlace semántico del mapa (sin geometría).