Skip to content
This repository was archived by the owner on Apr 3, 2018. It is now read-only.

Commit 082ec3f

Browse files
author
Eric Ernst
committed
kata-agent: add network gRPC support
Introduce initial support for setting up interface and routes when working with Kata agent. Fixes: #596 Signed-off-by: Eric Ernst <[email protected]>
1 parent 8b7fddf commit 082ec3f

File tree

1 file changed

+122
-0
lines changed

1 file changed

+122
-0
lines changed

kata_agent.go

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,83 @@ func (k *kataAgent) exec(pod *Pod, c Container, cmd Cmd) (*Process, error) {
300300
return prepareAndStartShim(pod, k.shim, c.id, req.ExecId, k.state.URL, cmd)
301301
}
302302

303+
func (k *kataAgent) generateInterfacesAndRoutes(networkNS NetworkNamespace) ([]*grpc.Interface, []*grpc.Route, error) {
304+
305+
if networkNS.NetNsPath == "" {
306+
return nil, nil, nil
307+
}
308+
309+
var routes []*grpc.Route
310+
var ifaces []*grpc.Interface
311+
312+
for _, endpoint := range networkNS.Endpoints {
313+
314+
var ipAddresses []*grpc.IPAddress
315+
for _, addr := range endpoint.Properties().Addrs {
316+
// Skip IPv6 because not supported
317+
if addr.IP.To4() == nil {
318+
// Skip IPv6 because not supported
319+
k.Logger().WithFields(logrus.Fields{
320+
"unsupported-address-type": "ipv6",
321+
"address": addr,
322+
}).Warn("unsupported address")
323+
continue
324+
}
325+
// Skip localhost interface
326+
if addr.IP.IsLoopback() {
327+
continue
328+
}
329+
netMask, _ := addr.Mask.Size()
330+
ipAddress := grpc.IPAddress{
331+
Family: grpc.IPFamily_v4,
332+
Address: addr.IP.String(),
333+
Mask: fmt.Sprintf("%d", netMask),
334+
}
335+
ipAddresses = append(ipAddresses, &ipAddress)
336+
}
337+
ifc := grpc.Interface{
338+
IPAddresses: ipAddresses,
339+
Device: endpoint.Name(),
340+
Name: endpoint.Name(),
341+
Mtu: uint64(endpoint.Properties().Iface.MTU),
342+
HwAddr: endpoint.HardwareAddr(),
343+
}
344+
345+
ifaces = append(ifaces, &ifc)
346+
347+
for _, route := range endpoint.Properties().Routes {
348+
var r grpc.Route
349+
350+
if route.Dst != nil {
351+
r.Dest = route.Dst.String()
352+
353+
if route.Dst.IP.To4() == nil {
354+
// Skip IPv6 because not supported
355+
k.Logger().WithFields(logrus.Fields{
356+
"unsupported-route-type": "ipv6",
357+
"destination": r.Dest,
358+
}).Warn("unsupported route")
359+
continue
360+
}
361+
}
362+
363+
if route.Gw != nil {
364+
r.Gateway = route.Gw.String()
365+
}
366+
367+
if route.Src != nil {
368+
r.Source = route.Src.String()
369+
}
370+
371+
r.Device = endpoint.Name()
372+
r.Scope = uint32(route.Scope)
373+
routes = append(routes, &r)
374+
375+
}
376+
}
377+
return ifaces, routes, nil
378+
}
379+
303380
func (k *kataAgent) startPod(pod Pod) error {
304381
if k.proxy == nil {
305382
return errorMissingProxy
@@ -335,6 +412,45 @@ func (k *kataAgent) startPod(pod Pod) error {
335412
hostname = hostname[:maxHostnameLen]
336413
}
337414

415+
//
416+
// Setup network interfaces and routes
417+
//
418+
interfaces, routes, err := k.generateInterfacesAndRoutes(pod.networkNS)
419+
if err != nil {
420+
return err
421+
}
422+
for _, ifc := range interfaces {
423+
// send update interface request
424+
ifcReq := &grpc.UpdateInterfaceRequest{
425+
Interface: ifc,
426+
}
427+
resultingInterface, err := k.sendReq(ifcReq)
428+
if err != nil {
429+
k.Logger().WithFields(logrus.Fields{
430+
"interface-requested": fmt.Sprintf("%+v", ifc),
431+
"resulting-interface": fmt.Sprintf("%+v", resultingInterface),
432+
}).WithError(err).Error("update interface request failed")
433+
return err
434+
}
435+
}
436+
437+
if routes != nil {
438+
routesReq := &grpc.UpdateRoutesRequest{
439+
Routes: &grpc.Routes{
440+
Routes: routes,
441+
},
442+
}
443+
444+
resultingRoutes, err := k.sendReq(routesReq)
445+
if err != nil {
446+
k.Logger().WithFields(logrus.Fields{
447+
"routes-requested": fmt.Sprintf("%+v", routes),
448+
"resulting-routes": fmt.Sprintf("%+v", resultingRoutes),
449+
}).WithError(err).Error("update routes request failed")
450+
return err
451+
}
452+
}
453+
338454
// We mount the shared directory in a predefined location
339455
// in the guest.
340456
// This is where at least some of the host config files
@@ -659,6 +775,12 @@ func (k *kataAgent) sendReq(request interface{}) (interface{}, error) {
659775
case *grpc.SignalProcessRequest:
660776
_, err := k.client.SignalProcess(context.Background(), req)
661777
return nil, err
778+
case *grpc.UpdateRoutesRequest:
779+
_, err := k.client.UpdateRoutes(context.Background(), req)
780+
return nil, err
781+
case *grpc.UpdateInterfaceRequest:
782+
ifc, err := k.client.UpdateInterface(context.Background(), req)
783+
return ifc, err
662784
default:
663785
return nil, fmt.Errorf("Unknown gRPC type %T", req)
664786
}

0 commit comments

Comments
 (0)