Skip to content

Commit

Permalink
fix netns info expire on sidecar mode
Browse files Browse the repository at this point in the history
Signed-off-by: bingshen.wbs <[email protected]>
  • Loading branch information
BSWANG committed Jul 17, 2024
1 parent b6b2f73 commit 193286e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
20 changes: 12 additions & 8 deletions pkg/exporter/nettop/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func initDefaultEntity(sidecarMode bool) error {
}
ent := []*Entity{defaultEntity}
entities.Store(&ent)
addEntityToCache(defaultEntity, false)
addEntityToCache(defaultEntity, false, true)
}

return nil
Expand Down Expand Up @@ -283,15 +283,19 @@ func cachePodsWithTimeout(timeout time.Duration) error {
}
}

func addEntityToCache(e *Entity, ignoreHostPod bool) {
func addEntityToCache(e *Entity, ignoreHostPod, noExpiration bool) {
expirationTime := 3 * cacheUpdateInterval
if noExpiration {
expirationTime = cache.NoExpiration
}
if !(ignoreHostPod && e.IsHostNetwork()) {
nsCache.Set(fmt.Sprintf("%d", e.inum), e, 3*cacheUpdateInterval)
nsCache.Set(fmt.Sprintf("%d", e.inum), e, expirationTime)
}
for _, ip := range e.ipList {
ipCache.Set(ip, e, 3*cacheUpdateInterval)
ipCache.Set(ip, e, expirationTime)
}
for _, pid := range e.pids {
pidCache.Set(fmt.Sprintf("%d", pid), e, 3*cacheUpdateInterval)
pidCache.Set(fmt.Sprintf("%d", pid), e, expirationTime)
}
}

Expand Down Expand Up @@ -333,7 +337,7 @@ func cacheNetTopology(ctx context.Context) error {

var newEntities []*Entity
newEntities = append(newEntities, defaultEntity)
addEntityToCache(defaultEntity, false)
addEntityToCache(defaultEntity, false, true)

sandboxList, err := criClient.ListPodSandbox(&v1.PodSandboxFilter{
State: &v1.PodSandboxStateValue{
Expand Down Expand Up @@ -393,7 +397,7 @@ func cacheNetTopology(ctx context.Context) error {
var ns *netnsMeta

if netnsNum == defaultEntity.inum {
ns = defaultEntity.netnsMeta
continue
} else {

Check warning on line 401 in pkg/exporter/nettop/cache.go

View workflow job for this annotation

GitHub Actions / lint

superfluous-else: if block ends with a continue statement, so drop this else and outdent its block (revive)
status := sandboxStatus.Status
if status.Network == nil || status.Network.Ip == "" {
Expand All @@ -420,7 +424,7 @@ func cacheNetTopology(ctx context.Context) error {
}

newEntities = append(newEntities, e)
addEntityToCache(e, true)
addEntityToCache(e, true, false)
}

entities.Store(&newEntities)
Expand Down
2 changes: 1 addition & 1 deletion pkg/exporter/nettop/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func GetEntityByNetns(nsinum int) (*Entity, error) {
if found {
return v.(*Entity), nil
}
return nil, fmt.Errorf("entify for netns %d not found", nsinum)
return nil, fmt.Errorf("entity for netns %d not found", nsinum)
}

func GetHostNetworkEntity() (*Entity, error) {
Expand Down

0 comments on commit 193286e

Please sign in to comment.