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.

Keep guest configuration reads bound to canonical identity

pulse-triage[bot]0f049a327acd
diff --git a/docs/release-control/v6/internal/subsystems/ai-runtime.md b/docs/release-control/v6/internal/subsystems/ai-runtime.md
index a9bf1695ef..fa473ad85e 100644
--- a/docs/release-control/v6/internal/subsystems/ai-runtime.md
+++ b/docs/release-control/v6/internal/subsystems/ai-runtime.md
@@ -25,6 +25,22 @@ that same result. Successful reads retain their content and execution provenance
## Purpose
+### Guest-config tools require an unambiguous resource identity
+
+The existing VM/system-container configuration tool selects an exact canonical
+resource ID before considering a name or bare VMID. A name/VMID matching more
+than one guest or a target lacking complete provider placement returns a useful
+error without invoking the config provider. Successful configuration and its
+governance metadata come directly from the selected typed guest view, never a
+second name-based resolution. Unique aliases remain supported; app-container configuration,
+command authority and provider permissions are unchanged.
+
+`TestGuestConfigToolIdentity` covers both inventory orders, shared names/VMIDs,
+canonical-ID/name collisions, unique aliases and missing/invalid placement at
+the actual tool/provider boundary. It checks config attribution and no provider
+read on refusal. Source controls are not installed multi-instance recovery.
+
+
### Permanent suppression rules remain reversible — issue #2528
The existing manual suppression API/store semantics remain unchanged. Patrol
diff --git a/docs/release-control/v6/internal/subsystems/monitoring.md b/docs/release-control/v6/internal/subsystems/monitoring.md
index eed66727d1..2d8f03230c 100644
--- a/docs/release-control/v6/internal/subsystems/monitoring.md
+++ b/docs/release-control/v6/internal/subsystems/monitoring.md
@@ -17,6 +17,23 @@
## Purpose
+### Guest configuration reads preserve installation and node identity
+
+Resolving omitted placement for the existing guest-config read honours every
+supplied instance/node constraint and requires one distinct current placement.
+A bare VMID or node/VMID shared by installations or missing placement returns an error without reading a provider. Current canonical
+inventory, including an empty inventory, takes precedence over stale state.
+Explicit complete placement still reads that exact provider and preserves its
+original error. This read neither dispatches QGA nor changes operation locks,
+backup policy, guest-command admission or permissions.
+
+`TestGuestConfigPlacement*` in `monitor_additional_test.go` verifies VM/LXC
+placement constraints, both inventory orders, zero-provider ambiguous/missing
+reads, canonical versus stale state, direct/provider-failure preservation and
+concurrent snapshot publication. These are synthetic config-read controls, not
+native recovery or a diagnosis of #2681.
+
+
### Automatic system-mount exclusions respect directory boundaries
The shared filesystem filter excludes a named system directory and its
diff --git a/docs/release-control/v6/internal/subsystems/unified-resources.md b/docs/release-control/v6/internal/subsystems/unified-resources.md
index 136aeb4895..73ee178432 100644
--- a/docs/release-control/v6/internal/subsystems/unified-resources.md
+++ b/docs/release-control/v6/internal/subsystems/unified-resources.md
@@ -23,6 +23,17 @@ and sort the complete canonical change table while startup and ingestion wait.
## Purpose
+### Guest views retain exact governance identity for configuration reads
+
+VM and system-container views expose cloned canonical policy/AI-safe metadata
+for their own resource. This lets guest-config reads retain the chosen identity
+when names repeat across installations without a second name-based resolution.
+The accessor exposes no raw resource or mutation; classification/redaction rules
+are unchanged. `TestGuestViewGovernanceIdentity` checks distinct same-name guests,
+policy cloning and nil-view handling. Tool controls check its use at the provider
+boundary; these are not native installation recovery.
+
+
### Native guest admission replaces the previous canonical facet
A complete native Proxmox guest observation (non-zero VMID) owns its filesystem
diff --git a/internal/ai/tools/guest_config_identity_test.go b/internal/ai/tools/guest_config_identity_test.go
new file mode 100644
index 0000000000..abd3e2f0a8
--- /dev/null
+++ b/internal/ai/tools/guest_config_identity_test.go
@@ -0,0 +1,125 @@
+package tools
+
+import (
+ "context"
+ "encoding/json"
+ "strings"
+ "testing"
+
+ "github.com/rcourtman/pulse-go-rewrite/internal/models"
+ "github.com/rcourtman/pulse-go-rewrite/internal/unifiedresources"
+)
+
+type identityGuestConfigProvider struct {
+ mockGuestConfigProvider
+ calls int
+}
+
+func (p *identityGuestConfigProvider) GetGuestConfig(kind, instance, node string, vmid int) (map[string]interface{}, error) {
+ p.calls++
+ return p.mockGuestConfigProvider.GetGuestConfig(kind, instance, node, vmid)
+}
+
+func TestGuestConfigToolIdentity(t *testing.T) {
+ for _, kind := range []string{"vm", "system-container"} {
+ for _, reverse := range []bool{false, true} {
+ for _, mode := range []string{"VMID collision", "name collision", "canonical ID", "ID shadows name", "unique VMID", "unique name", "missing", "missing node", "missing instance", "invalid VMID"} {
+ t.Run(kind+"/"+mode+"/reverse="+map[bool]string{true: "yes", false: "no"}[reverse], func(t *testing.T) {
+ vms := []models.VM{{ID: "first-id", VMID: 105, Name: "duplicate", Instance: "first", Node: "a"}, {ID: "second-id", VMID: 105, Name: "duplicate", Instance: "second", Node: "b", Tags: []string{"customer-data"}}}
+ ref, failure := "second-id", ""
+ switch mode {
+ case "VMID collision":
+ ref, failure = "105", "ambiguous"
+ case "name collision":
+ ref, failure = "duplicate", "ambiguous"
+ case "ID shadows name":
+ // Set after canonicalisation below, not to a source-only ID.
+ case "unique VMID":
+ vms[1].VMID, ref = 106, "106"
+ case "unique name":
+ vms[1].Name, ref = "selected", "selected"
+ case "missing":
+ ref, failure = "absent", "not found"
+ case "missing node":
+ vms[1].Node, failure = "", "placement is unavailable"
+ case "missing instance":
+ vms[1].Instance, failure = "", "placement is unavailable"
+ case "invalid VMID":
+ vms[1].VMID, failure = 0, "placement is unavailable"
+ }
+ wantVMID, wantName := vms[1].VMID, vms[1].Name
+ if reverse {
+ vms[0], vms[1] = vms[1], vms[0]
+ }
+ state := models.StateSnapshot{}
+ if kind == "vm" {
+ state.VMs = vms
+ } else {
+ for _, vm := range vms {
+ state.Containers = append(state.Containers, models.Container{ID: vm.ID, VMID: vm.VMID, Name: vm.Name, Instance: vm.Instance, Node: vm.Node, Tags: vm.Tags})
+ }
+ }
+ registry := unifiedresources.NewRegistry(nil)
+ registry.IngestSnapshot(state)
+ selectedID, selectedSummary := "", ""
+ var selectedPolicy *unifiedresources.ResourcePolicy
+ if kind == "vm" {
+ for _, vm := range registry.VMs() {
+ if vm.SourceID() == "second-id" {
+ selectedID = vm.ID()
+ selectedPolicy, selectedSummary = vm.GovernanceMetadata()
+ }
+ }
+ } else {
+ for _, ct := range registry.Containers() {
+ if ct.SourceID() == "second-id" {
+ selectedID = ct.ID()
+ selectedPolicy, selectedSummary = ct.GovernanceMetadata()
+ }
+ }
+ }
+ if selectedID == "" {
+ t.Fatal("selected canonical fixture is absent")
+ }
+ if ref == "second-id" {
+ ref = selectedID
+ }
+ if mode == "ID shadows name" {
+ for i := range state.VMs {
+ if state.VMs[i].ID == "first-id" {
+ state.VMs[i].Name = selectedID
+ }
+ }
+ for i := range state.Containers {
+ if state.Containers[i].ID == "first-id" {
+ state.Containers[i].Name = selectedID
+ }
+ }
+ }
+ provider := &identityGuestConfigProvider{mockGuestConfigProvider: mockGuestConfigProvider{config: map[string]interface{}{"ostype": "fixture"}}}
+ executor := NewPulseToolExecutor(ExecutorConfig{StateProvider: &mockStateProvider{state: state}, GuestConfigProvider: provider})
+ result, err := executor.executeGetResourceConfig(context.Background(), map[string]interface{}{"resource_type": kind, "resource_id": ref})
+ if err != nil {
+ t.Fatal(err)
+ }
+ if failure != "" {
+ if !result.IsError || !strings.Contains(result.Content[0].Text, failure) || provider.calls != 0 {
+ t.Fatalf("ambiguous/incomplete target reached provider: calls=%d result=%+v", provider.calls, result)
+ }
+ return
+ }
+ if result.IsError || provider.calls != 1 || provider.lastInstance != "second" || provider.lastNode != "b" || provider.lastVMID != wantVMID {
+ t.Fatalf("wrong provider target: %+v result=%+v", provider, result)
+ }
+ var response GuestConfigResponse
+ if err := json.Unmarshal([]byte(result.Content[0].Text), &response); err != nil {
+ t.Fatal(err)
+ }
+ if response.Instance != "second" || response.Node != "b" || response.VMID != wantVMID || response.Name != wantName || response.OSType != "fixture" || response.Policy == nil || response.Policy.Sensitivity != selectedPolicy.Sensitivity || response.AISafeSummary != selectedSummary {
+ t.Fatalf("wrong config attribution: %+v", response)
+ }
+ })
+ }
+ }
+ }
+}
diff --git a/internal/ai/tools/tools_query.go b/internal/ai/tools/tools_query.go
index 04762d047b..dcc46b4f27 100644
--- a/internal/ai/tools/tools_query.go
+++ b/internal/ai/tools/tools_query.go
@@ -5254,44 +5254,34 @@ func (e *PulseToolExecutor) executeGetResourceConfig(ctx context.Context, args m
return NewTextResult("Guest configuration not available."), nil
}
- var (
- guestType string
- vmID int
- name string
- node string
- instance string
- )
- var err error
rs, err := e.readStateForControl()
if err != nil {
return NewTextResult("State information not available."), nil
}
- governance := newGovernedQueryMetadataResolver(rs)
- guestType, vmID, name, node, instance, err = resolveGuestFromReadState(rs, resourceType, resourceID)
+ target, err := resolveGuestFromReadState(rs, resourceType, resourceID)
if err != nil {
return NewErrorResult(err), nil
}
-
// Normalize semantic type to provider-level type for guest config lookup.
// GetGuestConfig expects "container" or "vm", not "system-container".
- configType := guestType
+ configType := target.kind
if configType == "system-container" {
configType = "container"
}
- rawConfig, err := e.guestConfigProvider.GetGuestConfig(configType, instance, node, vmID)
+ rawConfig, err := e.guestConfigProvider.GetGuestConfig(configType, target.instance, target.node, target.vmid)
if err != nil {
return NewErrorResult(err), nil
}
response := EmptyGuestConfigResponse()
- response.GovernedResourceMetadata = governance.Resolve(name, fmt.Sprintf("%d", vmID))
- response.GuestType = guestType
- response.VMID = vmID
- response.Name = name
- response.Node = node
- response.Instance = instance
-
- switch guestType {
+ response.GovernedResourceMetadata = target.metadata
+ response.GuestType = target.kind
+ response.VMID = target.vmid
+ response.Name = target.name
+ response.Node = target.node
+ response.Instance = target.instance
+
+ switch target.kind {
case "system-container":
hostname, osType, onboot, rootfs, mounts := parseContainerConfig(rawConfig)
response.Hostname = hostname
@@ -5305,7 +5295,7 @@ func (e *PulseToolExecutor) executeGetResourceConfig(ctx context.Context, args m
response.Onboot = onboot
response.Disks = disks
default:
- return NewErrorResult(fmt.Errorf("unsupported guest type: %s", guestType)), nil
+ return NewErrorResult(fmt.Errorf("unsupported guest type: %s", target.kind)), nil
}
return NewJSONResult(response.NormalizeCollections()), nil
@@ -5428,31 +5418,68 @@ func (e *PulseToolExecutor) executeNativeAppContainerConfig(ctx context.Context,
return NewJSONResult(response.NormalizeCollections()), nil
}
-func resolveGuestFromReadState(rs unifiedresources.ReadState, resourceType, resourceID string) (guestType string, vmID int, name, node, instance string, err error) {
+type guestConfigTarget struct {
+ id, kind, name, node, instance string
+ vmid int
+ metadata GovernedResourceMetadata
+}
+
+func resolveGuestFromReadState(rs unifiedresources.ReadState, resourceType, resourceID string) (guestConfigTarget, error) {
resourceType = canonicalQueryResourceType(resourceType)
resourceID = strings.TrimSpace(resourceID)
if resourceType == "" || resourceID == "" {
- return "", 0, "", "", "", fmt.Errorf("resource_type and resource_id are required")
+ return guestConfigTarget{}, fmt.Errorf("resource_type and resource_id are required")
}
+ // A canonical ID wins over another guest's coincidentally matching name.
+ // Names and bare VMIDs remain convenient only when they identify one guest.
+ var exact, aliases []guestConfigTarget
+ type metadataView interface {
+ GovernanceMetadata() (*unifiedresources.ResourcePolicy, string)
+ }
+ add := func(target guestConfigTarget, view metadataView) {
+ if target.id != resourceID && strconv.Itoa(target.vmid) != resourceID && target.name != resourceID {
+ return
+ }
+ policy, summary := view.GovernanceMetadata()
+ target.metadata = GovernedResourceMetadata{Policy: policy, AISafeSummary: summary}
+ if target.id == resourceID {
+ exact = append(exact, target)
+ } else {
+ aliases = append(aliases, target)
+ }
+ }
switch resourceType {
case "system-container":
for _, ct := range rs.Containers() {
- if fmt.Sprintf("%d", ct.VMID()) == resourceID || ct.Name() == resourceID || ct.ID() == resourceID {
- return "system-container", ct.VMID(), ct.Name(), ct.Node(), ct.Instance(), nil
+ if ct != nil {
+ add(guestConfigTarget{id: ct.ID(), kind: resourceType, name: ct.Name(), node: ct.Node(), instance: ct.Instance(), vmid: ct.VMID()}, ct)
}
}
- return "", 0, "", "", "", fmt.Errorf("system-container not found: %s", resourceID)
case "vm":
for _, vm := range rs.VMs() {
- if fmt.Sprintf("%d", vm.VMID()) == resourceID || vm.Name() == resourceID || vm.ID() == resourceID {
- return "vm", vm.VMID(), vm.Name(), vm.Node(), vm.Instance(), nil
+ if vm != nil {
+ add(guestConfigTarget{id: vm.ID(), kind: resourceType, name: vm.Name(), node: vm.Node(), instance: vm.Instance(), vmid: vm.VMID()}, vm)
}
}
- return "", 0, "", "", "", fmt.Errorf("vm not found: %s", resourceID)
default:
- return "", 0, "", "", "", fmt.Errorf("invalid resource_type: %s. Use vm or system-container", resourceType)
+ return guestConfigTarget{}, fmt.Errorf("invalid resource_type: %s. Use vm or system-container", resourceType)
+ }
+ matches := aliases
+ if len(exact) > 0 {
+ matches = exact
+ }
+ if len(matches) == 0 {
+ return guestConfigTarget{}, fmt.Errorf("%s not found: %s", resourceType, resourceID)
+ }
+ if len(matches) > 1 {
+ return guestConfigTarget{}, fmt.Errorf("%s reference is ambiguous: %s; use the canonical resource ID", resourceType, resourceID)
+ }
+ target := matches[0]
+ if target.vmid <= 0 || target.node == "" || target.instance == "" {
+ return guestConfigTarget{}, fmt.Errorf("guest placement is unavailable; refresh the resource inventory")
}
+ return target, nil
}
func parseContainerConfig(config map[string]interface{}) (hostname, osType string, onboot *bool, rootfs string, mounts []GuestMountConfig) {
diff --git a/internal/monitoring/guest_config.go b/internal/monitoring/guest_config.go
index 9c3b815506..ac0d963d48 100644
--- a/internal/monitoring/guest_config.go
+++ b/internal/monitoring/guest_config.go
@@ -21,43 +21,49 @@ func (m *Monitor) GetGuestConfig(ctx context.Context, guestType, instance, node
return nil, fmt.Errorf("guest type is required")
}
- // Resolve instance/node from state if missing.
+ // Resolve missing placement only from a unique current guest. A VMID is
+ // unique within a PVE installation, not across every configured connection.
if instance == "" || node == "" {
m.mu.RLock()
- state := m.state
+ hasState := m.state != nil || m.resourceStore != nil
m.mu.RUnlock()
+ if !hasState {
+ return nil, fmt.Errorf("state not available")
+ }
+ state := m.currentModeReadState()
if state == nil {
return nil, fmt.Errorf("state not available")
}
-
+ type placement struct{ instance, node string }
+ matches := make(map[placement]bool)
+ add := func(id int, candidateInstance, candidateNode string) {
+ if id != vmid || instance != "" && instance != candidateInstance || node != "" && node != candidateNode {
+ return
+ }
+ matches[placement{candidateInstance, candidateNode}] = true
+ }
switch gt {
case "container", "lxc":
- for _, ct := range state.Containers {
- if ct.VMID == vmid {
- if instance == "" {
- instance = ct.Instance
- }
- if node == "" {
- node = ct.Node
- }
- break
+ for _, ct := range state.Containers() {
+ if ct != nil {
+ add(ct.VMID(), ct.Instance(), ct.Node())
}
}
case "vm":
- for _, vm := range state.VMs {
- if vm.VMID == vmid {
- if instance == "" {
- instance = vm.Instance
- }
- if node == "" {
- node = vm.Node
- }
- break
+ for _, vm := range state.VMs() {
+ if vm != nil {
+ add(vm.VMID(), vm.Instance(), vm.Node())
}
}
default:
return nil, fmt.Errorf("unsupported guest type: %s", guestType)
}
+ if len(matches) > 1 {
+ return nil, fmt.Errorf("guest placement is ambiguous; specify instance and node")
+ }
+ for match := range matches {
+ instance, node = match.instance, match.node
+ }
}
if instance == "" || node == "" {
diff --git a/internal/monitoring/monitor_additional_test.go b/internal/monitoring/monitor_additional_test.go
index 399421f227..b5bbb42a69 100644
--- a/internal/monitoring/monitor_additional_test.go
+++ b/internal/monitoring/monitor_additional_test.go
@@ -4,6 +4,9 @@ import (
"context"
"errors"
"net"
+ "strings"
+ "sync"
+ "sync/atomic"
"testing"
"time"
@@ -1847,3 +1850,142 @@ func TestMonitorPersistentFailureBackoffAndRecovery(t *testing.T) {
t.Fatal("new failure episode did not restart the original backoff")
}
}
+
+func TestGuestConfigPlacement(t *testing.T) {
+ for _, kind := range []string{"vm", "container", "lxc"} {
+ for _, reverse := range []bool{false, true} {
+ for _, tc := range []struct {
+ name, instance, node, wantInstance, wantNode, failure string
+ }{
+ {name: "instance constraint", instance: "second", wantInstance: "second", wantNode: "node-b"},
+ {name: "node constraint", node: "node-b", wantInstance: "second", wantNode: "node-b"},
+ {name: "bare VMID ambiguous", failure: "ambiguous"},
+ {name: "unknown instance", instance: "absent", failure: "unable to resolve"},
+ {name: "unknown node", node: "absent", failure: "unable to resolve"},
+ } {
+ t.Run(kind+"/"+tc.name+"/reverse="+map[bool]string{true: "yes", false: "no"}[reverse], func(t *testing.T) {
+ state := models.NewState()
+ vms := []models.VM{{ID: "a", VMID: 105, Instance: "first", Node: "node-a"}, {ID: "b", VMID: 105, Instance: "second", Node: "node-b"}}
+ if reverse {
+ vms[0], vms[1] = vms[1], vms[0]
+ }
+ if kind == "vm" {
+ state.VMs = vms
+ } else {
+ for _, vm := range vms {
+ state.Containers = append(state.Containers, models.Container{ID: vm.ID, VMID: vm.VMID, Instance: vm.Instance, Node: vm.Node})
+ }
+ }
+ first := &guestConfigCoverageClient{vmConfig: map[string]interface{}{"origin": "first"}, containerConfig: map[string]interface{}{"origin": "first"}}
+ second := &guestConfigCoverageClient{vmConfig: map[string]interface{}{"origin": "second"}, containerConfig: map[string]interface{}{"origin": "second"}}
+ m := &Monitor{state: state, pveClients: map[string]PVEClientInterface{"first": first, "second": second}}
+ got, err := m.GetGuestConfig(context.Background(), kind, tc.instance, tc.node, 105)
+ if tc.failure != "" {
+ if err == nil || !strings.Contains(err.Error(), tc.failure) || got != nil || first.vmCalls+first.containerCalls+second.vmCalls+second.containerCalls != 0 {
+ t.Fatalf("unresolved identity reached a provider: result=%v err=%v", got, err)
+ }
+ return
+ }
+ if err != nil || got["origin"] != tc.wantInstance || second.lastNode != tc.wantNode || second.lastVMID != 105 || second.vmCalls+second.containerCalls != 1 || first.vmCalls+first.containerCalls != 0 {
+ t.Fatalf("placement constraint was not honoured: result=%v err=%v target=%s/%d", got, err, second.lastNode, second.lastVMID)
+ }
+ })
+ }
+ }
+ }
+}
+
+func TestGuestConfigPlacementCurrentInventory(t *testing.T) {
+ for _, kind := range []string{"vm", "container"} {
+ for _, empty := range []bool{false, true} {
+ t.Run(kind+"/empty="+map[bool]string{true: "yes", false: "no"}[empty], func(t *testing.T) {
+ legacy := models.NewState()
+ legacy.VMs = []models.VM{{ID: "stale-vm", VMID: 105, Instance: "old", Node: "old-node"}}
+ legacy.Containers = []models.Container{{ID: "stale-ct", VMID: 105, Instance: "old", Node: "old-node"}}
+ registry := unifiedresources.NewRegistry(nil)
+ if !empty {
+ registry.IngestSnapshot(models.StateSnapshot{
+ VMs: []models.VM{{ID: "current-vm", VMID: 105, Instance: "current", Node: "current-node"}},
+ Containers: []models.Container{{ID: "current-ct", VMID: 105, Instance: "current", Node: "current-node"}},
+ })
+ }
+ old, current := &guestConfigCoverageClient{}, &guestConfigCoverageClient{}
+ m := &Monitor{state: legacy, resourceStore: unifiedresources.NewMonitorAdapter(registry), pveClients: map[string]PVEClientInterface{"old": old, "current": current}}
+ _, err := m.GetGuestConfig(context.Background(), kind, "", "", 105)
+ if empty {
+ if err == nil || current.vmCalls+current.containerCalls != 0 {
+ t.Fatal("empty current inventory fell back to stale placement")
+ }
+ } else if err != nil || current.lastNode != "current-node" || current.vmCalls+current.containerCalls != 1 {
+ t.Fatalf("current inventory did not own placement: %v", err)
+ }
+ if old.vmCalls+old.containerCalls != 0 {
+ t.Fatal("stale connection was read")
+ }
+ })
+ }
+ }
+}
+
+func TestGuestConfigPlacementAdverseAndDirect(t *testing.T) {
+ for _, kind := range []string{"vm", "container"} {
+ t.Run(kind, func(t *testing.T) {
+ failure := errors.New("provider read failed")
+ client := &guestConfigCoverageClient{vmErr: failure, containerErr: failure}
+ m := &Monitor{pveClients: map[string]PVEClientInterface{"chosen": client}}
+ _, err := m.GetGuestConfig(context.Background(), kind, "chosen", "chosen-node", 105)
+ if !errors.Is(err, failure) || client.lastNode != "chosen-node" || client.vmCalls+client.containerCalls != 1 {
+ t.Fatal("explicit placement or original provider failure was changed")
+ }
+ state := models.NewState()
+ state.VMs = []models.VM{{ID: "one", VMID: 105, Instance: "chosen", Node: "same-node"}, {ID: "two", VMID: 105, Instance: "other", Node: "same-node"}}
+ state.Containers = []models.Container{{ID: "one", VMID: 105, Instance: "chosen", Node: "same-node"}, {ID: "two", VMID: 105, Instance: "other", Node: "same-node"}}
+ m.state = state
+ _, err = m.GetGuestConfig(context.Background(), kind, "", "same-node", 105)
+ if err == nil || !strings.Contains(err.Error(), "ambiguous") || client.vmCalls+client.containerCalls != 1 {
+ t.Fatal("two installations sharing a node name were guessed")
+ }
+ state.VMs, state.Containers = []models.VM{{VMID: 105, Instance: "chosen"}}, []models.Container{{VMID: 105, Instance: "chosen"}}
+ _, err = m.GetGuestConfig(context.Background(), kind, "chosen", "", 105)
+ if err == nil || client.vmCalls+client.containerCalls != 1 {
+ t.Fatal("incomplete placement reached a provider")
+ }
+ })
+ }
+}
+
+type concurrentGuestConfigClient struct {
+ stubPVEClient
+ calls atomic.Int32
+}
+
+func (c *concurrentGuestConfigClient) GetVMConfig(_ context.Context, node string, vmid int) (map[string]interface{}, error) {
+ c.calls.Add(1)
+ return map[string]interface{}{"node": node, "vmid": vmid}, nil
+}
+
+func TestGuestConfigPlacementConcurrentSnapshot(t *testing.T) {
+ state := models.NewState()
+ state.UpdateVMsForInstance("chosen", []models.VM{{ID: "vm", VMID: 105, Instance: "chosen", Node: "a"}})
+ client := &concurrentGuestConfigClient{}
+ m := &Monitor{state: state, pveClients: map[string]PVEClientInterface{"chosen": client}}
+ var wg sync.WaitGroup
+ wg.Add(1)
+ go func() {
+ defer wg.Done()
+ for i := 0; i < 100; i++ {
+ state.UpdateVMsForInstance("chosen", []models.VM{{ID: "vm", VMID: 105, Instance: "chosen", Node: "b"}})
+ state.UpdateVMsForInstance("chosen", []models.VM{{ID: "vm", VMID: 105, Instance: "chosen", Node: "a"}})
+ }
+ }()
+ for i := 0; i < 100; i++ {
+ got, err := m.GetGuestConfig(context.Background(), "vm", "chosen", "", 105)
+ if err != nil || got["vmid"] != 105 || got["node"] != "a" && got["node"] != "b" {
+ t.Errorf("invalid concurrent snapshot: %v %v", got, err)
+ }
+ }
+ wg.Wait()
+ if client.calls.Load() != 100 {
+ t.Fatal("concurrent reads were replayed or lost")
+ }
+}
diff --git a/internal/unifiedresources/views.go b/internal/unifiedresources/views.go
index 34ee07a5f3..e452c79483 100644
--- a/internal/unifiedresources/views.go
+++ b/internal/unifiedresources/views.go
@@ -136,6 +136,12 @@ func (v VMView) ID() string {
return v.r.ID
}
+// GovernanceMetadata reads policy from this exact canonical guest rather
+// than resolving a potentially shared name in another installation.
+func (v VMView) GovernanceMetadata() (*ResourcePolicy, string) {
+ return CanonicalGovernanceMetadata(v.r)
+}
+
func (v VMView) Name() string {
if v.r == nil {
return ""
@@ -460,6 +466,12 @@ func (v ContainerView) ID() string {
return v.r.ID
}
+// GovernanceMetadata reads policy from this exact canonical guest rather
+// than resolving a potentially shared name in another installation.
+func (v ContainerView) GovernanceMetadata() (*ResourcePolicy, string) {
+ return CanonicalGovernanceMetadata(v.r)
+}
+
func (v ContainerView) Name() string {
if v.r == nil {
return ""
diff --git a/internal/unifiedresources/views_test.go b/internal/unifiedresources/views_test.go
index 999d3d2792..e5e330402e 100644
--- a/internal/unifiedresources/views_test.go
+++ b/internal/unifiedresources/views_test.go
@@ -2475,3 +2475,39 @@ func TestVMViewDiskFromLinkedAgentFollowsSelectedMetricSource(t *testing.T) {
}
}
}
+
+func TestGuestViewGovernanceIdentity(t *testing.T) {
+ for _, kind := range []ResourceType{ResourceTypeVM, ResourceTypeSystemContainer} {
+ first := &Resource{ID: "first", Name: "duplicate", Type: kind, Tags: []string{"customer-data"}, Proxmox: &ProxmoxData{VMID: 105, Instance: "first", NodeName: "a"}}
+ second := &Resource{ID: "second", Name: "duplicate", Type: kind, Proxmox: &ProxmoxData{VMID: 105, Instance: "second", NodeName: "b"}}
+ metadata := func(resource *Resource) (*ResourcePolicy, string) {
+ if kind == ResourceTypeVM {
+ return NewVMView(resource).GovernanceMetadata()
+ }
+ return NewContainerView(resource).GovernanceMetadata()
+ }
+ firstPolicy, _ := metadata(first)
+ secondPolicy, _ := metadata(second)
+ if firstPolicy.Sensitivity == secondPolicy.Sensitivity {
+ t.Fatal("fixture does not distinguish same-name guest policies")
+ }
+ for _, resource := range []*Resource{first, second} {
+ wantPolicy, wantSummary := CanonicalGovernanceMetadata(resource)
+ gotPolicy, gotSummary := metadata(resource)
+ if !reflect.DeepEqual(gotPolicy, wantPolicy) || gotSummary != wantSummary {
+ t.Fatal("metadata did not belong to the selected view")
+ }
+ gotPolicy.Sensitivity = ResourceSensitivityPublic
+ if len(gotPolicy.Routing.Redact) > 0 {
+ gotPolicy.Routing.Redact[0] = "changed"
+ }
+ again, _ := metadata(resource)
+ if !reflect.DeepEqual(again, wantPolicy) {
+ t.Fatal("returned policy aliases the canonical guest")
+ }
+ }
+ if policy, summary := metadata(nil); policy != nil || summary != "" {
+ t.Fatal("nil view created policy evidence")
+ }
+ }
+}