Skip to content

Commit 313c3b8

Browse files
committed
iptables proxier: 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 9e5a06c commit 313c3b8

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

pkg/proxy/iptables/proxier.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -796,6 +796,13 @@ func (proxier *Proxier) syncProxyRules() {
796796
klog.V(4).Infof("syncProxyRules took %v", time.Since(start))
797797
}()
798798

799+
localAddrs, err := utilproxy.GetLocalAddrs()
800+
if err != nil {
801+
klog.Errorf("Failed to get local addresses during proxy sync: %v, assuming external IPs are not local", err)
802+
} else if len(localAddrs) == 0 {
803+
klog.Warning("No local addresses found, assuming all external IPs are not local")
804+
}
805+
799806
// We assume that if this was called, we really want to sync them,
800807
// even if nothing changed in the meantime. In other words, callers are
801808
// responsible for detecting no-op changes and not calling this function.
@@ -848,7 +855,7 @@ func (proxier *Proxier) syncProxyRules() {
848855
// This will be a map of chain name to chain with rules as stored in iptables-save/iptables-restore
849856
existingFilterChains := make(map[utiliptables.Chain][]byte)
850857
proxier.existingFilterChainsData.Reset()
851-
err := proxier.iptables.SaveInto(utiliptables.TableFilter, proxier.existingFilterChainsData)
858+
err = proxier.iptables.SaveInto(utiliptables.TableFilter, proxier.existingFilterChainsData)
852859
if err != nil { // if we failed to get any rules
853860
klog.Errorf("Failed to execute iptables-save, syncing all rules: %v", err)
854861
} else { // otherwise parse the output
@@ -1030,9 +1037,7 @@ func (proxier *Proxier) syncProxyRules() {
10301037
// If the "external" IP happens to be an IP that is local to this
10311038
// machine, hold the local port open so no other process can open it
10321039
// (because the socket might open but it would never work).
1033-
if local, err := utilproxy.IsLocalIP(externalIP); err != nil {
1034-
klog.Errorf("can't determine if IP is local, assuming not: %v", err)
1035-
} else if local && (svcInfo.Protocol() != v1.ProtocolSCTP) {
1040+
if len(localAddrs) > 0 && (svcInfo.Protocol() != v1.ProtocolSCTP) && utilproxy.ContainsIP(localAddrs, net.ParseIP(externalIP)) {
10361041
lp := utilproxy.LocalPort{
10371042
Description: "externalIP for " + svcNameString,
10381043
IP: externalIP,

0 commit comments

Comments
 (0)