From f02ddd0e3bc27bb41f312bfeb8cd2ed95cf7707e Mon Sep 17 00:00:00 2001 From: Hemant Kumar Date: Fri, 15 May 2026 11:40:56 -0400 Subject: [PATCH] Only emit non-dupicated values as metrics --- .../selinuxwarning/cache/volumecache.go | 6 ++-- .../selinuxwarning/cache/volumecache_test.go | 29 +++++++++++++++++++ 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/pkg/controller/volume/selinuxwarning/cache/volumecache.go b/pkg/controller/volume/selinuxwarning/cache/volumecache.go index 56955724a52..32f7ff48651 100644 --- a/pkg/controller/volume/selinuxwarning/cache/volumecache.go +++ b/pkg/controller/volume/selinuxwarning/cache/volumecache.go @@ -326,9 +326,9 @@ func (c *volumeCache) GetConflicts(logger klog.Logger) []Conflict { logger.V(4).Info("Scraping conflicts") c.dump(logger) - result := make([]Conflict, 0) + result := sets.New[Conflict]() for _, volConflicts := range c.conflicts { - result = append(result, volConflicts...) + result.Insert(volConflicts...) } - return result + return result.UnsortedList() } diff --git a/pkg/controller/volume/selinuxwarning/cache/volumecache_test.go b/pkg/controller/volume/selinuxwarning/cache/volumecache_test.go index 766c5a6ebb6..82fd27e815f 100644 --- a/pkg/controller/volume/selinuxwarning/cache/volumecache_test.go +++ b/pkg/controller/volume/selinuxwarning/cache/volumecache_test.go @@ -541,6 +541,35 @@ func TestVolumeCache_MultiVolumeConflicts(t *testing.T) { if len(remaining) != 0 { t.Errorf("Expected no conflicts after deleting podA, got %d: %+v", len(remaining), remaining) } + + // Verify deduplication: podD and podE conflict on two volumes with the same labels. + // Identical Conflict entries from different volumes must be deduplicated by GetConflicts. + podD := cache.ObjectName{Namespace: "ns", Name: "podD"} + podE := cache.ObjectName{Namespace: "ns", Name: "podE"} + + c.AddVolume(logger, "vol3", podD, "system_u:system_r:labelD", v1.SELinuxChangePolicyMountOption, "driver1") + c.AddVolume(logger, "vol4", podD, "system_u:system_r:labelD", v1.SELinuxChangePolicyMountOption, "driver1") + + conflictsVol3 := c.AddVolume(logger, "vol3", podE, "system_u:system_r:labelE", v1.SELinuxChangePolicyMountOption, "driver1") + conflictsVol4 := c.AddVolume(logger, "vol4", podE, "system_u:system_r:labelE", v1.SELinuxChangePolicyMountOption, "driver1") + + if len(conflictsVol3) != len(conflictsVol4) { + t.Fatalf("Expected same number of conflicts from vol3 and vol4 (%d vs %d)", len(conflictsVol3), len(conflictsVol4)) + } + if len(conflictsVol3) == 0 { + t.Fatal("Expected conflicts between podD and podE") + } + + allConflicts = c.GetConflicts(logger) + deCount := 0 + for _, conflict := range allConflicts { + if conflict.Pod == podD || conflict.Pod == podE || conflict.OtherPod == podD || conflict.OtherPod == podE { + deCount++ + } + } + if deCount != len(conflictsVol3) { + t.Errorf("Expected %d deduplicated conflicts for podD/podE (from 2 volumes), got %d", len(conflictsVol3), deCount) + } } func TestVolumeCache_DeletePodConflicts(t *testing.T) {