Skip to content

Commit

Permalink
Merge pull request #325 from kubescape/loop
Browse files Browse the repository at this point in the history
avoid infinite loop while unregistering container
  • Loading branch information
matthyx authored Jul 11, 2024
2 parents 41d0a51 + cbe9297 commit 2224cda
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
8 changes: 6 additions & 2 deletions pkg/containerwatcher/v1/container_watcher_private.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ func (ch *IGContainerWatcher) containerCallback(notif containercollection.PubSub

// do not trace the node-agent pod
if ch.ignoreContainer(notif.Container.K8s.Namespace, notif.Container.K8s.PodName) {
ch.unregisterContainer(notif.Container)
// avoid loops when the container is being removed
if notif.Type == containercollection.EventTypeAddContainer {
ch.unregisterContainer(notif.Container)
}
return
}

Expand Down Expand Up @@ -66,6 +69,7 @@ func (ch *IGContainerWatcher) containerCallback(notif containercollection.PubSub
ch.timeBasedContainers.Remove(notif.Container.Runtime.ContainerID)
}
}

func (ch *IGContainerWatcher) startContainerCollection(ctx context.Context) error {
ch.ctx = ctx

Expand Down Expand Up @@ -345,7 +349,7 @@ func (ch *IGContainerWatcher) unregisterContainer(container *containercollection
return
}

logger.L().Info("stopping to monitor on container", helpers.String("container ID", container.Runtime.ContainerID), helpers.String("namespace", container.K8s.Namespace), helpers.String("PodName", container.K8s.PodName), helpers.String("ContainerName", container.K8s.ContainerName))
logger.L().Debug("stopping to monitor on container", helpers.String("container ID", container.Runtime.ContainerID), helpers.String("namespace", container.K8s.Namespace), helpers.String("PodName", container.K8s.PodName), helpers.String("ContainerName", container.K8s.ContainerName))

ch.containerCollection.RemoveContainer(container.Runtime.ContainerID)

Expand Down
6 changes: 4 additions & 2 deletions pkg/containerwatcher/v1/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package containerwatcher

import (
"fmt"
"strings"

"github.com/inspektor-gadget/inspektor-gadget/pkg/container-collection/networktracer"
tracernetwork "github.com/inspektor-gadget/inspektor-gadget/pkg/gadgets/trace/network/tracer"
Expand All @@ -21,9 +22,10 @@ func (ch *IGContainerWatcher) networkEventCallback(event *tracernetworktypes.Eve

if event.Type != types.NORMAL {
// dropped event
logger.L().Ctx(ch.ctx).Warning("network tracer got drop events - we may miss some realtime data", helpers.Interface("event", event), helpers.String("error", event.Message))
if !strings.Contains(event.Message, "stop tracing container") {
logger.L().Ctx(ch.ctx).Warning("network tracer got drop events - we may miss some realtime data", helpers.Interface("event", event), helpers.String("error", event.Message))
}
} else {

ch.containerCollection.EnrichByMntNs(&event.CommonData, event.MountNsID)
ch.containerCollection.EnrichByNetNs(&event.CommonData, event.NetNsID)

Expand Down

0 comments on commit 2224cda

Please sign in to comment.