Skip to content

Commit

Permalink
proto: Split reusable structures into their own package
Browse files Browse the repository at this point in the history
Some structures such as Interface and Route are not only used by the
kata-agent since they are generic enough to be reused by kata-runtime
or kata-netmon.

By splitting those structures into their separate package, we prevent
their consumers from importing the whole protocols/grpc package which
pulls a lot of unneeded content.

Depends-on: github.com/kata-containers/runtime#858

Fixes kata-containers#399

Signed-off-by: Sebastien Boeuf <sebastien.boeuf@intel.com>
  • Loading branch information
Sebastien Boeuf committed Oct 25, 2018
1 parent 03f040f commit a13144b
Show file tree
Hide file tree
Showing 11 changed files with 1,664 additions and 1,263 deletions.
9 changes: 5 additions & 4 deletions grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"time"

gpb "github.com/gogo/protobuf/types"
"github.com/kata-containers/agent/pkg/types"
pb "github.com/kata-containers/agent/protocols/grpc"
"github.com/opencontainers/runc/libcontainer"
"github.com/opencontainers/runc/libcontainer/configs"
Expand Down Expand Up @@ -1212,7 +1213,7 @@ func (a *agentGRPC) CreateSandbox(ctx context.Context, req *pb.CreateSandboxRequ

a.sandbox.hostname = req.Hostname
a.sandbox.containers = make(map[string]*container)
a.sandbox.network.ifaces = make(map[string]*pb.Interface)
a.sandbox.network.ifaces = make(map[string]*types.Interface)
a.sandbox.network.dns = req.Dns
a.sandbox.running = true
a.sandbox.sandboxPidNs = req.SandboxPidns
Expand Down Expand Up @@ -1310,15 +1311,15 @@ func (a *agentGRPC) DestroySandbox(ctx context.Context, req *pb.DestroySandboxRe
return emptyResp, nil
}

func (a *agentGRPC) AddInterface(ctx context.Context, req *pb.AddInterfaceRequest) (*pb.Interface, error) {
func (a *agentGRPC) AddInterface(ctx context.Context, req *pb.AddInterfaceRequest) (*types.Interface, error) {
return a.sandbox.addInterface(nil, req.Interface)
}

func (a *agentGRPC) UpdateInterface(ctx context.Context, req *pb.UpdateInterfaceRequest) (*pb.Interface, error) {
func (a *agentGRPC) UpdateInterface(ctx context.Context, req *pb.UpdateInterfaceRequest) (*types.Interface, error) {
return a.sandbox.updateInterface(nil, req.Interface)
}

func (a *agentGRPC) RemoveInterface(ctx context.Context, req *pb.RemoveInterfaceRequest) (*pb.Interface, error) {
func (a *agentGRPC) RemoveInterface(ctx context.Context, req *pb.RemoveInterfaceRequest) (*types.Interface, error) {
return a.sandbox.removeInterface(nil, req.Interface)
}

Expand Down
12 changes: 10 additions & 2 deletions hack/update-generated-agent-proto.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,16 @@
#
# SPDX-License-Identifier: Apache-2.0
#
protoc -I=$GOPATH/src -I=$GOPATH/src/github.com/gogo/protobuf/protobuf \
--proto_path=protocols/grpc --gogofast_out=\

protoc \
pkg/types/types.proto --gogofast_out=.

protoc \
-I=$GOPATH/src \
-I=$GOPATH/src/github.com/gogo/protobuf/protobuf \
--proto_path=protocols/grpc \
--gogofast_out=\
Mgithub.com/kata-containers/agent/pkg/types/types.proto=github.com/kata-containers/agent/pkg/types,\
Mgoogle/protobuf/any.proto=github.com/gogo/protobuf/types,\
Mgoogle/protobuf/duration.proto=github.com/gogo/protobuf/types,\
Mgoogle/protobuf/struct.proto=github.com/gogo/protobuf/types,\
Expand Down
25 changes: 13 additions & 12 deletions network.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (

"golang.org/x/sys/unix"

"github.com/kata-containers/agent/pkg/types"
pb "github.com/kata-containers/agent/protocols/grpc"
"github.com/sirupsen/logrus"
"github.com/vishvananda/netlink"
Expand All @@ -34,10 +35,10 @@ var (
// related information.
type network struct {
ifacesLock sync.Mutex
ifaces map[string]*pb.Interface
ifaces map[string]*types.Interface

routesLock sync.Mutex
routes []pb.Route
routes []types.Route

dns []string
}
Expand Down Expand Up @@ -82,7 +83,7 @@ func linkByHwAddr(netHandle *netlink.Handle, hwAddr string) (netlink.Link, error
return nil, grpcStatus.Errorf(codes.NotFound, "Could not find the link corresponding to HwAddr %q", hwAddr)
}

func updateLink(netHandle *netlink.Handle, link netlink.Link, iface *pb.Interface) error {
func updateLink(netHandle *netlink.Handle, link netlink.Link, iface *types.Interface) error {
if netHandle == nil {
return errNoHandle
}
Expand Down Expand Up @@ -134,7 +135,7 @@ func updateLink(netHandle *netlink.Handle, link netlink.Link, iface *pb.Interfac
return nil
}

func (s *sandbox) addInterface(netHandle *netlink.Handle, iface *pb.Interface) (resultingIfc *pb.Interface, err error) {
func (s *sandbox) addInterface(netHandle *netlink.Handle, iface *types.Interface) (resultingIfc *types.Interface, err error) {
if iface == nil {
return nil, errNoIF
}
Expand Down Expand Up @@ -179,7 +180,7 @@ func (s *sandbox) addInterface(netHandle *netlink.Handle, iface *pb.Interface) (

return iface, nil
}
func (s *sandbox) removeInterface(netHandle *netlink.Handle, iface *pb.Interface) (resultingIfc *pb.Interface, err error) {
func (s *sandbox) removeInterface(netHandle *netlink.Handle, iface *types.Interface) (resultingIfc *types.Interface, err error) {
if iface == nil {
return nil, errNoIF
}
Expand Down Expand Up @@ -217,10 +218,10 @@ func (s *sandbox) removeInterface(netHandle *netlink.Handle, iface *pb.Interface
return nil, nil
}

// updateInterface will update an existing interface with the values provided in the pb.Interface. It will identify the
// updateInterface will update an existing interface with the values provided in the types.Interface. It will identify the
// existing interface via MAC address and will return the state of the interface once the function completes as well an any
// errors observed.
func (s *sandbox) updateInterface(netHandle *netlink.Handle, iface *pb.Interface) (resultingIfc *pb.Interface, err error) {
func (s *sandbox) updateInterface(netHandle *netlink.Handle, iface *types.Interface) (resultingIfc *types.Interface, err error) {
if iface == nil {
return nil, errNoIF
}
Expand Down Expand Up @@ -301,7 +302,7 @@ func (s *sandbox) updateInterface(netHandle *netlink.Handle, iface *pb.Interface
}

// getInterface will retrieve interface details from the provided link
func getInterface(netHandle *netlink.Handle, link netlink.Link) (*pb.Interface, error) {
func getInterface(netHandle *netlink.Handle, link netlink.Link) (*types.Interface, error) {
if netHandle == nil {
return nil, errNoHandle
}
Expand All @@ -310,7 +311,7 @@ func getInterface(netHandle *netlink.Handle, link netlink.Link) (*pb.Interface,
return nil, errNoLink
}

var ifc pb.Interface
var ifc types.Interface
linkAttrs := link.Attrs()
ifc.Name = linkAttrs.Name
ifc.Mtu = uint64(linkAttrs.MTU)
Expand All @@ -323,7 +324,7 @@ func getInterface(netHandle *netlink.Handle, link netlink.Link) (*pb.Interface,
}
for _, addr := range addrs {
netMask, _ := addr.Mask.Size()
m := pb.IPAddress{
m := types.IPAddress{
Address: addr.IP.String(),
Mask: fmt.Sprintf("%d", netMask),
}
Expand Down Expand Up @@ -481,7 +482,7 @@ func getCurrentRoutes(netHandle *netlink.Handle) (*pb.Routes, error) {
}

for _, route := range finalRouteList {
var r pb.Route
var r types.Route
if route.Dst != nil {
r.Dest = route.Dst.String()
}
Expand All @@ -508,7 +509,7 @@ func getCurrentRoutes(netHandle *netlink.Handle) (*pb.Routes, error) {
return &routes, nil
}

func (s *sandbox) updateRoute(netHandle *netlink.Handle, route *pb.Route, add bool) (err error) {
func (s *sandbox) updateRoute(netHandle *netlink.Handle, route *types.Route, add bool) (err error) {
s.network.routesLock.Lock()
defer s.network.routesLock.Unlock()

Expand Down
19 changes: 10 additions & 9 deletions network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"runtime"
"testing"

"github.com/kata-containers/agent/pkg/types"
pb "github.com/kata-containers/agent/protocols/grpc"
"github.com/stretchr/testify/assert"
"github.com/vishvananda/netlink"
Expand All @@ -23,12 +24,12 @@ func TestUpdateRemoveInterface(t *testing.T) {

s := sandbox{}

ifc := pb.Interface{
ifc := types.Interface{
Name: "enoNumber",
Mtu: 1500,
HwAddr: "02:00:ca:fe:00:48",
}
ip := pb.IPAddress{
ip := types.IPAddress{
Family: 0,
Address: "192.168.0.101",
Mask: "24",
Expand Down Expand Up @@ -68,7 +69,7 @@ func TestUpdateRemoveInterface(t *testing.T) {
// Try with a different valid MTU. Make sure we can assign a new set of IP addresses
ifc.Mtu = 500
ifc.IPAddresses[0].Address = "192.168.0.102"
ip2 := pb.IPAddress{
ip2 := types.IPAddress{
Family: 0,
Address: "182.168.0.103",
Mask: "24",
Expand Down Expand Up @@ -146,7 +147,7 @@ func TestUpdateRoutes(t *testing.T) {
netHandle.AddrAdd(link, netlinkAddr)

//Test a simple route setup:
inputRoutesSimple := []*pb.Route{
inputRoutesSimple := []*types.Route{
{Dest: "", Gateway: "192.168.0.1", Source: "", Scope: 0, Device: "ifc-name"},
{Dest: "192.168.0.0/16", Gateway: "", Source: "192.168.0.2", Scope: 253, Device: "ifc-name"},
}
Expand All @@ -161,7 +162,7 @@ func TestUpdateRoutes(t *testing.T) {
"Interface created didn't match: got %+v, expecting %+v", results, testRoutes)

//Test a route setup mimicking what could be provided by PTP CNI plugin:
inputRoutesPTPExample := []*pb.Route{
inputRoutesPTPExample := []*types.Route{
{Dest: "", Gateway: "192.168.0.1", Source: "", Scope: 0, Device: "ifc-name"},
{Dest: "192.168.0.144/16", Gateway: "192.168.0.1", Source: "192.168.0.2", Scope: 0, Device: "ifc-name"},
{Dest: "192.168.0.1/32", Gateway: "", Source: "192.168.0.2", Scope: 254, Device: "ifc-name"},
Expand All @@ -174,7 +175,7 @@ func TestUpdateRoutes(t *testing.T) {
"Interface created didn't match: got %+v, expecting %+v", results, testRoutes)

//Test unreachable example (no scope provided for initial link route)
inputRoutesNoScope := []*pb.Route{
inputRoutesNoScope := []*types.Route{
{Dest: "", Gateway: "192.168.0.1", Source: "", Scope: 0, Device: "ifc-name"},
{Dest: "192.168.0.0/16", Gateway: "", Source: "192.168.0.2", Scope: 0, Device: "ifc-name"},
}
Expand All @@ -193,12 +194,12 @@ func TestListInterfaces(t *testing.T) {
assert := assert.New(t)

s := sandbox{}
ifc := pb.Interface{
ifc := types.Interface{
Name: "enoNumber",
Mtu: 1500,
HwAddr: "02:00:ca:fe:00:48",
}
ip := pb.IPAddress{
ip := types.IPAddress{
Family: 0,
Address: "192.168.0.101",
Mask: "24",
Expand Down Expand Up @@ -257,7 +258,7 @@ func TestListRoutes(t *testing.T) {
netHandle.AddrAdd(link, netlinkAddr)

//Test a simple route setup:
inputRoutesSimple := []*pb.Route{
inputRoutesSimple := []*types.Route{
{Dest: "", Gateway: "192.168.0.1", Source: "", Scope: 0, Device: "ifc-name"},
{Dest: "192.168.0.0/16", Gateway: "", Source: "192.168.0.2", Scope: 253, Device: "ifc-name"},
}
Expand Down
Loading

0 comments on commit a13144b

Please sign in to comment.