Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

process: implement lazy retrieval of network namespace #86

Merged
merged 1 commit into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions containers/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -922,7 +922,7 @@ func (c *Container) gc(now time.Time) {
listens := map[netaddr.IPPort]string{}
seenNamespaces := map[string]bool{}
for _, p := range c.processes {
if seenNamespaces[p.NetNsId] {
if seenNamespaces[p.NetNsId()] {
continue
}
sockets, err := proc.GetSockets(p.Pid)
Expand All @@ -937,7 +937,7 @@ func (c *Container) gc(now time.Time) {
establishedDst[s.DAddr] = struct{}{}
}
}
seenNamespaces[p.NetNsId] = true
seenNamespaces[p.NetNsId()] = true
}

for ns := range c.ipsByNs {
Expand Down
24 changes: 16 additions & 8 deletions containers/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ import (
type Process struct {
Pid uint32
StartedAt time.Time
NetNsId string

netNsId string

ctx context.Context
cancelFunc context.CancelFunc
Expand All @@ -28,19 +29,26 @@ type Process struct {
}

func NewProcess(pid uint32, stats *taskstats.Stats) *Process {
ns, err := proc.GetNetNs(pid)
if err != nil {
return nil
}
defer ns.Close()
p := &Process{Pid: pid, StartedAt: stats.BeginTime, NetNsId: ns.UniqueId()}
p := &Process{Pid: pid, StartedAt: stats.BeginTime}
p.ctx, p.cancelFunc = context.WithCancel(context.Background())
go p.instrument()
return p
}

func (p *Process) NetNsId() string {
if p.netNsId == "" {
ns, err := proc.GetNetNs(p.Pid)
if err != nil {
return ""
}
p.netNsId = ns.UniqueId()
_ = ns.Close()
}
return p.netNsId
}

func (p *Process) isHostNs() bool {
return p.NetNsId == hostNetNsId
return p.NetNsId() == hostNetNsId
}

func (p *Process) instrument() {
Expand Down