From 41a20b5852d2cc7aa75de452e5a59a9d321e318d Mon Sep 17 00:00:00 2001 From: Sascha Grunert Date: Thu, 5 Dec 2019 13:14:16 +0100 Subject: [PATCH] Expose network interfaces via runc events The libcontainer network statistics are unreachable without manually creating a libcontainer instance. To retrieve them via the CLI interface of runc, we now expose them as well. Signed-off-by: Sascha Grunert --- events.go | 1 + libcontainer/network_linux.go | 5 +++-- libcontainer/stats.go | 15 --------------- libcontainer/stats_linux.go | 9 ++++++--- types/events.go | 27 +++++++++++++++++++++------ 5 files changed, 31 insertions(+), 26 deletions(-) delete mode 100644 libcontainer/stats.go diff --git a/events.go b/events.go index 9962ca13..fb3f6302 100644 --- a/events.go +++ b/events.go @@ -163,6 +163,7 @@ func convertLibcontainerStats(ls *libcontainer.Stats) *types.Stats { } } + s.NetworkInterfaces = ls.Interfaces return &s } diff --git a/libcontainer/network_linux.go b/libcontainer/network_linux.go index 569c53f6..938d8ce0 100644 --- a/libcontainer/network_linux.go +++ b/libcontainer/network_linux.go @@ -10,6 +10,7 @@ import ( "strings" "github.com/opencontainers/runc/libcontainer/configs" + "github.com/opencontainers/runc/types" "github.com/vishvananda/netlink" ) @@ -37,8 +38,8 @@ func getStrategy(tpe string) (networkStrategy, error) { } // Returns the network statistics for the network interfaces represented by the NetworkRuntimeInfo. -func getNetworkInterfaceStats(interfaceName string) (*NetworkInterface, error) { - out := &NetworkInterface{Name: interfaceName} +func getNetworkInterfaceStats(interfaceName string) (*types.NetworkInterface, error) { + out := &types.NetworkInterface{Name: interfaceName} // This can happen if the network runtime information is missing - possible if the // container was created by an old version of libcontainer. if interfaceName == "" { diff --git a/libcontainer/stats.go b/libcontainer/stats.go deleted file mode 100644 index 303e4b94..00000000 --- a/libcontainer/stats.go +++ /dev/null @@ -1,15 +0,0 @@ -package libcontainer - -type NetworkInterface struct { - // Name is the name of the network interface. - Name string - - RxBytes uint64 - RxPackets uint64 - RxErrors uint64 - RxDropped uint64 - TxBytes uint64 - TxPackets uint64 - TxErrors uint64 - TxDropped uint64 -} diff --git a/libcontainer/stats_linux.go b/libcontainer/stats_linux.go index 29fd641e..fff9dd37 100644 --- a/libcontainer/stats_linux.go +++ b/libcontainer/stats_linux.go @@ -1,10 +1,13 @@ package libcontainer -import "github.com/opencontainers/runc/libcontainer/cgroups" -import "github.com/opencontainers/runc/libcontainer/intelrdt" +import ( + "github.com/opencontainers/runc/libcontainer/cgroups" + "github.com/opencontainers/runc/libcontainer/intelrdt" + "github.com/opencontainers/runc/types" +) type Stats struct { - Interfaces []*NetworkInterface + Interfaces []*types.NetworkInterface CgroupStats *cgroups.Stats IntelRdtStats *intelrdt.Stats } diff --git a/types/events.go b/types/events.go index 85def228..c6f0e97a 100644 --- a/types/events.go +++ b/types/events.go @@ -9,12 +9,13 @@ type Event struct { // stats is the runc specific stats structure for stability when encoding and decoding stats. type Stats struct { - CPU Cpu `json:"cpu"` - Memory Memory `json:"memory"` - Pids Pids `json:"pids"` - Blkio Blkio `json:"blkio"` - Hugetlb map[string]Hugetlb `json:"hugetlb"` - IntelRdt IntelRdt `json:"intel_rdt"` + CPU Cpu `json:"cpu"` + Memory Memory `json:"memory"` + Pids Pids `json:"pids"` + Blkio Blkio `json:"blkio"` + Hugetlb map[string]Hugetlb `json:"hugetlb"` + IntelRdt IntelRdt `json:"intel_rdt"` + NetworkInterfaces []*NetworkInterface `json:"network_interfaces"` } type Hugetlb struct { @@ -113,3 +114,17 @@ type IntelRdt struct { // The memory bandwidth schema in 'container_id' group MemBwSchema string `json:"mem_bw_schema,omitempty"` } + +type NetworkInterface struct { + // Name is the name of the network interface. + Name string + + RxBytes uint64 + RxPackets uint64 + RxErrors uint64 + RxDropped uint64 + TxBytes uint64 + TxPackets uint64 + TxErrors uint64 + TxDropped uint64 +}