Skip to content

Commit abfb705

Browse files
dlamottemurali-reddy
authored andcommitted
services: correct check for inactive service endpoints (#430)
* services: correct check for inactive service endpoints * services: avoid creating ipvs services that would later get deleted
1 parent 380a476 commit abfb705

File tree

1 file changed

+33
-3
lines changed

1 file changed

+33
-3
lines changed

pkg/controllers/proxy/network_services_controller.go

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,31 @@ type externalIPService struct {
428428
externalIp string
429429
}
430430

431+
func hasActiveEndpoints(svc *serviceInfo, endpoints []endpointsInfo, nodePodCidrStr string) bool {
432+
if svc.local {
433+
_, nodePodCidr, err := net.ParseCIDR(nodePodCidrStr)
434+
if err != nil {
435+
glog.Errorf("Failed to ParseCIDR %s for hasActiveEndpoints on service %s/%s",
436+
nodePodCidrStr, svc.namespace, svc.name)
437+
return false
438+
}
439+
for _, endpoint := range endpoints {
440+
ip := net.ParseIP(endpoint.ip)
441+
if ip == nil {
442+
glog.Errorf("Failed to ParseCIDR %s for endpoint in hasActiveEndpoints on service %s/%s",
443+
endpoint.ip, svc.namespace, svc.name)
444+
continue
445+
}
446+
if nodePodCidr.Contains(ip) {
447+
return true
448+
}
449+
}
450+
return false
451+
}
452+
453+
return len(endpoints) > 0
454+
}
455+
431456
// sync the ipvs service and server details configured to reflect the desired state of services and endpoint
432457
// as learned from services and endpoints information from the api server
433458
func (nsc *NetworkServicesController) syncIpvsServices(serviceInfoMap serviceInfoMap, endpointsInfoMap endpointsInfoMap) error {
@@ -493,6 +518,13 @@ func (nsc *NetworkServicesController) syncIpvsServices(serviceInfoMap serviceInf
493518
continue
494519
}
495520

521+
endpoints := endpointsInfoMap[k]
522+
523+
if !hasActiveEndpoints(svc, endpoints, nsc.podCidr) {
524+
glog.V(1).Infof("Skipping service %s/%s as it does not have active endpoints\n", svc.namespace, svc.name)
525+
continue
526+
}
527+
496528
// create IPVS service for the service to be exposed through the cluster ip
497529
ipvsClusterVipSvc, err := nsc.ln.ipvsAddService(ipvsSvcs, svc.clusterIP, protocol, uint16(svc.port), svc.sessionAffinity, svc.scheduler)
498530
if err != nil {
@@ -549,8 +581,6 @@ func (nsc *NetworkServicesController) syncIpvsServices(serviceInfoMap serviceInf
549581
}
550582
}
551583

552-
endpoints := endpointsInfoMap[k]
553-
554584
externalIpServices := make([]externalIPService, 0)
555585
// create IPVS service for the service to be exposed through the external IP's
556586
// For external IP (which are meant for ingress traffic) Kube-router setsup IPVS services
@@ -751,7 +781,7 @@ func (nsc *NetworkServicesController) syncIpvsServices(serviceInfoMap serviceInf
751781
}
752782

753783
endpoints, ok := activeServiceEndpointMap[key]
754-
if !ok {
784+
if !ok || len(endpoints) == 0 {
755785
glog.V(1).Infof("Found a IPVS service %s which is no longer needed so cleaning up",
756786
ipvsServiceString(ipvsSvc))
757787
err := nsc.ln.ipvsDelService(ipvsSvc)

0 commit comments

Comments
 (0)