Skip to content

Commit 041c055

Browse files
authored
minimize sync()'s done by the controllers (#399)
* minimize sync() done by the controllers * deprecate --config-sync-period * review comments
1 parent 71d16bf commit 041c055

File tree

7 files changed

+40
-28
lines changed

7 files changed

+40
-28
lines changed

cmd/kube-router/kube-router_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,6 @@ func TestMainHelp(t *testing.T) {
4545

4646
if !bytes.Contains(docBuf.Bytes(), exp) {
4747
t.Errorf("docs/README.md 'command line options' section does not match `kube-router --help`.\nExpected:\n%s", exp)
48+
t.Errorf("\nGot:\n%s", docBuf.Bytes())
4849
}
4950
}

docs/README.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,6 @@ Usage of kube-router:
107107
--cleanup-config Cleanup iptables rules, ipvs, ipset configuration and exit.
108108
--cluster-asn uint ASN number under which cluster nodes will run iBGP.
109109
--cluster-cidr string CIDR range of pods in the cluster. It is used to identify traffic originating from and destinated to pods.
110-
--config-sync-period duration The delay between apiserver configuration synchronizations (e.g. '5s', '1m'). Must be greater than 0. (default 1m0s)
111110
--enable-ibgp Enables peering with nodes with the same ASN, if disabled will only peer with external BGP peers (default true)
112111
--enable-overlay When enable-overlay set to true, IP-in-IP tunneling is used for pod-to-pod networking across nodes in different subnets. When set to false no tunneling is used and routing infrastrcture is expected to route traffic for pod-to-pod networking across nodes in different subnets (default true)
113112
--enable-pod-egress SNAT traffic from Pods to destinations outside the cluster. (default true)
@@ -116,8 +115,8 @@ Usage of kube-router:
116115
--health-port uint16 Health check port, 0 = Disabled (default 20244)
117116
-h, --help Print usage information.
118117
--hostname-override string Overrides the NodeName of the node. Set this if kube-router is unable to determine your NodeName automatically.
119-
--iptables-sync-period duration The delay between iptables rule synchronizations (e.g. '5s', '1m'). Must be greater than 0. (default 1m0s)
120-
--ipvs-sync-period duration The delay between ipvs config synchronizations (e.g. '5s', '1m', '2h22m'). Must be greater than 0. (default 1m0s)
118+
--iptables-sync-period duration The delay between iptables rule synchronizations (e.g. '5s', '1m'). Must be greater than 0. (default 5m0s)
119+
--ipvs-sync-period duration The delay between ipvs config synchronizations (e.g. '5s', '1m', '2h22m'). Must be greater than 0. (default 5m0s)
121120
--kubeconfig string Path to kubeconfig file with authorization information (the master location is set by the master flag).
122121
--masquerade-all SNAT all traffic to cluster IP/node port.
123122
--master string The address of the Kubernetes API server (overrides any value in kubeconfig).
@@ -129,7 +128,7 @@ Usage of kube-router:
129128
--peer-router-ips ipSlice The ip address of the external router to which all nodes will peer and advertise the cluster ip and pod cidr's. (default [])
130129
--peer-router-multihop-ttl uint8 Enable eBGP multihop supports -- sets multihop-ttl. (Relevant only if ttl >= 2)
131130
--peer-router-passwords strings Password for authenticating against the BGP peer defined with "--peer-router-ips".
132-
--routes-sync-period duration The delay between route updates and advertisements (e.g. '5s', '1m', '2h22m'). Must be greater than 0. (default 1m0s)
131+
--routes-sync-period duration The delay between route updates and advertisements (e.g. '5s', '1m', '2h22m'). Must be greater than 0. (default 5m0s)
133132
--run-firewall Enables Network Policy -- sets up iptables to provide ingress firewall for pods. (default true)
134133
--run-router Enables Pod Networking -- Advertises and learns the routes to Pods via iBGP. (default true)
135134
--run-service-proxy Enables Service Proxy -- sets up IPVS for Kubernetes Services. (default true)

pkg/cmd/kube-router.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ func (kr *KubeRouter) Run() error {
106106
kr.Config.MetricsEnabled = false
107107
}
108108

109-
informerFactory := informers.NewSharedInformerFactory(kr.Client, kr.Config.ConfigSyncPeriod)
109+
informerFactory := informers.NewSharedInformerFactory(kr.Client, 0)
110110

