Skip to content

Commit 082efdd

Browse files
authored
Merge pull request #72 from cloudnativelabs/bgp-export-policies
add a BGP export policy on each node so that, learned routes from iBGP peers are never advertised to global peer
2 parents e8ce4a9 + e3ea82a commit 082efdd

File tree

1 file changed

+69
-3
lines changed

1 file changed

+69
-3
lines changed

app/controllers/network_routes_controller.go

Lines changed: 69 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ package controllers
33
import (
44
"errors"
55
"fmt"
6-
"net/url"
76
"net"
7+
"net/url"
88
"strconv"
99
"strings"
1010
"sync"
@@ -50,6 +50,10 @@ var (
5050
activeNodes = make(map[string]bool)
5151
)
5252

53+
const (
54+
clustetNieghboursSet = "clusterneighboursset"
55+
)
56+
5357
func (nrc *NetworkRoutingController) Run(stopCh <-chan struct{}, wg *sync.WaitGroup) {
5458

5559
cidr, err := utils.GetPodCidrFromCniSpec("/etc/cni/net.d/10-kuberouter.conf")
@@ -110,6 +114,11 @@ func (nrc *NetworkRoutingController) Run(stopCh <-chan struct{}, wg *sync.WaitGr
110114
}
111115
}
112116

117+
err = nrc.initExportPolicies()
118+
if err != nil {
119+
glog.Errorf("Failed to add BGP export policies %s.", err.Error())
120+
}
121+
113122
// loop forever till notified to stop on stopCh
114123
for {
115124
select {
@@ -188,7 +197,6 @@ func (nrc *NetworkRoutingController) advertiseRoute() error {
188197
attrs := []bgp.PathAttributeInterface{
189198
bgp.NewPathAttributeOrigin(0),
190199
bgp.NewPathAttributeNextHop(nrc.nodeIP.String()),
191-
bgp.NewPathAttributeAsPath([]bgp.AsPathParamInterface{bgp.NewAs4PathParam(bgp.BGP_ASPATH_ATTR_TYPE_SEQ, []uint32{4000, 400000, 300000, 40001})}),
192200
}
193201
glog.Infof("Advertising route: '%s/%s via %s' to peers", subnet, strconv.Itoa(cidrLen), nrc.nodeIP.String())
194202
if _, err := nrc.bgpServer.AddPath("", []*table.Path{table.NewPath(nil, bgp.NewIPAddrPrefix(uint8(cidrLen),
@@ -203,7 +211,6 @@ func (nrc *NetworkRoutingController) AdvertiseClusterIp(clusterIp string) error
203211
attrs := []bgp.PathAttributeInterface{
204212
bgp.NewPathAttributeOrigin(0),
205213
bgp.NewPathAttributeNextHop(nrc.nodeIP.String()),
206-
bgp.NewPathAttributeAsPath([]bgp.AsPathParamInterface{bgp.NewAs4PathParam(bgp.BGP_ASPATH_ATTR_TYPE_SEQ, []uint32{4000, 400000, 300000, 40001})}),
207214
}
208215
glog.Infof("Advertising route: '%s/%s via %s' to peers", clusterIp, strconv.Itoa(32), nrc.nodeIP.String())
209216
if _, err := nrc.bgpServer.AddPath("", []*table.Path{table.NewPath(nil, bgp.NewIPAddrPrefix(uint8(32),
@@ -404,6 +411,65 @@ func (nrc *NetworkRoutingController) OnNodeUpdate(nodeUpdate *watchers.NodeUpdat
404411
}
405412
}
406413

414+
// add BGP export policy so that no learned route from the neightbour
415+
// is exported or advertised to global or per node peer
416+
func (nrc *NetworkRoutingController) initExportPolicies() error {
417+
418+
nodes, err := nrc.clientset.Core().Nodes().List(metav1.ListOptions{})
419+
if err != nil {
420+
return err
421+
}
422+
423+
nieghbors := make([]string, 0)
424+
for _, node := range nodes.Items {
425+
nodeIP, _ := getNodeIP(&node)
426+
if nodeIP.String() == nrc.nodeIP.String() {
427+
continue
428+
}
429+
nieghbors = append(nieghbors, nodeIP.String())
430+
}
431+
432+
ns, err := table.NewNeighborSet(config.NeighborSet{
433+
NeighborSetName: clustetNieghboursSet,
434+
NeighborInfoList: nieghbors,
435+
})
436+
if err != nil {
437+
return err
438+
}
439+
440+
err = nrc.bgpServer.AddDefinedSet(ns)
441+
if err != nil {
442+
return err
443+
}
444+
445+
definition := config.PolicyDefinition{
446+
Name: "kube_router",
447+
Statements: []config.Statement{
448+
config.Statement{
449+
Conditions: config.Conditions{
450+
MatchNeighborSet: config.MatchNeighborSet{
451+
NeighborSet: clustetNieghboursSet,
452+
},
453+
},
454+
Actions: config.Actions{
455+
RouteDisposition: config.ROUTE_DISPOSITION_REJECT_ROUTE,
456+
},
457+
},
458+
},
459+
}
460+
461+
policy, err := table.NewPolicy(definition)
462+
if err != nil {
463+
return err
464+
}
465+
if err = nrc.bgpServer.AddPolicy(policy, false); err != nil {
466+
return err
467+
}
468+
return nrc.bgpServer.AddPolicyAssignment("", table.POLICY_DIRECTION_EXPORT,
469+
[]*config.PolicyDefinition{&definition},
470+
table.ROUTE_TYPE_ACCEPT)
471+
}
472+
407473
func (nrc *NetworkRoutingController) startBgpServer() error {
408474

409475
var nodeAsnNumber uint32

0 commit comments

Comments
 (0)