@@ -48,6 +48,7 @@ type epSpec struct {
48
48
type epAttr struct {
49
49
IPAddress string
50
50
PortName string
51
+ Gateway string
51
52
}
52
53
53
54
// netdGetEndpoint is a utility that reads the EP oper state
@@ -168,6 +169,7 @@ func createEP(req *epSpec) (*epAttr, error) {
168
169
epResponse := epAttr {}
169
170
epResponse .PortName = ep .PortName
170
171
epResponse .IPAddress = ep .IPAddress + "/" + strconv .Itoa (int (nw .SubnetLen ))
172
+ epResponse .Gateway = nw .Gateway
171
173
172
174
return & epResponse , nil
173
175
}
@@ -206,13 +208,7 @@ func nsToPID(ns string) (int, error) {
206
208
}
207
209
208
210
// setIfAttrs sets the required attributes for the container interface
209
- func setIfAttrs (ifname , netns , cidr , newname string ) error {
210
-
211
- // convert netns to pid that netlink needs
212
- pid , err := nsToPID (netns )
213
- if err != nil {
214
- return err
215
- }
211
+ func setIfAttrs (pid int , ifname , cidr , newname string ) error {
216
212
217
213
nsenterPath , err := osexec .LookPath ("nsenter" )
218
214
if err != nil {
@@ -269,11 +265,33 @@ func setIfAttrs(ifname, netns, cidr, newname string) error {
269
265
cidr , newname , err )
270
266
return nil
271
267
}
272
- log .Infof ("Output from ip assign: %v" , bringUp )
268
+ log .Debugf ("Output from ip assign: %v" , bringUp )
273
269
return nil
274
270
275
271
}
276
272
273
+ // setDefGw sets the default gateway for the container namespace
274
+ func setDefGw (pid int , gw , intfName string ) error {
275
+ nsenterPath , err := osexec .LookPath ("nsenter" )
276
+ if err != nil {
277
+ return err
278
+ }
279
+ routePath , err := osexec .LookPath ("route" )
280
+ if err != nil {
281
+ return err
282
+ }
283
+ // set default gw
284
+ nsPid := fmt .Sprintf ("%d" , pid )
285
+ _ , err = osexec .Command (nsenterPath , "-t" , nsPid , "-n" , "-F" , "--" , routePath , "add" ,
286
+ "default" , "gw" , gw , intfName ).CombinedOutput ()
287
+ if err != nil {
288
+ log .Errorf ("unable to set default gw %s. Error: %s" ,
289
+ gw , err )
290
+ return nil
291
+ }
292
+ return nil
293
+ }
294
+
277
295
// getEPSpec gets the EP spec using the pod attributes
278
296
func getEPSpec (pInfo * cniapi.CNIPodAttr ) (* epSpec , error ) {
279
297
resp := epSpec {}
@@ -331,13 +349,26 @@ func addPod(r *http.Request) (interface{}, error) {
331
349
return resp , err
332
350
}
333
351
352
+ // convert netns to pid that netlink needs
353
+ pid , err := nsToPID (pInfo .NwNameSpace )
354
+ if err != nil {
355
+ return resp , err
356
+ }
357
+
334
358
// Set interface attributes for the new port
335
- err = setIfAttrs (ep . PortName , pInfo . NwNameSpace , ep .IPAddress , pInfo .IntfName )
359
+ err = setIfAttrs (pid , ep . PortName , ep .IPAddress , pInfo .IntfName )
336
360
if err != nil {
337
361
log .Errorf ("Error setting interface attributes. Err: %v" , err )
338
362
return resp , err
339
363
}
340
364
365
+ // Set default gateway
366
+ err = setDefGw (pid , ep .Gateway , pInfo .IntfName )
367
+ if err != nil {
368
+ log .Errorf ("Error setting default gateway. Err: %v" , err )
369
+ return resp , err
370
+ }
371
+
341
372
resp .IPAddress = ep .IPAddress
342
373
resp .EndpointID = pInfo .InfraContainerID
343
374
return resp , nil
0 commit comments