Skip to content

Commit da9215a

Browse files
author
jojimt
committed
Add default gateway into pod
1 parent 5c3308b commit da9215a

File tree

1 file changed

+40
-9
lines changed

1 file changed

+40
-9
lines changed

mgmtfn/k8splugin/driver.go

+40-9
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ type epSpec struct {
4848
type epAttr struct {
4949
IPAddress string
5050
PortName string
51+
Gateway string
5152
}
5253

5354
// netdGetEndpoint is a utility that reads the EP oper state
@@ -168,6 +169,7 @@ func createEP(req *epSpec) (*epAttr, error) {
168169
epResponse := epAttr{}
169170
epResponse.PortName = ep.PortName
170171
epResponse.IPAddress = ep.IPAddress + "/" + strconv.Itoa(int(nw.SubnetLen))
172+
epResponse.Gateway = nw.Gateway
171173

172174
return &epResponse, nil
173175
}
@@ -206,13 +208,7 @@ func nsToPID(ns string) (int, error) {
206208
}
207209

208210
// 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 {
216212

217213
nsenterPath, err := osexec.LookPath("nsenter")
218214
if err != nil {
@@ -269,11 +265,33 @@ func setIfAttrs(ifname, netns, cidr, newname string) error {
269265
cidr, newname, err)
270266
return nil
271267
}
272-
log.Infof("Output from ip assign: %v", bringUp)
268+
log.Debugf("Output from ip assign: %v", bringUp)
273269
return nil
274270

275271
}
276272

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+
277295
// getEPSpec gets the EP spec using the pod attributes
278296
func getEPSpec(pInfo *cniapi.CNIPodAttr) (*epSpec, error) {
279297
resp := epSpec{}
@@ -331,13 +349,26 @@ func addPod(r *http.Request) (interface{}, error) {
331349
return resp, err
332350
}
333351

352+
// convert netns to pid that netlink needs
353+
pid, err := nsToPID(pInfo.NwNameSpace)
354+
if err != nil {
355+
return resp, err
356+
}
357+
334358
// 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)
336360
if err != nil {
337361
log.Errorf("Error setting interface attributes. Err: %v", err)
338362
return resp, err
339363
}
340364

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+
341372
resp.IPAddress = ep.IPAddress
342373
resp.EndpointID = pInfo.InfraContainerID
343374
return resp, nil

0 commit comments

Comments
 (0)