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(models): extend namespace disambiguation to SyncGuestBackupTimes (#1095)

rcourtman1dda53826528
diff --git a/internal/alerts/alerts.go b/internal/alerts/alerts.go
index c8c8d778cb..509125c6bc 100644
--- a/internal/alerts/alerts.go
+++ b/internal/alerts/alerts.go
@@ -6229,9 +6229,18 @@ func namespaceMatchesInstance(namespace, instance string) bool {
return true
}
- // Check if namespace is contained in instance or vice versa
+ // Check if namespace is a suffix of instance
// e.g., namespace "nat" matches instance "pvenat" (normalized from "pve-nat")
- if strings.Contains(inst, ns) || strings.Contains(ns, inst) {
+ // This is more precise than substring matching because:
+ // - "nat" should match "pve-nat" but not "natpve"
+ // - "pve" should match "pve" but not "pve-nat" (handled by exact match above)
+ if strings.HasSuffix(inst, ns) {
+ return true
+ }
+
+ // Check if instance is a suffix of namespace (reverse case)
+ // e.g., namespace "pvebackups" could match instance "pve"
+ if strings.HasSuffix(ns, inst) {
return true
}
diff --git a/internal/alerts/alerts_test.go b/internal/alerts/alerts_test.go
index 1765c8261d..9aa30d1b97 100644
--- a/internal/alerts/alerts_test.go
+++ b/internal/alerts/alerts_test.go
@@ -15579,23 +15579,27 @@ func TestNamespaceMatchesInstance(t *testing.T) {
{"exact match", "pve", "pve", true},
{"exact match with numbers", "pve1", "pve1", true},
- // Partial matches (namespace in instance)
- {"namespace contained in instance", "nat", "pve-nat", true},
- {"namespace contained in instance no dash", "nat", "pvenat", true},
- {"longer namespace in instance", "production", "my-production-server", true},
+ // Suffix matches (namespace is suffix of instance)
+ {"namespace suffix of instance", "nat", "pve-nat", true},
+ {"namespace suffix of instance no dash", "nat", "pvenat", true},
- // Partial matches (instance in namespace)
- {"instance contained in namespace", "pve-backups", "pve", true},
+ // Suffix matches (instance is suffix of namespace)
+ {"instance suffix of namespace", "pvebackups", "pve", false}, // "pve" is not suffix of "pvebackups"
+ {"instance suffix of namespace 2", "backupspve", "pve", true}, // "pve" IS suffix of "backupspve"
// Case insensitive
{"case insensitive exact", "PVE", "pve", true},
- {"case insensitive partial", "NAT", "pve-nat", true},
+ {"case insensitive suffix", "NAT", "pve-nat", true},
// Special characters ignored
{"special chars in namespace", "pve_nat", "pvenat", true},
{"special chars in instance", "pvenat", "pve-nat", true},
{"both have special chars", "pve-1", "pve_1", true},
+ // No matches - substring but not suffix
+ {"no match substring not suffix", "production", "my-production-server", false}, // "production" is not suffix of "myproductionserver"
+ {"no match pve not suffix of pvenat", "pve", "pve-nat", false}, // "pve" is not suffix of "pvenat"
+
// No matches
{"no match", "production", "staging", false},
{"no match different names", "pve1", "pve2", false},
diff --git a/internal/models/models.go b/internal/models/models.go
index f642941c67..e735808bf5 100644
--- a/internal/models/models.go
+++ b/internal/models/models.go
@@ -1428,9 +1428,59 @@ func backupKey(instance string, vmid int) string {
return instance + "-" + strconv.Itoa(vmid)
}
+// namespaceMatchesInstance checks if a PBS namespace likely corresponds to a PVE instance.
+// This helps disambiguate backups when multiple PVE instances have VMs with the same VMID.
+// Examples: namespace "pve1" matches instance "pve1", namespace "nat" matches instance "pve-nat"
+func namespaceMatchesInstance(namespace, instance string) bool {
+ if namespace == "" || instance == "" {
+ return false
+ }
+
+ // Normalize both strings: lowercase and keep only alphanumeric
+ normalize := func(s string) string {
+ var b strings.Builder
+ for _, r := range strings.ToLower(s) {
+ if (r >= 'a' && r <= 'z') || (r >= '0' && r <= '9') {
+ b.WriteRune(r)
+ }
+ }
+ return b.String()
+ }
+
+ ns := normalize(namespace)
+ inst := normalize(instance)
+
+ if ns == "" || inst == "" {
+ return false
+ }
+
+ // Exact match after normalization
+ if ns == inst {
+ return true
+ }
+
+ // Check if namespace is a suffix of instance
+ // e.g., namespace "nat" matches instance "pvenat" (normalized from "pve-nat")
+ // This is more precise than substring matching because:
+ // - "nat" should match "pve-nat" but not "natpve"
+ // - "pve" should match "pve" but not "pve-nat" (handled by exact match above)
+ if strings.HasSuffix(inst, ns) {
+ return true
+ }
+
+ // Check if instance is a suffix of namespace (reverse case)
+ // e.g., namespace "pvebackups" could match instance "pve"
+ if strings.HasSuffix(ns, inst) {
+ return true
+ }
+
+ return false
+}
+
// SyncGuestBackupTimes updates LastBackup on VMs and Containers from storage backups and PBS backups.
// Call this after updating storage backups or PBS backups to ensure guest backup indicators are accurate.
// Matching is done by instance+VMID to prevent cross-instance VMID collisions.
+// For PBS backups with namespaces, namespace matching is used to disambiguate.
func (s *State) SyncGuestBackupTimes() {
s.mu.Lock()
defer s.mu.Unlock()
@@ -1451,43 +1501,77 @@ func (s *State) SyncGuestBackupTimes() {
}
// Process PBS backups (VMID is string, BackupTime is the timestamp)
- // Note: PBS backups use Instance field to identify the PBS server, but backups
- // from different PVE instances can be stored in the same PBS.
- // For PBS, we need to match against all guests since we can't reliably determine
- // which PVE instance the backup belongs to.
- pbsLatestByVMID := make(map[int]time.Time)
+ // PBS backups can have a Namespace field that often corresponds to the PVE instance.
+ // We use namespace matching to associate PBS backups with the correct PVE instance.
+ // Structure: map[vmid][]PBSBackup to handle multiple backups per VMID
+ pbsBackupsByVMID := make(map[int][]PBSBackup)
for _, backup := range s.PBSBackups {
vmid, err := strconv.Atoi(backup.VMID)
if err != nil || vmid <= 0 {
continue
}
- if existing, ok := pbsLatestByVMID[vmid]; !ok || backup.BackupTime.After(existing) {
- pbsLatestByVMID[vmid] = backup.BackupTime
+ pbsBackupsByVMID[vmid] = append(pbsBackupsByVMID[vmid], backup)
+ }
+
+ // findBestPBSBackup finds the most recent PBS backup for a given VMID and instance.
+ // If the backup has a namespace that matches the instance, it's preferred.
+ // Returns zero time if no suitable backup found.
+ findBestPBSBackup := func(vmid int, instance string) time.Time {
+ backups, ok := pbsBackupsByVMID[vmid]
+ if !ok || len(backups) == 0 {
+ return time.Time{}
}
+
+ var bestTime time.Time
+ var bestMatchTime time.Time // Best time among namespace-matched backups
+
+ for _, backup := range backups {
+ // If namespace matches this instance, track it separately
+ if backup.Namespace != "" && namespaceMatchesInstance(backup.Namespace, instance) {
+ if backup.BackupTime.After(bestMatchTime) {
+ bestMatchTime = backup.BackupTime
+ }
+ }
+ // Track overall best time as fallback
+ if backup.BackupTime.After(bestTime) {
+ bestTime = backup.BackupTime
+ }
+ }
+
+ // Prefer namespace-matched backup if available
+ if !bestMatchTime.IsZero() {
+ return bestMatchTime
+ }
+
+ // Fall back to any backup with this VMID (original behavior for backwards compat)
+ // This handles cases where namespaces aren't used or don't match
+ return bestTime
}
- // Update VMs - prefer instance-specific PVE backup, fall back to PBS by VMID
+ // Update VMs - prefer instance-specific PVE backup, fall back to PBS
for i := range s.VMs {
key := backupKey(s.VMs[i].Instance, s.VMs[i].VMID)
if backupTime, ok := latestBackup[key]; ok {
s.VMs[i].LastBackup = backupTime
}
// Check if PBS has a more recent backup
- if pbsTime, ok := pbsLatestByVMID[s.VMs[i].VMID]; ok {
+ pbsTime := findBestPBSBackup(s.VMs[i].VMID, s.VMs[i].Instance)
+ if !pbsTime.IsZero() {
if s.VMs[i].LastBackup.IsZero() || pbsTime.After(s.VMs[i].LastBackup) {
s.VMs[i].LastBackup = pbsTime
}
}
}
- // Update Containers - prefer instance-specific PVE backup, fall back to PBS by VMID
+ // Update Containers - prefer instance-specific PVE backup, fall back to PBS
for i := range s.Containers {
key := backupKey(s.Containers[i].Instance, s.Containers[i].VMID)
if backupTime, ok := latestBackup[key]; ok {
s.Containers[i].LastBackup = backupTime
}
// Check if PBS has a more recent backup
- if pbsTime, ok := pbsLatestByVMID[s.Containers[i].VMID]; ok {
+ pbsTime := findBestPBSBackup(s.Containers[i].VMID, s.Containers[i].Instance)
+ if !pbsTime.IsZero() {
if s.Containers[i].LastBackup.IsZero() || pbsTime.After(s.Containers[i].LastBackup) {
s.Containers[i].LastBackup = pbsTime
}
diff --git a/internal/models/state_host_test.go b/internal/models/state_host_test.go
index 264c028b8c..4ef27d9172 100644
--- a/internal/models/state_host_test.go
+++ b/internal/models/state_host_test.go
@@ -866,6 +866,78 @@ func TestSyncGuestBackupTimesCrossInstance(t *testing.T) {
}
}
+// TestSyncGuestBackupTimesNamespaceDisambiguation verifies that PBS namespace is used to
+// disambiguate backups when multiple VMs have the same VMID across different PVE instances.
+// This addresses issue #1095 where users have multiple PVE instances with overlapping VMIDs.
+func TestSyncGuestBackupTimesNamespaceDisambiguation(t *testing.T) {
+ state := NewState()
+
+ now := time.Now()
+ pveBackupTime := now.Add(-1 * time.Hour)
+ pveNatBackupTime := now.Add(-2 * time.Hour)
+
+ // Set up VMs with the SAME VMID on DIFFERENT instances
+ state.UpdateVMs([]VM{
+ {VMID: 100, Name: "webserver-pve", Instance: "pve", Node: "node1"},
+ {VMID: 100, Name: "webserver-nat", Instance: "pve-nat", Node: "node2"},
+ })
+
+ // Add PBS backups with namespaces that correspond to the PVE instances
+ state.mu.Lock()
+ state.PBSBackups = []PBSBackup{
+ {
+ ID: "pbs-pve-100",
+ VMID: "100",
+ Namespace: "pve",
+ BackupType: "vm",
+ BackupTime: pveBackupTime,
+ Instance: "pbs-main",
+ },
+ {
+ ID: "pbs-nat-100",
+ VMID: "100",
+ Namespace: "nat", // Should match "pve-nat" instance
+ BackupType: "vm",
+ BackupTime: pveNatBackupTime,
+ Instance: "pbs-main",
+ },
+ }
+ state.mu.Unlock()
+
+ // Sync backup times
+ state.SyncGuestBackupTimes()
+
+ snapshot := state.GetSnapshot()
+
+ // Find both VMs
+ var vmPVE, vmNAT *VM
+ for i := range snapshot.VMs {
+ if snapshot.VMs[i].Instance == "pve" && snapshot.VMs[i].VMID == 100 {
+ vmPVE = &snapshot.VMs[i]
+ }
+ if snapshot.VMs[i].Instance == "pve-nat" && snapshot.VMs[i].VMID == 100 {
+ vmNAT = &snapshot.VMs[i]
+ }
+ }
+
+ if vmPVE == nil {
+ t.Fatal("pve VM not found")
+ }
+ if vmNAT == nil {
+ t.Fatal("pve-nat VM not found")
+ }
+
+ // The pve VM should have the backup with namespace "pve"
+ if !vmPVE.LastBackup.Equal(pveBackupTime) {
+ t.Errorf("pve VM LastBackup = %v, expected %v (from namespace 'pve')", vmPVE.LastBackup, pveBackupTime)
+ }
+
+ // The pve-nat VM should have the backup with namespace "nat"
+ if !vmNAT.LastBackup.Equal(pveNatBackupTime) {
+ t.Errorf("pve-nat VM LastBackup = %v, expected %v (from namespace 'nat')", vmNAT.LastBackup, pveNatBackupTime)
+ }
+}
+
func TestUpdateStorageBackupsForInstance(t *testing.T) {
state := NewState()