From 8d698286b8bfc948fd99e4d15a772ed68fb26538 Mon Sep 17 00:00:00 2001 From: Maryam Tahhan Date: Mon, 26 Aug 2024 07:33:15 -0400 Subject: [PATCH] chore: introduce locking to pkg/cgroups Signed-off-by: Maryam Tahhan --- pkg/cgroup/resolve_container.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/pkg/cgroup/resolve_container.go b/pkg/cgroup/resolve_container.go index a0d28e77d7..f1eacbddaf 100644 --- a/pkg/cgroup/resolve_container.go +++ b/pkg/cgroup/resolve_container.go @@ -54,6 +54,7 @@ type cache struct { containerIDCache map[uint64]string containerIDToContainerInfo map[string]*ContainerInfo cGroupIDToPath map[uint64]string + mu sync.RWMutex } func Init() (*[]corev1.Pod, error) { @@ -119,12 +120,15 @@ func GetContainerInfo(cGroupID, pid uint64, withCGroupID bool) (*ContainerInfo, return info, err } + instance.mu.Lock() + defer instance.mu.Unlock() if i, ok := instance.containerIDToContainerInfo[containerID]; ok { return i, nil } else { info.ContainerID = containerID instance.containerIDToContainerInfo[containerID] = info } + return instance.containerIDToContainerInfo[containerID], nil } @@ -151,15 +155,20 @@ func getContainerIDFromPath(cGroupID, pid uint64, withCGroupID bool) (string, er // AddContainerIDToCache add the container id to cache using the pid as the key func AddContainerIDToCache(pid uint64, containerID string) { ensureCacheInitialized() + instance.mu.Lock() + defer instance.mu.Unlock() instance.containerIDCache[pid] = containerID } // GetContainerIDFromPID find the container ID using the process PID func GetContainerIDFromPID(pid uint64) (string, error) { ensureCacheInitialized() + instance.mu.RLock() if p, ok := instance.containerIDCache[pid]; ok { + instance.mu.RUnlock() return p, nil } + instance.mu.RUnlock() var err error var path string @@ -169,6 +178,7 @@ func GetContainerIDFromPID(pid uint64) (string, error) { containerID, err := extractPodContainerIDfromPath(path) AddContainerIDToCache(pid, containerID) + return instance.containerIDCache[pid], err } @@ -196,9 +206,12 @@ func getPathFromPID(searchPath string, pid uint64) (string, error) { } func getContainerIDFromcGroupID(cGroupID uint64) (string, error) { + instance.mu.RLock() if id, ok := instance.containerIDCache[cGroupID]; ok { + instance.mu.RUnlock() return id, nil } + instance.mu.RUnlock() var err error var path string @@ -208,12 +221,15 @@ func getContainerIDFromcGroupID(cGroupID uint64) (string, error) { containerID, err := extractPodContainerIDfromPath(path) AddContainerIDToCache(cGroupID, containerID) + return instance.containerIDCache[cGroupID], err } // getPathFromcGroupID uses cgroupfs to get cgroup path from id // it needs cgroup v2 (per https://github.com/iovisor/bpftrace/issues/950) and kernel 4.18+ (https://github.com/torvalds/linux/commit/bf6fa2c893c5237b48569a13fa3c673041430b6c) func getPathFromcGroupID(cgroupID uint64) (string, error) { + instance.mu.Lock() + defer instance.mu.Unlock() if p, ok := instance.cGroupIDToPath[cgroupID]; ok { return p, nil } @@ -278,6 +294,9 @@ func extractPodContainerIDfromPathWithCgroup(path string) (string, error) { func getAliveContainers(pods *[]corev1.Pod) map[string]bool { aliveContainers := make(map[string]bool) + instance.mu.Lock() + defer instance.mu.Unlock() + for i := 0; i < len(*pods); i++ { statuses := (*pods)[i].Status.InitContainerStatuses for j := 0; j < len(statuses); j++ {