Skip to content

Commit b180094

Browse files
committed
set priority for custom routing tables so that, FWMARK packets are delivered locally
and for the rest of the traffic policy routing sets endpoints (dev kube-bridge) as next hop for external IP's
1 parent 5f425eb commit b180094

File tree

1 file changed

+28
-7
lines changed

1 file changed

+28
-7
lines changed

app/controllers/network_services_controller.go

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -253,14 +253,15 @@ func (nsc *NetworkServicesController) syncIpvsServices(serviceInfoMap serviceInf
253253
if err != nil {
254254
return errors.New("Failed setup PBR for DSR due to: " + err.Error())
255255
}
256-
glog.Infof("Custom routing table " + customDSRRouteTableName + "required for Direct Server Return is setup as expected.")
256+
glog.Infof("Custom routing table " + customDSRRouteTableName + " required for Direct Server Return is setup as expected.")
257257

258258
glog.Infof("Setting up custom route table required to add routes for external IP's.")
259259
err = setupRoutesForExternalIPForDSR(serviceInfoMap)
260260
if err != nil {
261+
glog.Errorf("Failed setup custom routing table required to add routes for external IP's due to: " + err.Error())
261262
return errors.New("Failed setup custom routing table required to add routes for external IP's due to: " + err.Error())
262263
}
263-
glog.Infof("Custom routing table " + externalIPRouteTableName + "required for Direct Server Return is setup as expected.")
264+
glog.Infof("Custom routing table " + externalIPRouteTableName + " required for Direct Server Return is setup as expected.")
264265

265266
// map of active services and service endpoints
266267
activeServiceEndpointMap := make(map[string][]string)
@@ -1255,7 +1256,7 @@ func routeVIPTrafficToDirector(fwmark string) error {
12551256
return errors.New("Failed to verify if `ip rule` exists due to: " + err.Error())
12561257
}
12571258
if !strings.Contains(string(out), fwmark) {
1258-
err = exec.Command("ip", "rule", "add", "fwmark", fwmark, "table", customDSRRouteTableID).Run()
1259+
err = exec.Command("ip", "rule", "add", "prio", "32764", "fwmark", fwmark, "table", customDSRRouteTableID).Run()
12591260
if err != nil {
12601261
return errors.New("Failed to add policy rule to lookup traffic to VIP through the custom " +
12611262
" routing table due to " + err.Error())
@@ -1272,12 +1273,13 @@ func setupPolicyRoutingForDSR() error {
12721273
if err != nil {
12731274
return errors.New("Failed to setup policy routing required for DSR due to " + err.Error())
12741275
}
1276+
12751277
if !strings.Contains(string(b), customDSRRouteTableName) {
12761278
f, err := os.OpenFile("/etc/iproute2/rt_tables", os.O_APPEND|os.O_WRONLY, 0600)
12771279
if err != nil {
12781280
return errors.New("Failed to setup policy routing required for DSR due to " + err.Error())
12791281
}
1280-
if _, err = f.WriteString(customDSRRouteTableID + " " + customDSRRouteTableName); err != nil {
1282+
if _, err = f.WriteString(customDSRRouteTableID + " " + customDSRRouteTableName + "\n"); err != nil {
12811283
return errors.New("Failed to setup policy routing required for DSR due to " + err.Error())
12821284
}
12831285
}
@@ -1305,21 +1307,40 @@ func setupRoutesForExternalIPForDSR(serviceInfoMap serviceInfoMap) error {
13051307
if err != nil {
13061308
return errors.New("Failed to setup external ip routing table required for DSR due to " + err.Error())
13071309
}
1310+
13081311
if !strings.Contains(string(b), externalIPRouteTableName) {
13091312
f, err := os.OpenFile("/etc/iproute2/rt_tables", os.O_APPEND|os.O_WRONLY, 0600)
13101313
if err != nil {
1311-
return errors.New("Failed to setup external ip routing table required for DSR due to " + err.Error())
1314+
return errors.New("Failed setup external ip routing table required for DSR due to " + err.Error())
13121315
}
1313-
if _, err = f.WriteString(externalIPRouteTableId + " " + externalIPRouteTableName); err != nil {
1314-
return errors.New("Failed to setup external ip routing table required for DSR due to " + err.Error())
1316+
if _, err = f.WriteString(externalIPRouteTableId + " " + externalIPRouteTableName + "\n"); err != nil {
1317+
return errors.New("Failed setup external ip routing table required for DSR due to " + err.Error())
13151318
}
13161319
}
1320+
13171321
out, err := exec.Command("ip", "route", "list", "table", externalIPRouteTableId).Output()
13181322
if err != nil {
13191323
return errors.New("Failed to verify required routing table for external IP's exists. " +
13201324
"Failed to setup policy routing required for DSR due to " + err.Error())
13211325
}
13221326

1327+
out, err = exec.Command("ip", "rule", "list").Output()
1328+
if err != nil {
1329+
return errors.New("Failed to verify if `ip rule add prio 32765 from all lookup external_ip` exists due to: " + err.Error())
1330+
}
1331+
1332+
if ! (strings.Contains(string(out), externalIPRouteTableName) || strings.Contains(string(out), externalIPRouteTableId)) {
1333+
err = exec.Command("ip", "rule", "add", "prio", "32765", "from", "all", "lookup", externalIPRouteTableId).Run()
1334+
if err != nil {
1335+
glog.Infof("Failed to add policy rule `ip rule add prio 32765 from all lookup external_ip` due to " + err.Error())
1336+
return errors.New("Failed to add policy rule `ip rule add prio 32765 from all lookup external_ip` due to " + err.Error())
1337+
}
1338+
}
1339+
1340+
out, err = exec.Command("ip", "route", "list", "table", externalIPRouteTableId).Output()
1341+
if err != nil {
1342+
return errors.New("Failed to get routes in external_ip table due to: " + err.Error())
1343+
}
13231344
for _, svc := range serviceInfoMap {
13241345
for _, externalIP := range svc.externalIPs {
13251346
if !strings.Contains(string(out), externalIP) {

0 commit comments

Comments
 (0)