Skip to content

Commit

Permalink
feat(internal/app): add pod deletion handling and related metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
nullswan committed Sep 22, 2024
1 parent 7a4575f commit 366953e
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
6 changes: 5 additions & 1 deletion internal/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,15 @@ func Run(
go bpf.ConsumeEvents(ctx, log, bpfCtx.NetworkRingBuffer, networkEventChan)

var shaResolver *workload.ShaResolver
var deletedPodChan chan string
if kubernetesMode {
shaResolver, err = workload.NewShaResolver()
deletedPodChan = make(chan string)
shaResolver, err = workload.NewShaResolver(log, deletedPodChan)
if err != nil {
return fmt.Errorf("failed to create sha resolver: %w", err)
}

go deletePods(ctx, log, deletedPodChan)
}

bannedCgroupIDs := lru.New[uint64, struct{}](cacheBannedSz)
Expand Down
26 changes: 26 additions & 0 deletions internal/app/delete_pods.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package app

import (
"context"
"log/slog"

"github.com/nullswan/bpfsnitch/internal/metrics"
)

func deletePods(
ctx context.Context,
logger *slog.Logger,
deletedPodsChan chan string,
) {
for {
select {
case <-ctx.Done():
return
case podID := <-deletedPodsChan:
for _, counter := range metrics.PodBasedMetrics {
logger.With("pod", podID).Info("Deleted pod-based metrics")
counter.DeleteLabelValues(podID)
}
}
}
}
9 changes: 9 additions & 0 deletions internal/metrics/counters.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,12 @@ var NetworkReceivedPacketsCounter = prometheus.NewCounterVec(
},
[]string{"pod", "remote_subnet"},
)

var PodBasedMetrics = []*prometheus.CounterVec{
DNSQueryCounter,
SyscallCounter,
NetworkReceivedBytesCounter,
NetworkSentBytesCounter,
NetworkSentPacketsCounter,
NetworkReceivedPacketsCounter,
}

0 comments on commit 366953e

Please sign in to comment.