diff --git a/go.mod b/go.mod index 96d2877254d..2ab72ed3c00 100644 --- a/go.mod +++ b/go.mod @@ -558,7 +558,6 @@ replace ( k8s.io/kubelet => ./staging/src/k8s.io/kubelet k8s.io/legacy-cloud-providers => ./staging/src/k8s.io/legacy-cloud-providers k8s.io/metrics => ./staging/src/k8s.io/metrics - k8s.io/node-api => ./staging/src/k8s.io/node-api k8s.io/repo-infra => k8s.io/repo-infra v0.0.1-alpha.1 k8s.io/sample-apiserver => ./staging/src/k8s.io/sample-apiserver k8s.io/sample-cli-plugin => ./staging/src/k8s.io/sample-cli-plugin diff --git a/pkg/proxy/iptables/proxier.go b/pkg/proxy/iptables/proxier.go index cda24fa4d69..1d082acb473 100644 --- a/pkg/proxy/iptables/proxier.go +++ b/pkg/proxy/iptables/proxier.go @@ -803,6 +803,9 @@ func (proxier *Proxier) syncProxyRules() { klog.Warning("No local addresses found, assuming all external IPs are not local") } + localAddrSet := utilnet.IPSet{} + localAddrSet.Insert(localAddrs...) + // We assume that if this was called, we really want to sync them, // even if nothing changed in the meantime. In other words, callers are // responsible for detecting no-op changes and not calling this function. @@ -1037,7 +1040,7 @@ func (proxier *Proxier) syncProxyRules() { // If the "external" IP happens to be an IP that is local to this // machine, hold the local port open so no other process can open it // (because the socket might open but it would never work). - if len(localAddrs) > 0 && (svcInfo.Protocol() != v1.ProtocolSCTP) && utilproxy.ContainsIP(localAddrs, net.ParseIP(externalIP)) { + if localAddrSet.Len() > 0 && (svcInfo.Protocol() != v1.ProtocolSCTP) && localAddrSet.Has(net.ParseIP(externalIP)) { lp := utilproxy.LocalPort{ Description: "externalIP for " + svcNameString, IP: externalIP, diff --git a/pkg/proxy/ipvs/proxier.go b/pkg/proxy/ipvs/proxier.go index f16f031390a..473294e03cf 100644 --- a/pkg/proxy/ipvs/proxier.go +++ b/pkg/proxy/ipvs/proxier.go @@ -1016,6 +1016,9 @@ func (proxier *Proxier) syncProxyRules() { klog.Warning("No local addresses found, assuming all external IPs are not local") } + localAddrSet := utilnet.IPSet{} + localAddrSet.Insert(localAddrs...) + // We assume that if this was called, we really want to sync them, // even if nothing changed in the meantime. In other words, callers are // responsible for detecting no-op changes and not calling this function. @@ -1200,7 +1203,7 @@ func (proxier *Proxier) syncProxyRules() { // If the "external" IP happens to be an IP that is local to this // machine, hold the local port open so no other process can open it // (because the socket might open but it would never work). - if len(localAddrs) > 0 && (svcInfo.Protocol() != v1.ProtocolSCTP) && utilproxy.ContainsIP(localAddrs, net.ParseIP(externalIP)) { + if localAddrSet.Len() > 0 && (svcInfo.Protocol() != v1.ProtocolSCTP) && localAddrSet.Has(net.ParseIP(externalIP)) { // We do not start listening on SCTP ports, according to our agreement in the SCTP support KEP lp := utilproxy.LocalPort{ Description: "externalIP for " + svcNameString, diff --git a/pkg/proxy/userspace/BUILD b/pkg/proxy/userspace/BUILD index 9c76a02c01e..79348fdb792 100644 --- a/pkg/proxy/userspace/BUILD +++ b/pkg/proxy/userspace/BUILD @@ -36,6 +36,7 @@ go_library( "//staging/src/k8s.io/cloud-provider/service/helpers:go_default_library", "//vendor/k8s.io/klog:go_default_library", "//vendor/k8s.io/utils/exec:go_default_library", + "//vendor/k8s.io/utils/net:go_default_library", ] + select({ "@io_bazel_rules_go//go/platform:android": [ "//vendor/golang.org/x/sys/unix:go_default_library", diff --git a/pkg/proxy/userspace/proxier.go b/pkg/proxy/userspace/proxier.go index 0345a553489..49bd24f705d 100644 --- a/pkg/proxy/userspace/proxier.go +++ b/pkg/proxy/userspace/proxier.go @@ -41,6 +41,7 @@ import ( "k8s.io/kubernetes/pkg/util/conntrack" "k8s.io/kubernetes/pkg/util/iptables" utilexec "k8s.io/utils/exec" + netutils "k8s.io/utils/net" ) type portal struct { @@ -127,7 +128,7 @@ type Proxier struct { listenIP net.IP iptables iptables.Interface hostIP net.IP - localAddrs []net.IP + localAddrs netutils.IPSet proxyPorts PortAllocator makeProxySocket ProxySocketFunc exec utilexec.Interface @@ -378,7 +379,10 @@ func (proxier *Proxier) syncProxyRules() { } else if len(localAddrs) == 0 { klog.Warning("No local addresses were found, assuming all external IPs are not local") } - proxier.localAddrs = localAddrs + + localAddrSet := netutils.IPSet{} + localAddrSet.Insert(localAddrs...) + proxier.localAddrs = localAddrSet proxier.ensurePortals() proxier.cleanupStaleStickySessions() @@ -734,7 +738,7 @@ func (proxier *Proxier) openPortal(service proxy.ServicePortName, info *ServiceI } func (proxier *Proxier) openOnePortal(portal portal, protocol v1.Protocol, proxyIP net.IP, proxyPort int, name proxy.ServicePortName) error { - if len(proxier.localAddrs) > 0 && utilproxy.ContainsIP(proxier.localAddrs, portal.ip) { + if proxier.localAddrs.Len() > 0 && proxier.localAddrs.Has(portal.ip) { err := proxier.claimNodePort(portal.ip, portal.port, protocol, name) if err != nil { return err @@ -910,7 +914,7 @@ func (proxier *Proxier) closePortal(service proxy.ServicePortName, info *Service func (proxier *Proxier) closeOnePortal(portal portal, protocol v1.Protocol, proxyIP net.IP, proxyPort int, name proxy.ServicePortName) []error { el := []error{} - if len(proxier.localAddrs) > 0 && utilproxy.ContainsIP(proxier.localAddrs, portal.ip) { + if proxier.localAddrs.Len() > 0 && proxier.localAddrs.Has(portal.ip) { if err := proxier.releaseNodePort(portal.ip, portal.port, protocol, name); err != nil { el = append(el, err) } diff --git a/pkg/proxy/util/utils.go b/pkg/proxy/util/utils.go index fb3fe870010..b4f1b4304c1 100644 --- a/pkg/proxy/util/utils.go +++ b/pkg/proxy/util/utils.go @@ -123,23 +123,25 @@ func IsProxyableHostname(ctx context.Context, resolv Resolver, hostname string) return nil } -// IsLocalIP checks if a given IP address is bound to an interface -// on the local system -func IsLocalIP(ip string) (bool, error) { +// GetLocalAddrs returns a list of all network addresses on the local system +func GetLocalAddrs() ([]net.IP, error) { + var localAddrs []net.IP + addrs, err := net.InterfaceAddrs() if err != nil { - return false, err + return nil, err } - for i := range addrs { - intf, _, err := net.ParseCIDR(addrs[i].String()) + + for _, addr := range addrs { + ip, _, err := net.ParseCIDR(addr.String()) if err != nil { - return false, err - } - if net.ParseIP(ip).Equal(intf) { - return true, nil + return nil, err } + + localAddrs = append(localAddrs, ip) } - return false, nil + + return localAddrs, nil } // ShouldSkipService checks if a given service should skip proxying diff --git a/staging/repos_generated.bzl b/staging/repos_generated.bzl index ae154f5747e..77c9b555686 100644 --- a/staging/repos_generated.bzl +++ b/staging/repos_generated.bzl @@ -36,7 +36,6 @@ staging_repos = [ "k8s.io/kubelet", "k8s.io/legacy-cloud-providers", "k8s.io/metrics", - "k8s.io/node-api", "k8s.io/sample-apiserver", "k8s.io/sample-cli-plugin", "k8s.io/sample-controller", diff --git a/staging/src/BUILD b/staging/src/BUILD index 1624e17b527..e42197078aa 100644 --- a/staging/src/BUILD +++ b/staging/src/BUILD @@ -35,7 +35,6 @@ filegroup( "//staging/src/k8s.io/kubelet:all-srcs", "//staging/src/k8s.io/legacy-cloud-providers:all-srcs", "//staging/src/k8s.io/metrics:all-srcs", - "//staging/src/k8s.io/node-api:all-srcs", "//staging/src/k8s.io/sample-apiserver:all-srcs", "//staging/src/k8s.io/sample-cli-plugin:all-srcs", "//staging/src/k8s.io/sample-controller:all-srcs", diff --git a/staging/src/k8s.io/node-api/BUILD b/staging/src/k8s.io/node-api/BUILD deleted file mode 100644 index 6df04e38cd7..00000000000 --- a/staging/src/k8s.io/node-api/BUILD +++ /dev/null @@ -1,13 +0,0 @@ -filegroup( - name = "package-srcs", - srcs = glob(["**"]), - tags = ["automanaged"], - visibility = ["//visibility:private"], -) - -filegroup( - name = "all-srcs", - srcs = [":package-srcs"], - tags = ["automanaged"], - visibility = ["//visibility:public"], -) diff --git a/staging/src/k8s.io/node-api/go.mod b/staging/src/k8s.io/node-api/go.mod deleted file mode 100644 index 3b52a6665ed..00000000000 --- a/staging/src/k8s.io/node-api/go.mod +++ /dev/null @@ -1,7 +0,0 @@ -// This is a generated file. Do not edit directly. - -module k8s.io/node-api - -go 1.13 - -replace k8s.io/node-api => ../node-api diff --git a/staging/src/k8s.io/node-api/go.sum b/staging/src/k8s.io/node-api/go.sum deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/vendor/k8s.io/node-api b/vendor/k8s.io/node-api deleted file mode 120000 index 61fba3899f4..00000000000 --- a/vendor/k8s.io/node-api +++ /dev/null @@ -1 +0,0 @@ -../../staging/src/k8s.io/node-api \ No newline at end of file