-
Notifications
You must be signed in to change notification settings - Fork 349
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: rksharma95 <[email protected]>
- Loading branch information
1 parent
cd03ced
commit d44b2ca
Showing
5 changed files
with
73 additions
and
1 deletion.
There are no files selected for viewing
File renamed without changes.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package base | ||
|
||
import ( | ||
"errors" | ||
"os" | ||
"sync" | ||
|
||
"github.com/cilium/ebpf" | ||
) | ||
|
||
// NsKey struct | ||
type NsKey struct { | ||
PidNS uint32 | ||
MntNS uint32 | ||
} | ||
|
||
// ContainerVal struct | ||
type ContainerVal struct { | ||
NsKey NsKey | ||
Policy string | ||
} | ||
|
||
// Containers struct | ||
type Containers struct { | ||
BPFContainerMap *ebpf.Map | ||
// ContainerID -> NsKey | ||
ContainerMap map[string]ContainerVal | ||
ContainerMapLock *sync.RWMutex | ||
} | ||
|
||
// NewContainers func | ||
func NewContainers(emap *ebpf.Map) *Containers { | ||
c := &Containers{} | ||
c.BPFContainerMap = emap | ||
c.ContainerMap = make(map[string]ContainerVal) | ||
c.ContainerMapLock = new(sync.RWMutex) | ||
|
||
return c | ||
} | ||
|
||
// AddContainerIDToMap function adds container to containers map | ||
func (c *Containers) AddContainerIDToMap(containerID string, pidns, mntns uint32) { | ||
ckv := NsKey{PidNS: pidns, MntNS: mntns} | ||
c.ContainerMapLock.Lock() | ||
defer c.ContainerMapLock.Unlock() | ||
c.ContainerMap[containerID] = ContainerVal{NsKey: ckv} | ||
} | ||
|
||
// DeleteContainerIDFromMap function removed container from container map and subsequently | ||
// from BPF Map as well returns error if failed | ||
func (c *Containers) DeleteContainerIDFromMap(id string) error { | ||
c.ContainerMapLock.Lock() | ||
defer c.ContainerMapLock.Unlock() | ||
|
||
if val, ok := c.ContainerMap[id]; ok { | ||
if err := c.DeleteContainerIDFromBPFMap(val.NsKey); err != nil { | ||
return err | ||
} | ||
delete(c.ContainerMap, id) | ||
} | ||
return nil | ||
} | ||
|
||
// DeleteContainerIDFromBPFMap deletes the container from BPF map and returns error if failed | ||
func (c *Containers) DeleteContainerIDFromBPFMap(ckv NsKey) error { | ||
if err := c.BPFContainerMap.Delete(ckv); err != nil { | ||
if !errors.Is(err, os.ErrNotExist) { | ||
return err | ||
} | ||
} | ||
return nil | ||
} |