diff --git a/pkg/proxy/ipvs/proxier.go b/pkg/proxy/ipvs/proxier.go index 6618447ec9c..2d8796ae480 100644 --- a/pkg/proxy/ipvs/proxier.go +++ b/pkg/proxy/ipvs/proxier.go @@ -300,7 +300,8 @@ type realIPGetter struct { // 172.17.0.1 dev docker0 scope host src 172.17.0.1 // 192.168.122.1 dev virbr0 scope host src 192.168.122.1 // Then filter out dev==kube-ipvs0, and cut the unique src IP fields, -// Node IP set: [100.106.89.164, 127.0.0.1, 172.17.0.1, 192.168.122.1] +// Node IP set: [100.106.89.164, 172.17.0.1, 192.168.122.1] +// Note that loopback addresses are excluded. func (r *realIPGetter) NodeIPs() (ips []net.IP, err error) { // Pass in empty filter device name for list all LOCAL type addresses. nodeAddress, err := r.nl.GetLocalAddresses("", DefaultDummyDevice) @@ -309,7 +310,11 @@ func (r *realIPGetter) NodeIPs() (ips []net.IP, err error) { } // translate ip string to IP for _, ipStr := range nodeAddress.UnsortedList() { - ips = append(ips, net.ParseIP(ipStr)) + a := net.ParseIP(ipStr) + if a.IsLoopback() { + continue + } + ips = append(ips, a) } return ips, nil } @@ -1131,6 +1136,10 @@ func (proxier *Proxier) syncProxyRules() { } else { nodeAddresses = nodeAddrSet.List() for _, address := range nodeAddresses { + a := net.ParseIP(address) + if a.IsLoopback() { + continue + } if utilproxy.IsZeroCIDR(address) { nodeIPs, err = proxier.ipGetter.NodeIPs() if err != nil { @@ -1138,7 +1147,7 @@ func (proxier *Proxier) syncProxyRules() { } break } - nodeIPs = append(nodeIPs, net.ParseIP(address)) + nodeIPs = append(nodeIPs, a) } } } diff --git a/pkg/proxy/ipvs/proxier_test.go b/pkg/proxy/ipvs/proxier_test.go index ac20a461b9d..5b2654cc763 100644 --- a/pkg/proxy/ipvs/proxier_test.go +++ b/pkg/proxy/ipvs/proxier_test.go @@ -393,12 +393,12 @@ func TestGetNodeIPs(t *testing.T) { // case 0 { devAddresses: map[string][]string{"eth0": {"1.2.3.4"}, "lo": {"127.0.0.1"}}, - expectIPs: []string{"1.2.3.4", "127.0.0.1"}, + expectIPs: []string{"1.2.3.4"}, }, // case 1 { devAddresses: map[string][]string{"lo": {"127.0.0.1"}}, - expectIPs: []string{"127.0.0.1"}, + expectIPs: []string{}, }, // case 2 { @@ -408,22 +408,22 @@ func TestGetNodeIPs(t *testing.T) { // case 3 { devAddresses: map[string][]string{"encap0": {"10.20.30.40"}, "lo": {"127.0.0.1"}, "docker0": {"172.17.0.1"}}, - expectIPs: []string{"10.20.30.40", "127.0.0.1", "172.17.0.1"}, + expectIPs: []string{"10.20.30.40", "172.17.0.1"}, }, // case 4 { devAddresses: map[string][]string{"encaps9": {"10.20.30.40"}, "lo": {"127.0.0.1"}, "encap7": {"10.20.30.31"}}, - expectIPs: []string{"10.20.30.40", "127.0.0.1", "10.20.30.31"}, + expectIPs: []string{"10.20.30.40", "10.20.30.31"}, }, // case 5 { devAddresses: map[string][]string{"kube-ipvs0": {"1.2.3.4"}, "lo": {"127.0.0.1"}, "encap7": {"10.20.30.31"}}, - expectIPs: []string{"127.0.0.1", "10.20.30.31"}, + expectIPs: []string{"10.20.30.31"}, }, // case 6 { devAddresses: map[string][]string{"kube-ipvs0": {"1.2.3.4", "2.3.4.5"}, "lo": {"127.0.0.1"}}, - expectIPs: []string{"127.0.0.1"}, + expectIPs: []string{}, }, // case 7 { @@ -433,12 +433,12 @@ func TestGetNodeIPs(t *testing.T) { // case 8 { devAddresses: map[string][]string{"kube-ipvs0": {"1.2.3.4", "2.3.4.5"}, "eth5": {"3.4.5.6"}, "lo": {"127.0.0.1"}}, - expectIPs: []string{"127.0.0.1", "3.4.5.6"}, + expectIPs: []string{"3.4.5.6"}, }, // case 9 { devAddresses: map[string][]string{"ipvs0": {"1.2.3.4"}, "lo": {"127.0.0.1"}, "encap7": {"10.20.30.31"}}, - expectIPs: []string{"127.0.0.1", "10.20.30.31", "1.2.3.4"}, + expectIPs: []string{"10.20.30.31", "1.2.3.4"}, }, }