Skip to content

Commit 4ca0afa

Browse files
authored
Support for advertising service external IP to be configured BGP peers (#203)
introduces new flag `--advertise-external-ip` Fixes #161
1 parent ba7697b commit 4ca0afa

File tree

3 files changed

+44
-3
lines changed

3 files changed

+44
-3
lines changed

Documentation/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,8 @@ Also you can choose to run kube-router as agent running on each cluster node. Al
9797

9898
```
9999
Usage of ./kube-router:
100-
--advertise-cluster-ip Add Cluster IP to the RIB and advertise to peers.
100+
--advertise-cluster-ip Add Cluster IP of the service to the RIB so that it gets advertises to the BGP peers.
101+
--advertise-external-ip Add External IP of service to the RIB so that it gets advertised to the BGP peers.
101102
--cleanup-config Cleanup iptables rules, ipvs, ipset configuration and exit.
102103
--cluster-asn uint ASN number under which cluster nodes will run iBGP.
103104
--cluster-cidr string CIDR range of pods in the cluster. It is used to identify traffic originating from and destinated to pods.

app/controllers/network_routes_controller.go

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ type NetworkRoutingController struct {
4747
enablePodEgress bool
4848
hostnameOverride string
4949
advertiseClusterIp bool
50+
advertiseExternalIp bool
5051
defaultNodeAsnNumber uint32
5152
nodeAsnNumber uint32
5253
globalPeerRouters []*config.NeighborConfig
@@ -207,7 +208,7 @@ func (nrc *NetworkRoutingController) Run(stopCh <-chan struct{}, wg *sync.WaitGr
207208

208209
// advertise cluster IP for the service to be reachable via host
209210
if nrc.advertiseClusterIp {
210-
glog.Infof("Advertising cluster ips")
211+
glog.Infof("Advertising cluster ips of services to the external BGP peers")
211212
for _, svc := range watchers.ServiceWatcher.List() {
212213
if svc.Spec.Type == "ClusterIP" || svc.Spec.Type == "NodePort" || svc.Spec.Type == "LoadBalancer" {
213214

@@ -222,6 +223,22 @@ func (nrc *NetworkRoutingController) Run(stopCh <-chan struct{}, wg *sync.WaitGr
222223
}
223224
}
224225

226+
// advertise cluster IP for the service to be reachable via host
227+
if nrc.advertiseExternalIp {
228+
glog.Infof("Advertising external ips of the services to the external BGP peers")
229+
for _, svc := range watchers.ServiceWatcher.List() {
230+
if svc.Spec.Type == "ClusterIP" || svc.Spec.Type == "NodePort" {
231+
// skip headless services
232+
if svc.Spec.ClusterIP == "None" || svc.Spec.ClusterIP == "" {
233+
continue
234+
}
235+
for _, externalIP := range svc.Spec.ExternalIPs {
236+
nrc.AdvertiseClusterIp(externalIP)
237+
}
238+
}
239+
}
240+
}
241+
225242
glog.Infof("Performing periodic syn of the routes")
226243
err = nrc.advertiseRoute()
227244
if err != nil {
@@ -370,6 +387,21 @@ func (nrc *NetworkRoutingController) getClusterIps() ([]string, error) {
370387
return clusterIpList, nil
371388
}
372389

390+
func (nrc *NetworkRoutingController) getExternalIps() ([]string, error) {
391+
externalIpList := make([]string, 0)
392+
for _, svc := range watchers.ServiceWatcher.List() {
393+
if svc.Spec.Type == "ClusterIP" || svc.Spec.Type == "NodePort" {
394+
395+
// skip headless services
396+
if svc.Spec.ClusterIP == "None" || svc.Spec.ClusterIP == "" {
397+
continue
398+
}
399+
externalIpList = append(externalIpList, svc.Spec.ExternalIPs...)
400+
}
401+
}
402+
return externalIpList, nil
403+
}
404+
373405
// Used for processing Annotations that may contain multiple items
374406
// Pass this the string and the delimiter
375407
func stringToSlice(s, d string) []string {
@@ -525,6 +557,10 @@ func (nrc *NetworkRoutingController) addExportPolicies() error {
525557
for _, ip := range clusterIps {
526558
clusterIpPrefixList = append(clusterIpPrefixList, config.Prefix{IpPrefix: ip + "/32"})
527559
}
560+
externalIps, _ := nrc.getExternalIps()
561+
for _, ip := range externalIps {
562+
clusterIpPrefixList = append(clusterIpPrefixList, config.Prefix{IpPrefix: ip + "/32"})
563+
}
528564
clusterIpPrefixSet, err := table.NewPrefixSet(config.PrefixSet{
529565
PrefixSetName: "clusteripprefixset",
530566
PrefixList: clusterIpPrefixList,
@@ -1307,6 +1343,7 @@ func NewNetworkRoutingController(clientset *kubernetes.Clientset,
13071343
}
13081344

13091345
nrc.advertiseClusterIp = kubeRouterConfig.AdvertiseClusterIp
1346+
nrc.advertiseExternalIp = kubeRouterConfig.AdvertiseExternalIp
13101347

13111348
nrc.enableOverlays = kubeRouterConfig.EnableOverlay
13121349

app/options/options.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ type KubeRouterConfig struct {
2424
EnablePodEgress bool
2525
HostnameOverride string
2626
AdvertiseClusterIp bool
27+
AdvertiseExternalIp bool
2728
PeerRouters []net.IP
2829
PeerASNs []uint
2930
ClusterAsn uint
@@ -74,7 +75,9 @@ func (s *KubeRouterConfig) AddFlags(fs *pflag.FlagSet) {
7475
fs.DurationVar(&s.RoutesSyncPeriod, "routes-sync-period", s.RoutesSyncPeriod,
7576
"The delay between route updates and advertisements (e.g. '5s', '1m', '2h22m'). Must be greater than 0.")
7677
fs.BoolVar(&s.AdvertiseClusterIp, "advertise-cluster-ip", false,
77-
"Add Cluster IP to the RIB and advertise to peers.")
78+
"Add Cluster IP of the service to the RIB so that it gets advertises to the BGP peers.")
79+
fs.BoolVar(&s.AdvertiseExternalIp, "advertise-external-ip", false,
80+
"Add External IP of service to the RIB so that it gets advertised to the BGP peers.")
7881
fs.IPSliceVar(&s.PeerRouters, "peer-router-ips", s.PeerRouters,
7982
"The ip address of the external router to which all nodes will peer and advertise the cluster ip and pod cidr's.")
8083
fs.UintVar(&s.ClusterAsn, "cluster-asn", s.ClusterAsn,

0 commit comments

Comments
 (0)