mirror of
https://github.com/kata-containers/kata-containers.git
synced 2025-07-20 10:20:39 +00:00
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:
parent
309dcf9977
commit
38d56c994e
@ -21,6 +21,7 @@ import (
|
|||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/kata-containers/agent/pkg/types"
|
||||||
"github.com/kata-containers/runtime/pkg/signals"
|
"github.com/kata-containers/runtime/pkg/signals"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
lSyslog "github.com/sirupsen/logrus/hooks/syslog"
|
lSyslog "github.com/sirupsen/logrus/hooks/syslog"
|
||||||
@ -28,41 +29,6 @@ import (
|
|||||||
"golang.org/x/sys/unix"
|
"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 (
|
const (
|
||||||
netmonName = "kata-netmon"
|
netmonName = "kata-netmon"
|
||||||
|
|
||||||
@ -104,7 +70,7 @@ type netmon struct {
|
|||||||
storagePath string
|
storagePath string
|
||||||
sharedFile string
|
sharedFile string
|
||||||
|
|
||||||
netIfaces map[int]Interface
|
netIfaces map[int]types.Interface
|
||||||
|
|
||||||
linkUpdateCh chan netlink.LinkUpdate
|
linkUpdateCh chan netlink.LinkUpdate
|
||||||
linkDoneCh chan struct{}
|
linkDoneCh chan struct{}
|
||||||
@ -185,7 +151,7 @@ func newNetmon(params netmonParams) (*netmon, error) {
|
|||||||
netmonParams: params,
|
netmonParams: params,
|
||||||
storagePath: filepath.Join(storageParentPath, params.sandboxID),
|
storagePath: filepath.Join(storageParentPath, params.sandboxID),
|
||||||
sharedFile: filepath.Join(storageParentPath, params.sandboxID, sharedFile),
|
sharedFile: filepath.Join(storageParentPath, params.sandboxID, sharedFile),
|
||||||
netIfaces: make(map[int]Interface),
|
netIfaces: make(map[int]types.Interface),
|
||||||
linkUpdateCh: make(chan netlink.LinkUpdate),
|
linkUpdateCh: make(chan netlink.LinkUpdate),
|
||||||
linkDoneCh: make(chan struct{}),
|
linkDoneCh: make(chan struct{}),
|
||||||
rtUpdateCh: make(chan netlink.RouteUpdate),
|
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
|
// convertInterface converts a link and its IP addresses as defined by netlink
|
||||||
// package, into the Interface structure format expected by kata-runtime to
|
// package, into the Interface structure format expected by kata-runtime to
|
||||||
// describe an interface and its associated IP addresses.
|
// 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 {
|
if linkAttrs == nil {
|
||||||
netmonLog.Warn("Link attributes are nil")
|
netmonLog.Warn("Link attributes are nil")
|
||||||
return Interface{}
|
return types.Interface{}
|
||||||
}
|
}
|
||||||
|
|
||||||
var ipAddrs []*IPAddress
|
var ipAddrs []*types.IPAddress
|
||||||
|
|
||||||
for _, addr := range addrs {
|
for _, addr := range addrs {
|
||||||
if addr.IPNet == nil {
|
if addr.IPNet == nil {
|
||||||
@ -308,8 +274,8 @@ func convertInterface(linkAttrs *netlink.LinkAttrs, addrs []netlink.Addr) Interf
|
|||||||
|
|
||||||
netMask, _ := addr.Mask.Size()
|
netMask, _ := addr.Mask.Size()
|
||||||
|
|
||||||
ipAddr := &IPAddress{
|
ipAddr := &types.IPAddress{
|
||||||
Family: IPFamily(netlinkFamily),
|
Family: types.IPFamily(netlinkFamily),
|
||||||
Address: addr.IP.String(),
|
Address: addr.IP.String(),
|
||||||
Mask: fmt.Sprintf("%d", netMask),
|
Mask: fmt.Sprintf("%d", netMask),
|
||||||
}
|
}
|
||||||
@ -317,7 +283,7 @@ func convertInterface(linkAttrs *netlink.LinkAttrs, addrs []netlink.Addr) Interf
|
|||||||
ipAddrs = append(ipAddrs, ipAddr)
|
ipAddrs = append(ipAddrs, ipAddr)
|
||||||
}
|
}
|
||||||
|
|
||||||
iface := Interface{
|
iface := types.Interface{
|
||||||
Device: linkAttrs.Name,
|
Device: linkAttrs.Name,
|
||||||
Name: linkAttrs.Name,
|
Name: linkAttrs.Name,
|
||||||
IPAddresses: ipAddrs,
|
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,
|
// convertRoutes converts a list of routes as defined by netlink package,
|
||||||
// into a list of Route structure format expected by kata-runtime to
|
// into a list of Route structure format expected by kata-runtime to
|
||||||
// describe a set of routes.
|
// describe a set of routes.
|
||||||
func convertRoutes(netRoutes []netlink.Route) []Route {
|
func convertRoutes(netRoutes []netlink.Route) []types.Route {
|
||||||
var routes []Route
|
var routes []types.Route
|
||||||
|
|
||||||
// Ignore routes with IPv6 addresses as this is not supported
|
// Ignore routes with IPv6 addresses as this is not supported
|
||||||
// by Kata yet.
|
// by Kata yet.
|
||||||
@ -368,7 +334,7 @@ func convertRoutes(netRoutes []netlink.Route) []Route {
|
|||||||
dev = iface.Name
|
dev = iface.Name
|
||||||
}
|
}
|
||||||
|
|
||||||
route := Route{
|
route := types.Route{
|
||||||
Dest: dst,
|
Dest: dst,
|
||||||
Gateway: gw,
|
Gateway: gw,
|
||||||
Device: dev,
|
Device: dev,
|
||||||
@ -440,7 +406,7 @@ func (n *netmon) execKataCmd(subCmd string) error {
|
|||||||
return os.Remove(n.sharedFile)
|
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 {
|
if err := n.storeDataToSend(iface); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -448,7 +414,7 @@ func (n *netmon) addInterfaceCLI(iface Interface) error {
|
|||||||
return n.execKataCmd(kataCLIAddIfaceCmd)
|
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 {
|
if err := n.storeDataToSend(iface); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -456,7 +422,7 @@ func (n *netmon) delInterfaceCLI(iface Interface) error {
|
|||||||
return n.execKataCmd(kataCLIDelIfaceCmd)
|
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 {
|
if err := n.storeDataToSend(routes); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -16,6 +16,7 @@ import (
|
|||||||
"runtime"
|
"runtime"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/kata-containers/agent/pkg/types"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/vishvananda/netlink"
|
"github.com/vishvananda/netlink"
|
||||||
@ -173,14 +174,14 @@ func TestConvertInterface(t *testing.T) {
|
|||||||
HardwareAddr: hwAddr,
|
HardwareAddr: hwAddr,
|
||||||
}
|
}
|
||||||
|
|
||||||
expected := Interface{
|
expected := types.Interface{
|
||||||
Device: testIfaceName,
|
Device: testIfaceName,
|
||||||
Name: testIfaceName,
|
Name: testIfaceName,
|
||||||
Mtu: uint64(testMTU),
|
Mtu: uint64(testMTU),
|
||||||
HwAddr: testHwAddr,
|
HwAddr: testHwAddr,
|
||||||
IPAddresses: []*IPAddress{
|
IPAddresses: []*types.IPAddress{
|
||||||
{
|
{
|
||||||
Family: IPFamily(netlinkFamily),
|
Family: types.IPFamily(netlinkFamily),
|
||||||
Address: testIPAddress,
|
Address: testIPAddress,
|
||||||
Mask: "0",
|
Mask: "0",
|
||||||
},
|
},
|
||||||
@ -207,7 +208,7 @@ func TestConvertRoutes(t *testing.T) {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
expected := []Route{
|
expected := []types.Route{
|
||||||
{
|
{
|
||||||
Dest: testIPAddress,
|
Dest: testIPAddress,
|
||||||
Gateway: 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)
|
hwAddr, err := net.ParseMAC(testHwAddr)
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
|
|
||||||
@ -262,7 +263,7 @@ func testCreateDummyNetwork(t *testing.T, handler *netlink.Handle) (int, Interfa
|
|||||||
attrs := link.Attrs()
|
attrs := link.Attrs()
|
||||||
assert.NotNil(t, attrs)
|
assert.NotNil(t, attrs)
|
||||||
|
|
||||||
iface := Interface{
|
iface := types.Interface{
|
||||||
Device: testIfaceName,
|
Device: testIfaceName,
|
||||||
Name: testIfaceName,
|
Name: testIfaceName,
|
||||||
Mtu: uint64(testMTU),
|
Mtu: uint64(testMTU),
|
||||||
@ -284,7 +285,7 @@ func TestScanNetwork(t *testing.T) {
|
|||||||
idx, expected := testCreateDummyNetwork(t, handler)
|
idx, expected := testCreateDummyNetwork(t, handler)
|
||||||
|
|
||||||
n := &netmon{
|
n := &netmon{
|
||||||
netIfaces: make(map[int]Interface),
|
netIfaces: make(map[int]types.Interface),
|
||||||
netHandler: handler,
|
netHandler: handler,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -295,9 +296,9 @@ func TestScanNetwork(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestStoreDataToSend(t *testing.T) {
|
func TestStoreDataToSend(t *testing.T) {
|
||||||
var got Interface
|
var got types.Interface
|
||||||
|
|
||||||
expected := Interface{
|
expected := types.Interface{
|
||||||
Device: testIfaceName,
|
Device: testIfaceName,
|
||||||
Name: testIfaceName,
|
Name: testIfaceName,
|
||||||
Mtu: uint64(testMTU),
|
Mtu: uint64(testMTU),
|
||||||
@ -394,15 +395,15 @@ func TestActionsCLI(t *testing.T) {
|
|||||||
defer os.RemoveAll(testStorageParentPath)
|
defer os.RemoveAll(testStorageParentPath)
|
||||||
|
|
||||||
// Test addInterfaceCLI
|
// Test addInterfaceCLI
|
||||||
err = n.addInterfaceCLI(Interface{})
|
err = n.addInterfaceCLI(types.Interface{})
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
|
|
||||||
// Test delInterfaceCLI
|
// Test delInterfaceCLI
|
||||||
err = n.delInterfaceCLI(Interface{})
|
err = n.delInterfaceCLI(types.Interface{})
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
|
|
||||||
// Test updateRoutesCLI
|
// Test updateRoutesCLI
|
||||||
err = n.updateRoutesCLI([]Route{})
|
err = n.updateRoutesCLI([]types.Route{})
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
|
|
||||||
tearDownNetworkCb := testSetupNetwork(t)
|
tearDownNetworkCb := testSetupNetwork(t)
|
||||||
@ -460,8 +461,8 @@ func TestHandleRTMNewLink(t *testing.T) {
|
|||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
|
|
||||||
// Interface already exist in list
|
// Interface already exist in list
|
||||||
n.netIfaces = make(map[int]Interface)
|
n.netIfaces = make(map[int]types.Interface)
|
||||||
n.netIfaces[testIfaceIndex] = Interface{}
|
n.netIfaces[testIfaceIndex] = types.Interface{}
|
||||||
ev = netlink.LinkUpdate{
|
ev = netlink.LinkUpdate{
|
||||||
Link: &netlink.Dummy{
|
Link: &netlink.Dummy{
|
||||||
LinkAttrs: netlink.LinkAttrs{
|
LinkAttrs: netlink.LinkAttrs{
|
||||||
@ -474,7 +475,7 @@ func TestHandleRTMNewLink(t *testing.T) {
|
|||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
|
|
||||||
// Flags are not up and running
|
// Flags are not up and running
|
||||||
n.netIfaces = make(map[int]Interface)
|
n.netIfaces = make(map[int]types.Interface)
|
||||||
ev = netlink.LinkUpdate{
|
ev = netlink.LinkUpdate{
|
||||||
Link: &netlink.Dummy{
|
Link: &netlink.Dummy{
|
||||||
LinkAttrs: netlink.LinkAttrs{
|
LinkAttrs: netlink.LinkAttrs{
|
||||||
@ -487,7 +488,7 @@ func TestHandleRTMNewLink(t *testing.T) {
|
|||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
|
|
||||||
// Invalid link
|
// Invalid link
|
||||||
n.netIfaces = make(map[int]Interface)
|
n.netIfaces = make(map[int]types.Interface)
|
||||||
ev = netlink.LinkUpdate{
|
ev = netlink.LinkUpdate{
|
||||||
Link: &netlink.Dummy{
|
Link: &netlink.Dummy{
|
||||||
LinkAttrs: netlink.LinkAttrs{
|
LinkAttrs: netlink.LinkAttrs{
|
||||||
@ -528,7 +529,7 @@ func TestHandleRTMDelLink(t *testing.T) {
|
|||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
|
|
||||||
// Interface does not exist in list
|
// Interface does not exist in list
|
||||||
n.netIfaces = make(map[int]Interface)
|
n.netIfaces = make(map[int]types.Interface)
|
||||||
ev = netlink.LinkUpdate{
|
ev = netlink.LinkUpdate{
|
||||||
Link: &netlink.Dummy{
|
Link: &netlink.Dummy{
|
||||||
LinkAttrs: netlink.LinkAttrs{
|
LinkAttrs: netlink.LinkAttrs{
|
||||||
@ -543,7 +544,7 @@ func TestHandleRTMDelLink(t *testing.T) {
|
|||||||
|
|
||||||
func TestHandleRTMNewRouteIfaceNotFound(t *testing.T) {
|
func TestHandleRTMNewRouteIfaceNotFound(t *testing.T) {
|
||||||
n := &netmon{
|
n := &netmon{
|
||||||
netIfaces: make(map[int]Interface),
|
netIfaces: make(map[int]types.Interface),
|
||||||
}
|
}
|
||||||
|
|
||||||
err := n.handleRTMNewRoute(netlink.RouteUpdate{})
|
err := n.handleRTMNewRoute(netlink.RouteUpdate{})
|
||||||
|
Loading…
Reference in New Issue
Block a user