netmon: Rely on agent/pkg/types instead of duplicating types

Now that the agent has split the generic types in their own package,
kata-netmon can use them directly and get rid of the duplication of
those. This is very helpful as it will prevent structures from being
out of sync between kata-netmon and the kata-runtime, without bringing
in the huge overhead that the initial grpc package was introducing.

Fixes #857

Signed-off-by: Sebastien Boeuf <sebastien.boeuf@intel.com>
This commit is contained in:
Sebastien Boeuf 2018-10-25 09:52:45 -07:00
parent 309dcf9977
commit 38d56c994e
2 changed files with 34 additions and 67 deletions

View File

@ -21,6 +21,7 @@ import (
"syscall"
"time"
"github.com/kata-containers/agent/pkg/types"
"github.com/kata-containers/runtime/pkg/signals"
"github.com/sirupsen/logrus"
lSyslog "github.com/sirupsen/logrus/hooks/syslog"
@ -28,41 +29,6 @@ import (
"golang.org/x/sys/unix"
)
// The following types and structures have to be kept in sync with the
// description of the agent protocol. Those definitions need to be in their
// own separate package so that they can be imported directly from this code.
// The reason for not importing them now, is because importing the whole agent
// protocol adds up too much overhead because of the grpc protocol involved.
// IPFamily define the IP address family type.
type IPFamily int32
// IPAddress describes the IP address format expected by Kata API.
type IPAddress struct {
Family IPFamily `json:"family,omitempty"`
Address string `json:"address,omitempty"`
Mask string `json:"mask,omitempty"`
}
// Interface describes the network interface format expected by Kata API.
type Interface struct {
Device string `json:"device,omitempty"`
Name string `json:"name,omitempty"`
IPAddresses []*IPAddress `json:"IPAddresses,omitempty"`
Mtu uint64 `json:"mtu,omitempty"`
HwAddr string `json:"hwAddr,omitempty"`
PciAddr string `json:"pciAddr,omitempty"`
}
// Route describes the network route format expected by Kata API.
type Route struct {
Dest string `json:"dest,omitempty"`
Gateway string `json:"gateway,omitempty"`
Device string `json:"device,omitempty"`
Source string `json:"source,omitempty"`
Scope uint32 `json:"scope,omitempty"`
}
const (
netmonName = "kata-netmon"
@ -104,7 +70,7 @@ type netmon struct {
storagePath string
sharedFile string
netIfaces map[int]Interface
netIfaces map[int]types.Interface
linkUpdateCh chan netlink.LinkUpdate
linkDoneCh chan struct{}
@ -185,7 +151,7 @@ func newNetmon(params netmonParams) (*netmon, error) {
netmonParams: params,
storagePath: filepath.Join(storageParentPath, params.sandboxID),
sharedFile: filepath.Join(storageParentPath, params.sandboxID, sharedFile),
netIfaces: make(map[int]Interface),
netIfaces: make(map[int]types.Interface),
linkUpdateCh: make(chan netlink.LinkUpdate),
linkDoneCh: make(chan struct{}),
rtUpdateCh: make(chan netlink.RouteUpdate),
@ -293,13 +259,13 @@ func (n *netmon) listenNetlinkEvents() error {
// convertInterface converts a link and its IP addresses as defined by netlink
// package, into the Interface structure format expected by kata-runtime to
// describe an interface and its associated IP addresses.
func convertInterface(linkAttrs *netlink.LinkAttrs, addrs []netlink.Addr) Interface {
func convertInterface(linkAttrs *netlink.LinkAttrs, addrs []netlink.Addr) types.Interface {
if linkAttrs == nil {
netmonLog.Warn("Link attributes are nil")
return Interface{}
return types.Interface{}
}
var ipAddrs []*IPAddress
var ipAddrs []*types.IPAddress
for _, addr := range addrs {
if addr.IPNet == nil {
@ -308,8 +274,8 @@ func convertInterface(linkAttrs *netlink.LinkAttrs, addrs []netlink.Addr) Interf
netMask, _ := addr.Mask.Size()
ipAddr := &IPAddress{
Family: IPFamily(netlinkFamily),
ipAddr := &types.IPAddress{
Family: types.IPFamily(netlinkFamily),
Address: addr.IP.String(),
Mask: fmt.Sprintf("%d", netMask),
}
@ -317,7 +283,7 @@ func convertInterface(linkAttrs *netlink.LinkAttrs, addrs []netlink.Addr) Interf
ipAddrs = append(ipAddrs, ipAddr)
}
iface := Interface{
iface := types.Interface{
Device: linkAttrs.Name,
Name: linkAttrs.Name,
IPAddresses: ipAddrs,
@ -333,8 +299,8 @@ func convertInterface(linkAttrs *netlink.LinkAttrs, addrs []netlink.Addr) Interf
// convertRoutes converts a list of routes as defined by netlink package,
// into a list of Route structure format expected by kata-runtime to
// describe a set of routes.
func convertRoutes(netRoutes []netlink.Route) []Route {
var routes []Route
func convertRoutes(netRoutes []netlink.Route) []types.Route {
var routes []types.Route
// Ignore routes with IPv6 addresses as this is not supported
// by Kata yet.
@ -368,7 +334,7 @@ func convertRoutes(netRoutes []netlink.Route) []Route {
dev = iface.Name
}
route := Route{
route := types.Route{
Dest: dst,
Gateway: gw,
Device: dev,
@ -440,7 +406,7 @@ func (n *netmon) execKataCmd(subCmd string) error {
return os.Remove(n.sharedFile)
}
func (n *netmon) addInterfaceCLI(iface Interface) error {
func (n *netmon) addInterfaceCLI(iface types.Interface) error {
if err := n.storeDataToSend(iface); err != nil {
return err
}
@ -448,7 +414,7 @@ func (n *netmon) addInterfaceCLI(iface Interface) error {
return n.execKataCmd(kataCLIAddIfaceCmd)
}
func (n *netmon) delInterfaceCLI(iface Interface) error {
func (n *netmon) delInterfaceCLI(iface types.Interface) error {
if err := n.storeDataToSend(iface); err != nil {
return err
}
@ -456,7 +422,7 @@ func (n *netmon) delInterfaceCLI(iface Interface) error {
return n.execKataCmd(kataCLIDelIfaceCmd)
}
func (n *netmon) updateRoutesCLI(routes []Route) error {
func (n *netmon) updateRoutesCLI(routes []types.Route) error {
if err := n.storeDataToSend(routes); err != nil {
return err
}

View File

@ -16,6 +16,7 @@ import (
"runtime"
"testing"
"github.com/kata-containers/agent/pkg/types"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/vishvananda/netlink"
@ -173,14 +174,14 @@ func TestConvertInterface(t *testing.T) {
HardwareAddr: hwAddr,
}
expected := Interface{
expected := types.Interface{
Device: testIfaceName,
Name: testIfaceName,
Mtu: uint64(testMTU),
HwAddr: testHwAddr,
IPAddresses: []*IPAddress{
IPAddresses: []*types.IPAddress{
{
Family: IPFamily(netlinkFamily),
Family: types.IPFamily(netlinkFamily),
Address: testIPAddress,
Mask: "0",
},
@ -207,7 +208,7 @@ func TestConvertRoutes(t *testing.T) {
},
}
expected := []Route{
expected := []types.Route{
{
Dest: testIPAddress,
Gateway: testIPAddress,
@ -241,7 +242,7 @@ func testSetupNetwork(t *testing.T) testTeardownNetwork {
}
}
func testCreateDummyNetwork(t *testing.T, handler *netlink.Handle) (int, Interface) {
func testCreateDummyNetwork(t *testing.T, handler *netlink.Handle) (int, types.Interface) {
hwAddr, err := net.ParseMAC(testHwAddr)
assert.Nil(t, err)
@ -262,7 +263,7 @@ func testCreateDummyNetwork(t *testing.T, handler *netlink.Handle) (int, Interfa
attrs := link.Attrs()
assert.NotNil(t, attrs)
iface := Interface{
iface := types.Interface{
Device: testIfaceName,
Name: testIfaceName,
Mtu: uint64(testMTU),
@ -284,7 +285,7 @@ func TestScanNetwork(t *testing.T) {
idx, expected := testCreateDummyNetwork(t, handler)
n := &netmon{
netIfaces: make(map[int]Interface),
netIfaces: make(map[int]types.Interface),
netHandler: handler,
}
@ -295,9 +296,9 @@ func TestScanNetwork(t *testing.T) {
}
func TestStoreDataToSend(t *testing.T) {
var got Interface
var got types.Interface
expected := Interface{
expected := types.Interface{
Device: testIfaceName,
Name: testIfaceName,
Mtu: uint64(testMTU),
@ -394,15 +395,15 @@ func TestActionsCLI(t *testing.T) {
defer os.RemoveAll(testStorageParentPath)
// Test addInterfaceCLI
err = n.addInterfaceCLI(Interface{})
err = n.addInterfaceCLI(types.Interface{})
assert.Nil(t, err)
// Test delInterfaceCLI
err = n.delInterfaceCLI(Interface{})
err = n.delInterfaceCLI(types.Interface{})
assert.Nil(t, err)
// Test updateRoutesCLI
err = n.updateRoutesCLI([]Route{})
err = n.updateRoutesCLI([]types.Route{})
assert.Nil(t, err)
tearDownNetworkCb := testSetupNetwork(t)
@ -460,8 +461,8 @@ func TestHandleRTMNewLink(t *testing.T) {
assert.Nil(t, err)
// Interface already exist in list
n.netIfaces = make(map[int]Interface)
n.netIfaces[testIfaceIndex] = Interface{}
n.netIfaces = make(map[int]types.Interface)
n.netIfaces[testIfaceIndex] = types.Interface{}
ev = netlink.LinkUpdate{
Link: &netlink.Dummy{
LinkAttrs: netlink.LinkAttrs{
@ -474,7 +475,7 @@ func TestHandleRTMNewLink(t *testing.T) {
assert.Nil(t, err)
// Flags are not up and running
n.netIfaces = make(map[int]Interface)
n.netIfaces = make(map[int]types.Interface)
ev = netlink.LinkUpdate{
Link: &netlink.Dummy{
LinkAttrs: netlink.LinkAttrs{
@ -487,7 +488,7 @@ func TestHandleRTMNewLink(t *testing.T) {
assert.Nil(t, err)
// Invalid link
n.netIfaces = make(map[int]Interface)
n.netIfaces = make(map[int]types.Interface)
ev = netlink.LinkUpdate{
Link: &netlink.Dummy{
LinkAttrs: netlink.LinkAttrs{
@ -528,7 +529,7 @@ func TestHandleRTMDelLink(t *testing.T) {
assert.Nil(t, err)
// Interface does not exist in list
n.netIfaces = make(map[int]Interface)
n.netIfaces = make(map[int]types.Interface)
ev = netlink.LinkUpdate{
Link: &netlink.Dummy{
LinkAttrs: netlink.LinkAttrs{
@ -543,7 +544,7 @@ func TestHandleRTMDelLink(t *testing.T) {
func TestHandleRTMNewRouteIfaceNotFound(t *testing.T) {
n := &netmon{
netIfaces: make(map[int]Interface),
netIfaces: make(map[int]types.Interface),
}
err := n.handleRTMNewRoute(netlink.RouteUpdate{})