Skip to content

Commit

Permalink
chore: introduce locking to pkg/cgroups
Browse files Browse the repository at this point in the history
Signed-off-by: Maryam Tahhan <[email protected]>
  • Loading branch information
maryamtahhan committed Aug 26, 2024
1 parent 309982e commit 8d69828
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions pkg/cgroup/resolve_container.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
}

Expand All @@ -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
Expand All @@ -169,6 +178,7 @@ func GetContainerIDFromPID(pid uint64) (string, error) {

containerID, err := extractPodContainerIDfromPath(path)
AddContainerIDToCache(pid, containerID)

return instance.containerIDCache[pid], err
}

Expand Down Expand Up @@ -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
Expand All @@ -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
}
Expand Down Expand Up @@ -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++ {
Expand Down

0 comments on commit 8d69828

Please sign in to comment.