Skip to content

Commit

Permalink
Fix pod targeting (#116)
Browse files Browse the repository at this point in the history
* Fix pod targeting

* Fix pod targeting
  • Loading branch information
iluxa authored Nov 13, 2024
1 parent 788decd commit 52003e9
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
29 changes: 23 additions & 6 deletions pkg/cgroup/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,21 @@ func (e *CgroupsControllerImpl) AddCgroupPath(cgroupPath string) (cgroupID uint6

e.containerMtx.Lock()
defer e.containerMtx.Unlock()
//TODO: check same already exists
if !e.containerToCgroup.Contains(containerID) {
e.containerToCgroup.Add(containerID, []CgroupInfo{item})
} else {
v, _ := e.containerToCgroup.Get(containerID)
v = append(v, item)
e.containerToCgroup.Add(containerID, v)
found := false
for _, it := range v {
if it.CgroupID == item.CgroupID && it.CgroupPath == item.CgroupPath {
found = true
break
}
}
if !found {
v = append(v, item)
e.containerToCgroup.Add(containerID, v)
}
}

ok = true
Expand Down Expand Up @@ -159,10 +167,19 @@ func (e *CgroupsControllerImpl) PopulateSocketsInodes(inodeMap *ebpf.Map) error
continue
}
if err := inodeMap.Update(inode, cgroup.CgroupID, ebpf.UpdateNoExist); err != nil {
log.Error().Err(err).Msg("Update inodemap failed")
break
if errors.Is(err, ebpf.ErrKeyExist) {
// two processes in the same cgroup can share one socket
var cgroupExist uint64
if err := inodeMap.Lookup(inode, &cgroupExist); err != nil {
log.Error().Err(err).Str("Cgroup Path", cgroup.CgroupPath).Uint64("Cgroup ID", cgroup.CgroupID).Uint64("inode", inode).Msg("Lookup inodemap failed")
}
if cgroup.CgroupID != cgroupExist {
log.Error().Err(err).Str("Cgroup Path", cgroup.CgroupPath).Uint64("Cgroup ID", cgroup.CgroupID).Uint64("inode", inode).Uint64("Cgroup ID exists", cgroupExist).Msg("Update inodemap failed")
}
}
} else {
log.Debug().Str("Cgroup Path", cgroup.CgroupPath).Uint64("Cgroup ID", cgroup.CgroupID).Uint64("inode", inode).Msg("Found socket inode")
}
log.Debug().Uint64("Cgroup ID", cgroup.CgroupID).Uint64("inode", inode).Msg("Found socket inode")
}
}
}
Expand Down
8 changes: 5 additions & 3 deletions tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,10 @@ func (t *Tracer) updateTargets(addPods, removePods []*v1.Pod, settings uint32) e
t.eventsDiscoverer.UntargetCgroup(cInfo.cgroupID)

if err := t.bpfObjects.BpfObjs.CgroupIds.Delete(cInfo.cgroupID); err != nil {
log.Error().Err(err).Uint64("Cgroup ID", cInfo.cgroupID).Msg("Cgroup IDs delete failed")
return err
if !errors.Is(err, ebpf.ErrKeyNotExist) {
log.Error().Err(err).Uint64("Cgroup ID", cInfo.cgroupID).Msg("Cgroup IDs delete failed")
return err
}
}
}
log.Info().Str("pod", pod.Name).Msg("Detached pod:")
Expand All @@ -136,7 +138,7 @@ func (t *Tracer) updateTargets(addPods, removePods []*v1.Pod, settings uint32) e
pd.containers = append(pd.containers, cInfo)

if err := t.bpfObjects.BpfObjs.CgroupIds.Update(cInfo.cgroupID, uint32(0), ebpf.UpdateAny); err != nil {
log.Error().Err(err).Uint64("Cgroup ID", cInfo.cgroupID).Msg("Cgroup IDs update failed")
log.Error().Err(err).Str("Cgroup Path", cInfo.cgroupPath).Str("Container ID", containerId).Uint64("Cgroup ID", cInfo.cgroupID).Msg("Cgroup IDs update failed")
return err
}

Expand Down

0 comments on commit 52003e9

Please sign in to comment.