From 31a572749e24ec2548ab137c70e6a35204b1dfcf Mon Sep 17 00:00:00 2001 From: "bingshen.wbs" Date: Wed, 17 Jul 2024 17:47:24 +0800 Subject: [PATCH] fix netns info expire on sidecar mode Signed-off-by: bingshen.wbs --- pkg/exporter/nettop/cache.go | 17 ++++++++++------- pkg/exporter/nettop/interface.go | 2 +- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/pkg/exporter/nettop/cache.go b/pkg/exporter/nettop/cache.go index 34e7a4fe..c62c2b09 100644 --- a/pkg/exporter/nettop/cache.go +++ b/pkg/exporter/nettop/cache.go @@ -119,8 +119,8 @@ func initDefaultEntity(sidecarMode bool) error { } ent := []*Entity{defaultEntity} entities.Store(&ent) - addEntityToCache(defaultEntity, false) } + addEntityToCache(defaultEntity, false, true) return nil } @@ -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) } } @@ -333,7 +337,6 @@ func cacheNetTopology(ctx context.Context) error { var newEntities []*Entity newEntities = append(newEntities, defaultEntity) - addEntityToCache(defaultEntity, false) sandboxList, err := criClient.ListPodSandbox(&v1.PodSandboxFilter{ State: &v1.PodSandboxStateValue{ @@ -420,7 +423,7 @@ func cacheNetTopology(ctx context.Context) error { } newEntities = append(newEntities, e) - addEntityToCache(e, true) + addEntityToCache(e, true, false) } entities.Store(&newEntities) diff --git a/pkg/exporter/nettop/interface.go b/pkg/exporter/nettop/interface.go index 2edfe4d6..fb2d0881 100644 --- a/pkg/exporter/nettop/interface.go +++ b/pkg/exporter/nettop/interface.go @@ -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) {