@@ -300,6 +300,83 @@ func (k *kataAgent) exec(pod *Pod, c Container, cmd Cmd) (*Process, error) {
300
300
return prepareAndStartShim (pod , k .shim , c .id , req .ExecId , k .state .URL , cmd )
301
301
}
302
302
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
+
303
380
func (k * kataAgent ) startPod (pod Pod ) error {
304
381
if k .proxy == nil {
305
382
return errorMissingProxy
@@ -335,6 +412,45 @@ func (k *kataAgent) startPod(pod Pod) error {
335
412
hostname = hostname [:maxHostnameLen ]
336
413
}
337
414
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
+
338
454
// We mount the shared directory in a predefined location
339
455
// in the guest.
340
456
// This is where at least some of the host config files
@@ -659,6 +775,12 @@ func (k *kataAgent) sendReq(request interface{}) (interface{}, error) {
659
775
case * grpc.SignalProcessRequest :
660
776
_ , err := k .client .SignalProcess (context .Background (), req )
661
777
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
662
784
default :
663
785
return nil , fmt .Errorf ("Unknown gRPC type %T" , req )
664
786
}
0 commit comments