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 <kiman@vmware.com>
This commit is contained in:
Andrew Sy Kim
2019-11-27 13:55:04 -05:00
committed by andrewsykim
parent 77feb1126e
commit 1653476e3f
12 changed files with 30 additions and 41 deletions

View File

@@ -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