@@ -153,11 +153,35 @@ type endpointsInfo struct {
153
153
}
154
154
155
155
// returns a new serviceInfo struct
156
- func newServiceInfo (service proxy.ServicePortName ) * serviceInfo {
157
- return & serviceInfo {
158
- sessionAffinityType : api .ServiceAffinityNone , // default
159
- stickyMaxAgeMinutes : 180 , // TODO: paramaterize this in the API.
156
+ func newServiceInfo (serviceName proxy.ServicePortName , port * api.ServicePort , service * api.Service ) * serviceInfo {
157
+ onlyNodeLocalEndpoints := apiservice .NeedsHealthCheck (service ) && featuregate .DefaultFeatureGate .ExternalTrafficLocalOnly () && (service .Spec .Type == api .ServiceTypeLoadBalancer || service .Spec .Type == api .ServiceTypeNodePort )
158
+ info := & serviceInfo {
159
+ clusterIP : net .ParseIP (service .Spec .ClusterIP ),
160
+ port : int (port .Port ),
161
+ protocol : port .Protocol ,
162
+ nodePort : int (port .NodePort ),
163
+ // Deep-copy in case the service instance changes
164
+ loadBalancerStatus : * api .LoadBalancerStatusDeepCopy (& service .Status .LoadBalancer ),
165
+ sessionAffinityType : service .Spec .SessionAffinity ,
166
+ stickyMaxAgeMinutes : 180 , // TODO: paramaterize this in the API.
167
+ externalIPs : make ([]string , len (service .Spec .ExternalIPs )),
168
+ loadBalancerSourceRanges : make ([]string , len (service .Spec .LoadBalancerSourceRanges )),
169
+ onlyNodeLocalEndpoints : onlyNodeLocalEndpoints ,
170
+ }
171
+ copy (info .loadBalancerSourceRanges , service .Spec .LoadBalancerSourceRanges )
172
+ copy (info .externalIPs , service .Spec .ExternalIPs )
173
+
174
+ if info .onlyNodeLocalEndpoints {
175
+ p := apiservice .GetServiceHealthCheckNodePort (service )
176
+ if p == 0 {
177
+ glog .Errorf ("Service does not contain necessary annotation %v" ,
178
+ apiservice .BetaAnnotationHealthCheckNodePort )
179
+ } else {
180
+ info .healthCheckNodePort = int (p )
181
+ }
160
182
}
183
+
184
+ return info
161
185
}
162
186
163
187
type proxyServiceMap map [proxy.ServicePortName ]* serviceInfo
@@ -384,44 +408,6 @@ func CleanupLeftovers(ipt utiliptables.Interface) (encounteredError bool) {
384
408
return encounteredError
385
409
}
386
410
387
- func sameConfig (info * serviceInfo , service * api.Service , port * api.ServicePort ) bool {
388
- if info .protocol != port .Protocol || info .port != int (port .Port ) || info .nodePort != int (port .NodePort ) {
389
- return false
390
- }
391
- if ! info .clusterIP .Equal (net .ParseIP (service .Spec .ClusterIP )) {
392
- return false
393
- }
394
- if ! ipsEqual (info .externalIPs , service .Spec .ExternalIPs ) {
395
- return false
396
- }
397
- if ! api .LoadBalancerStatusEqual (& info .loadBalancerStatus , & service .Status .LoadBalancer ) {
398
- return false
399
- }
400
- if info .sessionAffinityType != service .Spec .SessionAffinity {
401
- return false
402
- }
403
- onlyNodeLocalEndpoints := apiservice .NeedsHealthCheck (service ) && featuregate .DefaultFeatureGate .ExternalTrafficLocalOnly ()
404
- if info .onlyNodeLocalEndpoints != onlyNodeLocalEndpoints {
405
- return false
406
- }
407
- if ! reflect .DeepEqual (info .loadBalancerSourceRanges , service .Spec .LoadBalancerSourceRanges ) {
408
- return false
409
- }
410
- return true
411
- }
412
-
413
- func ipsEqual (lhs , rhs []string ) bool {
414
- if len (lhs ) != len (rhs ) {
415
- return false
416
- }
417
- for i := range lhs {
418
- if lhs [i ] != rhs [i ] {
419
- return false
420
- }
421
- }
422
- return true
423
- }
424
-
425
411
// Sync is called to immediately synchronize the proxier state to iptables
426
412
func (proxier * Proxier ) Sync () {
427
413
proxier .mu .Lock ()
@@ -449,16 +435,10 @@ type healthCheckPort struct {
449
435
// service map, a list of healthcheck ports to add to or remove from the health
450
436
// checking listener service, and a set of stale UDP services.
451
437
func buildServiceMap (allServices []api.Service , oldServiceMap proxyServiceMap ) (proxyServiceMap , []healthCheckPort , []healthCheckPort , sets.String ) {
438
+ newServiceMap := make (proxyServiceMap )
452
439
healthCheckAdd := make ([]healthCheckPort , 0 )
453
440
healthCheckDel := make ([]healthCheckPort , 0 )
454
441
455
- newServiceMap := make (proxyServiceMap )
456
- for key , value := range oldServiceMap {
457
- newServiceMap [key ] = value
458
- }
459
-
460
- activeServices := make (map [proxy.ServicePortName ]bool ) // use a map as a set
461
-
462
442
for i := range allServices {
463
443
service := & allServices [i ]
464
444
svcName := types.NamespacedName {
@@ -484,57 +464,37 @@ func buildServiceMap(allServices []api.Service, oldServiceMap proxyServiceMap) (
484
464
NamespacedName : svcName ,
485
465
Port : servicePort .Name ,
486
466
}
487
- activeServices [serviceName ] = true
488
- info , exists := newServiceMap [serviceName ]
489
- if exists {
490
- if sameConfig (info , service , servicePort ) {
491
- // Nothing changed.
492
- continue
493
- }
494
- // Something changed.
495
- glog .V (3 ).Infof ("Something changed for service %q: removing it" , serviceName )
496
- delete (newServiceMap , serviceName )
467
+
468
+ info := newServiceInfo (serviceName , servicePort , service )
469
+ oldInfo , exists := oldServiceMap [serviceName ]
470
+ equal := reflect .DeepEqual (info , oldInfo )
471
+ if ! exists {
472
+ glog .V (1 ).Infof ("Adding new service %q at %s:%d/%s" , serviceName , info .clusterIP , servicePort .Port , servicePort .Protocol )
473
+ } else if ! equal {
474
+ glog .V (1 ).Infof ("Updating existing service %q at %s:%d/%s" , serviceName , info .clusterIP , servicePort .Port , servicePort .Protocol )
497
475
}
498
- serviceIP := net .ParseIP (service .Spec .ClusterIP )
499
- glog .V (1 ).Infof ("Adding new service %q at %s:%d/%s" , serviceName , serviceIP , servicePort .Port , servicePort .Protocol )
500
- info = newServiceInfo (serviceName )
501
- info .clusterIP = serviceIP
502
- info .port = int (servicePort .Port )
503
- info .protocol = servicePort .Protocol
504
- info .nodePort = int (servicePort .NodePort )
505
- info .externalIPs = service .Spec .ExternalIPs
506
- // Deep-copy in case the service instance changes
507
- info .loadBalancerStatus = * api .LoadBalancerStatusDeepCopy (& service .Status .LoadBalancer )
508
- info .sessionAffinityType = service .Spec .SessionAffinity
509
- info .loadBalancerSourceRanges = service .Spec .LoadBalancerSourceRanges
510
- info .onlyNodeLocalEndpoints = apiservice .NeedsHealthCheck (service ) && featuregate .DefaultFeatureGate .ExternalTrafficLocalOnly () && (service .Spec .Type == api .ServiceTypeLoadBalancer || service .Spec .Type == api .ServiceTypeNodePort )
511
- if info .onlyNodeLocalEndpoints {
512
- p := apiservice .GetServiceHealthCheckNodePort (service )
513
- if p == 0 {
514
- glog .Errorf ("Service does not contain necessary annotation %v" ,
515
- apiservice .BetaAnnotationHealthCheckNodePort )
516
- } else {
517
- info .healthCheckNodePort = int (p )
476
+
477
+ if ! exists || ! equal {
478
+ if info .onlyNodeLocalEndpoints && info .healthCheckNodePort > 0 {
518
479
healthCheckAdd = append (healthCheckAdd , healthCheckPort {serviceName .NamespacedName , info .healthCheckNodePort })
480
+ } else {
481
+ healthCheckDel = append (healthCheckDel , healthCheckPort {serviceName .NamespacedName , 0 })
519
482
}
520
- } else {
521
- healthCheckDel = append (healthCheckDel , healthCheckPort {serviceName .NamespacedName , 0 })
522
483
}
523
- newServiceMap [serviceName ] = info
524
484
485
+ newServiceMap [serviceName ] = info
525
486
glog .V (4 ).Infof ("added serviceInfo(%s): %s" , serviceName , spew .Sdump (info ))
526
487
}
527
488
}
528
489
529
490
staleUDPServices := sets .NewString ()
530
491
// Remove serviceports missing from the update.
531
- for name , info := range newServiceMap {
532
- if ! activeServices [name ] {
492
+ for name , info := range oldServiceMap {
493
+ if _ , exists := newServiceMap [name ]; ! exists {
533
494
glog .V (1 ).Infof ("Removing service %q" , name )
534
495
if info .protocol == api .ProtocolUDP {
535
496
staleUDPServices .Insert (info .clusterIP .String ())
536
497
}
537
- delete (newServiceMap , name )
538
498
if info .onlyNodeLocalEndpoints && info .healthCheckNodePort > 0 {
539
499
healthCheckDel = append (healthCheckDel , healthCheckPort {name .NamespacedName , info .healthCheckNodePort })
540
500
}
0 commit comments