@@ -990,33 +990,6 @@ func (proxier *Proxier) OnNodeSynced() {
990
990
// EntryInvalidErr indicates if an ipset entry is invalid or not
991
991
const EntryInvalidErr = "error adding entry %s to ipset %s"
992
992
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
-
1020
993
// This is where all of the ipvs calls happen.
1021
994
// assumes proxier.mu is held
1022
995
func (proxier * Proxier ) syncProxyRules () {
@@ -1036,11 +1009,16 @@ func (proxier *Proxier) syncProxyRules() {
1036
1009
klog .V (4 ).Infof ("syncProxyRules took %v" , time .Since (start ))
1037
1010
}()
1038
1011
1039
- localAddrs , err := getLocalAddrs ()
1012
+ localAddrs , err := utilproxy . GetLocalAddrs ()
1040
1013
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" )
1042
1017
}
1043
1018
1019
+ localAddrSet := utilnet.IPSet {}
1020
+ localAddrSet .Insert (localAddrs ... )
1021
+
1044
1022
// We assume that if this was called, we really want to sync them,
1045
1023
// even if nothing changed in the meantime. In other words, callers are
1046
1024
// responsible for detecting no-op changes and not calling this function.
@@ -1222,9 +1200,10 @@ func (proxier *Proxier) syncProxyRules() {
1222
1200
1223
1201
// Capture externalIPs.
1224
1202
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 ) {
1203
+ // If the "external" IP happens to be an IP that is local to this
1204
+ // machine, hold the local port open so no other process can open it
1205
+ // (because the socket might open but it would never work).
1206
+ if localAddrSet .Len () > 0 && (svcInfo .Protocol () != v1 .ProtocolSCTP ) && localAddrSet .Has (net .ParseIP (externalIP )) {
1228
1207
// We do not start listening on SCTP ports, according to our agreement in the SCTP support KEP
1229
1208
lp := utilproxy.LocalPort {
1230
1209
Description : "externalIP for " + svcNameString ,
0 commit comments