Skip to content

Commit 8f5e851

Browse files
authored
Merge pull request kubernetes#90103 from SataQiu/refactor-proxy-20200413
kube-proxy: move GetNodeAddresses call out of internal loop to avoid repeated computation
2 parents eda662b + b68312e commit 8f5e851

File tree

1 file changed

+26
-28
lines changed

1 file changed

+26
-28
lines changed

pkg/proxy/iptables/proxier.go

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -813,6 +813,11 @@ func (proxier *Proxier) syncProxyRules() {
813813
localAddrSet := utilnet.IPSet{}
814814
localAddrSet.Insert(localAddrs...)
815815

816+
nodeAddresses, err := utilproxy.GetNodeAddresses(proxier.nodePortAddresses, proxier.networkInterfacer)
817+
if err != nil {
818+
klog.Errorf("Failed to get node ip address matching nodeport cidrs %v, services with nodeport may not work as intended: %v", proxier.nodePortAddresses, err)
819+
}
820+
816821
// We assume that if this was called, we really want to sync them,
817822
// even if nothing changed in the meantime. In other words, callers are
818823
// responsible for detecting no-op changes and not calling this function.
@@ -1199,14 +1204,12 @@ func (proxier *Proxier) syncProxyRules() {
11991204
if svcInfo.NodePort() != 0 {
12001205
// Hold the local port open so no other process can open it
12011206
// (because the socket might open but it would never work).
1202-
addresses, err := utilproxy.GetNodeAddresses(proxier.nodePortAddresses, proxier.networkInterfacer)
1203-
if err != nil {
1204-
klog.Errorf("Failed to get node ip address matching nodeport cidr: %v", err)
1207+
if len(nodeAddresses) == 0 {
12051208
continue
12061209
}
12071210

12081211
lps := make([]utilproxy.LocalPort, 0)
1209-
for address := range addresses {
1212+
for address := range nodeAddresses {
12101213
lp := utilproxy.LocalPort{
12111214
Description: "nodePort for " + svcNameString,
12121215
IP: address,
@@ -1468,36 +1471,31 @@ func (proxier *Proxier) syncProxyRules() {
14681471

14691472
// Finally, tail-call to the nodeports chain. This needs to be after all
14701473
// other service portal rules.
1471-
addresses, err := utilproxy.GetNodeAddresses(proxier.nodePortAddresses, proxier.networkInterfacer)
1472-
if err != nil {
1473-
klog.Errorf("Failed to get node ip address matching nodeport cidr")
1474-
} else {
1475-
isIPv6 := proxier.iptables.IsIPv6()
1476-
for address := range addresses {
1477-
// TODO(thockin, m1093782566): If/when we have dual-stack support we will want to distinguish v4 from v6 zero-CIDRs.
1478-
if utilproxy.IsZeroCIDR(address) {
1479-
args = append(args[:0],
1480-
"-A", string(kubeServicesChain),
1481-
"-m", "comment", "--comment", `"kubernetes service nodeports; NOTE: this must be the last rule in this chain"`,
1482-
"-m", "addrtype", "--dst-type", "LOCAL",
1483-
"-j", string(kubeNodePortsChain))
1484-
writeLine(proxier.natRules, args...)
1485-
// Nothing else matters after the zero CIDR.
1486-
break
1487-
}
1488-
// Ignore IP addresses with incorrect version
1489-
if isIPv6 && !utilnet.IsIPv6String(address) || !isIPv6 && utilnet.IsIPv6String(address) {
1490-
klog.Errorf("IP address %s has incorrect IP version", address)
1491-
continue
1492-
}
1493-
// create nodeport rules for each IP one by one
1474+
isIPv6 := proxier.iptables.IsIPv6()
1475+
for address := range nodeAddresses {
1476+
// TODO(thockin, m1093782566): If/when we have dual-stack support we will want to distinguish v4 from v6 zero-CIDRs.
1477+
if utilproxy.IsZeroCIDR(address) {
14941478
args = append(args[:0],
14951479
"-A", string(kubeServicesChain),
14961480
"-m", "comment", "--comment", `"kubernetes service nodeports; NOTE: this must be the last rule in this chain"`,
1497-
"-d", address,
1481+
"-m", "addrtype", "--dst-type", "LOCAL",
14981482
"-j", string(kubeNodePortsChain))
14991483
writeLine(proxier.natRules, args...)
1484+
// Nothing else matters after the zero CIDR.
1485+
break
1486+
}
1487+
// Ignore IP addresses with incorrect version
1488+
if isIPv6 && !utilnet.IsIPv6String(address) || !isIPv6 && utilnet.IsIPv6String(address) {
1489+
klog.Errorf("IP address %s has incorrect IP version", address)
1490+
continue
15001491
}
1492+
// create nodeport rules for each IP one by one
1493+
args = append(args[:0],
1494+
"-A", string(kubeServicesChain),
1495+
"-m", "comment", "--comment", `"kubernetes service nodeports; NOTE: this must be the last rule in this chain"`,
1496+
"-d", address,
1497+
"-j", string(kubeNodePortsChain))
1498+
writeLine(proxier.natRules, args...)
15011499
}
15021500

15031501
// Drop the packets in INVALID state, which would potentially cause

0 commit comments

Comments
 (0)