From 5b15e9ef4fc0d46f3d2ccaba6d2f42af2d4a0d43 Mon Sep 17 00:00:00 2001 From: Peng Tao Date: Wed, 15 Jul 2020 14:46:36 +0800 Subject: [PATCH] runtime: consolidate types definition We do not need the vc types translation for network data structures. Just use the protocol buffer definitions. Fixes: #415 Signed-off-by: Peng Tao --- src/runtime/netmon/netmon.go | 36 ++-- src/runtime/netmon/netmon_test.go | 54 ++--- src/runtime/virtcontainers/agent.go | 10 +- src/runtime/virtcontainers/api.go | 13 +- src/runtime/virtcontainers/container.go | 10 +- src/runtime/virtcontainers/interfaces.go | 12 +- src/runtime/virtcontainers/kata_agent.go | 192 ++---------------- src/runtime/virtcontainers/kata_agent_test.go | 4 +- src/runtime/virtcontainers/mock_agent.go | 10 +- src/runtime/virtcontainers/network.go | 34 ++-- src/runtime/virtcontainers/network_test.go | 21 +- src/runtime/virtcontainers/pkg/types/types.go | 49 ----- src/runtime/virtcontainers/pkg/vcmock/mock.go | 12 +- .../virtcontainers/pkg/vcmock/mock_test.go | 12 +- .../virtcontainers/pkg/vcmock/sandbox.go | 12 +- .../virtcontainers/pkg/vcmock/types.go | 22 +- src/runtime/virtcontainers/sandbox.go | 15 +- src/runtime/virtcontainers/utils/utils.go | 15 ++ 18 files changed, 171 insertions(+), 362 deletions(-) delete mode 100644 src/runtime/virtcontainers/pkg/types/types.go diff --git a/src/runtime/netmon/netmon.go b/src/runtime/netmon/netmon.go index fea06c4c8..9becf8f0b 100644 --- a/src/runtime/netmon/netmon.go +++ b/src/runtime/netmon/netmon.go @@ -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" @@ -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{} @@ -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), @@ -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 { @@ -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") @@ -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 := "" @@ -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, @@ -420,7 +422,7 @@ 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 } @@ -428,7 +430,7 @@ func (n *netmon) addInterfaceCLI(iface vcTypes.Interface) error { 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 } @@ -436,7 +438,7 @@ func (n *netmon) delInterfaceCLI(iface vcTypes.Interface) error { 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 } diff --git a/src/runtime/netmon/netmon_test.go b/src/runtime/netmon/netmon_test.go index 064ee5690..c8edd0b8e 100644 --- a/src/runtime/netmon/netmon_test.go +++ b/src/runtime/netmon/netmon_test.go @@ -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" @@ -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) @@ -239,7 +241,7 @@ func TestConvertRoutes(t *testing.T) { }, } - expected := []vcTypes.Route{ + expected := []pbTypes.Route{ { Dest: testIPAddressWithMask, Gateway: testIPAddress, @@ -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) @@ -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 { @@ -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, } @@ -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, } @@ -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), @@ -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) @@ -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{ @@ -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{ @@ -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{ @@ -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{ @@ -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{}) diff --git a/src/runtime/virtcontainers/agent.go b/src/runtime/virtcontainers/agent.go index 9a583d5b3..52e93ead8 100644 --- a/src/runtime/virtcontainers/agent.go +++ b/src/runtime/virtcontainers/agent.go @@ -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" @@ -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) diff --git a/src/runtime/virtcontainers/api.go b/src/runtime/virtcontainers/api.go index f7c3b0cb9..4182b966c 100644 --- a/src/runtime/virtcontainers/api.go +++ b/src/runtime/virtcontainers/api.go @@ -14,6 +14,7 @@ import ( deviceApi "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/device/api" deviceConfig "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/device/config" "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/persist" + pbTypes "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/pkg/agent/protocols" "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/pkg/cgroups" "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/pkg/compatoci" vcTypes "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/pkg/types" @@ -798,7 +799,7 @@ func AddDevice(ctx context.Context, sandboxID string, info deviceConfig.DeviceIn return s.AddDevice(info) } -func toggleInterface(ctx context.Context, sandboxID string, inf *vcTypes.Interface, add bool) (*vcTypes.Interface, error) { +func toggleInterface(ctx context.Context, sandboxID string, inf *pbTypes.Interface, add bool) (*pbTypes.Interface, error) { if sandboxID == "" { return nil, vcTypes.ErrNeedSandboxID } @@ -822,7 +823,7 @@ func toggleInterface(ctx context.Context, sandboxID string, inf *vcTypes.Interfa } // AddInterface is the virtcontainers add interface entry point. -func AddInterface(ctx context.Context, sandboxID string, inf *vcTypes.Interface) (*vcTypes.Interface, error) { +func AddInterface(ctx context.Context, sandboxID string, inf *pbTypes.Interface) (*pbTypes.Interface, error) { span, ctx := trace(ctx, "AddInterface") defer span.Finish() @@ -830,7 +831,7 @@ func AddInterface(ctx context.Context, sandboxID string, inf *vcTypes.Interface) } // RemoveInterface is the virtcontainers remove interface entry point. -func RemoveInterface(ctx context.Context, sandboxID string, inf *vcTypes.Interface) (*vcTypes.Interface, error) { +func RemoveInterface(ctx context.Context, sandboxID string, inf *pbTypes.Interface) (*pbTypes.Interface, error) { span, ctx := trace(ctx, "RemoveInterface") defer span.Finish() @@ -838,7 +839,7 @@ func RemoveInterface(ctx context.Context, sandboxID string, inf *vcTypes.Interfa } // ListInterfaces is the virtcontainers list interfaces entry point. -func ListInterfaces(ctx context.Context, sandboxID string) ([]*vcTypes.Interface, error) { +func ListInterfaces(ctx context.Context, sandboxID string) ([]*pbTypes.Interface, error) { span, ctx := trace(ctx, "ListInterfaces") defer span.Finish() @@ -861,7 +862,7 @@ func ListInterfaces(ctx context.Context, sandboxID string) ([]*vcTypes.Interface } // UpdateRoutes is the virtcontainers update routes entry point. -func UpdateRoutes(ctx context.Context, sandboxID string, routes []*vcTypes.Route) ([]*vcTypes.Route, error) { +func UpdateRoutes(ctx context.Context, sandboxID string, routes []*pbTypes.Route) ([]*pbTypes.Route, error) { span, ctx := trace(ctx, "UpdateRoutes") defer span.Finish() @@ -884,7 +885,7 @@ func UpdateRoutes(ctx context.Context, sandboxID string, routes []*vcTypes.Route } // ListRoutes is the virtcontainers list routes entry point. -func ListRoutes(ctx context.Context, sandboxID string) ([]*vcTypes.Route, error) { +func ListRoutes(ctx context.Context, sandboxID string) ([]*pbTypes.Route, error) { span, ctx := trace(ctx, "ListRoutes") defer span.Finish() diff --git a/src/runtime/virtcontainers/container.go b/src/runtime/virtcontainers/container.go index eaab22442..449b84276 100644 --- a/src/runtime/virtcontainers/container.go +++ b/src/runtime/virtcontainers/container.go @@ -16,20 +16,20 @@ import ( "syscall" "time" - "github.com/containerd/cgroups" + "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/device/config" + "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/device/manager" vccgroups "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/pkg/cgroups" + "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/pkg/rootless" vcTypes "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/pkg/types" "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/types" "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/utils" + + "github.com/containerd/cgroups" specs "github.com/opencontainers/runtime-spec/specs-go" opentracing "github.com/opentracing/opentracing-go" "github.com/pkg/errors" "github.com/sirupsen/logrus" "golang.org/x/sys/unix" - - "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/device/config" - "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/device/manager" - "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/pkg/rootless" ) // https://github.com/torvalds/linux/blob/master/include/uapi/linux/major.h diff --git a/src/runtime/virtcontainers/interfaces.go b/src/runtime/virtcontainers/interfaces.go index cef697491..c04acb676 100644 --- a/src/runtime/virtcontainers/interfaces.go +++ b/src/runtime/virtcontainers/interfaces.go @@ -12,7 +12,7 @@ import ( "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/device/api" "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/device/config" - 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/types" specs "github.com/opencontainers/runtime-spec/specs-go" "github.com/sirupsen/logrus" @@ -65,11 +65,11 @@ type VCSandbox interface { AddDevice(info config.DeviceInfo) (api.Device, error) - AddInterface(inf *vcTypes.Interface) (*vcTypes.Interface, error) - RemoveInterface(inf *vcTypes.Interface) (*vcTypes.Interface, error) - ListInterfaces() ([]*vcTypes.Interface, error) - UpdateRoutes(routes []*vcTypes.Route) ([]*vcTypes.Route, error) - ListRoutes() ([]*vcTypes.Route, error) + AddInterface(inf *pbTypes.Interface) (*pbTypes.Interface, error) + RemoveInterface(inf *pbTypes.Interface) (*pbTypes.Interface, error) + ListInterfaces() ([]*pbTypes.Interface, error) + UpdateRoutes(routes []*pbTypes.Route) ([]*pbTypes.Route, error) + ListRoutes() ([]*pbTypes.Route, error) GetOOMEvent() (string, error) diff --git a/src/runtime/virtcontainers/kata_agent.go b/src/runtime/virtcontainers/kata_agent.go index 36f1df9e6..65007b3cc 100644 --- a/src/runtime/virtcontainers/kata_agent.go +++ b/src/runtime/virtcontainers/kata_agent.go @@ -21,7 +21,7 @@ import ( "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/device/api" "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/device/config" persistapi "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/persist/api" - aTypes "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/pkg/agent/protocols" + pbTypes "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/pkg/agent/protocols" kataclient "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/pkg/agent/protocols/client" "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/pkg/agent/protocols/grpc" vcAnnotations "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/pkg/annotations" @@ -36,7 +36,6 @@ import ( "github.com/opencontainers/runtime-spec/specs-go" opentracing "github.com/opentracing/opentracing-go" "github.com/sirupsen/logrus" - "github.com/vishvananda/netlink" "golang.org/x/net/context" "golang.org/x/sys/unix" "google.golang.org/grpc/codes" @@ -565,10 +564,10 @@ func (k *kataAgent) exec(sandbox *Sandbox, c Container, cmd types.Cmd) (*Process return buildProcessFromExecID(req.ExecId) } -func (k *kataAgent) updateInterface(ifc *vcTypes.Interface) (*vcTypes.Interface, error) { +func (k *kataAgent) updateInterface(ifc *pbTypes.Interface) (*pbTypes.Interface, error) { // send update interface request ifcReq := &grpc.UpdateInterfaceRequest{ - Interface: k.convertToKataAgentInterface(ifc), + Interface: ifc, } resultingInterface, err := k.sendReq(ifcReq) if err != nil { @@ -577,13 +576,13 @@ func (k *kataAgent) updateInterface(ifc *vcTypes.Interface) (*vcTypes.Interface, "resulting-interface": fmt.Sprintf("%+v", resultingInterface), }).WithError(err).Error("update interface request failed") } - if resultInterface, ok := resultingInterface.(*vcTypes.Interface); ok { + if resultInterface, ok := resultingInterface.(*pbTypes.Interface); ok { return resultInterface, err } return nil, err } -func (k *kataAgent) updateInterfaces(interfaces []*vcTypes.Interface) error { +func (k *kataAgent) updateInterfaces(interfaces []*pbTypes.Interface) error { for _, ifc := range interfaces { if _, err := k.updateInterface(ifc); err != nil { return err @@ -592,11 +591,11 @@ func (k *kataAgent) updateInterfaces(interfaces []*vcTypes.Interface) error { return nil } -func (k *kataAgent) updateRoutes(routes []*vcTypes.Route) ([]*vcTypes.Route, error) { +func (k *kataAgent) updateRoutes(routes []*pbTypes.Route) ([]*pbTypes.Route, error) { if routes != nil { routesReq := &grpc.UpdateRoutesRequest{ Routes: &grpc.Routes{ - Routes: k.convertToKataAgentRoutes(routes), + Routes: routes, }, } resultingRoutes, err := k.sendReq(routesReq) @@ -608,18 +607,18 @@ func (k *kataAgent) updateRoutes(routes []*vcTypes.Route) ([]*vcTypes.Route, err } resultRoutes, ok := resultingRoutes.(*grpc.Routes) if ok && resultRoutes != nil { - return k.convertToRoutes(resultRoutes.Routes), err + return resultRoutes.Routes, err } return nil, err } return nil, nil } -func (k *kataAgent) addARPNeighbors(neighs []*vcTypes.ARPNeighbor) error { +func (k *kataAgent) addARPNeighbors(neighs []*pbTypes.ARPNeighbor) error { if neighs != nil { neighsReq := &grpc.AddARPNeighborsRequest{ Neighbors: &grpc.ARPNeighbors{ - ARPNeighbors: k.convertToKataAgentNeighbors(neighs), + ARPNeighbors: neighs, }, } _, err := k.sendReq(neighsReq) @@ -639,7 +638,7 @@ func (k *kataAgent) addARPNeighbors(neighs []*vcTypes.ARPNeighbor) error { return nil } -func (k *kataAgent) listInterfaces() ([]*vcTypes.Interface, error) { +func (k *kataAgent) listInterfaces() ([]*pbTypes.Interface, error) { req := &grpc.ListInterfacesRequest{} resultingInterfaces, err := k.sendReq(req) if err != nil { @@ -647,12 +646,12 @@ func (k *kataAgent) listInterfaces() ([]*vcTypes.Interface, error) { } resultInterfaces, ok := resultingInterfaces.(*grpc.Interfaces) if ok { - return k.convertToInterfaces(resultInterfaces.Interfaces), err + return resultInterfaces.Interfaces, err } return nil, err } -func (k *kataAgent) listRoutes() ([]*vcTypes.Route, error) { +func (k *kataAgent) listRoutes() ([]*pbTypes.Route, error) { req := &grpc.ListRoutesRequest{} resultingRoutes, err := k.sendReq(req) if err != nil { @@ -660,7 +659,7 @@ func (k *kataAgent) listRoutes() ([]*vcTypes.Route, error) { } resultRoutes, ok := resultingRoutes.(*grpc.Routes) if ok { - return k.convertToRoutes(resultRoutes.Routes), err + return resultRoutes.Routes, err } return nil, err } @@ -2165,169 +2164,6 @@ func (k *kataAgent) setGuestDateTime(tv time.Time) error { return err } -func (k *kataAgent) convertToKataAgentIPFamily(ipFamily int) aTypes.IPFamily { - switch ipFamily { - case netlink.FAMILY_V4: - return aTypes.IPFamily_v4 - case netlink.FAMILY_V6: - return aTypes.IPFamily_v6 - } - - return aTypes.IPFamily_v4 -} - -func (k *kataAgent) convertToIPFamily(ipFamily aTypes.IPFamily) int { - switch ipFamily { - case aTypes.IPFamily_v4: - return netlink.FAMILY_V4 - case aTypes.IPFamily_v6: - return netlink.FAMILY_V6 - } - - return netlink.FAMILY_V4 -} - -func (k *kataAgent) convertToKataAgentIPAddress(ipAddr *vcTypes.IPAddress) (aIPAddr *aTypes.IPAddress) { - if ipAddr == nil { - return nil - } - - aIPAddr = &aTypes.IPAddress{ - Family: k.convertToKataAgentIPFamily(ipAddr.Family), - Address: ipAddr.Address, - Mask: ipAddr.Mask, - } - - return aIPAddr -} - -func (k *kataAgent) convertToKataAgentIPAddresses(ipAddrs []*vcTypes.IPAddress) (aIPAddrs []*aTypes.IPAddress) { - for _, ipAddr := range ipAddrs { - if ipAddr == nil { - continue - } - - aIPAddr := k.convertToKataAgentIPAddress(ipAddr) - aIPAddrs = append(aIPAddrs, aIPAddr) - } - - return aIPAddrs -} - -func (k *kataAgent) convertToIPAddresses(aIPAddrs []*aTypes.IPAddress) (ipAddrs []*vcTypes.IPAddress) { - for _, aIPAddr := range aIPAddrs { - if aIPAddr == nil { - continue - } - - ipAddr := &vcTypes.IPAddress{ - Family: k.convertToIPFamily(aIPAddr.Family), - Address: aIPAddr.Address, - Mask: aIPAddr.Mask, - } - - ipAddrs = append(ipAddrs, ipAddr) - } - - return ipAddrs -} - -func (k *kataAgent) convertToKataAgentInterface(iface *vcTypes.Interface) *aTypes.Interface { - if iface == nil { - return nil - } - - return &aTypes.Interface{ - Device: iface.Device, - Name: iface.Name, - IPAddresses: k.convertToKataAgentIPAddresses(iface.IPAddresses), - Mtu: iface.Mtu, - RawFlags: iface.RawFlags, - HwAddr: iface.HwAddr, - PciAddr: iface.PciAddr, - } -} - -func (k *kataAgent) convertToInterfaces(aIfaces []*aTypes.Interface) (ifaces []*vcTypes.Interface) { - for _, aIface := range aIfaces { - if aIface == nil { - continue - } - - iface := &vcTypes.Interface{ - Device: aIface.Device, - Name: aIface.Name, - IPAddresses: k.convertToIPAddresses(aIface.IPAddresses), - Mtu: aIface.Mtu, - HwAddr: aIface.HwAddr, - PciAddr: aIface.PciAddr, - } - - ifaces = append(ifaces, iface) - } - - return ifaces -} - -func (k *kataAgent) convertToKataAgentRoutes(routes []*vcTypes.Route) (aRoutes []*aTypes.Route) { - for _, route := range routes { - if route == nil { - continue - } - - aRoute := &aTypes.Route{ - Dest: route.Dest, - Gateway: route.Gateway, - Device: route.Device, - Source: route.Source, - Scope: route.Scope, - } - - aRoutes = append(aRoutes, aRoute) - } - - return aRoutes -} - -func (k *kataAgent) convertToKataAgentNeighbors(neighs []*vcTypes.ARPNeighbor) (aNeighs []*aTypes.ARPNeighbor) { - for _, neigh := range neighs { - if neigh == nil { - continue - } - - aNeigh := &aTypes.ARPNeighbor{ - ToIPAddress: k.convertToKataAgentIPAddress(neigh.ToIPAddress), - Device: neigh.Device, - State: int32(neigh.State), - Lladdr: neigh.LLAddr, - } - - aNeighs = append(aNeighs, aNeigh) - } - - return aNeighs -} - -func (k *kataAgent) convertToRoutes(aRoutes []*aTypes.Route) (routes []*vcTypes.Route) { - for _, aRoute := range aRoutes { - if aRoute == nil { - continue - } - - route := &vcTypes.Route{ - Dest: aRoute.Dest, - Gateway: aRoute.Gateway, - Device: aRoute.Device, - Source: aRoute.Source, - Scope: aRoute.Scope, - } - - routes = append(routes, route) - } - - return routes -} - func (k *kataAgent) copyFile(src, dst string) error { var st unix.Stat_t diff --git a/src/runtime/virtcontainers/kata_agent_test.go b/src/runtime/virtcontainers/kata_agent_test.go index e2ab5cd00..070a44714 100644 --- a/src/runtime/virtcontainers/kata_agent_test.go +++ b/src/runtime/virtcontainers/kata_agent_test.go @@ -29,11 +29,11 @@ import ( "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/device/manager" "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/persist" aTypes "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/pkg/agent/protocols" + pbTypes "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/pkg/agent/protocols" pb "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/pkg/agent/protocols/grpc" vcAnnotations "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/pkg/annotations" "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/pkg/mock" "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/pkg/rootless" - vcTypes "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/pkg/types" "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/types" ) @@ -1062,7 +1062,7 @@ func TestAgentNetworkOperation(t *testing.T) { _, err = k.listInterfaces() assert.Nil(err) - _, err = k.updateRoutes([]*vcTypes.Route{}) + _, err = k.updateRoutes([]*pbTypes.Route{}) assert.Nil(err) _, err = k.listRoutes() diff --git a/src/runtime/virtcontainers/mock_agent.go b/src/runtime/virtcontainers/mock_agent.go index 9401fabb7..028860830 100644 --- a/src/runtime/virtcontainers/mock_agent.go +++ b/src/runtime/virtcontainers/mock_agent.go @@ -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" @@ -111,22 +111,22 @@ func (n *mockAgent) onlineCPUMem(cpus uint32, cpuOnly bool) error { } // updateInterface is the Noop agent Interface update implementation. It does nothing. -func (n *mockAgent) updateInterface(inf *vcTypes.Interface) (*vcTypes.Interface, error) { +func (n *mockAgent) updateInterface(inf *pbTypes.Interface) (*pbTypes.Interface, error) { return nil, nil } // listInterfaces is the Noop agent Interfaces list implementation. It does nothing. -func (n *mockAgent) listInterfaces() ([]*vcTypes.Interface, error) { +func (n *mockAgent) listInterfaces() ([]*pbTypes.Interface, error) { return nil, nil } // updateRoutes is the Noop agent Routes update implementation. It does nothing. -func (n *mockAgent) updateRoutes(routes []*vcTypes.Route) ([]*vcTypes.Route, error) { +func (n *mockAgent) updateRoutes(routes []*pbTypes.Route) ([]*pbTypes.Route, error) { return nil, nil } // listRoutes is the Noop agent Routes list implementation. It does nothing. -func (n *mockAgent) listRoutes() ([]*vcTypes.Route, error) { +func (n *mockAgent) listRoutes() ([]*pbTypes.Route, error) { return nil, nil } diff --git a/src/runtime/virtcontainers/network.go b/src/runtime/virtcontainers/network.go index 2eb6b300a..a2a8963f1 100644 --- a/src/runtime/virtcontainers/network.go +++ b/src/runtime/virtcontainers/network.go @@ -26,8 +26,8 @@ import ( "github.com/vishvananda/netns" "golang.org/x/sys/unix" + pbTypes "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/pkg/agent/protocols" "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/pkg/rootless" - vcTypes "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/pkg/types" "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/pkg/uuid" "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/utils" ) @@ -943,19 +943,19 @@ func deleteNetNS(netNSPath string) error { return nil } -func generateVCNetworkStructures(networkNS NetworkNamespace) ([]*vcTypes.Interface, []*vcTypes.Route, []*vcTypes.ARPNeighbor, error) { +func generateVCNetworkStructures(networkNS NetworkNamespace) ([]*pbTypes.Interface, []*pbTypes.Route, []*pbTypes.ARPNeighbor, error) { if networkNS.NetNsPath == "" { return nil, nil, nil, nil } - var routes []*vcTypes.Route - var ifaces []*vcTypes.Interface - var neighs []*vcTypes.ARPNeighbor + var routes []*pbTypes.Route + var ifaces []*pbTypes.Interface + var neighs []*pbTypes.ARPNeighbor for _, endpoint := range networkNS.Endpoints { - var ipAddresses []*vcTypes.IPAddress + var ipAddresses []*pbTypes.IPAddress for _, addr := range endpoint.Properties().Addrs { // Skip localhost interface if addr.IP.IsLoopback() { @@ -963,19 +963,19 @@ func generateVCNetworkStructures(networkNS NetworkNamespace) ([]*vcTypes.Interfa } netMask, _ := addr.Mask.Size() - ipAddress := vcTypes.IPAddress{ - Family: netlink.FAMILY_V4, + ipAddress := pbTypes.IPAddress{ + Family: utils.ConvertNetlinkFamily(netlink.FAMILY_V4), Address: addr.IP.String(), Mask: fmt.Sprintf("%d", netMask), } if addr.IP.To4() == nil { - ipAddress.Family = netlink.FAMILY_V6 + ipAddress.Family = utils.ConvertNetlinkFamily(netlink.FAMILY_V6) } ipAddresses = append(ipAddresses, &ipAddress) } noarp := endpoint.Properties().Iface.RawFlags & unix.IFF_NOARP - ifc := vcTypes.Interface{ + ifc := pbTypes.Interface{ IPAddresses: ipAddresses, Device: endpoint.Name(), Name: endpoint.Name(), @@ -988,7 +988,7 @@ func generateVCNetworkStructures(networkNS NetworkNamespace) ([]*vcTypes.Interfa ifaces = append(ifaces, &ifc) for _, route := range endpoint.Properties().Routes { - var r vcTypes.Route + var r pbTypes.Route if route.Protocol == unix.RTPROT_KERNEL { continue @@ -1013,7 +1013,7 @@ func generateVCNetworkStructures(networkNS NetworkNamespace) ([]*vcTypes.Interfa } for _, neigh := range endpoint.Properties().Neighbors { - var n vcTypes.ARPNeighbor + var n pbTypes.ARPNeighbor // We add only static ARP entries if neigh.State != netlink.NUD_PERMANENT { @@ -1021,15 +1021,15 @@ func generateVCNetworkStructures(networkNS NetworkNamespace) ([]*vcTypes.Interfa } n.Device = endpoint.Name() - n.State = neigh.State - n.Flags = neigh.Flags + n.State = int32(neigh.State) + n.Flags = int32(neigh.Flags) if neigh.HardwareAddr != nil { - n.LLAddr = neigh.HardwareAddr.String() + n.Lladdr = neigh.HardwareAddr.String() } - n.ToIPAddress = &vcTypes.IPAddress{ - Family: netlink.FAMILY_V4, + n.ToIPAddress = &pbTypes.IPAddress{ + Family: utils.ConvertNetlinkFamily(netlink.FAMILY_V4), Address: neigh.IP.String(), } if neigh.IP.To4() == nil { diff --git a/src/runtime/virtcontainers/network_test.go b/src/runtime/virtcontainers/network_test.go index fdf0c402f..ff35e2467 100644 --- a/src/runtime/virtcontainers/network_test.go +++ b/src/runtime/virtcontainers/network_test.go @@ -14,7 +14,8 @@ import ( "github.com/containernetworking/plugins/pkg/ns" 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/stretchr/testify/assert" "github.com/vishvananda/netlink" ) @@ -96,29 +97,29 @@ func TestGenerateInterfacesAndRoutes(t *testing.T) { // // Build expected results: // - expectedAddresses := []*vcTypes.IPAddress{ - {Family: netlink.FAMILY_V4, Address: "172.17.0.2", Mask: "16"}, - {Family: netlink.FAMILY_V4, Address: "182.17.0.2", Mask: "16"}, - {Family: netlink.FAMILY_V6, Address: "2001:db8:1::242:ac11:2", Mask: "64"}, + expectedAddresses := []*pbTypes.IPAddress{ + {Family: utils.ConvertNetlinkFamily(netlink.FAMILY_V4), Address: "172.17.0.2", Mask: "16"}, + {Family: utils.ConvertNetlinkFamily(netlink.FAMILY_V4), Address: "182.17.0.2", Mask: "16"}, + {Family: utils.ConvertNetlinkFamily(netlink.FAMILY_V6), Address: "2001:db8:1::242:ac11:2", Mask: "64"}, } - expectedInterfaces := []*vcTypes.Interface{ + expectedInterfaces := []*pbTypes.Interface{ {Device: "eth0", Name: "eth0", IPAddresses: expectedAddresses, Mtu: 1500, HwAddr: "02:00:ca:fe:00:04"}, } - expectedRoutes := []*vcTypes.Route{ + expectedRoutes := []*pbTypes.Route{ {Dest: "", Gateway: "172.17.0.1", Device: "eth0", Source: "", Scope: uint32(254)}, {Dest: "172.17.0.0/16", Gateway: "172.17.0.1", Device: "eth0", Source: "172.17.0.2"}, {Dest: "2001:db8:1::/64", Gateway: "", Device: "eth0", Source: ""}, {Dest: "", Gateway: "2001:db8:1::1", Device: "eth0", Source: ""}, } - expectedNeighs := []*vcTypes.ARPNeighbor{ + expectedNeighs := []*pbTypes.ARPNeighbor{ { Device: "eth0", State: netlink.NUD_PERMANENT, - LLAddr: "6a:92:3a:59:70:aa", - ToIPAddress: &vcTypes.IPAddress{Address: "192.168.0.101", Family: netlink.FAMILY_V4}, + Lladdr: "6a:92:3a:59:70:aa", + ToIPAddress: &pbTypes.IPAddress{Address: "192.168.0.101", Family: utils.ConvertNetlinkFamily(netlink.FAMILY_V4)}, }, } diff --git a/src/runtime/virtcontainers/pkg/types/types.go b/src/runtime/virtcontainers/pkg/types/types.go deleted file mode 100644 index 5abb4922c..000000000 --- a/src/runtime/virtcontainers/pkg/types/types.go +++ /dev/null @@ -1,49 +0,0 @@ -// Copyright 2018 Intel Corporation. -// -// SPDX-License-Identifier: Apache-2.0 -// - -package types - -// IPAddress describes an IP address. -type IPAddress struct { - Family int - Address string - Mask string -} - -// Interface describes a network interface. -type Interface struct { - Device string - Name string - IPAddresses []*IPAddress - Mtu uint64 - RawFlags uint32 - HwAddr string - // pciAddr is the PCI address in the format "bridgeAddr/deviceAddr". - // Here, bridgeAddr is the address at which the bridge is attached on the root bus, - // while deviceAddr is the address at which the network device is attached on the bridge. - PciAddr string - // LinkType defines the type of interface described by this structure. - // The expected values are the one that are defined by the netlink - // library, regarding each type of link. Here is a non exhaustive - // list: "veth", "macvtap", "vlan", "macvlan", "tap", ... - LinkType string -} - -// Route describes a network route. -type Route struct { - Dest string - Gateway string - Device string - Source string - Scope uint32 -} - -type ARPNeighbor struct { - ToIPAddress *IPAddress - Device string - LLAddr string - State int - Flags int -} diff --git a/src/runtime/virtcontainers/pkg/vcmock/mock.go b/src/runtime/virtcontainers/pkg/vcmock/mock.go index e4f01a6f9..b75292e8e 100644 --- a/src/runtime/virtcontainers/pkg/vcmock/mock.go +++ b/src/runtime/virtcontainers/pkg/vcmock/mock.go @@ -23,7 +23,7 @@ import ( vc "github.com/kata-containers/kata-containers/src/runtime/virtcontainers" "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/device/api" "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/device/config" - 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/types" specs "github.com/opencontainers/runtime-spec/specs-go" "github.com/sirupsen/logrus" @@ -246,7 +246,7 @@ func (m *VCMock) AddDevice(ctx context.Context, sandboxID string, info config.De } // AddInterface implements the VC function of the same name. -func (m *VCMock) AddInterface(ctx context.Context, sandboxID string, inf *vcTypes.Interface) (*vcTypes.Interface, error) { +func (m *VCMock) AddInterface(ctx context.Context, sandboxID string, inf *pbTypes.Interface) (*pbTypes.Interface, error) { if m.AddInterfaceFunc != nil { return m.AddInterfaceFunc(ctx, sandboxID, inf) } @@ -255,7 +255,7 @@ func (m *VCMock) AddInterface(ctx context.Context, sandboxID string, inf *vcType } // RemoveInterface implements the VC function of the same name. -func (m *VCMock) RemoveInterface(ctx context.Context, sandboxID string, inf *vcTypes.Interface) (*vcTypes.Interface, error) { +func (m *VCMock) RemoveInterface(ctx context.Context, sandboxID string, inf *pbTypes.Interface) (*pbTypes.Interface, error) { if m.RemoveInterfaceFunc != nil { return m.RemoveInterfaceFunc(ctx, sandboxID, inf) } @@ -264,7 +264,7 @@ func (m *VCMock) RemoveInterface(ctx context.Context, sandboxID string, inf *vcT } // ListInterfaces implements the VC function of the same name. -func (m *VCMock) ListInterfaces(ctx context.Context, sandboxID string) ([]*vcTypes.Interface, error) { +func (m *VCMock) ListInterfaces(ctx context.Context, sandboxID string) ([]*pbTypes.Interface, error) { if m.ListInterfacesFunc != nil { return m.ListInterfacesFunc(ctx, sandboxID) } @@ -273,7 +273,7 @@ func (m *VCMock) ListInterfaces(ctx context.Context, sandboxID string) ([]*vcTyp } // UpdateRoutes implements the VC function of the same name. -func (m *VCMock) UpdateRoutes(ctx context.Context, sandboxID string, routes []*vcTypes.Route) ([]*vcTypes.Route, error) { +func (m *VCMock) UpdateRoutes(ctx context.Context, sandboxID string, routes []*pbTypes.Route) ([]*pbTypes.Route, error) { if m.UpdateRoutesFunc != nil { return m.UpdateRoutesFunc(ctx, sandboxID, routes) } @@ -282,7 +282,7 @@ func (m *VCMock) UpdateRoutes(ctx context.Context, sandboxID string, routes []*v } // ListRoutes implements the VC function of the same name. -func (m *VCMock) ListRoutes(ctx context.Context, sandboxID string) ([]*vcTypes.Route, error) { +func (m *VCMock) ListRoutes(ctx context.Context, sandboxID string) ([]*pbTypes.Route, error) { if m.ListRoutesFunc != nil { return m.ListRoutesFunc(ctx, sandboxID) } diff --git a/src/runtime/virtcontainers/pkg/vcmock/mock_test.go b/src/runtime/virtcontainers/pkg/vcmock/mock_test.go index 357e90cdf..0a0292bfc 100644 --- a/src/runtime/virtcontainers/pkg/vcmock/mock_test.go +++ b/src/runtime/virtcontainers/pkg/vcmock/mock_test.go @@ -13,7 +13,7 @@ import ( vc "github.com/kata-containers/kata-containers/src/runtime/virtcontainers" "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/factory" - 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/types" "github.com/sirupsen/logrus" "github.com/stretchr/testify/assert" @@ -694,7 +694,7 @@ func TestVCMockAddInterface(t *testing.T) { assert.Error(err) assert.True(IsMockError(err)) - m.AddInterfaceFunc = func(ctx context.Context, sid string, inf *vcTypes.Interface) (*vcTypes.Interface, error) { + m.AddInterfaceFunc = func(ctx context.Context, sid string, inf *pbTypes.Interface) (*pbTypes.Interface, error) { return nil, nil } @@ -721,7 +721,7 @@ func TestVCMockRemoveInterface(t *testing.T) { assert.Error(err) assert.True(IsMockError(err)) - m.RemoveInterfaceFunc = func(ctx context.Context, sid string, inf *vcTypes.Interface) (*vcTypes.Interface, error) { + m.RemoveInterfaceFunc = func(ctx context.Context, sid string, inf *pbTypes.Interface) (*pbTypes.Interface, error) { return nil, nil } @@ -748,7 +748,7 @@ func TestVCMockListInterfaces(t *testing.T) { assert.Error(err) assert.True(IsMockError(err)) - m.ListInterfacesFunc = func(ctx context.Context, sid string) ([]*vcTypes.Interface, error) { + m.ListInterfacesFunc = func(ctx context.Context, sid string) ([]*pbTypes.Interface, error) { return nil, nil } @@ -775,7 +775,7 @@ func TestVCMockUpdateRoutes(t *testing.T) { assert.Error(err) assert.True(IsMockError(err)) - m.UpdateRoutesFunc = func(ctx context.Context, sid string, routes []*vcTypes.Route) ([]*vcTypes.Route, error) { + m.UpdateRoutesFunc = func(ctx context.Context, sid string, routes []*pbTypes.Route) ([]*pbTypes.Route, error) { return nil, nil } @@ -802,7 +802,7 @@ func TestVCMockListRoutes(t *testing.T) { assert.Error(err) assert.True(IsMockError(err)) - m.ListRoutesFunc = func(ctx context.Context, sid string) ([]*vcTypes.Route, error) { + m.ListRoutesFunc = func(ctx context.Context, sid string) ([]*pbTypes.Route, error) { return nil, nil } diff --git a/src/runtime/virtcontainers/pkg/vcmock/sandbox.go b/src/runtime/virtcontainers/pkg/vcmock/sandbox.go index cec576e5c..a7d4c6b7e 100644 --- a/src/runtime/virtcontainers/pkg/vcmock/sandbox.go +++ b/src/runtime/virtcontainers/pkg/vcmock/sandbox.go @@ -13,7 +13,7 @@ import ( vc "github.com/kata-containers/kata-containers/src/runtime/virtcontainers" "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/device/api" "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/device/config" - 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/types" specs "github.com/opencontainers/runtime-spec/specs-go" ) @@ -196,27 +196,27 @@ func (s *Sandbox) AddDevice(info config.DeviceInfo) (api.Device, error) { } // AddInterface implements the VCSandbox function of the same name. -func (s *Sandbox) AddInterface(inf *vcTypes.Interface) (*vcTypes.Interface, error) { +func (s *Sandbox) AddInterface(inf *pbTypes.Interface) (*pbTypes.Interface, error) { return nil, nil } // RemoveInterface implements the VCSandbox function of the same name. -func (s *Sandbox) RemoveInterface(inf *vcTypes.Interface) (*vcTypes.Interface, error) { +func (s *Sandbox) RemoveInterface(inf *pbTypes.Interface) (*pbTypes.Interface, error) { return nil, nil } // ListInterfaces implements the VCSandbox function of the same name. -func (s *Sandbox) ListInterfaces() ([]*vcTypes.Interface, error) { +func (s *Sandbox) ListInterfaces() ([]*pbTypes.Interface, error) { return nil, nil } // UpdateRoutes implements the VCSandbox function of the same name. -func (s *Sandbox) UpdateRoutes(routes []*vcTypes.Route) ([]*vcTypes.Route, error) { +func (s *Sandbox) UpdateRoutes(routes []*pbTypes.Route) ([]*pbTypes.Route, error) { return nil, nil } // ListRoutes implements the VCSandbox function of the same name. -func (s *Sandbox) ListRoutes() ([]*vcTypes.Route, error) { +func (s *Sandbox) ListRoutes() ([]*pbTypes.Route, error) { return nil, nil } diff --git a/src/runtime/virtcontainers/pkg/vcmock/types.go b/src/runtime/virtcontainers/pkg/vcmock/types.go index 89cf6ed33..39ea6fc4f 100644 --- a/src/runtime/virtcontainers/pkg/vcmock/types.go +++ b/src/runtime/virtcontainers/pkg/vcmock/types.go @@ -13,7 +13,7 @@ import ( vc "github.com/kata-containers/kata-containers/src/runtime/virtcontainers" "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/device/api" "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/device/config" - 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/types" specs "github.com/opencontainers/runtime-spec/specs-go" "github.com/sirupsen/logrus" @@ -59,11 +59,11 @@ type Sandbox struct { WinsizeProcessFunc func(containerID, processID string, height, width uint32) error IOStreamFunc func(containerID, processID string) (io.WriteCloser, io.Reader, io.Reader, error) AddDeviceFunc func(info config.DeviceInfo) (api.Device, error) - AddInterfaceFunc func(inf *vcTypes.Interface) (*vcTypes.Interface, error) - RemoveInterfaceFunc func(inf *vcTypes.Interface) (*vcTypes.Interface, error) - ListInterfacesFunc func() ([]*vcTypes.Interface, error) - UpdateRoutesFunc func(routes []*vcTypes.Route) ([]*vcTypes.Route, error) - ListRoutesFunc func() ([]*vcTypes.Route, error) + AddInterfaceFunc func(inf *pbTypes.Interface) (*pbTypes.Interface, error) + RemoveInterfaceFunc func(inf *pbTypes.Interface) (*pbTypes.Interface, error) + ListInterfacesFunc func() ([]*pbTypes.Interface, error) + UpdateRoutesFunc func(routes []*pbTypes.Route) ([]*pbTypes.Route, error) + ListRoutesFunc func() ([]*pbTypes.Route, error) UpdateRuntimeMetricsFunc func() error GetAgentMetricsFunc func() (string, error) StatsFunc func() (vc.SandboxStats, error) @@ -111,10 +111,10 @@ type VCMock struct { AddDeviceFunc func(ctx context.Context, sandboxID string, info config.DeviceInfo) (api.Device, error) - AddInterfaceFunc func(ctx context.Context, sandboxID string, inf *vcTypes.Interface) (*vcTypes.Interface, error) - RemoveInterfaceFunc func(ctx context.Context, sandboxID string, inf *vcTypes.Interface) (*vcTypes.Interface, error) - ListInterfacesFunc func(ctx context.Context, sandboxID string) ([]*vcTypes.Interface, error) - UpdateRoutesFunc func(ctx context.Context, sandboxID string, routes []*vcTypes.Route) ([]*vcTypes.Route, error) - ListRoutesFunc func(ctx context.Context, sandboxID string) ([]*vcTypes.Route, error) + AddInterfaceFunc func(ctx context.Context, sandboxID string, inf *pbTypes.Interface) (*pbTypes.Interface, error) + RemoveInterfaceFunc func(ctx context.Context, sandboxID string, inf *pbTypes.Interface) (*pbTypes.Interface, error) + ListInterfacesFunc func(ctx context.Context, sandboxID string) ([]*pbTypes.Interface, error) + UpdateRoutesFunc func(ctx context.Context, sandboxID string, routes []*pbTypes.Route) ([]*pbTypes.Route, error) + ListRoutesFunc func(ctx context.Context, sandboxID string) ([]*pbTypes.Route, error) CleanupContainerFunc func(ctx context.Context, sandboxID, containerID string, force bool) error } diff --git a/src/runtime/virtcontainers/sandbox.go b/src/runtime/virtcontainers/sandbox.go index cfece3563..e7cce34f7 100644 --- a/src/runtime/virtcontainers/sandbox.go +++ b/src/runtime/virtcontainers/sandbox.go @@ -32,6 +32,7 @@ import ( exp "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/experimental" "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/persist" 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" "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/pkg/annotations" vccgroups "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/pkg/cgroups" @@ -846,7 +847,7 @@ func (s *Sandbox) removeNetwork() error { return s.network.Remove(s.ctx, &s.networkNS, s.hypervisor) } -func (s *Sandbox) generateNetInfo(inf *vcTypes.Interface) (NetworkInfo, error) { +func (s *Sandbox) generateNetInfo(inf *pbTypes.Interface) (NetworkInfo, error) { hw, err := net.ParseMAC(inf.HwAddr) if err != nil { return NetworkInfo{}, err @@ -870,14 +871,14 @@ func (s *Sandbox) generateNetInfo(inf *vcTypes.Interface) (NetworkInfo, error) { HardwareAddr: hw, MTU: int(inf.Mtu), }, - Type: inf.LinkType, + Type: inf.Type, }, Addrs: addrs, }, nil } // AddInterface adds new nic to the sandbox. -func (s *Sandbox) AddInterface(inf *vcTypes.Interface) (*vcTypes.Interface, error) { +func (s *Sandbox) AddInterface(inf *pbTypes.Interface) (*pbTypes.Interface, error) { netInfo, err := s.generateNetInfo(inf) if err != nil { return nil, err @@ -908,7 +909,7 @@ func (s *Sandbox) AddInterface(inf *vcTypes.Interface) (*vcTypes.Interface, erro } // RemoveInterface removes a nic of the sandbox. -func (s *Sandbox) RemoveInterface(inf *vcTypes.Interface) (*vcTypes.Interface, error) { +func (s *Sandbox) RemoveInterface(inf *pbTypes.Interface) (*pbTypes.Interface, error) { for i, endpoint := range s.networkNS.Endpoints { if endpoint.HardwareAddr() == inf.HwAddr { s.Logger().WithField("endpoint-type", endpoint.Type()).Info("Hot detaching endpoint") @@ -928,17 +929,17 @@ func (s *Sandbox) RemoveInterface(inf *vcTypes.Interface) (*vcTypes.Interface, e } // ListInterfaces lists all nics and their configurations in the sandbox. -func (s *Sandbox) ListInterfaces() ([]*vcTypes.Interface, error) { +func (s *Sandbox) ListInterfaces() ([]*pbTypes.Interface, error) { return s.agent.listInterfaces() } // UpdateRoutes updates the sandbox route table (e.g. for portmapping support). -func (s *Sandbox) UpdateRoutes(routes []*vcTypes.Route) ([]*vcTypes.Route, error) { +func (s *Sandbox) UpdateRoutes(routes []*pbTypes.Route) ([]*pbTypes.Route, error) { return s.agent.updateRoutes(routes) } // ListRoutes lists all routes and their configurations in the sandbox. -func (s *Sandbox) ListRoutes() ([]*vcTypes.Route, error) { +func (s *Sandbox) ListRoutes() ([]*pbTypes.Route, error) { return s.agent.listRoutes() } diff --git a/src/runtime/virtcontainers/utils/utils.go b/src/runtime/virtcontainers/utils/utils.go index a83466833..146f428cf 100644 --- a/src/runtime/virtcontainers/utils/utils.go +++ b/src/runtime/virtcontainers/utils/utils.go @@ -13,6 +13,10 @@ import ( "os" "os/exec" "path/filepath" + + "github.com/vishvananda/netlink" + + pbTypes "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/pkg/agent/protocols" ) const cpBinaryName = "cp" @@ -318,3 +322,14 @@ func (p *ProgramLogger) StartLogger(output io.ReadCloser) error { func (p ProgramLogger) String() string { return p.cmd.Path } + +func ConvertNetlinkFamily(netlinkFamily int32) pbTypes.IPFamily { + switch netlinkFamily { + case netlink.FAMILY_V6: + return pbTypes.IPFamily_v6 + case netlink.FAMILY_V4: + fallthrough + default: + return pbTypes.IPFamily_v4 + } +}