Skip to content

Commit

Permalink
Merge pull request #416 from bergwolf/cleanup
Browse files Browse the repository at this point in the history
runtime: consolidate network types definition
  • Loading branch information
gnawux committed Jul 15, 2020
2 parents dacb2fd + 5b15e9e commit c052e46
Show file tree
Hide file tree
Showing 18 changed files with 171 additions and 362 deletions.
36 changes: 19 additions & 17 deletions src/runtime/netmon/netmon.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ import (
"time"

"github.com/kata-containers/kata-containers/src/runtime/pkg/signals"
vcTypes "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/pkg/types"
pbTypes "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/pkg/agent/protocols"
"github.com/kata-containers/kata-containers/src/runtime/virtcontainers/utils"

"github.com/sirupsen/logrus"
lSyslog "github.com/sirupsen/logrus/hooks/syslog"
"github.com/vishvananda/netlink"
Expand Down Expand Up @@ -70,7 +72,7 @@ type netmon struct {
storagePath string
sharedFile string

netIfaces map[int]vcTypes.Interface
netIfaces map[int]pbTypes.Interface

linkUpdateCh chan netlink.LinkUpdate
linkDoneCh chan struct{}
Expand Down Expand Up @@ -151,7 +153,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]vcTypes.Interface),
netIfaces: make(map[int]pbTypes.Interface),
linkUpdateCh: make(chan netlink.LinkUpdate),
linkDoneCh: make(chan struct{}),
rtUpdateCh: make(chan netlink.RouteUpdate),
Expand Down Expand Up @@ -259,13 +261,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, linkType string, addrs []netlink.Addr) vcTypes.Interface {
func convertInterface(linkAttrs *netlink.LinkAttrs, linkType string, addrs []netlink.Addr) pbTypes.Interface {
if linkAttrs == nil {
netmonLog.Warn("Link attributes are nil")
return vcTypes.Interface{}
return pbTypes.Interface{}
}

var ipAddrs []*vcTypes.IPAddress
var ipAddrs []*pbTypes.IPAddress

for _, addr := range addrs {
if addr.IPNet == nil {
Expand All @@ -274,27 +276,27 @@ func convertInterface(linkAttrs *netlink.LinkAttrs, linkType string, addrs []net

netMask, _ := addr.Mask.Size()

ipAddr := &vcTypes.IPAddress{
ipAddr := &pbTypes.IPAddress{
Address: addr.IP.String(),
Mask: fmt.Sprintf("%d", netMask),
}

if addr.IP.To4() != nil {
ipAddr.Family = netlink.FAMILY_V4
ipAddr.Family = utils.ConvertNetlinkFamily(netlink.FAMILY_V4)
} else {
ipAddr.Family = netlink.FAMILY_V6
ipAddr.Family = utils.ConvertNetlinkFamily(netlink.FAMILY_V6)
}

ipAddrs = append(ipAddrs, ipAddr)
}

iface := vcTypes.Interface{
iface := pbTypes.Interface{
Device: linkAttrs.Name,
Name: linkAttrs.Name,
IPAddresses: ipAddrs,
Mtu: uint64(linkAttrs.MTU),
HwAddr: linkAttrs.HardwareAddr.String(),
LinkType: linkType,
Type: linkType,
}

netmonLog.WithField("interface", iface).Debug("Interface converted")
Expand All @@ -305,8 +307,8 @@ func convertInterface(linkAttrs *netlink.LinkAttrs, linkType string, addrs []net
// 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) []vcTypes.Route {
var routes []vcTypes.Route
func convertRoutes(netRoutes []netlink.Route) []pbTypes.Route {
var routes []pbTypes.Route

for _, netRoute := range netRoutes {
dst := ""
Expand Down Expand Up @@ -348,7 +350,7 @@ func convertRoutes(netRoutes []netlink.Route) []vcTypes.Route {
dev = iface.Name
}

route := vcTypes.Route{
route := pbTypes.Route{
Dest: dst,
Gateway: gw,
Device: dev,
Expand Down Expand Up @@ -420,23 +422,23 @@ func (n *netmon) execKataCmd(subCmd string) error {
return os.Remove(n.sharedFile)
}

func (n *netmon) addInterfaceCLI(iface vcTypes.Interface) error {
func (n *netmon) addInterfaceCLI(iface pbTypes.Interface) error {
if err := n.storeDataToSend(iface); err != nil {
return err
}

return n.execKataCmd(kataCLIAddIfaceCmd)
}

func (n *netmon) delInterfaceCLI(iface vcTypes.Interface) error {
func (n *netmon) delInterfaceCLI(iface pbTypes.Interface) error {
if err := n.storeDataToSend(iface); err != nil {
return err
}

return n.execKataCmd(kataCLIDelIfaceCmd)
}

func (n *netmon) updateRoutesCLI(routes []vcTypes.Route) error {
func (n *netmon) updateRoutesCLI(routes []pbTypes.Route) error {
if err := n.storeDataToSend(routes); err != nil {
return err
}
Expand Down
54 changes: 28 additions & 26 deletions src/runtime/netmon/netmon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ import (
"testing"

ktu "github.com/kata-containers/kata-containers/src/runtime/pkg/katatestutils"
vcTypes "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/pkg/types"
pbTypes "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/pkg/agent/protocols"
"github.com/kata-containers/kata-containers/src/runtime/virtcontainers/utils"

"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/vishvananda/netlink"
Expand Down Expand Up @@ -187,24 +189,24 @@ func TestConvertInterface(t *testing.T) {

linkType := "link_type_test"

expected := vcTypes.Interface{
expected := pbTypes.Interface{
Device: testIfaceName,
Name: testIfaceName,
Mtu: uint64(testMTU),
HwAddr: testHwAddr,
IPAddresses: []*vcTypes.IPAddress{
IPAddresses: []*pbTypes.IPAddress{
{
Family: netlink.FAMILY_V4,
Family: utils.ConvertNetlinkFamily(netlink.FAMILY_V4),
Address: testIPAddress,
Mask: "0",
},
{
Family: netlink.FAMILY_V6,
Family: utils.ConvertNetlinkFamily(netlink.FAMILY_V6),
Address: testIP6Address,
Mask: "0",
},
},
LinkType: linkType,
Type: linkType,
}

got := convertInterface(linkAttrs, linkType, addrs)
Expand Down Expand Up @@ -239,7 +241,7 @@ func TestConvertRoutes(t *testing.T) {
},
}

expected := []vcTypes.Route{
expected := []pbTypes.Route{
{
Dest: testIPAddressWithMask,
Gateway: testIPAddress,
Expand Down Expand Up @@ -279,7 +281,7 @@ func testSetupNetwork(t *testing.T) testTeardownNetwork {
}
}

func testCreateDummyNetwork(t *testing.T, handler *netlink.Handle) (int, vcTypes.Interface) {
func testCreateDummyNetwork(t *testing.T, handler *netlink.Handle) (int, pbTypes.Interface) {
hwAddr, err := net.ParseMAC(testHwAddr)
assert.Nil(t, err)

Expand All @@ -303,7 +305,7 @@ func testCreateDummyNetwork(t *testing.T, handler *netlink.Handle) (int, vcTypes
addrs, err := handler.AddrList(link, netlinkFamily)
assert.Nil(t, err)

var ipAddrs []*vcTypes.IPAddress
var ipAddrs []*pbTypes.IPAddress

// Scan addresses for ipv6 link local address which is automatically assigned
for _, addr := range addrs {
Expand All @@ -313,26 +315,26 @@ func testCreateDummyNetwork(t *testing.T, handler *netlink.Handle) (int, vcTypes

netMask, _ := addr.Mask.Size()

ipAddr := &vcTypes.IPAddress{
ipAddr := &pbTypes.IPAddress{
Address: addr.IP.String(),
Mask: fmt.Sprintf("%d", netMask),
}

if addr.IP.To4() != nil {
ipAddr.Family = netlink.FAMILY_V4
ipAddr.Family = utils.ConvertNetlinkFamily(netlink.FAMILY_V4)
} else {
ipAddr.Family = netlink.FAMILY_V6
ipAddr.Family = utils.ConvertNetlinkFamily(netlink.FAMILY_V6)
}

ipAddrs = append(ipAddrs, ipAddr)
}

iface := vcTypes.Interface{
iface := pbTypes.Interface{
Device: testIfaceName,
Name: testIfaceName,
Mtu: uint64(testMTU),
HwAddr: testHwAddr,
LinkType: link.Type(),
Type: link.Type(),
IPAddresses: ipAddrs,
}

Expand All @@ -351,7 +353,7 @@ func TestScanNetwork(t *testing.T) {
idx, expected := testCreateDummyNetwork(t, handler)

n := &netmon{
netIfaces: make(map[int]vcTypes.Interface),
netIfaces: make(map[int]pbTypes.Interface),
netHandler: handler,
}

Expand All @@ -362,9 +364,9 @@ func TestScanNetwork(t *testing.T) {
}

func TestStoreDataToSend(t *testing.T) {
var got vcTypes.Interface
var got pbTypes.Interface

expected := vcTypes.Interface{
expected := pbTypes.Interface{
Device: testIfaceName,
Name: testIfaceName,
Mtu: uint64(testMTU),
Expand Down Expand Up @@ -461,15 +463,15 @@ func TestActionsCLI(t *testing.T) {
defer os.RemoveAll(testStorageParentPath)

// Test addInterfaceCLI
err = n.addInterfaceCLI(vcTypes.Interface{})
err = n.addInterfaceCLI(pbTypes.Interface{})
assert.Nil(t, err)

// Test delInterfaceCLI
err = n.delInterfaceCLI(vcTypes.Interface{})
err = n.delInterfaceCLI(pbTypes.Interface{})
assert.Nil(t, err)

// Test updateRoutesCLI
err = n.updateRoutesCLI([]vcTypes.Route{})
err = n.updateRoutesCLI([]pbTypes.Route{})
assert.Nil(t, err)

tearDownNetworkCb := testSetupNetwork(t)
Expand Down Expand Up @@ -527,8 +529,8 @@ func TestHandleRTMNewLink(t *testing.T) {
assert.Nil(t, err)

// Interface already exist in list
n.netIfaces = make(map[int]vcTypes.Interface)
n.netIfaces[testIfaceIndex] = vcTypes.Interface{}
n.netIfaces = make(map[int]pbTypes.Interface)
n.netIfaces[testIfaceIndex] = pbTypes.Interface{}
ev = netlink.LinkUpdate{
Link: &netlink.Dummy{
LinkAttrs: netlink.LinkAttrs{
Expand All @@ -541,7 +543,7 @@ func TestHandleRTMNewLink(t *testing.T) {
assert.Nil(t, err)

// Flags are not up and running
n.netIfaces = make(map[int]vcTypes.Interface)
n.netIfaces = make(map[int]pbTypes.Interface)
ev = netlink.LinkUpdate{
Link: &netlink.Dummy{
LinkAttrs: netlink.LinkAttrs{
Expand All @@ -554,7 +556,7 @@ func TestHandleRTMNewLink(t *testing.T) {
assert.Nil(t, err)

// Invalid link
n.netIfaces = make(map[int]vcTypes.Interface)
n.netIfaces = make(map[int]pbTypes.Interface)
ev = netlink.LinkUpdate{
Link: &netlink.Dummy{
LinkAttrs: netlink.LinkAttrs{
Expand Down Expand Up @@ -595,7 +597,7 @@ func TestHandleRTMDelLink(t *testing.T) {
assert.Nil(t, err)

// Interface does not exist in list
n.netIfaces = make(map[int]vcTypes.Interface)
n.netIfaces = make(map[int]pbTypes.Interface)
ev = netlink.LinkUpdate{
Link: &netlink.Dummy{
LinkAttrs: netlink.LinkAttrs{
Expand All @@ -610,7 +612,7 @@ func TestHandleRTMDelLink(t *testing.T) {

func TestHandleRTMNewRouteIfaceNotFound(t *testing.T) {
n := &netmon{
netIfaces: make(map[int]vcTypes.Interface),
netIfaces: make(map[int]pbTypes.Interface),
}

err := n.handleRTMNewRoute(netlink.RouteUpdate{})
Expand Down
10 changes: 5 additions & 5 deletions src/runtime/virtcontainers/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
"time"

persistapi "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/persist/api"
pbTypes "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/pkg/agent/protocols"
"github.com/kata-containers/kata-containers/src/runtime/virtcontainers/pkg/agent/protocols/grpc"
vcTypes "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/pkg/types"
"github.com/kata-containers/kata-containers/src/runtime/virtcontainers/types"
specs "github.com/opencontainers/runtime-spec/specs-go"
"golang.org/x/net/context"
Expand Down Expand Up @@ -182,16 +182,16 @@ type agent interface {
reseedRNG(data []byte) error

// updateInterface will tell the agent to update a nic for an existed Sandbox.
updateInterface(inf *vcTypes.Interface) (*vcTypes.Interface, error)
updateInterface(inf *pbTypes.Interface) (*pbTypes.Interface, error)

// listInterfaces will tell the agent to list interfaces of an existed Sandbox
listInterfaces() ([]*vcTypes.Interface, error)
listInterfaces() ([]*pbTypes.Interface, error)

// updateRoutes will tell the agent to update route table for an existed Sandbox.
updateRoutes(routes []*vcTypes.Route) ([]*vcTypes.Route, error)
updateRoutes(routes []*pbTypes.Route) ([]*pbTypes.Route, error)

// listRoutes will tell the agent to list routes of an existed Sandbox
listRoutes() ([]*vcTypes.Route, error)
listRoutes() ([]*pbTypes.Route, error)

// getGuestDetails will tell the agent to get some information of guest
getGuestDetails(*grpc.GuestDetailsRequest) (*grpc.GuestDetailsResponse, error)
Expand Down

0 comments on commit c052e46

Please sign in to comment.