111111
svcInformer := informerFactory.Core().V1().Services().Informer()
112112
epInformer := informerFactory.Core().V1().Endpoints().Informer()
@@ -115,7 +115,7 @@ func (kr *KubeRouter) Run() error {
115115
nsInformer := informerFactory.Core().V1().Namespaces().Informer()
116116
npInformer := informerFactory.Networking().V1().NetworkPolicies().Informer()
117117

118-
go informerFactory.Start(stopCh)
118+
informerFactory.Start(stopCh)
119119
informerFactory.WaitForCacheSync(stopCh)
120120

121121
if kr.Config.RunFirewall {

pkg/controllers/network_policy_controller.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -150,30 +150,31 @@ func (npc *NetworkPolicyController) Run(healthChan chan<- *ControllerHeartbeat,
150150
// OnPodUpdate handles updates to pods from the Kubernetes api server
151151
func (npc *NetworkPolicyController) OnPodUpdate(obj interface{}) {
152152
pod := obj.(*api.Pod)
153-
glog.V(2).Infof("Received pod update namespace:%s pod name:%s", pod.Namespace, pod.Name)
153+
glog.V(2).Infof("Received update to pod: %s/%s", pod.Namespace, pod.Name)
154154

155155
err := npc.Sync()
156156
if err != nil {
157-
glog.Errorf("Error syncing on pod update: %s", err)
157+
glog.Errorf("Error syncing network policy for the update to pod: %s/%s Error: %s", pod.Namespace, pod.Name, err)
158158
}
159159
}
160160

161161
// OnNetworkPolicyUpdate handles updates to network policy from the kubernetes api server
162162
func (npc *NetworkPolicyController) OnNetworkPolicyUpdate(obj interface{}) {
163+
netpol := obj.(*networking.NetworkPolicy)
164+
glog.V(2).Infof("Received update for network policy: %s/%s", netpol.Namespace, netpol.Name)
163165
err := npc.Sync()
164166
if err != nil {
165-
glog.Errorf("Error syncing on network policy update: %s", err)
167+
glog.Errorf("Error syncing network policy for the update to network policy: %s/%s Error: %s", netpol.Namespace, netpol.Name, err)
166168
}
167169
}
168170

169171
// OnNamespaceUpdate handles updates to namespace from kubernetes api server
170172
func (npc *NetworkPolicyController) OnNamespaceUpdate(obj interface{}) {
173+
namespace := obj.(*api.Namespace)
171174
// namespace (and annotations on it) has no significance in GA ver of network policy
172175
if npc.v1NetworkPolicy {
173176
return
174177
}
175-
176-
namespace := obj.(*api.Namespace)
177178
glog.V(2).Infof("Received update for namespace: %s", namespace.Name)
178179

179180
err := npc.Sync()

pkg/controllers/network_routes_controller.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -261,10 +261,11 @@ func (nrc *NetworkRoutingController) Run(healthChan chan<- *ControllerHeartbeat,
261261
glog.Errorf("failed to get routes to advertise/withdraw %s", err)
262262
}
263263

264+
glog.V(1).Infof("Performing periodic sync of service VIP routes")
264265
nrc.advertiseVIPs(toAdvertise)
265266
nrc.withdrawVIPs(toWithdraw)
266267

267-
glog.V(1).Info("Performing periodic sync of the routes")
268+
glog.V(1).Info("Performing periodic sync of pod CIDR routes")
268269
err = nrc.advertisePodRoute()
269270
if err != nil {
270271
glog.Errorf("Error advertising route: %s", err.Error())
@@ -1438,6 +1439,7 @@ func (nrc *NetworkRoutingController) OnServiceUpdate(obj interface{}) {
14381439
return
14391440
}
14401441

1442+
glog.V(1).Infof("Received update to service: %s/%s from watch API", svc.Namespace, svc.Name)
14411443
toAdvertise, toWithdraw, err := nrc.getVIPsForService(svc, true)
14421444
if err != nil {
14431445
glog.Errorf("error getting routes for service: %s, err: %s", svc.Name, err)
@@ -1464,9 +1466,10 @@ func (nrc *NetworkRoutingController) OnServiceDelete(obj interface{}) {
14641466
return
14651467
}
14661468

1469+
glog.V(1).Infof("Received event to delete service: %s/%s from watch API", svc.Namespace, svc.Name)
14671470
toAdvertise, toWithdraw, err := nrc.getVIPsForService(svc, true)
14681471
if err != nil {
1469-
glog.Errorf("failed to get clean up routes for deleted service %s", svc.Name)
1472+
glog.Errorf("failed to get clean up routes for deleted service: %s/%s", svc.Namespace, svc.Name)
14701473
return
14711474
}
14721475

pkg/controllers/network_services_controller.go

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -353,8 +353,6 @@ func (nsc *NetworkServicesController) OnEndpointsUpdate(obj interface{}) {
353353
nsc.mu.Lock()
354354
defer nsc.mu.Unlock()
355355

356-
glog.V(1).Info("Received endpoints update from watch API")
357-
358356
ep, ok := obj.(*api.Endpoints)
359357
if !ok {
360358
glog.Error("could not convert endpoints update object to *v1.Endpoints")
@@ -365,14 +363,19 @@ func (nsc *NetworkServicesController) OnEndpointsUpdate(obj interface{}) {
365363
return
366364
}
367365

368-
// build new endpoints map to reflect the change
366+
glog.V(1).Infof("Received update to endpoint: %s/%s from watch API", ep.Namespace, ep.Name)
367+
368+
// build new service and endpoints map to reflect the change
369+
newServiceMap := nsc.buildServicesInfo()
369370
newEndpointsMap := nsc.buildEndpointsInfo()
370371

371372
if len(newEndpointsMap) != len(nsc.endpointsMap) || !reflect.DeepEqual(newEndpointsMap, nsc.endpointsMap) {
372373
nsc.endpointsMap = newEndpointsMap
374+
nsc.serviceMap = newServiceMap
375+
glog.V(1).Infof("Syncing IPVS services sync for update to endpoint: %s/%s", ep.Namespace, ep.Name)
373376
nsc.syncIpvsServices(nsc.serviceMap, nsc.endpointsMap)
374377
} else {
375-
glog.V(1).Info("Skipping ipvs server sync on endpoints because nothing changed")
378+
glog.V(1).Infof("Skipping IPVS services sync on endpoint: %s/%s update as nothing changed", ep.Namespace, ep.Name)
376379
}
377380
}
378381

@@ -381,16 +384,25 @@ func (nsc *NetworkServicesController) OnServiceUpdate(obj interface{}) {
381384
nsc.mu.Lock()
382385
defer nsc.mu.Unlock()
383386

384-
glog.V(1).Info("Received service update from watch API")
387+
svc, ok := obj.(*api.Service)
388+
if !ok {
389+
glog.Error("could not convert service update object to *v1.Service")
390+
return
391+
}
392+
393+
glog.V(1).Infof("Received update to service: %s/%s from watch API", svc.Namespace, svc.Name)
385394

386-
// build new services map to reflect the change
395+
// build new service and endpoints map to reflect the change
387396
newServiceMap := nsc.buildServicesInfo()
397+
newEndpointsMap := nsc.buildEndpointsInfo()
388398

389399
if len(newServiceMap) != len(nsc.serviceMap) || !reflect.DeepEqual(newServiceMap, nsc.serviceMap) {
400+
nsc.endpointsMap = newEndpointsMap
390401
nsc.serviceMap = newServiceMap
402+
glog.V(1).Infof("Syncing IPVS services sync on update to service: %s/%s", svc.Namespace, svc.Name)
391403
nsc.syncIpvsServices(nsc.serviceMap, nsc.endpointsMap)
392404
} else {
393-
glog.V(1).Info("Skipping ipvs server sync on service update because nothing changed")
405+
glog.V(1).Infof("Skipping syncing IPVS services for update to service: %s/%s as nothing changed", svc.Namespace, svc.Name)
394406
}
395407
}
396408

@@ -532,7 +544,6 @@ func (nsc *NetworkServicesController) syncIpvsServices(serviceInfoMap serviceInf
532544
if !svc.skipLbIps {
533545
extIPSet = extIPSet.Union(sets.NewString(svc.loadBalancerIPs...))
534546
}
535-
glog.V(2).Infof("Service \"%s\" using extIPSet: %v", svc.name, extIPSet.List())
536547

537548
for _, externalIP := range extIPSet.List() {
538549
var externalIpServiceId string

pkg/options/options.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ type KubeRouterConfig struct {
1515
CleanupConfig bool
1616
ClusterAsn uint
1717
ClusterCIDR string
18-
ConfigSyncPeriod time.Duration
1918
EnableiBGP bool
2019
EnableOverlay bool
2120
EnablePodEgress bool
@@ -48,10 +47,10 @@ type KubeRouterConfig struct {
4847
}
4948

5049
func NewKubeRouterConfig() *KubeRouterConfig {
51-
return &KubeRouterConfig{ConfigSyncPeriod: 1 * time.Minute,
52-
IpvsSyncPeriod: 1 * time.Minute,
53-
IPTablesSyncPeriod: 1 * time.Minute,
54-
RoutesSyncPeriod: 1 * time.Minute,
50+
return &KubeRouterConfig{
51+
IpvsSyncPeriod: 5 * time.Minute,
52+
IPTablesSyncPeriod: 5 * time.Minute,
53+
RoutesSyncPeriod: 5 * time.Minute,
5554
EnableOverlay: true,
5655
}
5756
}
@@ -79,8 +78,6 @@ func (s *KubeRouterConfig) AddFlags(fs *pflag.FlagSet) {
7978
"CIDR range of pods in the cluster. It is used to identify traffic originating from and destinated to pods.")
8079
fs.BoolVar(&s.EnablePodEgress, "enable-pod-egress", true,
8180
"SNAT traffic from Pods to destinations outside the cluster.")
82-
fs.DurationVar(&s.ConfigSyncPeriod, "config-sync-period", s.ConfigSyncPeriod,
83-
"The delay between apiserver configuration synchronizations (e.g. '5s', '1m'). Must be greater than 0.")
8481
fs.DurationVar(&s.IPTablesSyncPeriod, "iptables-sync-period", s.IPTablesSyncPeriod,
8582
"The delay between iptables rule synchronizations (e.g. '5s', '1m'). Must be greater than 0.")
8683
fs.DurationVar(&s.IpvsSyncPeriod, "ipvs-sync-period", s.IpvsSyncPeriod,

0 commit comments

Comments
 (0)