From b81d36c4624da0fe5eef4357a91eb240e8b1e99d Mon Sep 17 00:00:00 2001 From: Gildas Cuisinier Date: Tue, 23 Jan 2024 15:57:31 +0100 Subject: [PATCH] Add lock to avoid concurrent access (#56) --- main.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/main.go b/main.go index df1270a..5b24176 100644 --- a/main.go +++ b/main.go @@ -52,6 +52,7 @@ var ( nodeSlice []string maxNodeConcurrency int podResourceLookup map[string]podContainers + lock sync.RWMutex ) func getEnv(key, fallback string) string { @@ -151,7 +152,9 @@ func getContainerRequestLimits(p v1.Pod) { containers = append(containers, setContainer) } + lock.Lock() podResourceLookup[p.Name] = podContainers{containers: containers} + lock.Unlock() } func initGetPodsResourceLimits() { @@ -297,7 +300,9 @@ func generateLabels(podName string, podNamespace string, nodeName string, usedBy var labelsList []CollectMetric if ephemeralStorageContainerLimitsPercentage { + lock.RLock() podResult, ok := podResourceLookup[podName] + lock.RUnlock() if ok { for _, c := range podResult.containers { labels := prometheus.Labels{"pod_namespace": podNamespace,