@@ -33,7 +33,6 @@ import (
33
33
"github.com/google/cadvisor/zfs"
34
34
"github.com/opencontainers/runc/libcontainer/cgroups"
35
35
36
- dockercontainer "github.com/docker/docker/api/types/container"
37
36
docker "github.com/docker/docker/client"
38
37
"golang.org/x/net/context"
39
38
"k8s.io/klog/v2"
@@ -72,9 +71,6 @@ type dockerContainerHandler struct {
72
71
// Image name used for this container.
73
72
image string
74
73
75
- // The network mode of the container
76
- networkMode dockercontainer.NetworkMode
77
-
78
74
// Filesystem handler.
79
75
fsHandler common.FsHandler
80
76
@@ -188,6 +184,9 @@ func newDockerContainerHandler(
188
184
return nil , fmt .Errorf ("failed to inspect container %q: %v" , id , err )
189
185
}
190
186
187
+ // Do not report network metrics for containers that share netns with another container.
188
+ metrics := common .RemoveNetMetrics (includedMetrics , ctnr .HostConfig .NetworkMode .IsContainer ())
189
+
191
190
// TODO: extract object mother method
192
191
handler := & dockerContainerHandler {
193
192
machineInfoFactory : machineInfoFactory ,
@@ -198,7 +197,7 @@ func newDockerContainerHandler(
198
197
rootfsStorageDir : rootfsStorageDir ,
199
198
envs : make (map [string ]string ),
200
199
labels : ctnr .Config .Labels ,
201
- includedMetrics : includedMetrics ,
200
+ includedMetrics : metrics ,
202
201
zfsParent : zfsParent ,
203
202
}
204
203
// Timestamp returned by Docker is in time.RFC3339Nano format.
@@ -207,7 +206,7 @@ func newDockerContainerHandler(
207
206
// This should not happen, report the error just in case
208
207
return nil , fmt .Errorf ("failed to parse the create timestamp %q for container %q: %v" , ctnr .Created , id , err )
209
208
}
210
- handler .libcontainerHandler = containerlibcontainer .NewHandler (cgroupManager , rootFs , ctnr .State .Pid , includedMetrics )
209
+ handler .libcontainerHandler = containerlibcontainer .NewHandler (cgroupManager , rootFs , ctnr .State .Pid , metrics )
211
210
212
211
// Add the name and bare ID as aliases of the container.
213
212
handler .reference = info.ContainerReference {
@@ -217,7 +216,6 @@ func newDockerContainerHandler(
217
216
Namespace : DockerNamespace ,
218
217
}
219
218
handler .image = ctnr .Config .Image
220
- handler .networkMode = ctnr .HostConfig .NetworkMode
221
219
// Only adds restartcount label if it's greater than 0
222
220
if ctnr .RestartCount > 0 {
223
221
handler .labels ["restartcount" ] = strconv .Itoa (ctnr .RestartCount )
@@ -344,16 +342,10 @@ func (h *dockerContainerHandler) ContainerReference() (info.ContainerReference,
344
342
return h .reference , nil
345
343
}
346
344
347
- func (h * dockerContainerHandler ) needNet () bool {
348
- if h .includedMetrics .Has (container .NetworkUsageMetrics ) {
349
- return ! h .networkMode .IsContainer ()
350
- }
351
- return false
352
- }
353
-
354
345
func (h * dockerContainerHandler ) GetSpec () (info.ContainerSpec , error ) {
355
346
hasFilesystem := h .includedMetrics .Has (container .DiskUsageMetrics )
356
- spec , err := common .GetSpec (h .cgroupPaths , h .machineInfoFactory , h .needNet (), hasFilesystem )
347
+ hasNetwork := h .includedMetrics .Has (container .NetworkUsageMetrics )
348
+ spec , err := common .GetSpec (h .cgroupPaths , h .machineInfoFactory , hasNetwork , hasFilesystem )
357
349
358
350
spec .Labels = h .labels
359
351
spec .Envs = h .envs
@@ -462,13 +454,6 @@ func (h *dockerContainerHandler) GetStats() (*info.ContainerStats, error) {
462
454
if err != nil {
463
455
return stats , err
464
456
}
465
- // Clean up stats for containers that don't have their own network - this
466
- // includes containers running in Kubernetes pods that use the network of the
467
- // infrastructure container. This stops metrics being reported multiple times
468
- // for each container in a pod.
469
- if ! h .needNet () {
470
- stats .Network = info.NetworkStats {}
471
- }
472
457
473
458
// Get filesystem stats.
474
459
err = h .getFsStats (stats )
0 commit comments