Kube-proxy/ipvs; Use go "net" lib to get nodeIPs
The nodeIPs to be used for nodePorts were collected using netlink which was unnecessary complex and caused se #93858
This commit is contained in:
@@ -18,6 +18,7 @@ package testing
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"k8s.io/utils/net"
|
||||
|
||||
"k8s.io/apimachinery/pkg/util/sets"
|
||||
)
|
||||
@@ -27,6 +28,8 @@ type FakeNetlinkHandle struct {
|
||||
// localAddresses is a network interface name to all of its IP addresses map, e.g.
|
||||
// eth0 -> [1.2.3.4, 10.20.30.40]
|
||||
localAddresses map[string][]string
|
||||
|
||||
IsIPv6 bool
|
||||
}
|
||||
|
||||
// NewFakeNetlinkHandle will create a new FakeNetlinkHandle
|
||||
@@ -112,23 +115,25 @@ func (h *FakeNetlinkHandle) ListBindAddress(devName string) ([]string, error) {
|
||||
}
|
||||
|
||||
// GetLocalAddresses is a mock implementation
|
||||
func (h *FakeNetlinkHandle) GetLocalAddresses(dev, filterDev string) (sets.String, error) {
|
||||
func (h *FakeNetlinkHandle) GetLocalAddresses(dev string) (sets.String, error) {
|
||||
res := sets.NewString()
|
||||
if len(dev) != 0 {
|
||||
// list all addresses from a given network interface.
|
||||
for _, addr := range h.localAddresses[dev] {
|
||||
// list all addresses from a given network interface.
|
||||
for _, addr := range h.localAddresses[dev] {
|
||||
if h.isValidForSet(addr) {
|
||||
res.Insert(addr)
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
// If filterDev is not given, will list all addresses from all available network interface.
|
||||
return res, nil
|
||||
}
|
||||
func (h *FakeNetlinkHandle) GetAllLocalAddresses() (sets.String, error) {
|
||||
res := sets.NewString()
|
||||
// List all addresses from all available network interfaces.
|
||||
for linkName := range h.localAddresses {
|
||||
if linkName == filterDev {
|
||||
continue
|
||||
}
|
||||
// list all addresses from a given network interface.
|
||||
for _, addr := range h.localAddresses[linkName] {
|
||||
res.Insert(addr)
|
||||
if h.isValidForSet(addr) {
|
||||
res.Insert(addr)
|
||||
}
|
||||
}
|
||||
}
|
||||
return res, nil
|
||||
@@ -146,3 +151,17 @@ func (h *FakeNetlinkHandle) SetLocalAddresses(dev string, ips ...string) error {
|
||||
h.localAddresses[dev] = append(h.localAddresses[dev], ips...)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (h *FakeNetlinkHandle) isValidForSet(ipString string) bool {
|
||||
ip := net.ParseIPSloppy(ipString)
|
||||
if h.IsIPv6 != (ip.To4() == nil) {
|
||||
return false
|
||||
}
|
||||
if h.IsIPv6 && ip.IsLinkLocalUnicast() {
|
||||
return false
|
||||
}
|
||||
if ip.IsLoopback() {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
@@ -27,22 +27,22 @@ func TestSetGetLocalAddresses(t *testing.T) {
|
||||
fake := NewFakeNetlinkHandle()
|
||||
fake.SetLocalAddresses("eth0", "1.2.3.4")
|
||||
expected := sets.NewString("1.2.3.4")
|
||||
addr, _ := fake.GetLocalAddresses("eth0", "")
|
||||
addr, _ := fake.GetLocalAddresses("eth0")
|
||||
if !reflect.DeepEqual(expected, addr) {
|
||||
t.Errorf("Unexpected mismatch, expected: %v, got: %v", expected, addr)
|
||||
}
|
||||
list, _ := fake.GetLocalAddresses("", "")
|
||||
list, _ := fake.GetAllLocalAddresses()
|
||||
if !reflect.DeepEqual(expected, list) {
|
||||
t.Errorf("Unexpected mismatch, expected: %v, got: %v", expected, list)
|
||||
}
|
||||
fake.SetLocalAddresses("lo", "127.0.0.1")
|
||||
expected = sets.NewString("127.0.0.1")
|
||||
addr, _ = fake.GetLocalAddresses("lo", "")
|
||||
expected = sets.NewString()
|
||||
addr, _ = fake.GetLocalAddresses("lo")
|
||||
if !reflect.DeepEqual(expected, addr) {
|
||||
t.Errorf("Unexpected mismatch, expected: %v, got: %v", expected, addr)
|
||||
}
|
||||
list, _ = fake.GetLocalAddresses("", "")
|
||||
expected = sets.NewString("1.2.3.4", "127.0.0.1")
|
||||
list, _ = fake.GetAllLocalAddresses()
|
||||
expected = sets.NewString("1.2.3.4")
|
||||
if !reflect.DeepEqual(expected, list) {
|
||||
t.Errorf("Unexpected mismatch, expected: %v, got: %v", expected, list)
|
||||
}
|
||||
|
Reference in New Issue
Block a user