Skip to content

Commit 126bf5a

Browse files
committed
ipvs proxier: use util proxy methods for getting local addresses
Signed-off-by: Andrew Sy Kim <[email protected]>
1 parent 313c3b8 commit 126bf5a

File tree

1 file changed

+8
-32
lines changed

1 file changed

+8
-32
lines changed

pkg/proxy/ipvs/proxier.go

Lines changed: 8 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -990,33 +990,6 @@ func (proxier *Proxier) OnNodeSynced() {
990990
// EntryInvalidErr indicates if an ipset entry is invalid or not
991991
const EntryInvalidErr = "error adding entry %s to ipset %s"
992992

993-
func getLocalAddrs() ([]net.IP, error) {
994-
var localAddrs []net.IP
995-
996-
addrs, err := net.InterfaceAddrs()
997-
if err != nil {
998-
return nil, err
999-
}
1000-
1001-
for _, addr := range addrs {
1002-
ip, _, err := net.ParseCIDR(addr.String())
1003-
if err != nil {
1004-
return nil, err
1005-
}
1006-
localAddrs = append(localAddrs, ip)
1007-
}
1008-
return localAddrs, nil
1009-
}
1010-
1011-
func ipExists(ip net.IP, addrs []net.IP) bool {
1012-
for _, addr := range addrs {
1013-
if ip.Equal(addr) {
1014-
return true
1015-
}
1016-
}
1017-
return false
1018-
}
1019-
1020993
// This is where all of the ipvs calls happen.
1021994
// assumes proxier.mu is held
1022995
func (proxier *Proxier) syncProxyRules() {
@@ -1036,9 +1009,11 @@ func (proxier *Proxier) syncProxyRules() {
10361009
klog.V(4).Infof("syncProxyRules took %v", time.Since(start))
10371010
}()
10381011

1039-
localAddrs, err := getLocalAddrs()
1012+
localAddrs, err := utilproxy.GetLocalAddrs()
10401013
if err != nil {
1041-
klog.Errorf("Failed to get local addresses during proxy sync: %v", err)
1014+
klog.Errorf("Failed to get local addresses during proxy sync: %v, assuming external IPs are not local", err)
1015+
} else if len(localAddrs) == 0 {
1016+
klog.Warning("No local addresses found, assuming all external IPs are not local")
10421017
}
10431018

10441019
// We assume that if this was called, we really want to sync them,
@@ -1222,9 +1197,10 @@ func (proxier *Proxier) syncProxyRules() {
12221197

12231198
// Capture externalIPs.
12241199
for _, externalIP := range svcInfo.ExternalIPStrings() {
1225-
if len(localAddrs) == 0 {
1226-
klog.Errorf("couldn't find any local IPs, assuming %s is not local", externalIP)
1227-
} else if (svcInfo.Protocol() != v1.ProtocolSCTP) && ipExists(net.ParseIP(externalIP), localAddrs) {
1200+
// If the "external" IP happens to be an IP that is local to this
1201+
// machine, hold the local port open so no other process can open it
1202+
// (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)) {
12281204
// We do not start listening on SCTP ports, according to our agreement in the SCTP support KEP
12291205
lp := utilproxy.LocalPort{
12301206
Description: "externalIP for " + svcNameString,

0 commit comments

Comments
 (0)