@@ -2,20 +2,27 @@ package routing
22
33import (
44 "errors"
5+ "fmt"
56
67 "github.com/cloudnativelabs/kube-router/pkg/utils"
78 "github.com/osrg/gobgp/config"
89 "github.com/osrg/gobgp/table"
10+ v1core "k8s.io/api/core/v1"
911)
1012
11- // Each node advertises its pod CIDR to the nodes with same ASN (iBGP peers) and to the global BGP peer
12- // or per node BGP peer. Each node ends up advertising not only pod CIDR assigned to the self but other
13- // learned routes to the node pod CIDR's as well to global BGP peer or per node BGP peers. external BGP
14- // peer will randomly (since all path have equal selection attributes) select the routes from multiple
15- // routes to a pod CIDR which will result in extra hop. To prevent this behaviour this methods add
16- // defult export policy to reject everything and an explicit policy is added so that each node only
17- // advertised the pod CIDR assigned to it. Additionally export policy is added so that each node
18- // advertises cluster IP's ONLY to the external BGP peers (and not to iBGP peers).
13+ // BGP export policies are added so that following conditions are met
14+ //
15+ // - by default export of all routes from the RIB to the neighbour's is denied, and explicity statements are added i
16+ // to permit the desired routes to be exported
17+ // - each node is allowed to advertise its assigned pod CIDR's to all of its iBGP peer neighbours with same ASN
18+ // - each node is allowed to advertise its assigned pod CIDR's to all of its external BGP peer neighbours
19+ // only if --advertise-pod-cidr flag is set to true
20+ // - each node is NOT allowed to advertise its assigned pod CIDR's to all of its external BGP peer neighbours
21+ // only if --advertise-pod-cidr flag is set to false
22+ // - each node is allowed to advertise service VIP's (cluster ip, load balancer ip, external IP) ONLY to external
23+ // BGP peers
24+ // - each node is NOT allowed to advertise service VIP's (cluster ip, load balancer ip, external IP) to
25+ // iBGP peers
1926func (nrc * NetworkRoutingController ) addExportPolicies () error {
2027
2128 // we are rr server do not add export policies
@@ -59,13 +66,35 @@ func (nrc *NetworkRoutingController) addExportPolicies() error {
5966
6067 statements := make ([]config.Statement , 0 )
6168
69+ // Get the current list of the nodes from the local cache
70+ nodes := nrc .nodeLister .List ()
71+ iBGPPeers := make ([]string , 0 )
72+ for _ , node := range nodes {
73+ nodeObj := node .(* v1core.Node )
74+ nodeIP , err := utils .GetNodeIP (nodeObj )
75+ if err != nil {
76+ return fmt .Errorf ("Failed to find a node IP: %s" , err )
77+ }
78+ iBGPPeers = append (iBGPPeers , nodeIP .String ())
79+ }
80+ iBGPPeerNS , _ := table .NewNeighborSet (config.NeighborSet {
81+ NeighborSetName : "iBGPpeerset" ,
82+ NeighborInfoList : iBGPPeers ,
83+ })
84+ err = nrc .bgpServer .ReplaceDefinedSet (iBGPPeerNS )
85+ if err != nil {
86+ nrc .bgpServer .AddDefinedSet (iBGPPeerNS )
87+ }
6288 // statement to represent the export policy to permit advertising node's pod CIDR
6389 statements = append (statements ,
6490 config.Statement {
6591 Conditions : config.Conditions {
6692 MatchPrefixSet : config.MatchPrefixSet {
6793 PrefixSet : "podcidrprefixset" ,
6894 },
95+ MatchNeighborSet : config.MatchNeighborSet {
96+ NeighborSet : "iBGPpeerset" ,
97+ },
6998 },
7099 Actions : config.Actions {
71100 RouteDisposition : config .ROUTE_DISPOSITION_ACCEPT_ROUTE ,
@@ -107,6 +136,21 @@ func (nrc *NetworkRoutingController) addExportPolicies() error {
107136 RouteDisposition : config .ROUTE_DISPOSITION_ACCEPT_ROUTE ,
108137 },
109138 })
139+ if nrc .advertisePodCidr {
140+ statements = append (statements , config.Statement {
141+ Conditions : config.Conditions {
142+ MatchPrefixSet : config.MatchPrefixSet {
143+ PrefixSet : "podcidrprefixset" ,
144+ },
145+ MatchNeighborSet : config.MatchNeighborSet {
146+ NeighborSet : "externalpeerset" ,
147+ },
148+ },
149+ Actions : config.Actions {
150+ RouteDisposition : config .ROUTE_DISPOSITION_ACCEPT_ROUTE ,
151+ },
152+ })
153+ }
110154 }
111155
112156 definition := config.PolicyDefinition {
0 commit comments