Skip to content

Commit e25c174

Browse files
authored
support service.spec externalTrafficPolicy=Local. Takes precedence over kube-router.io/service.local annotation. (#303)
Also dynamically cleanup service endpoints based on the service spec is set for Local only services or not.
1 parent 617c773 commit e25c174

File tree

1 file changed

+26
-7
lines changed

1 file changed

+26
-7
lines changed

app/controllers/network_services_controller.go

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -478,8 +478,10 @@ func (nsc *NetworkServicesController) syncIpvsServices(serviceInfoMap serviceInf
478478

479479
activeServiceEndpointMap[externalIpServiceId] = make([]string, 0)
480480
for _, endpoint := range endpoints {
481-
activeServiceEndpointMap[externalIpServiceId] =
482-
append(activeServiceEndpointMap[externalIpServiceId], endpoint.ip)
481+
isLocal, _ := isLocalEndpoint(endpoint.ip, nsc.podCidr)
482+
if !svc.local || (svc.local && isLocal) {
483+
activeServiceEndpointMap[externalIpServiceId] = append(activeServiceEndpointMap[externalIpServiceId], endpoint.ip)
484+
}
483485
}
484486
}
485487

@@ -492,13 +494,15 @@ func (nsc *NetworkServicesController) syncIpvsServices(serviceInfoMap serviceInf
492494
Weight: 1,
493495
}
494496

495-
err := ipvsAddServer(ipvsClusterVipSvc, &dst, false, nsc.podCidr)
497+
err := ipvsAddServer(ipvsClusterVipSvc, &dst, svc.local, nsc.podCidr)
496498
if err != nil {
497499
glog.Errorf(err.Error())
498500
}
499501

500-
activeServiceEndpointMap[clusterServiceId] =
501-
append(activeServiceEndpointMap[clusterServiceId], endpoint.ip)
502+
isLocal, err := isLocalEndpoint(endpoint.ip, nsc.podCidr)
503+
if !svc.local || (svc.local && isLocal) {
504+
activeServiceEndpointMap[clusterServiceId] = append(activeServiceEndpointMap[clusterServiceId], endpoint.ip)
505+
}
502506

503507
if svc.nodePort != 0 {
504508
for i := 0; i < len(ipvsNodeportSvcs); i++ {
@@ -507,8 +511,9 @@ func (nsc *NetworkServicesController) syncIpvsServices(serviceInfoMap serviceInf
507511
glog.Errorf(err.Error())
508512
}
509513

510-
activeServiceEndpointMap[nodeServiceIds[i]] =
511-
append(activeServiceEndpointMap[clusterServiceId], endpoint.ip)
514+
if !svc.local || (svc.local && isLocal) {
515+
activeServiceEndpointMap[nodeServiceIds[i]] = append(activeServiceEndpointMap[clusterServiceId], endpoint.ip)
516+
}
512517
}
513518
}
514519

@@ -624,6 +629,17 @@ func (nsc *NetworkServicesController) syncIpvsServices(serviceInfoMap serviceInf
624629
return nil
625630
}
626631

632+
func isLocalEndpoint(ip, podCidr string) (bool, error) {
633+
_, ipnet, err := net.ParseCIDR(podCidr)
634+
if err != nil {
635+
return false, err
636+
}
637+
if ipnet.Contains(net.ParseIP(ip)) {
638+
return true, nil
639+
}
640+
return false, nil
641+
}
642+
627643
func getPodObjectForEndpoint(endpointIP string) (*api.Pod, error) {
628644
for _, pod := range watchers.PodWatcher.List() {
629645
if strings.Compare(pod.Status.PodIP, endpointIP) == 0 {
@@ -845,6 +861,9 @@ func buildServicesInfo() serviceInfoMap {
845861
svcInfo.sessionAffinity = svc.Spec.SessionAffinity == "ClientIP"
846862
_, svcInfo.hairpin = svc.ObjectMeta.Annotations["kube-router.io/service.hairpin"]
847863
_, svcInfo.local = svc.ObjectMeta.Annotations["kube-router.io/service.local"]
864+
if svc.Spec.ExternalTrafficPolicy == api.ServiceExternalTrafficPolicyTypeLocal {
865+
svcInfo.local = true
866+
}
848867

849868
svcId := generateServiceId(svc.Namespace, svc.Name, port.Name)
850869
serviceMap[svcId] = &svcInfo

0 commit comments

Comments
 (0)