diff --git a/container/crio/handler.go b/container/crio/handler.go index a68325187e..8ac72f8f16 100644 --- a/container/crio/handler.go +++ b/container/crio/handler.go @@ -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) diff --git a/container/crio/handler_test.go b/container/crio/handler_test.go index dd713b559e..0a6a7bf38a 100644 --- a/container/crio/handler_test.go +++ b/container/crio/handler_test.go @@ -54,7 +54,7 @@ func TestHandler(t *testing.T) { nil, false, nil, - nil, + container.AllMetrics, true, "no client returned", @@ -70,7 +70,7 @@ func TestHandler(t *testing.T) { nil, false, nil, - nil, + container.AllMetrics, true, "no container with id 81e5c2990803c383229c9680ce964738d5e566d97f5bd436ac34808d2ec75d5f", @@ -90,7 +90,7 @@ func TestHandler(t *testing.T) { nil, false, nil, - nil, + container.AllMetrics, false, "",