in pkg/exporter/nettop/cache.go [394:492]
func cacheNetTopology(ctx context.Context) error {
lock.Lock()
defer lock.Unlock()
var newEntities []*Entity
newEntities = append(newEntities, defaultEntity)
sandboxList, err := criClient.ListPodSandbox(&v1.PodSandboxFilter{
State: &v1.PodSandboxStateValue{
State: v1.PodSandboxState_SANDBOX_READY,
},
})
if err != nil {
return fmt.Errorf("failed list pod sandboxes: %w", err)
}
for _, sandbox := range sandboxList {
if contextDone(ctx) {
return fmt.Errorf("timeout")
}
if sandbox.Metadata == nil {
log.Errorf("invalid sandbox who has no metadata, id %s", sandbox.Id)
}
namespace := sandbox.Metadata.Namespace
name := sandbox.Metadata.Name
labels := sandbox.Labels
sandboxStatus, err := criClient.PodSandboxStatus(sandbox.Id, true)
if err != nil {
log.Errorf("sandbox: %s/%s failed get status err: %v", namespace, name, err)
continue
}
if sandboxStatus.Status == nil {
log.Errorf("sandbox %s/%s: invalid sandbox status", sandbox.Metadata.Namespace, sandbox.Metadata.Name)
continue
}
info, err := getSandboxInfoSpec(sandboxStatus)
if err != nil {
log.Errorf("failed get sandbox info: %v", err)
continue
}
netnsNum, err := getNsInumByPid(info.Pid)
if err != nil {
log.Errorf("failed get netns for initPid %d, err: %v", info.Pid, err)
continue
}
podCgroupPath := info.Config.Linux.CgroupParent
var pids []int
if podCgroupPath != "" {
pids = tasksInsidePodCgroup(podCgroupPath, false)
if len(pids) == 0 {
log.Warnf("sandbox %s/%s: found 0 pids under cgroup %s", namespace, name, podCgroupPath)
}
}
var ns *netnsMeta
if netnsNum == defaultEntity.inum {
ns = defaultEntity.netnsMeta
} else {
status := sandboxStatus.Status
if status.Network == nil || status.Network.Ip == "" {
log.Errorf("sanbox %s/%s: invalid sandbox status, no ip", sandbox.Metadata.Namespace, sandbox.Metadata.Name)
continue
}
ns = &netnsMeta{
inum: netnsNum,
mountPath: fmt.Sprintf("/proc/%d/ns/net", info.Pid),
isHostNetwork: false,
ipList: []string{status.Network.Ip},
}
}
e := &Entity{
netnsMeta: ns,
podMeta: podMeta{
name: name,
namespace: namespace,
},
initPid: info.Pid,
labels: labels,
pids: pids,
}
newEntities = append(newEntities, e)
addEntityToCache(e, true, false)
}
entities.Store(&newEntities)
log.Debug("finished cache process")
return nil
}