Skip to content

Commit b68312e

Browse files
committed
kube-proxy: move GetNodeAddresses call out of internal loop to avoid repeated computation
Signed-off-by: SataQiu <[email protected]>
1 parent b17ddac commit b68312e

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
@@ -809,6 +809,11 @@ func (proxier *Proxier) syncProxyRules() {
809809
localAddrSet := utilnet.IPSet{}
810810
localAddrSet.Insert(localAddrs...)
811811

812+
nodeAddresses, err := utilproxy.GetNodeAddresses(proxier.nodePortAddresses, proxier.networkInterfacer)
813+
if err != nil {
814+
klog.Errorf("Failed to get node ip address matching nodeport cidrs %v, services with nodeport may not work as intended: %v", proxier.nodePortAddresses, err)
815+
}
816+
812817
// We assume that if this was called, we really want to sync them,
813818
// even if nothing changed in the meantime. In other words, callers are
814819
// responsible for detecting no-op changes and not calling this function.
@@ -1195,14 +1200,12 @@ func (proxier *Proxier) syncProxyRules() {
11951200
if svcInfo.NodePort() != 0 {
11961201
// Hold the local port open so no other process can open it
11971202
// (because the socket might open but it would never work).
1198-
addresses, err := utilproxy.GetNodeAddresses(proxier.nodePortAddresses, proxier.networkInterfacer)
1199-
if err != nil {
1200-
klog.Errorf("Failed to get node ip address matching nodeport cidr: %v", err)
1203+
if len(nodeAddresses) == 0 {
12011204
continue
12021205
}
12031206

12041207
lps := make([]utilproxy.LocalPort, 0)
1205-
for address := range addresses {
1208+
for address := range nodeAddresses {
12061209
lp := utilproxy.LocalPort{
12071210
Description: "nodePort for " + svcNameString,
12081211
IP: address,
@@ -1464,36 +1467,31 @@ func (proxier *Proxier) syncProxyRules() {
14641467

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

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

0 commit comments

Comments
 (0)