Skip to content

Commit 77feb11

Browse files
committed
userspace proxy: get local addresses only once per sync loop
This avoids fetching all local network interfaces everytime we sync an external IP. For clusters with many external IPs this gets really expensive. This change caches all local addresses once per sync. Signed-off-by: Andrew Sy Kim <[email protected]>
1 parent 126bf5a commit 77feb11

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

pkg/proxy/userspace/proxier.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ type Proxier struct {
127127
listenIP net.IP
128128
iptables iptables.Interface
129129
hostIP net.IP
130+
localAddrs []net.IP
130131
proxyPorts PortAllocator
131132
makeProxySocket ProxySocketFunc
132133
exec utilexec.Interface
@@ -371,6 +372,14 @@ func (proxier *Proxier) syncProxyRules() {
371372
proxier.unmergeService(change.previous, existingPorts)
372373
}
373374

375+
localAddrs, err := utilproxy.GetLocalAddrs()
376+
if err != nil {
377+
klog.Errorf("Failed to get local addresses during proxy sync: %s, assuming IPs are not local", err)
378+
} else if len(localAddrs) == 0 {
379+
klog.Warning("No local addresses were found, assuming all external IPs are not local")
380+
}
381+
proxier.localAddrs = localAddrs
382+
374383
proxier.ensurePortals()
375384
proxier.cleanupStaleStickySessions()
376385
}
@@ -725,9 +734,7 @@ func (proxier *Proxier) openPortal(service proxy.ServicePortName, info *ServiceI
725734
}
726735

727736
func (proxier *Proxier) openOnePortal(portal portal, protocol v1.Protocol, proxyIP net.IP, proxyPort int, name proxy.ServicePortName) error {
728-
if local, err := utilproxy.IsLocalIP(portal.ip.String()); err != nil {
729-
return fmt.Errorf("can't determine if IP %s is local, assuming not: %v", portal.ip, err)
730-
} else if local {
737+
if len(proxier.localAddrs) > 0 && utilproxy.ContainsIP(proxier.localAddrs, portal.ip) {
731738
err := proxier.claimNodePort(portal.ip, portal.port, protocol, name)
732739
if err != nil {
733740
return err
@@ -903,10 +910,7 @@ func (proxier *Proxier) closePortal(service proxy.ServicePortName, info *Service
903910

904911
func (proxier *Proxier) closeOnePortal(portal portal, protocol v1.Protocol, proxyIP net.IP, proxyPort int, name proxy.ServicePortName) []error {
905912
el := []error{}
906-
907-
if local, err := utilproxy.IsLocalIP(portal.ip.String()); err != nil {
908-
el = append(el, fmt.Errorf("can't determine if IP %s is local, assuming not: %v", portal.ip, err))
909-
} else if local {
913+
if len(proxier.localAddrs) > 0 && utilproxy.ContainsIP(proxier.localAddrs, portal.ip) {
910914
if err := proxier.releaseNodePort(portal.ip, portal.port, protocol, name); err != nil {
911915
el = append(el, err)
912916
}

0 commit comments

Comments
 (0)