Skip to content

Commit 1653476

Browse files
committed
proxier: use IPSet from k8s.io/utils/net to store local addresses
This allows the proxier to cache local addresses instead of fetching all local addresses every time in IsLocalIP. Signed-off-by: Andrew Sy Kim <[email protected]>
1 parent 77feb11 commit 1653476

File tree

12 files changed

+30
-41
lines changed

12 files changed

+30
-41
lines changed

go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,6 @@ replace (
558558
k8s.io/kubelet => ./staging/src/k8s.io/kubelet
559559
k8s.io/legacy-cloud-providers => ./staging/src/k8s.io/legacy-cloud-providers
560560
k8s.io/metrics => ./staging/src/k8s.io/metrics
561-
k8s.io/node-api => ./staging/src/k8s.io/node-api
562561
k8s.io/repo-infra => k8s.io/repo-infra v0.0.1-alpha.1
563562
k8s.io/sample-apiserver => ./staging/src/k8s.io/sample-apiserver
564563
k8s.io/sample-cli-plugin => ./staging/src/k8s.io/sample-cli-plugin

pkg/proxy/iptables/proxier.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -803,6 +803,9 @@ func (proxier *Proxier) syncProxyRules() {
803803
klog.Warning("No local addresses found, assuming all external IPs are not local")
804804
}
805805

806+
localAddrSet := utilnet.IPSet{}
807+
localAddrSet.Insert(localAddrs...)
808+
806809
// We assume that if this was called, we really want to sync them,
807810
// even if nothing changed in the meantime. In other words, callers are
808811
// responsible for detecting no-op changes and not calling this function.
@@ -1037,7 +1040,7 @@ func (proxier *Proxier) syncProxyRules() {
10371040
// If the "external" IP happens to be an IP that is local to this
10381041
// machine, hold the local port open so no other process can open it
10391042
// (because the socket might open but it would never work).
1040-
if len(localAddrs) > 0 && (svcInfo.Protocol() != v1.ProtocolSCTP) && utilproxy.ContainsIP(localAddrs, net.ParseIP(externalIP)) {
1043+
if localAddrSet.Len() > 0 && (svcInfo.Protocol() != v1.ProtocolSCTP) && localAddrSet.Has(net.ParseIP(externalIP)) {
10411044
lp := utilproxy.LocalPort{
10421045
Description: "externalIP for " + svcNameString,
10431046
IP: externalIP,

pkg/proxy/ipvs/proxier.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1016,6 +1016,9 @@ func (proxier *Proxier) syncProxyRules() {
10161016
klog.Warning("No local addresses found, assuming all external IPs are not local")
10171017
}
10181018

1019+
localAddrSet := utilnet.IPSet{}
1020+
localAddrSet.Insert(localAddrs...)
1021+
10191022
// We assume that if this was called, we really want to sync them,
10201023
// even if nothing changed in the meantime. In other words, callers are
10211024
// responsible for detecting no-op changes and not calling this function.
@@ -1200,7 +1203,7 @@ func (proxier *Proxier) syncProxyRules() {
12001203
// If the "external" IP happens to be an IP that is local to this
12011204
// machine, hold the local port open so no other process can open it
12021205
// (because the socket might open but it would never work).
1203-
if len(localAddrs) > 0 && (svcInfo.Protocol() != v1.ProtocolSCTP) && utilproxy.ContainsIP(localAddrs, net.ParseIP(externalIP)) {
1206+
if localAddrSet.Len() > 0 && (svcInfo.Protocol() != v1.ProtocolSCTP) && localAddrSet.Has(net.ParseIP(externalIP)) {
12041207
// We do not start listening on SCTP ports, according to our agreement in the SCTP support KEP
12051208
lp := utilproxy.LocalPort{
12061209
Description: "externalIP for " + svcNameString,

pkg/proxy/userspace/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ go_library(
3636
"//staging/src/k8s.io/cloud-provider/service/helpers:go_default_library",
3737
"//vendor/k8s.io/klog:go_default_library",
3838
"//vendor/k8s.io/utils/exec:go_default_library",
39+
"//vendor/k8s.io/utils/net:go_default_library",
3940
] + select({
4041
"@io_bazel_rules_go//go/platform:android": [
4142
"//vendor/golang.org/x/sys/unix:go_default_library",

pkg/proxy/userspace/proxier.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import (
4141
"k8s.io/kubernetes/pkg/util/conntrack"
4242
"k8s.io/kubernetes/pkg/util/iptables"
4343
utilexec "k8s.io/utils/exec"
44+
netutils "k8s.io/utils/net"
4445
)
4546

4647
type portal struct {
@@ -127,7 +128,7 @@ type Proxier struct {
127128
listenIP net.IP
128129
iptables iptables.Interface
129130
hostIP net.IP
130-
localAddrs []net.IP
131+
localAddrs netutils.IPSet
131132
proxyPorts PortAllocator
132133
makeProxySocket ProxySocketFunc
133134
exec utilexec.Interface
@@ -378,7 +379,10 @@ func (proxier *Proxier) syncProxyRules() {
378379
} else if len(localAddrs) == 0 {
379380
klog.Warning("No local addresses were found, assuming all external IPs are not local")
380381
}
381-
proxier.localAddrs = localAddrs
382+
383+
localAddrSet := netutils.IPSet{}
384+
localAddrSet.Insert(localAddrs...)
385+
proxier.localAddrs = localAddrSet
382386

383387
proxier.ensurePortals()
384388
proxier.cleanupStaleStickySessions()
@@ -734,7 +738,7 @@ func (proxier *Proxier) openPortal(service proxy.ServicePortName, info *ServiceI
734738
}
735739

736740
func (proxier *Proxier) openOnePortal(portal portal, protocol v1.Protocol, proxyIP net.IP, proxyPort int, name proxy.ServicePortName) error {
737-
if len(proxier.localAddrs) > 0 && utilproxy.ContainsIP(proxier.localAddrs, portal.ip) {
741+
if proxier.localAddrs.Len() > 0 && proxier.localAddrs.Has(portal.ip) {
738742
err := proxier.claimNodePort(portal.ip, portal.port, protocol, name)
739743
if err != nil {
740744
return err
@@ -910,7 +914,7 @@ func (proxier *Proxier) closePortal(service proxy.ServicePortName, info *Service
910914

911915
func (proxier *Proxier) closeOnePortal(portal portal, protocol v1.Protocol, proxyIP net.IP, proxyPort int, name proxy.ServicePortName) []error {
912916
el := []error{}
913-
if len(proxier.localAddrs) > 0 && utilproxy.ContainsIP(proxier.localAddrs, portal.ip) {
917+
if proxier.localAddrs.Len() > 0 && proxier.localAddrs.Has(portal.ip) {
914918
if err := proxier.releaseNodePort(portal.ip, portal.port, protocol, name); err != nil {
915919
el = append(el, err)
916920
}

pkg/proxy/util/utils.go

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -123,23 +123,25 @@ func IsProxyableHostname(ctx context.Context, resolv Resolver, hostname string)
123123
return nil
124124
}
125125

126-
// IsLocalIP checks if a given IP address is bound to an interface
127-
// on the local system
128-
func IsLocalIP(ip string) (bool, error) {
126+
// GetLocalAddrs returns a list of all network addresses on the local system
127+
func GetLocalAddrs() ([]net.IP, error) {
128+
var localAddrs []net.IP
129+
129130
addrs, err := net.InterfaceAddrs()
130131
if err != nil {
131-
return false, err
132+
return nil, err
132133
}
133-
for i := range addrs {
134-
intf, _, err := net.ParseCIDR(addrs[i].String())
134+
135+
for _, addr := range addrs {
136+
ip, _, err := net.ParseCIDR(addr.String())
135137
if err != nil {
136-
return false, err
137-
}
138-
if net.ParseIP(ip).Equal(intf) {
139-
return true, nil
138+
return nil, err
140139
}
140+
141+
localAddrs = append(localAddrs, ip)
141142
}
142-
return false, nil
143+
144+
return localAddrs, nil
143145
}
144146

145147
// ShouldSkipService checks if a given service should skip proxying

staging/repos_generated.bzl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ staging_repos = [
3636
"k8s.io/kubelet",
3737
"k8s.io/legacy-cloud-providers",
3838
"k8s.io/metrics",
39-
"k8s.io/node-api",
4039
"k8s.io/sample-apiserver",
4140
"k8s.io/sample-cli-plugin",
4241
"k8s.io/sample-controller",

staging/src/BUILD

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ filegroup(
3535
"//staging/src/k8s.io/kubelet:all-srcs",
3636
"//staging/src/k8s.io/legacy-cloud-providers:all-srcs",
3737
"//staging/src/k8s.io/metrics:all-srcs",
38-
"//staging/src/k8s.io/node-api:all-srcs",
3938
"//staging/src/k8s.io/sample-apiserver:all-srcs",
4039
"//staging/src/k8s.io/sample-cli-plugin:all-srcs",
4140
"//staging/src/k8s.io/sample-controller:all-srcs",

staging/src/k8s.io/node-api/BUILD

Lines changed: 0 additions & 13 deletions
This file was deleted.

staging/src/k8s.io/node-api/go.mod

Lines changed: 0 additions & 7 deletions
This file was deleted.

0 commit comments

Comments
 (0)