From 39b95cc365a36b82aaaf75c96bc6c9e08adf3468 Mon Sep 17 00:00:00 2001 From: Sebastien Boeuf Date: Tue, 30 Oct 2018 07:38:04 -0700 Subject: [PATCH 1/4] virtcontainers: Create a new package "types" Instead of relying on the kata agent to define generic structures, the logic is to define those as virtcontainers "types" package. This way, all consumers of those structures, such as kata-runtime, kata-netmon, and kata-containerd-shim, don't have to import some dependency from the kata-agent. Fixes #876 Signed-off-by: Sebastien Boeuf --- virtcontainers/pkg/types/types.go | 40 +++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 virtcontainers/pkg/types/types.go diff --git a/virtcontainers/pkg/types/types.go b/virtcontainers/pkg/types/types.go new file mode 100644 index 0000000000..9fd0b201a4 --- /dev/null +++ b/virtcontainers/pkg/types/types.go @@ -0,0 +1,40 @@ +// 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 + 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 +} From 7bf84d05ada43f069ae68dc8fdd81e4e1764b103 Mon Sep 17 00:00:00 2001 From: Sebastien Boeuf Date: Wed, 31 Oct 2018 11:44:31 -0700 Subject: [PATCH 2/4] types: Replace agent/pkg/types with virtcontainers/pkg/types This commit replaces every place where the "types" package from the Kata agent was used, with the new "types" package from virtcontainers. In order to do so, it introduces a few translation functions between the agent and virtcontainers types, since this is needed by the kata agent implementation. Signed-off-by: Sebastien Boeuf --- cli/network.go | 2 +- cli/network_test.go | 2 +- netmon/netmon.go | 4 +- netmon/netmon_test.go | 4 +- virtcontainers/agent.go | 2 +- virtcontainers/api.go | 2 +- virtcontainers/api_test.go | 2 +- virtcontainers/hyperstart_agent.go | 2 +- virtcontainers/implementation.go | 2 +- virtcontainers/interfaces.go | 2 +- virtcontainers/kata_agent.go | 148 ++++++++++++++++++++++++- virtcontainers/kata_agent_test.go | 11 +- virtcontainers/network.go | 4 +- virtcontainers/network_test.go | 6 +- virtcontainers/noop_agent.go | 2 +- virtcontainers/pkg/vcmock/mock.go | 2 +- virtcontainers/pkg/vcmock/mock_test.go | 2 +- virtcontainers/pkg/vcmock/sandbox.go | 2 +- virtcontainers/pkg/vcmock/types.go | 2 +- virtcontainers/sandbox.go | 2 +- 20 files changed, 171 insertions(+), 34 deletions(-) diff --git a/cli/network.go b/cli/network.go index 86745ed994..57e367c2f6 100644 --- a/cli/network.go +++ b/cli/network.go @@ -17,8 +17,8 @@ import ( "golang.org/x/sys/unix" "github.com/containernetworking/plugins/pkg/ns" - "github.com/kata-containers/agent/pkg/types" vc "github.com/kata-containers/runtime/virtcontainers" + "github.com/kata-containers/runtime/virtcontainers/pkg/types" "github.com/sirupsen/logrus" "github.com/urfave/cli" ) diff --git a/cli/network_test.go b/cli/network_test.go index 389b9df7c9..5a7ede4f6d 100644 --- a/cli/network_test.go +++ b/cli/network_test.go @@ -18,8 +18,8 @@ import ( "golang.org/x/sys/unix" "github.com/containernetworking/plugins/pkg/ns" - "github.com/kata-containers/agent/pkg/types" vc "github.com/kata-containers/runtime/virtcontainers" + "github.com/kata-containers/runtime/virtcontainers/pkg/types" "github.com/stretchr/testify/assert" ) diff --git a/netmon/netmon.go b/netmon/netmon.go index c29713034b..138cd952ec 100644 --- a/netmon/netmon.go +++ b/netmon/netmon.go @@ -21,8 +21,8 @@ import ( "syscall" "time" - "github.com/kata-containers/agent/pkg/types" "github.com/kata-containers/runtime/pkg/signals" + "github.com/kata-containers/runtime/virtcontainers/pkg/types" "github.com/sirupsen/logrus" lSyslog "github.com/sirupsen/logrus/hooks/syslog" "github.com/vishvananda/netlink" @@ -275,7 +275,7 @@ func convertInterface(linkAttrs *netlink.LinkAttrs, addrs []netlink.Addr) types. netMask, _ := addr.Mask.Size() ipAddr := &types.IPAddress{ - Family: types.IPFamily(netlinkFamily), + Family: netlinkFamily, Address: addr.IP.String(), Mask: fmt.Sprintf("%d", netMask), } diff --git a/netmon/netmon_test.go b/netmon/netmon_test.go index 463ea62b6f..bb5e62ebb9 100644 --- a/netmon/netmon_test.go +++ b/netmon/netmon_test.go @@ -16,7 +16,7 @@ import ( "runtime" "testing" - "github.com/kata-containers/agent/pkg/types" + "github.com/kata-containers/runtime/virtcontainers/pkg/types" "github.com/sirupsen/logrus" "github.com/stretchr/testify/assert" "github.com/vishvananda/netlink" @@ -181,7 +181,7 @@ func TestConvertInterface(t *testing.T) { HwAddr: testHwAddr, IPAddresses: []*types.IPAddress{ { - Family: types.IPFamily(netlinkFamily), + Family: netlinkFamily, Address: testIPAddress, Mask: "0", }, diff --git a/virtcontainers/agent.go b/virtcontainers/agent.go index f2e5e0ca2d..d95cdbf635 100644 --- a/virtcontainers/agent.go +++ b/virtcontainers/agent.go @@ -9,8 +9,8 @@ import ( "fmt" "syscall" - "github.com/kata-containers/agent/pkg/types" "github.com/kata-containers/agent/protocols/grpc" + "github.com/kata-containers/runtime/virtcontainers/pkg/types" "github.com/mitchellh/mapstructure" specs "github.com/opencontainers/runtime-spec/specs-go" "golang.org/x/net/context" diff --git a/virtcontainers/api.go b/virtcontainers/api.go index dce9e1f0cb..a89847b67f 100644 --- a/virtcontainers/api.go +++ b/virtcontainers/api.go @@ -11,9 +11,9 @@ import ( "runtime" "syscall" - "github.com/kata-containers/agent/pkg/types" deviceApi "github.com/kata-containers/runtime/virtcontainers/device/api" deviceConfig "github.com/kata-containers/runtime/virtcontainers/device/config" + "github.com/kata-containers/runtime/virtcontainers/pkg/types" specs "github.com/opencontainers/runtime-spec/specs-go" opentracing "github.com/opentracing/opentracing-go" "github.com/sirupsen/logrus" diff --git a/virtcontainers/api_test.go b/virtcontainers/api_test.go index a7e172fcef..e6b1252bf8 100644 --- a/virtcontainers/api_test.go +++ b/virtcontainers/api_test.go @@ -18,8 +18,8 @@ import ( "testing" "github.com/containernetworking/plugins/pkg/ns" - "github.com/kata-containers/agent/pkg/types" "github.com/kata-containers/runtime/virtcontainers/pkg/mock" + "github.com/kata-containers/runtime/virtcontainers/pkg/types" specs "github.com/opencontainers/runtime-spec/specs-go" "github.com/stretchr/testify/assert" ) diff --git a/virtcontainers/hyperstart_agent.go b/virtcontainers/hyperstart_agent.go index 4cfb4e3697..34b381b135 100644 --- a/virtcontainers/hyperstart_agent.go +++ b/virtcontainers/hyperstart_agent.go @@ -18,11 +18,11 @@ import ( "github.com/vishvananda/netlink" proxyClient "github.com/clearcontainers/proxy/client" - "github.com/kata-containers/agent/pkg/types" "github.com/kata-containers/agent/protocols/grpc" "github.com/kata-containers/runtime/virtcontainers/device/config" "github.com/kata-containers/runtime/virtcontainers/pkg/hyperstart" ns "github.com/kata-containers/runtime/virtcontainers/pkg/nsenter" + "github.com/kata-containers/runtime/virtcontainers/pkg/types" "github.com/kata-containers/runtime/virtcontainers/utils" specs "github.com/opencontainers/runtime-spec/specs-go" "golang.org/x/net/context" diff --git a/virtcontainers/implementation.go b/virtcontainers/implementation.go index 152f330452..22e910b4f6 100644 --- a/virtcontainers/implementation.go +++ b/virtcontainers/implementation.go @@ -13,9 +13,9 @@ import ( "context" "syscall" - "github.com/kata-containers/agent/pkg/types" "github.com/kata-containers/runtime/virtcontainers/device/api" "github.com/kata-containers/runtime/virtcontainers/device/config" + "github.com/kata-containers/runtime/virtcontainers/pkg/types" specs "github.com/opencontainers/runtime-spec/specs-go" "github.com/sirupsen/logrus" ) diff --git a/virtcontainers/interfaces.go b/virtcontainers/interfaces.go index 22d93ea28f..1731fb4a85 100644 --- a/virtcontainers/interfaces.go +++ b/virtcontainers/interfaces.go @@ -10,9 +10,9 @@ import ( "io" "syscall" - "github.com/kata-containers/agent/pkg/types" "github.com/kata-containers/runtime/virtcontainers/device/api" "github.com/kata-containers/runtime/virtcontainers/device/config" + "github.com/kata-containers/runtime/virtcontainers/pkg/types" specs "github.com/opencontainers/runtime-spec/specs-go" "github.com/sirupsen/logrus" ) diff --git a/virtcontainers/kata_agent.go b/virtcontainers/kata_agent.go index 0eef94c190..2cfa365736 100644 --- a/virtcontainers/kata_agent.go +++ b/virtcontainers/kata_agent.go @@ -18,12 +18,13 @@ import ( "syscall" "time" - "github.com/kata-containers/agent/pkg/types" + aTypes "github.com/kata-containers/agent/pkg/types" kataclient "github.com/kata-containers/agent/protocols/client" "github.com/kata-containers/agent/protocols/grpc" "github.com/kata-containers/runtime/virtcontainers/device/config" vcAnnotations "github.com/kata-containers/runtime/virtcontainers/pkg/annotations" ns "github.com/kata-containers/runtime/virtcontainers/pkg/nsenter" + "github.com/kata-containers/runtime/virtcontainers/pkg/types" "github.com/kata-containers/runtime/virtcontainers/pkg/uuid" "github.com/kata-containers/runtime/virtcontainers/utils" opentracing "github.com/opentracing/opentracing-go" @@ -31,6 +32,7 @@ import ( "github.com/gogo/protobuf/proto" "github.com/opencontainers/runtime-spec/specs-go" "github.com/sirupsen/logrus" + "github.com/vishvananda/netlink" "golang.org/x/net/context" golangGrpc "google.golang.org/grpc" "google.golang.org/grpc/codes" @@ -387,7 +389,7 @@ func (k *kataAgent) exec(sandbox *Sandbox, c Container, cmd Cmd) (*Process, erro func (k *kataAgent) updateInterface(ifc *types.Interface) (*types.Interface, error) { // send update interface request ifcReq := &grpc.UpdateInterfaceRequest{ - Interface: ifc, + Interface: k.convertToKataAgentInterface(ifc), } resultingInterface, err := k.sendReq(ifcReq) if err != nil { @@ -415,7 +417,7 @@ func (k *kataAgent) updateRoutes(routes []*types.Route) ([]*types.Route, error) if routes != nil { routesReq := &grpc.UpdateRoutesRequest{ Routes: &grpc.Routes{ - Routes: routes, + Routes: k.convertToKataAgentRoutes(routes), }, } resultingRoutes, err := k.sendReq(routesReq) @@ -427,7 +429,7 @@ func (k *kataAgent) updateRoutes(routes []*types.Route) ([]*types.Route, error) } resultRoutes, ok := resultingRoutes.(*grpc.Routes) if ok && resultRoutes != nil { - return resultRoutes.Routes, err + return k.convertToRoutes(resultRoutes.Routes), err } return nil, err } @@ -442,7 +444,7 @@ func (k *kataAgent) listInterfaces() ([]*types.Interface, error) { } resultInterfaces, ok := resultingInterfaces.(*grpc.Interfaces) if ok { - return resultInterfaces.Interfaces, err + return k.convertToInterfaces(resultInterfaces.Interfaces), err } return nil, err } @@ -455,7 +457,7 @@ func (k *kataAgent) listRoutes() ([]*types.Route, error) { } resultRoutes, ok := resultingRoutes.(*grpc.Routes) if ok { - return resultRoutes.Routes, err + return k.convertToRoutes(resultRoutes.Routes), err } return nil, err } @@ -1549,3 +1551,137 @@ func (k *kataAgent) getGuestDetails(req *grpc.GuestDetailsRequest) (*grpc.GuestD return resp.(*grpc.GuestDetailsResponse), nil } + +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) convertToKataAgentIPAddresses(ipAddrs []*types.IPAddress) (aIPAddrs []*aTypes.IPAddress) { + for _, ipAddr := range ipAddrs { + if ipAddr == nil { + continue + } + + aIPAddr := &aTypes.IPAddress{ + Family: k.convertToKataAgentIPFamily(ipAddr.Family), + Address: ipAddr.Address, + Mask: ipAddr.Mask, + } + + aIPAddrs = append(aIPAddrs, aIPAddr) + } + + return aIPAddrs +} + +func (k *kataAgent) convertToIPAddresses(aIPAddrs []*aTypes.IPAddress) (ipAddrs []*types.IPAddress) { + for _, aIPAddr := range aIPAddrs { + if aIPAddr == nil { + continue + } + + ipAddr := &types.IPAddress{ + Family: k.convertToIPFamily(aIPAddr.Family), + Address: aIPAddr.Address, + Mask: aIPAddr.Mask, + } + + ipAddrs = append(ipAddrs, ipAddr) + } + + return ipAddrs +} + +func (k *kataAgent) convertToKataAgentInterface(iface *types.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, + HwAddr: iface.HwAddr, + PciAddr: iface.PciAddr, + } +} + +func (k *kataAgent) convertToInterfaces(aIfaces []*aTypes.Interface) (ifaces []*types.Interface) { + for _, aIface := range aIfaces { + if aIface == nil { + continue + } + + iface := &types.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 []*types.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) convertToRoutes(aRoutes []*aTypes.Route) (routes []*types.Route) { + for _, aRoute := range aRoutes { + if aRoute == nil { + continue + } + + route := &types.Route{ + Dest: aRoute.Dest, + Gateway: aRoute.Gateway, + Device: aRoute.Device, + Source: aRoute.Source, + Scope: aRoute.Scope, + } + + routes = append(routes, route) + } + + return routes +} diff --git a/virtcontainers/kata_agent_test.go b/virtcontainers/kata_agent_test.go index 3fc9f2ade3..b57b13f610 100644 --- a/virtcontainers/kata_agent_test.go +++ b/virtcontainers/kata_agent_test.go @@ -23,7 +23,7 @@ import ( "golang.org/x/net/context" "google.golang.org/grpc" - "github.com/kata-containers/agent/pkg/types" + aTypes "github.com/kata-containers/agent/pkg/types" pb "github.com/kata-containers/agent/protocols/grpc" "github.com/kata-containers/runtime/virtcontainers/device/api" "github.com/kata-containers/runtime/virtcontainers/device/config" @@ -31,6 +31,7 @@ import ( "github.com/kata-containers/runtime/virtcontainers/device/manager" vcAnnotations "github.com/kata-containers/runtime/virtcontainers/pkg/annotations" "github.com/kata-containers/runtime/virtcontainers/pkg/mock" + "github.com/kata-containers/runtime/virtcontainers/pkg/types" ) var ( @@ -185,16 +186,16 @@ func (p *gRPCProxy) DestroySandbox(ctx context.Context, req *pb.DestroySandboxRe return emptyResp, nil } -func (p *gRPCProxy) AddInterface(ctx context.Context, req *pb.AddInterfaceRequest) (*types.Interface, error) { +func (p *gRPCProxy) AddInterface(ctx context.Context, req *pb.AddInterfaceRequest) (*aTypes.Interface, error) { return nil, nil } -func (p *gRPCProxy) RemoveInterface(ctx context.Context, req *pb.RemoveInterfaceRequest) (*types.Interface, error) { +func (p *gRPCProxy) RemoveInterface(ctx context.Context, req *pb.RemoveInterfaceRequest) (*aTypes.Interface, error) { return nil, nil } -func (p *gRPCProxy) UpdateInterface(ctx context.Context, req *pb.UpdateInterfaceRequest) (*types.Interface, error) { - return &types.Interface{}, nil +func (p *gRPCProxy) UpdateInterface(ctx context.Context, req *pb.UpdateInterfaceRequest) (*aTypes.Interface, error) { + return &aTypes.Interface{}, nil } func (p *gRPCProxy) UpdateRoutes(ctx context.Context, req *pb.UpdateRoutesRequest) (*pb.Routes, error) { diff --git a/virtcontainers/network.go b/virtcontainers/network.go index ae276e0078..ada08e876a 100644 --- a/virtcontainers/network.go +++ b/virtcontainers/network.go @@ -22,7 +22,7 @@ import ( "github.com/vishvananda/netns" "golang.org/x/sys/unix" - "github.com/kata-containers/agent/pkg/types" + "github.com/kata-containers/runtime/virtcontainers/pkg/types" "github.com/kata-containers/runtime/virtcontainers/pkg/uuid" "github.com/kata-containers/runtime/virtcontainers/utils" ) @@ -1182,7 +1182,7 @@ func generateInterfacesAndRoutes(networkNS NetworkNamespace) ([]*types.Interface } netMask, _ := addr.Mask.Size() ipAddress := types.IPAddress{ - Family: types.IPFamily_v4, + Family: netlink.FAMILY_V4, Address: addr.IP.String(), Mask: fmt.Sprintf("%d", netMask), } diff --git a/virtcontainers/network_test.go b/virtcontainers/network_test.go index 7aa900c59e..7e2bac192d 100644 --- a/virtcontainers/network_test.go +++ b/virtcontainers/network_test.go @@ -11,7 +11,7 @@ import ( "reflect" "testing" - "github.com/kata-containers/agent/pkg/types" + "github.com/kata-containers/runtime/virtcontainers/pkg/types" "github.com/stretchr/testify/assert" "github.com/vishvananda/netlink" ) @@ -162,8 +162,8 @@ func TestGenerateInterfacesAndRoutes(t *testing.T) { // Build expected results: // expectedAddresses := []*types.IPAddress{ - {Family: 0, Address: "172.17.0.2", Mask: "16"}, - {Family: 0, Address: "182.17.0.2", Mask: "16"}, + {Family: netlink.FAMILY_V4, Address: "172.17.0.2", Mask: "16"}, + {Family: netlink.FAMILY_V4, Address: "182.17.0.2", Mask: "16"}, } expectedInterfaces := []*types.Interface{ diff --git a/virtcontainers/noop_agent.go b/virtcontainers/noop_agent.go index 94cb1b8b9e..f9e447f2d3 100644 --- a/virtcontainers/noop_agent.go +++ b/virtcontainers/noop_agent.go @@ -8,8 +8,8 @@ package virtcontainers import ( "syscall" - "github.com/kata-containers/agent/pkg/types" "github.com/kata-containers/agent/protocols/grpc" + "github.com/kata-containers/runtime/virtcontainers/pkg/types" specs "github.com/opencontainers/runtime-spec/specs-go" "golang.org/x/net/context" ) diff --git a/virtcontainers/pkg/vcmock/mock.go b/virtcontainers/pkg/vcmock/mock.go index 04e6715fce..5c3dc317ff 100644 --- a/virtcontainers/pkg/vcmock/mock.go +++ b/virtcontainers/pkg/vcmock/mock.go @@ -20,10 +20,10 @@ import ( "fmt" "syscall" - "github.com/kata-containers/agent/pkg/types" vc "github.com/kata-containers/runtime/virtcontainers" "github.com/kata-containers/runtime/virtcontainers/device/api" "github.com/kata-containers/runtime/virtcontainers/device/config" + "github.com/kata-containers/runtime/virtcontainers/pkg/types" specs "github.com/opencontainers/runtime-spec/specs-go" "github.com/sirupsen/logrus" ) diff --git a/virtcontainers/pkg/vcmock/mock_test.go b/virtcontainers/pkg/vcmock/mock_test.go index ebc43e66b3..eb8ef19142 100644 --- a/virtcontainers/pkg/vcmock/mock_test.go +++ b/virtcontainers/pkg/vcmock/mock_test.go @@ -11,9 +11,9 @@ import ( "syscall" "testing" - "github.com/kata-containers/agent/pkg/types" vc "github.com/kata-containers/runtime/virtcontainers" "github.com/kata-containers/runtime/virtcontainers/factory" + "github.com/kata-containers/runtime/virtcontainers/pkg/types" "github.com/sirupsen/logrus" "github.com/stretchr/testify/assert" ) diff --git a/virtcontainers/pkg/vcmock/sandbox.go b/virtcontainers/pkg/vcmock/sandbox.go index 38b6aa4e21..9fac78a076 100644 --- a/virtcontainers/pkg/vcmock/sandbox.go +++ b/virtcontainers/pkg/vcmock/sandbox.go @@ -9,10 +9,10 @@ import ( "io" "syscall" - "github.com/kata-containers/agent/pkg/types" vc "github.com/kata-containers/runtime/virtcontainers" "github.com/kata-containers/runtime/virtcontainers/device/api" "github.com/kata-containers/runtime/virtcontainers/device/config" + "github.com/kata-containers/runtime/virtcontainers/pkg/types" specs "github.com/opencontainers/runtime-spec/specs-go" ) diff --git a/virtcontainers/pkg/vcmock/types.go b/virtcontainers/pkg/vcmock/types.go index 88c2f81c61..cd935feffb 100644 --- a/virtcontainers/pkg/vcmock/types.go +++ b/virtcontainers/pkg/vcmock/types.go @@ -9,10 +9,10 @@ import ( "context" "syscall" - "github.com/kata-containers/agent/pkg/types" vc "github.com/kata-containers/runtime/virtcontainers" "github.com/kata-containers/runtime/virtcontainers/device/api" "github.com/kata-containers/runtime/virtcontainers/device/config" + "github.com/kata-containers/runtime/virtcontainers/pkg/types" specs "github.com/opencontainers/runtime-spec/specs-go" "github.com/sirupsen/logrus" ) diff --git a/virtcontainers/sandbox.go b/virtcontainers/sandbox.go index a35f033156..8746e6eeba 100644 --- a/virtcontainers/sandbox.go +++ b/virtcontainers/sandbox.go @@ -21,12 +21,12 @@ import ( opentracing "github.com/opentracing/opentracing-go" "github.com/sirupsen/logrus" - "github.com/kata-containers/agent/pkg/types" "github.com/kata-containers/agent/protocols/grpc" "github.com/kata-containers/runtime/virtcontainers/device/api" "github.com/kata-containers/runtime/virtcontainers/device/config" "github.com/kata-containers/runtime/virtcontainers/device/drivers" deviceManager "github.com/kata-containers/runtime/virtcontainers/device/manager" + "github.com/kata-containers/runtime/virtcontainers/pkg/types" "github.com/vishvananda/netlink" ) From 45b219107c7253b1ab530ef0ad8f550adacce2d2 Mon Sep 17 00:00:00 2001 From: Sebastien Boeuf Date: Mon, 29 Oct 2018 09:03:31 -0700 Subject: [PATCH 3/4] netmon: Rely on new interface field LinkType In order to provide the right information about the interface that needs to be added, kata-netmon provisions the new field LinkType of the Interface structure. Signed-off-by: Sebastien Boeuf --- netmon/netmon.go | 7 ++++--- netmon/netmon_test.go | 14 +++++++++----- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/netmon/netmon.go b/netmon/netmon.go index 138cd952ec..ab932abc8f 100644 --- a/netmon/netmon.go +++ b/netmon/netmon.go @@ -259,7 +259,7 @@ 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) types.Interface { +func convertInterface(linkAttrs *netlink.LinkAttrs, linkType string, addrs []netlink.Addr) types.Interface { if linkAttrs == nil { netmonLog.Warn("Link attributes are nil") return types.Interface{} @@ -289,6 +289,7 @@ func convertInterface(linkAttrs *netlink.LinkAttrs, addrs []netlink.Addr) types. IPAddresses: ipAddrs, Mtu: uint64(linkAttrs.MTU), HwAddr: linkAttrs.HardwareAddr.String(), + LinkType: linkType, } netmonLog.WithField("interface", iface).Debug("Interface converted") @@ -369,7 +370,7 @@ func (n *netmon) scanNetwork() error { continue } - iface := convertInterface(linkAttrs, addrs) + iface := convertInterface(linkAttrs, link.Type(), addrs) n.netIfaces[linkAttrs.Index] = iface } @@ -497,7 +498,7 @@ func (n *netmon) handleRTMNewLink(ev netlink.LinkUpdate) error { } // Convert the interfaces in the appropriate structure format. - iface := convertInterface(linkAttrs, addrs) + iface := convertInterface(linkAttrs, ev.Link.Type(), addrs) // Add the interface through the Kata CLI. if err := n.addInterfaceCLI(iface); err != nil { diff --git a/netmon/netmon_test.go b/netmon/netmon_test.go index bb5e62ebb9..cc5793e120 100644 --- a/netmon/netmon_test.go +++ b/netmon/netmon_test.go @@ -174,6 +174,8 @@ func TestConvertInterface(t *testing.T) { HardwareAddr: hwAddr, } + linkType := "link_type_test" + expected := types.Interface{ Device: testIfaceName, Name: testIfaceName, @@ -186,9 +188,10 @@ func TestConvertInterface(t *testing.T) { Mask: "0", }, }, + LinkType: linkType, } - got := convertInterface(linkAttrs, addrs) + got := convertInterface(linkAttrs, linkType, addrs) assert.True(t, reflect.DeepEqual(expected, got), "Got %+v\nExpected %+v", got, expected) } @@ -264,10 +267,11 @@ func testCreateDummyNetwork(t *testing.T, handler *netlink.Handle) (int, types.I assert.NotNil(t, attrs) iface := types.Interface{ - Device: testIfaceName, - Name: testIfaceName, - Mtu: uint64(testMTU), - HwAddr: testHwAddr, + Device: testIfaceName, + Name: testIfaceName, + Mtu: uint64(testMTU), + HwAddr: testHwAddr, + LinkType: link.Type(), } return attrs.Index, iface From 51997775bd4251f73b4fd1a924ba8738229dc687 Mon Sep 17 00:00:00 2001 From: Sebastien Boeuf Date: Mon, 29 Oct 2018 09:10:17 -0700 Subject: [PATCH 4/4] virtcontainers: Rely on new interface LinkType field Now that Interface structure includes the useful information about the type of interface, Kata does not need to do any assumption about the type of interface that needs to be added. Fixes #866 Signed-off-by: Sebastien Boeuf --- virtcontainers/sandbox.go | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/virtcontainers/sandbox.go b/virtcontainers/sandbox.go index 8746e6eeba..3d723e0042 100644 --- a/virtcontainers/sandbox.go +++ b/virtcontainers/sandbox.go @@ -1098,13 +1098,6 @@ func (s *Sandbox) generateNetInfo(inf *types.Interface) (NetworkInfo, error) { addrs = append(addrs, *netlinkAddr) } - var ifaceType string - if s.config.NetworkConfig.InterworkingModel == NetXConnectNoneModel { - ifaceType = "tap" - } else { - ifaceType = "veth" - } - return NetworkInfo{ Iface: NetlinkIface{ LinkAttrs: netlink.LinkAttrs{ @@ -1112,7 +1105,7 @@ func (s *Sandbox) generateNetInfo(inf *types.Interface) (NetworkInfo, error) { HardwareAddr: hw, MTU: int(inf.Mtu), }, - Type: ifaceType, + Type: inf.LinkType, }, Addrs: addrs, }, nil