From 126bf5a231c95eee4d2a2379d369dd4c779298fd Mon Sep 17 00:00:00 2001 From: Andrew Sy Kim Date: Wed, 27 Nov 2019 13:51:37 -0500 Subject: [PATCH] ipvs proxier: use util proxy methods for getting local addresses Signed-off-by: Andrew Sy Kim --- pkg/proxy/ipvs/proxier.go | 40 ++++++++------------------------------- 1 file changed, 8 insertions(+), 32 deletions(-) diff --git a/pkg/proxy/ipvs/proxier.go b/pkg/proxy/ipvs/proxier.go index 83c9a01b3b1..f16f031390a 100644 --- a/pkg/proxy/ipvs/proxier.go +++ b/pkg/proxy/ipvs/proxier.go @@ -990,33 +990,6 @@ func (proxier *Proxier) OnNodeSynced() { // EntryInvalidErr indicates if an ipset entry is invalid or not const EntryInvalidErr = "error adding entry %s to ipset %s" -func getLocalAddrs() ([]net.IP, error) { - var localAddrs []net.IP - - addrs, err := net.InterfaceAddrs() - if err != nil { - return nil, err - } - - for _, addr := range addrs { - ip, _, err := net.ParseCIDR(addr.String()) - if err != nil { - return nil, err - } - localAddrs = append(localAddrs, ip) - } - return localAddrs, nil -} - -func ipExists(ip net.IP, addrs []net.IP) bool { - for _, addr := range addrs { - if ip.Equal(addr) { - return true - } - } - return false -} - // This is where all of the ipvs calls happen. // assumes proxier.mu is held func (proxier *Proxier) syncProxyRules() { @@ -1036,9 +1009,11 @@ func (proxier *Proxier) syncProxyRules() { klog.V(4).Infof("syncProxyRules took %v", time.Since(start)) }() - localAddrs, err := getLocalAddrs() + localAddrs, err := utilproxy.GetLocalAddrs() if err != nil { - klog.Errorf("Failed to get local addresses during proxy sync: %v", err) + klog.Errorf("Failed to get local addresses during proxy sync: %v, assuming external IPs are not local", err) + } else if len(localAddrs) == 0 { + klog.Warning("No local addresses found, assuming all external IPs are not local") } // We assume that if this was called, we really want to sync them, @@ -1222,9 +1197,10 @@ func (proxier *Proxier) syncProxyRules() { // Capture externalIPs. for _, externalIP := range svcInfo.ExternalIPStrings() { - if len(localAddrs) == 0 { - klog.Errorf("couldn't find any local IPs, assuming %s is not local", externalIP) - } else if (svcInfo.Protocol() != v1.ProtocolSCTP) && ipExists(net.ParseIP(externalIP), localAddrs) { + // 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)) { // We do not start listening on SCTP ports, according to our agreement in the SCTP support KEP lp := utilproxy.LocalPort{ Description: "externalIP for " + svcNameString,