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.

Avoid full resource copies in metadata read helpers

pulse-triage[bot]b0c5eb1a82ad
diff --git a/internal/unifiedresources/clone_test.go b/internal/unifiedresources/clone_test.go
index 078799b8eb..113205f28f 100644
--- a/internal/unifiedresources/clone_test.go
+++ b/internal/unifiedresources/clone_test.go
@@ -13,6 +13,10 @@ import (
func TestCloneResourceRefreshesScopeAndPolicyMetadata(t *testing.T) {
// Registry mutation paths can leave derived metadata stale until the next
// read. Keep refresh on every clone, including after source/tag edits.
+ observation := models.MemoryObservation{
+ State: "last-known", Source: "guest-agent-meminfo",
+ ObservedAt: time.Date(2026, 10, 4, 12, 0, 0, 0, time.UTC),
+ }
original := &Resource{
ID: "app-container:lab", Type: ResourceTypeAppContainer,
Sources: []DataSource{SourceDocker, SourceAgent, SourceDocker},
@@ -21,6 +25,9 @@ func TestCloneResourceRefreshesScopeAndPolicyMetadata(t *testing.T) {
Docker: &DockerData{HostSourceID: "proxmox-lxc-docker:lab:node:100"},
Policy: &ResourcePolicy{Sensitivity: ResourceSensitivityRestricted},
AISafeSummary: "stale",
+ Metrics: &ResourceMetrics{Memory: &MetricValue{
+ Percent: 50, Source: SourceProxmox, Observation: observation,
+ }},
}
for _, test := range []struct {
name string
@@ -44,6 +51,14 @@ func TestCloneResourceRefreshesScopeAndPolicyMetadata(t *testing.T) {
}
cloned.PlatformScopes[0] = "mutated"
cloned.Policy.Sensitivity = ResourceSensitivityPublic
+ if cloned.Metrics == original.Metrics || cloned.Metrics.Memory == original.Metrics.Memory || cloned.Metrics.Memory.Observation != observation || cloned.Metrics.Memory.Source != SourceProxmox {
+ t.Fatal("metadata read aliased or renewed the selected memory observation")
+ }
+ cloned.Metrics.Memory.Observation.State = "current"
+ cloned.Metrics.Memory.Observation.ObservedAt = observation.ObservedAt.Add(time.Hour)
+ if original.Metrics.Memory.Observation != observation {
+ t.Fatal("borrowed metadata readers lost detached metric ownership")
+ }
if !reflect.DeepEqual(original.PlatformScopes, []string{"stale"}) || original.Policy.Sensitivity != ResourceSensitivityRestricted || original.AISafeSummary != "stale" {
t.Fatal("clone modified the original derived metadata")
}
diff --git a/internal/unifiedresources/metadata_bench_test.go b/internal/unifiedresources/metadata_bench_test.go
index 5a5bc958d5..fd3737b0d8 100644
--- a/internal/unifiedresources/metadata_bench_test.go
+++ b/internal/unifiedresources/metadata_bench_test.go
@@ -25,6 +25,6 @@ func BenchmarkClassifyResourceSensitivity(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
- benchmarkSensitivity = classifyResourceSensitivity(resource)
+ benchmarkSensitivity = classifyResourceSensitivity(&resource)
}
}
diff --git a/internal/unifiedresources/platform_scopes.go b/internal/unifiedresources/platform_scopes.go
index 774e1c44a6..0456db40e6 100644
--- a/internal/unifiedresources/platform_scopes.go
+++ b/internal/unifiedresources/platform_scopes.go
@@ -47,8 +47,8 @@ func RefreshPlatformScopes(resource *Resource) {
add(platformScopeForSource(source))
}
- addPlatformScopesForFacets(add, *resource)
- if shouldAddDockerPlatformScope(*resource) {
+ addPlatformScopesForFacets(add, resource)
+ if shouldAddDockerPlatformScope(resource) {
add("docker")
}
@@ -88,7 +88,9 @@ func platformScopeForSource(source DataSource) string {
}
}
-func addPlatformScopesForFacets(add func(string), resource Resource) {
+// Facet readers borrow the caller-owned resource without retaining or mutating
+// it; only RefreshPlatformScopes replaces its derived membership slice.
+func addPlatformScopesForFacets(add func(string), resource *Resource) {
if resource.Agent != nil {
add("agent")
}
@@ -110,12 +112,12 @@ func addPlatformScopesForFacets(add func(string), resource Resource) {
if resource.VMware != nil {
add("vmware-vsphere")
}
- if len(AvailabilityChecksForResource(resource)) > 0 || CanonicalResourceType(resource.Type) == ResourceTypeNetworkEndpoint {
+ if len(AvailabilityChecksForResource(*resource)) > 0 || CanonicalResourceType(resource.Type) == ResourceTypeNetworkEndpoint {
add("availability")
}
}
-func shouldAddDockerPlatformScope(resource Resource) bool {
+func shouldAddDockerPlatformScope(resource *Resource) bool {
if resource.Docker == nil {
return false
}
diff --git a/internal/unifiedresources/policy_metadata.go b/internal/unifiedresources/policy_metadata.go
index e7ccc952f6..f7b1f52ec8 100644
--- a/internal/unifiedresources/policy_metadata.go
+++ b/internal/unifiedresources/policy_metadata.go
@@ -77,7 +77,7 @@ func RefreshPolicyMetadata(resource *Resource) {
return
}
- sensitivity := classifyResourceSensitivity(*resource)
+ sensitivity := classifyResourceSensitivity(resource)
resource.Policy = &ResourcePolicy{
Sensitivity: sensitivity,
Routing: buildResourceRoutingPolicy(*resource, sensitivity),
@@ -112,7 +112,10 @@ func RefreshCanonicalMetadataSlice(resources []Resource) []Resource {
return out
}
-func classifyResourceSensitivity(resource Resource) ResourceSensitivity {
+// Borrow the detached resource for this read-only scan. A full value copy on
+// every registry read is unnecessary, including when tag precedence returns
+// before any of the resource's other fields are read.
+func classifyResourceSensitivity(resource *Resource) ResourceSensitivity {
// Clones classify every registry read. Scan once without building a tag
// map, preserving public > restricted > sensitive precedence independently
// of tag order, and normalising each tag only once.
diff --git a/internal/unifiedresources/policy_metadata_test.go b/internal/unifiedresources/policy_metadata_test.go
index 459002e787..dcc7c9aba4 100644
--- a/internal/unifiedresources/policy_metadata_test.go
+++ b/internal/unifiedresources/policy_metadata_test.go
@@ -17,13 +17,13 @@ func TestClassifyResourceSensitivityTagAliasesAndPrecedence(t *testing.T) {
t.Run(tag, func(t *testing.T) {
normalised := " " + strings.ToUpper(tag) + " "
for _, tags := range [][]string{{normalised, "unknown", normalised}, {"unknown", normalised}} {
- if got := classifyResourceSensitivity(Resource{Type: ResourceTypeVM, Tags: tags}); got != group.want {
+ if got := classifyResourceSensitivity(&Resource{Type: ResourceTypeVM, Tags: tags}); got != group.want {
t.Fatalf("tags %v sensitivity = %q, want %q", tags, got, group.want)
}
}
for _, tags := range [][]string{{" PUBLIC ", normalised}, {normalised, "public"}} {
// Explicit public wins even over secret-type defaults, as before.
- if got := classifyResourceSensitivity(Resource{Type: ResourceTypeK8sSecret, Tags: tags}); got != ResourceSensitivityPublic {
+ if got := classifyResourceSensitivity(&Resource{Type: ResourceTypeK8sSecret, Tags: tags}); got != ResourceSensitivityPublic {
t.Fatalf("public precedence changed for %v: %q", tags, got)
}
}