Skip to content
This repository was archived by the owner on May 12, 2021. It is now read-only.

Commit dd8f32c

Browse files
author
Julio Montes
authored
Merge pull request #400 from sboeuf/split_pkg
proto: Split reusable structures into their own package
2 parents 03f040f + a13144b commit dd8f32c

File tree

11 files changed

+1664
-1263
lines changed

11 files changed

+1664
-1263
lines changed

grpc.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"time"
2222

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

12131214
a.sandbox.hostname = req.Hostname
12141215
a.sandbox.containers = make(map[string]*container)
1215-
a.sandbox.network.ifaces = make(map[string]*pb.Interface)
1216+
a.sandbox.network.ifaces = make(map[string]*types.Interface)
12161217
a.sandbox.network.dns = req.Dns
12171218
a.sandbox.running = true
12181219
a.sandbox.sandboxPidNs = req.SandboxPidns
@@ -1310,15 +1311,15 @@ func (a *agentGRPC) DestroySandbox(ctx context.Context, req *pb.DestroySandboxRe
13101311
return emptyResp, nil
13111312
}
13121313

1313-
func (a *agentGRPC) AddInterface(ctx context.Context, req *pb.AddInterfaceRequest) (*pb.Interface, error) {
1314+
func (a *agentGRPC) AddInterface(ctx context.Context, req *pb.AddInterfaceRequest) (*types.Interface, error) {
13141315
return a.sandbox.addInterface(nil, req.Interface)
13151316
}
13161317

1317-
func (a *agentGRPC) UpdateInterface(ctx context.Context, req *pb.UpdateInterfaceRequest) (*pb.Interface, error) {
1318+
func (a *agentGRPC) UpdateInterface(ctx context.Context, req *pb.UpdateInterfaceRequest) (*types.Interface, error) {
13181319
return a.sandbox.updateInterface(nil, req.Interface)
13191320
}
13201321

1321-
func (a *agentGRPC) RemoveInterface(ctx context.Context, req *pb.RemoveInterfaceRequest) (*pb.Interface, error) {
1322+
func (a *agentGRPC) RemoveInterface(ctx context.Context, req *pb.RemoveInterfaceRequest) (*types.Interface, error) {
13221323
return a.sandbox.removeInterface(nil, req.Interface)
13231324
}
13241325

hack/update-generated-agent-proto.sh

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,16 @@
33
#
44
# SPDX-License-Identifier: Apache-2.0
55
#
6-
protoc -I=$GOPATH/src -I=$GOPATH/src/github.com/gogo/protobuf/protobuf \
7-
--proto_path=protocols/grpc --gogofast_out=\
6+
7+
protoc \
8+
pkg/types/types.proto --gogofast_out=.
9+
10+
protoc \
11+
-I=$GOPATH/src \
12+
-I=$GOPATH/src/github.com/gogo/protobuf/protobuf \
13+
--proto_path=protocols/grpc \
14+
--gogofast_out=\
15+
Mgithub.com/kata-containers/agent/pkg/types/types.proto=github.com/kata-containers/agent/pkg/types,\
816
Mgoogle/protobuf/any.proto=github.com/gogo/protobuf/types,\
917
Mgoogle/protobuf/duration.proto=github.com/gogo/protobuf/types,\
1018
Mgoogle/protobuf/struct.proto=github.com/gogo/protobuf/types,\

network.go

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515

1616
"golang.org/x/sys/unix"
1717

18+
"github.com/kata-containers/agent/pkg/types"
1819
pb "github.com/kata-containers/agent/protocols/grpc"
1920
"github.com/sirupsen/logrus"
2021
"github.com/vishvananda/netlink"
@@ -34,10 +35,10 @@ var (
3435
// related information.
3536
type network struct {
3637
ifacesLock sync.Mutex
37-
ifaces map[string]*pb.Interface
38+
ifaces map[string]*types.Interface
3839

3940
routesLock sync.Mutex
40-
routes []pb.Route
41+
routes []types.Route
4142

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

85-
func updateLink(netHandle *netlink.Handle, link netlink.Link, iface *pb.Interface) error {
86+
func updateLink(netHandle *netlink.Handle, link netlink.Link, iface *types.Interface) error {
8687
if netHandle == nil {
8788
return errNoHandle
8889
}
@@ -134,7 +135,7 @@ func updateLink(netHandle *netlink.Handle, link netlink.Link, iface *pb.Interfac
134135
return nil
135136
}
136137

137-
func (s *sandbox) addInterface(netHandle *netlink.Handle, iface *pb.Interface) (resultingIfc *pb.Interface, err error) {
138+
func (s *sandbox) addInterface(netHandle *netlink.Handle, iface *types.Interface) (resultingIfc *types.Interface, err error) {
138139
if iface == nil {
139140
return nil, errNoIF
140141
}
@@ -179,7 +180,7 @@ func (s *sandbox) addInterface(netHandle *netlink.Handle, iface *pb.Interface) (
179180

180181
return iface, nil
181182
}
182-
func (s *sandbox) removeInterface(netHandle *netlink.Handle, iface *pb.Interface) (resultingIfc *pb.Interface, err error) {
183+
func (s *sandbox) removeInterface(netHandle *netlink.Handle, iface *types.Interface) (resultingIfc *types.Interface, err error) {
183184
if iface == nil {
184185
return nil, errNoIF
185186
}
@@ -217,10 +218,10 @@ func (s *sandbox) removeInterface(netHandle *netlink.Handle, iface *pb.Interface
217218
return nil, nil
218219
}
219220

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

303304
// getInterface will retrieve interface details from the provided link
304-
func getInterface(netHandle *netlink.Handle, link netlink.Link) (*pb.Interface, error) {
305+
func getInterface(netHandle *netlink.Handle, link netlink.Link) (*types.Interface, error) {
305306
if netHandle == nil {
306307
return nil, errNoHandle
307308
}
@@ -310,7 +311,7 @@ func getInterface(netHandle *netlink.Handle, link netlink.Link) (*pb.Interface,
310311
return nil, errNoLink
311312
}
312313

313-
var ifc pb.Interface
314+
var ifc types.Interface
314315
linkAttrs := link.Attrs()
315316
ifc.Name = linkAttrs.Name
316317
ifc.Mtu = uint64(linkAttrs.MTU)
@@ -323,7 +324,7 @@ func getInterface(netHandle *netlink.Handle, link netlink.Link) (*pb.Interface,
323324
}
324325
for _, addr := range addrs {
325326
netMask, _ := addr.Mask.Size()
326-
m := pb.IPAddress{
327+
m := types.IPAddress{
327328
Address: addr.IP.String(),
328329
Mask: fmt.Sprintf("%d", netMask),
329330
}
@@ -481,7 +482,7 @@ func getCurrentRoutes(netHandle *netlink.Handle) (*pb.Routes, error) {
481482
}
482483

483484
for _, route := range finalRouteList {
484-
var r pb.Route
485+
var r types.Route
485486
if route.Dst != nil {
486487
r.Dest = route.Dst.String()
487488
}
@@ -508,7 +509,7 @@ func getCurrentRoutes(netHandle *netlink.Handle) (*pb.Routes, error) {
508509
return &routes, nil
509510
}
510511

511-
func (s *sandbox) updateRoute(netHandle *netlink.Handle, route *pb.Route, add bool) (err error) {
512+
func (s *sandbox) updateRoute(netHandle *netlink.Handle, route *types.Route, add bool) (err error) {
512513
s.network.routesLock.Lock()
513514
defer s.network.routesLock.Unlock()
514515

network_test.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"runtime"
1313
"testing"
1414

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

2425
s := sandbox{}
2526

26-
ifc := pb.Interface{
27+
ifc := types.Interface{
2728
Name: "enoNumber",
2829
Mtu: 1500,
2930
HwAddr: "02:00:ca:fe:00:48",
3031
}
31-
ip := pb.IPAddress{
32+
ip := types.IPAddress{
3233
Family: 0,
3334
Address: "192.168.0.101",
3435
Mask: "24",
@@ -68,7 +69,7 @@ func TestUpdateRemoveInterface(t *testing.T) {
6869
// Try with a different valid MTU. Make sure we can assign a new set of IP addresses
6970
ifc.Mtu = 500
7071
ifc.IPAddresses[0].Address = "192.168.0.102"
71-
ip2 := pb.IPAddress{
72+
ip2 := types.IPAddress{
7273
Family: 0,
7374
Address: "182.168.0.103",
7475
Mask: "24",
@@ -146,7 +147,7 @@ func TestUpdateRoutes(t *testing.T) {
146147
netHandle.AddrAdd(link, netlinkAddr)
147148

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

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

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

195196
s := sandbox{}
196-
ifc := pb.Interface{
197+
ifc := types.Interface{
197198
Name: "enoNumber",
198199
Mtu: 1500,
199200
HwAddr: "02:00:ca:fe:00:48",
200201
}
201-
ip := pb.IPAddress{
202+
ip := types.IPAddress{
202203
Family: 0,
203204
Address: "192.168.0.101",
204205
Mask: "24",
@@ -257,7 +258,7 @@ func TestListRoutes(t *testing.T) {
257258
netHandle.AddrAdd(link, netlinkAddr)
258259

259260
//Test a simple route setup:
260-
inputRoutesSimple := []*pb.Route{
261+
inputRoutesSimple := []*types.Route{
261262
{Dest: "", Gateway: "192.168.0.1", Source: "", Scope: 0, Device: "ifc-name"},
262263
{Dest: "192.168.0.0/16", Gateway: "", Source: "192.168.0.2", Scope: 253, Device: "ifc-name"},
263264
}

0 commit comments

Comments
 (0)