Skip to content

Commit

Permalink
crio: only collect required metrics for infra container
Browse files Browse the repository at this point in the history
as we'll only use the network metrics from it

Signed-off-by: Peter Hunt <[email protected]>
  • Loading branch information
haircommander committed Sep 12, 2024
1 parent c15f44e commit 39f93dc
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
23 changes: 16 additions & 7 deletions container/crio/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,13 +148,22 @@ func newCrioContainerHandler(
Namespace: CrioNamespace,
}

// Find out if we need network metrics reported for this container.
// Containers that don't have their own network -- this includes
// containers running in Kubernetes pods that use the network of the
// infrastructure container -- does not need their stats to be
// reported. This stops metrics being reported multiple times for each
// container in a pod.
metrics := common.RemoveNetMetrics(includedMetrics, cInfo.Labels["io.kubernetes.container.name"] != "POD")
nonNetworkMetrics := common.RemoveNetMetrics(includedMetrics, true)
// If it's a non-infra container, then we can exclude network metrics,
// because in kubernetes a container always shares the network namespace
// with the infra container.
metrics := nonNetworkMetrics
// If this is the infra container cgroup, then we only need to collect network metrics
// as the rest will be discarded.
if cInfo.Labels["io.kubernetes.container.name"] == "POD" {
// Only include the non-non-network metrics. This way, we can tell if there are no network metrics
metrics = includedMetrics.Difference(nonNetworkMetrics)
}

// No metrics are being collected, no need to create a handler.
if len(metrics) == 0 {
return nil, nil
}

libcontainerHandler := containerlibcontainer.NewHandler(cgroupManager, rootFs, cInfo.Pid, metrics)

Expand Down
6 changes: 3 additions & 3 deletions container/crio/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func TestHandler(t *testing.T) {
nil,
false,
nil,
nil,
container.AllMetrics,

true,
"no client returned",
Expand All @@ -70,7 +70,7 @@ func TestHandler(t *testing.T) {
nil,
false,
nil,
nil,
container.AllMetrics,

true,
"no container with id 81e5c2990803c383229c9680ce964738d5e566d97f5bd436ac34808d2ec75d5f",
Expand All @@ -90,7 +90,7 @@ func TestHandler(t *testing.T) {
nil,
false,
nil,
nil,
container.AllMetrics,

false,
"",
Expand Down

0 comments on commit 39f93dc

Please sign in to comment.