Update unit-tests

This commit is contained in:
Lars Ekman 2023-02-19 18:14:38 +01:00
parent 32f8066119
commit 6ad09dc418
3 changed files with 90 additions and 212 deletions

View File

@ -62,19 +62,6 @@ import (
const testHostname = "test-hostname" const testHostname = "test-hostname"
type fakeIPGetter struct {
nodeIPs []net.IP
bindedIPs sets.String
}
func (f *fakeIPGetter) NodeIPs() ([]net.IP, error) {
return f.nodeIPs, nil
}
func (f *fakeIPGetter) BindedIPs() (sets.String, error) {
return f.bindedIPs, nil
}
// fakeIpvs implements utilipvs.Interface // fakeIpvs implements utilipvs.Interface
type fakeIpvs struct { type fakeIpvs struct {
ipvsErr string ipvsErr string
@ -139,21 +126,10 @@ func (fake *fakeIPSetVersioner) GetVersion() (string, error) {
return fake.version, fake.err return fake.version, fake.err
} }
func NewFakeProxier(ipt utiliptables.Interface, ipvs utilipvs.Interface, ipset utilipset.Interface, nodeIPs []net.IP, excludeCIDRs []*net.IPNet, ipFamily v1.IPFamily) *Proxier { func NewFakeProxier(ipt utiliptables.Interface, ipvs utilipvs.Interface, ipset utilipset.Interface, nodeIPs []string, excludeCIDRs []*net.IPNet, ipFamily v1.IPFamily) *Proxier {
// unlike actual proxier, this fake proxier does not filter node IPs per family requested
// which can lead to false postives.
// filter node IPs by proxier ipfamily netlinkHandle := netlinktest.NewFakeNetlinkHandle(ipFamily == v1.IPv6Protocol)
idx := 0 netlinkHandle.SetLocalAddresses("eth0", nodeIPs...)
for _, nodeIP := range nodeIPs {
if (ipFamily == v1.IPv6Protocol) == netutils.IsIPv6(nodeIP) {
nodeIPs[idx] = nodeIP
idx++
}
}
// reset slice to filtered entries
nodeIPs = nodeIPs[:idx]
fcmd := fakeexec.FakeCmd{ fcmd := fakeexec.FakeCmd{
CombinedOutputScript: []fakeexec.FakeAction{ CombinedOutputScript: []fakeexec.FakeAction{
@ -188,14 +164,13 @@ func NewFakeProxier(ipt utiliptables.Interface, ipvs utilipvs.Interface, ipset u
hostname: testHostname, hostname: testHostname,
serviceHealthServer: healthcheck.NewFakeServiceHealthServer(), serviceHealthServer: healthcheck.NewFakeServiceHealthServer(),
ipvsScheduler: defaultScheduler, ipvsScheduler: defaultScheduler,
ipGetter: &fakeIPGetter{nodeIPs: nodeIPs},
iptablesData: bytes.NewBuffer(nil), iptablesData: bytes.NewBuffer(nil),
filterChainsData: bytes.NewBuffer(nil), filterChainsData: bytes.NewBuffer(nil),
natChains: utilproxy.LineBuffer{}, natChains: utilproxy.LineBuffer{},
natRules: utilproxy.LineBuffer{}, natRules: utilproxy.LineBuffer{},
filterChains: utilproxy.LineBuffer{}, filterChains: utilproxy.LineBuffer{},
filterRules: utilproxy.LineBuffer{}, filterRules: utilproxy.LineBuffer{},
netlinkHandle: netlinktest.NewFakeNetlinkHandle(), netlinkHandle: netlinkHandle,
ipsetList: ipsetList, ipsetList: ipsetList,
nodePortAddresses: make([]string, 0), nodePortAddresses: make([]string, 0),
networkInterfacer: proxyutiltest.NewFakeNetwork(), networkInterfacer: proxyutiltest.NewFakeNetwork(),
@ -438,23 +413,22 @@ func TestGetNodeIPs(t *testing.T) {
}, },
} }
for i := range testCases { for i, tc := range testCases {
fake := netlinktest.NewFakeNetlinkHandle() fake := netlinktest.NewFakeNetlinkHandle(tc.isIPv6)
fake.IsIPv6 = testCases[i].isIPv6
for dev, addresses := range testCases[i].devAddresses { for dev, addresses := range testCases[i].devAddresses {
fake.SetLocalAddresses(dev, addresses...) fake.SetLocalAddresses(dev, addresses...)
} }
r := realIPGetter{nl: fake} ips, err := fake.GetAllLocalAddresses()
ips, err := r.NodeIPs()
if err != nil { if err != nil {
t.Errorf("Unexpected error: %v", err) t.Errorf("Unexpected error: %v", err)
} }
ipStrs := sets.NewString() devIps, err := fake.GetLocalAddresses("kube-ipvs0")
for _, ip := range ips { if err != nil {
ipStrs.Insert(ip.String()) t.Errorf("Unexpected error: %v", err)
} }
if !ipStrs.Equal(sets.NewString(testCases[i].expectIPs...)) { ips = ips.Difference(devIps)
t.Errorf("case[%d], unexpected mismatch, expected: %v, got: %v", i, testCases[i].expectIPs, ips) if !ips.Equal(sets.New(tc.expectIPs...)) {
t.Errorf("case[%d], unexpected mismatch, expected: %v, got: %v", i, tc.expectIPs, ips)
} }
} }
} }
@ -467,7 +441,7 @@ func TestNodePortIPv4(t *testing.T) {
name string name string
services []*v1.Service services []*v1.Service
endpoints []*discovery.EndpointSlice endpoints []*discovery.EndpointSlice
nodeIPs []net.IP nodeIPs []string
nodePortAddresses []string nodePortAddresses []string
expectedIPVS *ipvstest.FakeIPVS expectedIPVS *ipvstest.FakeIPVS
expectedIPSets netlinktest.ExpectedIPSet expectedIPSets netlinktest.ExpectedIPSet
@ -511,10 +485,7 @@ func TestNodePortIPv4(t *testing.T) {
}} }}
}), }),
}, },
nodeIPs: []net.IP{ nodeIPs: []string{"100.101.102.103", "2001:db8::1:1"},
netutils.ParseIPSloppy("100.101.102.103"),
netutils.ParseIPSloppy("2001:db8::1:1"),
},
nodePortAddresses: []string{}, nodePortAddresses: []string{},
expectedIPVS: &ipvstest.FakeIPVS{ expectedIPVS: &ipvstest.FakeIPVS{
Services: map[ipvstest.ServiceKey]*utilipvs.VirtualServer{ Services: map[ipvstest.ServiceKey]*utilipvs.VirtualServer{
@ -592,9 +563,7 @@ func TestNodePortIPv4(t *testing.T) {
}} }}
}), }),
}, },
nodeIPs: []net.IP{ nodeIPs: []string{"100.101.102.103"},
netutils.ParseIPSloppy("100.101.102.103"),
},
nodePortAddresses: []string{"0.0.0.0/0"}, nodePortAddresses: []string{"0.0.0.0/0"},
expectedIPVS: &ipvstest.FakeIPVS{ expectedIPVS: &ipvstest.FakeIPVS{
Services: map[ipvstest.ServiceKey]*utilipvs.VirtualServer{ Services: map[ipvstest.ServiceKey]*utilipvs.VirtualServer{
@ -682,10 +651,8 @@ func TestNodePortIPv4(t *testing.T) {
}} }}
}), }),
}, },
endpoints: []*discovery.EndpointSlice{}, endpoints: []*discovery.EndpointSlice{},
nodeIPs: []net.IP{ nodeIPs: []string{"100.101.102.103"},
netutils.ParseIPSloppy("100.101.102.103"),
},
nodePortAddresses: []string{}, nodePortAddresses: []string{},
expectedIPVS: &ipvstest.FakeIPVS{ expectedIPVS: &ipvstest.FakeIPVS{
Services: map[ipvstest.ServiceKey]*utilipvs.VirtualServer{ Services: map[ipvstest.ServiceKey]*utilipvs.VirtualServer{
@ -751,13 +718,13 @@ func TestNodePortIPv4(t *testing.T) {
}} }}
}), }),
}, },
nodeIPs: []net.IP{ nodeIPs: []string{
netutils.ParseIPSloppy("100.101.102.103"), "100.101.102.103",
netutils.ParseIPSloppy("100.101.102.104"), "100.101.102.104",
netutils.ParseIPSloppy("100.101.102.105"), "100.101.102.105",
netutils.ParseIPSloppy("2001:db8::1:1"), "2001:db8::1:1",
netutils.ParseIPSloppy("2001:db8::1:2"), "2001:db8::1:2",
netutils.ParseIPSloppy("2001:db8::1:3"), "2001:db8::1:3",
}, },
nodePortAddresses: []string{}, nodePortAddresses: []string{},
expectedIPVS: &ipvstest.FakeIPVS{ expectedIPVS: &ipvstest.FakeIPVS{
@ -905,9 +872,7 @@ func TestNodePortIPv4(t *testing.T) {
}} }}
}), }),
}, },
nodeIPs: []net.IP{ nodeIPs: []string{"100.101.102.103"},
netutils.ParseIPSloppy("100.101.102.103"),
},
nodePortAddresses: []string{}, nodePortAddresses: []string{},
expectedIPVS: &ipvstest.FakeIPVS{ expectedIPVS: &ipvstest.FakeIPVS{
Services: map[ipvstest.ServiceKey]*utilipvs.VirtualServer{ Services: map[ipvstest.ServiceKey]*utilipvs.VirtualServer{
@ -1030,7 +995,7 @@ func TestNodePortIPv6(t *testing.T) {
name string name string
services []*v1.Service services []*v1.Service
endpoints []*discovery.EndpointSlice endpoints []*discovery.EndpointSlice
nodeIPs []net.IP nodeIPs []string
nodePortAddresses []string nodePortAddresses []string
expectedIPVS *ipvstest.FakeIPVS expectedIPVS *ipvstest.FakeIPVS
expectedIPSets netlinktest.ExpectedIPSet expectedIPSets netlinktest.ExpectedIPSet
@ -1074,10 +1039,7 @@ func TestNodePortIPv6(t *testing.T) {
}} }}
}), }),
}, },
nodeIPs: []net.IP{ nodeIPs: []string{"100.101.102.103", "2001:db8::1:1"},
netutils.ParseIPSloppy("100.101.102.103"),
netutils.ParseIPSloppy("2001:db8::1:1"),
},
nodePortAddresses: []string{}, nodePortAddresses: []string{},
expectedIPVS: &ipvstest.FakeIPVS{ expectedIPVS: &ipvstest.FakeIPVS{
Services: map[ipvstest.ServiceKey]*utilipvs.VirtualServer{ Services: map[ipvstest.ServiceKey]*utilipvs.VirtualServer{
@ -1157,9 +1119,7 @@ func TestNodePortIPv6(t *testing.T) {
}} }}
}), }),
}, },
nodeIPs: []net.IP{ nodeIPs: []string{"100.101.102.103"},
netutils.ParseIPSloppy("100.101.102.103"),
},
nodePortAddresses: []string{"0.0.0.0/0"}, nodePortAddresses: []string{"0.0.0.0/0"},
/*since this is a node with only IPv4, proxier should not do anything */ /*since this is a node with only IPv4, proxier should not do anything */
expectedIPVS: &ipvstest.FakeIPVS{ expectedIPVS: &ipvstest.FakeIPVS{
@ -1184,11 +1144,8 @@ func TestNodePortIPv6(t *testing.T) {
}} }}
}), }),
}, },
endpoints: []*discovery.EndpointSlice{}, endpoints: []*discovery.EndpointSlice{},
nodeIPs: []net.IP{ nodeIPs: []string{"100.101.102.103", "2001:db8::1:1"},
netutils.ParseIPSloppy("100.101.102.103"),
netutils.ParseIPSloppy("2001:db8::1:1"),
},
nodePortAddresses: []string{}, nodePortAddresses: []string{},
expectedIPVS: &ipvstest.FakeIPVS{ expectedIPVS: &ipvstest.FakeIPVS{
Services: map[ipvstest.ServiceKey]*utilipvs.VirtualServer{ Services: map[ipvstest.ServiceKey]*utilipvs.VirtualServer{
@ -1255,10 +1212,7 @@ func TestNodePortIPv6(t *testing.T) {
}} }}
}), }),
}, },
nodeIPs: []net.IP{ nodeIPs: []string{"2001:db8::1:1", "2001:db8::1:2"},
netutils.ParseIPSloppy("2001:db8::1:1"),
netutils.ParseIPSloppy("2001:db8::1:2"),
},
nodePortAddresses: []string{}, nodePortAddresses: []string{},
expectedIPVS: &ipvstest.FakeIPVS{ expectedIPVS: &ipvstest.FakeIPVS{
Services: map[ipvstest.ServiceKey]*utilipvs.VirtualServer{ Services: map[ipvstest.ServiceKey]*utilipvs.VirtualServer{
@ -2794,8 +2748,8 @@ func TestSessionAffinity(t *testing.T) {
ipt := iptablestest.NewFake() ipt := iptablestest.NewFake()
ipvs := ipvstest.NewFake() ipvs := ipvstest.NewFake()
ipset := ipsettest.NewFake(testIPSetVersion) ipset := ipsettest.NewFake(testIPSetVersion)
nodeIP := netutils.ParseIPSloppy("100.101.102.103") nodeIP := "100.101.102.103"
fp := NewFakeProxier(ipt, ipvs, ipset, []net.IP{nodeIP}, nil, v1.IPv4Protocol) fp := NewFakeProxier(ipt, ipvs, ipset, []string{nodeIP}, nil, v1.IPv4Protocol)
svcIP := "10.20.30.41" svcIP := "10.20.30.41"
svcPort := 80 svcPort := 80
svcNodePort := 3001 svcNodePort := 3001
@ -3706,11 +3660,11 @@ func compareEndpointsMaps(t *testing.T, tci int, newMap proxy.EndpointsMap, expe
func Test_syncService(t *testing.T) { func Test_syncService(t *testing.T) {
testCases := []struct { testCases := []struct {
oldVirtualServer *utilipvs.VirtualServer oldVirtualServer *utilipvs.VirtualServer
svcName string svcName string
newVirtualServer *utilipvs.VirtualServer newVirtualServer *utilipvs.VirtualServer
bindAddr bool bindAddr bool
bindedAddrs sets.String alreadyBoundAddrs sets.Set[string]
}{ }{
{ {
// case 0, old virtual server is same as new virtual server // case 0, old virtual server is same as new virtual server
@ -3729,8 +3683,8 @@ func Test_syncService(t *testing.T) {
Scheduler: "rr", Scheduler: "rr",
Flags: utilipvs.FlagHashed, Flags: utilipvs.FlagHashed,
}, },
bindAddr: false, bindAddr: false,
bindedAddrs: sets.NewString(), alreadyBoundAddrs: nil,
}, },
{ {
// case 1, old virtual server is different from new virtual server // case 1, old virtual server is different from new virtual server
@ -3749,8 +3703,8 @@ func Test_syncService(t *testing.T) {
Scheduler: "rr", Scheduler: "rr",
Flags: utilipvs.FlagPersistent, Flags: utilipvs.FlagPersistent,
}, },
bindAddr: false, bindAddr: false,
bindedAddrs: sets.NewString(), alreadyBoundAddrs: nil,
}, },
{ {
// case 2, old virtual server is different from new virtual server // case 2, old virtual server is different from new virtual server
@ -3769,8 +3723,8 @@ func Test_syncService(t *testing.T) {
Scheduler: "wlc", Scheduler: "wlc",
Flags: utilipvs.FlagHashed, Flags: utilipvs.FlagHashed,
}, },
bindAddr: false, bindAddr: false,
bindedAddrs: sets.NewString(), alreadyBoundAddrs: nil,
}, },
{ {
// case 3, old virtual server is nil, and create new virtual server // case 3, old virtual server is nil, and create new virtual server
@ -3783,8 +3737,8 @@ func Test_syncService(t *testing.T) {
Scheduler: "rr", Scheduler: "rr",
Flags: utilipvs.FlagHashed, Flags: utilipvs.FlagHashed,
}, },
bindAddr: true, bindAddr: true,
bindedAddrs: sets.NewString(), alreadyBoundAddrs: nil,
}, },
{ {
// case 4, SCTP, old virtual server is same as new virtual server // case 4, SCTP, old virtual server is same as new virtual server
@ -3803,8 +3757,8 @@ func Test_syncService(t *testing.T) {
Scheduler: "rr", Scheduler: "rr",
Flags: utilipvs.FlagHashed, Flags: utilipvs.FlagHashed,
}, },
bindAddr: false, bindAddr: false,
bindedAddrs: sets.NewString(), alreadyBoundAddrs: nil,
}, },
{ {
// case 5, old virtual server is different from new virtual server // case 5, old virtual server is different from new virtual server
@ -3823,8 +3777,8 @@ func Test_syncService(t *testing.T) {
Scheduler: "rr", Scheduler: "rr",
Flags: utilipvs.FlagPersistent, Flags: utilipvs.FlagPersistent,
}, },
bindAddr: false, bindAddr: false,
bindedAddrs: sets.NewString(), alreadyBoundAddrs: nil,
}, },
{ {
// case 6, old virtual server is different from new virtual server // case 6, old virtual server is different from new virtual server
@ -3843,8 +3797,8 @@ func Test_syncService(t *testing.T) {
Scheduler: "wlc", Scheduler: "wlc",
Flags: utilipvs.FlagHashed, Flags: utilipvs.FlagHashed,
}, },
bindAddr: false, bindAddr: false,
bindedAddrs: sets.NewString(), alreadyBoundAddrs: nil,
}, },
{ {
// case 7, old virtual server is nil, and create new virtual server // case 7, old virtual server is nil, and create new virtual server
@ -3857,8 +3811,8 @@ func Test_syncService(t *testing.T) {
Scheduler: "rr", Scheduler: "rr",
Flags: utilipvs.FlagHashed, Flags: utilipvs.FlagHashed,
}, },
bindAddr: true, bindAddr: true,
bindedAddrs: sets.NewString(), alreadyBoundAddrs: sets.New[string](),
}, },
{ {
// case 8, virtual server address already binded, skip sync // case 8, virtual server address already binded, skip sync
@ -3877,8 +3831,8 @@ func Test_syncService(t *testing.T) {
Scheduler: "rr", Scheduler: "rr",
Flags: utilipvs.FlagHashed, Flags: utilipvs.FlagHashed,
}, },
bindAddr: true, bindAddr: true,
bindedAddrs: sets.NewString("1.2.3.4"), alreadyBoundAddrs: sets.New("1.2.3.4"),
}, },
} }
@ -3894,7 +3848,7 @@ func Test_syncService(t *testing.T) {
t.Errorf("Case [%d], unexpected add IPVS virtual server error: %v", i, err) t.Errorf("Case [%d], unexpected add IPVS virtual server error: %v", i, err)
} }
} }
if err := proxier.syncService(testCases[i].svcName, testCases[i].newVirtualServer, testCases[i].bindAddr, testCases[i].bindedAddrs); err != nil { if err := proxier.syncService(testCases[i].svcName, testCases[i].newVirtualServer, testCases[i].bindAddr, testCases[i].alreadyBoundAddrs); err != nil {
t.Errorf("Case [%d], unexpected sync IPVS virtual server error: %v", i, err) t.Errorf("Case [%d], unexpected sync IPVS virtual server error: %v", i, err)
} }
// check // check
@ -4012,7 +3966,7 @@ func TestCleanLegacyService(t *testing.T) {
fp := NewFakeProxier(ipt, ipvs, ipset, nil, excludeCIDRs, v1.IPv4Protocol) fp := NewFakeProxier(ipt, ipvs, ipset, nil, excludeCIDRs, v1.IPv4Protocol)
// All ipvs services that were processed in the latest sync loop. // All ipvs services that were processed in the latest sync loop.
activeServices := map[string]bool{"ipvs0": true, "ipvs1": true} activeServices := sets.New("ipvs0", "ipvs1")
// All ipvs services in the system. // All ipvs services in the system.
currentServices := map[string]*utilipvs.VirtualServer{ currentServices := map[string]*utilipvs.VirtualServer{
// Created by kube-proxy. // Created by kube-proxy.
@ -4068,15 +4022,7 @@ func TestCleanLegacyService(t *testing.T) {
fp.ipvs.AddVirtualServer(currentServices[v]) fp.ipvs.AddVirtualServer(currentServices[v])
} }
fp.netlinkHandle.EnsureDummyDevice(defaultDummyDevice) fp.cleanLegacyService(activeServices, currentServices)
activeBindAddrs := map[string]bool{"1.1.1.1": true, "2.2.2.2": true, "3.3.3.3": true, "4.4.4.4": true}
// This is ipv4-only so ipv6 addresses should be ignored
currentBindAddrs := []string{"1.1.1.1", "2.2.2.2", "3.3.3.3", "4.4.4.4", "5.5.5.5", "6.6.6.6", "fd80::1:2:3", "fd80::1:2:4"}
for i := range currentBindAddrs {
fp.netlinkHandle.EnsureAddressBind(currentBindAddrs[i], defaultDummyDevice)
}
fp.cleanLegacyService(activeServices, currentServices, map[string]bool{"5.5.5.5": true, "6.6.6.6": true})
// ipvs4 and ipvs5 should have been cleaned. // ipvs4 and ipvs5 should have been cleaned.
remainingVirtualServers, _ := fp.ipvs.GetVirtualServers() remainingVirtualServers, _ := fp.ipvs.GetVirtualServers()
if len(remainingVirtualServers) != 4 { if len(remainingVirtualServers) != 4 {
@ -4091,24 +4037,6 @@ func TestCleanLegacyService(t *testing.T) {
t.Errorf("Expected ipvs5 to be removed after cleanup. It still remains") t.Errorf("Expected ipvs5 to be removed after cleanup. It still remains")
} }
} }
// Addresses 5.5.5.5 and 6.6.6.6 should not be bound any more, but the ipv6 addresses should remain
remainingAddrs, _ := fp.netlinkHandle.ListBindAddress(defaultDummyDevice)
if len(remainingAddrs) != 6 {
t.Errorf("Expected number of remaining bound addrs after cleanup to be %v. Got %v", 6, len(remainingAddrs))
}
// check that address "1.1.1.1", "2.2.2.2", "3.3.3.3", "4.4.4.4" are bound, ignore ipv6 addresses
remainingAddrsMap := make(map[string]bool)
for _, a := range remainingAddrs {
if netutils.ParseIPSloppy(a).To4() == nil {
continue
}
remainingAddrsMap[a] = true
}
if !reflect.DeepEqual(activeBindAddrs, remainingAddrsMap) {
t.Errorf("Expected remainingAddrsMap %v, got %v", activeBindAddrs, remainingAddrsMap)
}
} }
func TestCleanLegacyServiceWithRealServers(t *testing.T) { func TestCleanLegacyServiceWithRealServers(t *testing.T) {
@ -4118,7 +4046,7 @@ func TestCleanLegacyServiceWithRealServers(t *testing.T) {
fp := NewFakeProxier(ipt, ipvs, ipset, nil, nil, v1.IPv4Protocol) fp := NewFakeProxier(ipt, ipvs, ipset, nil, nil, v1.IPv4Protocol)
// all deleted expect ipvs2 // all deleted expect ipvs2
activeServices := map[string]bool{"ipvs2": true} activeServices := sets.New("ipvs2")
// All ipvs services in the system. // All ipvs services in the system.
currentServices := map[string]*utilipvs.VirtualServer{ currentServices := map[string]*utilipvs.VirtualServer{
"ipvs0": { // deleted with real servers "ipvs0": { // deleted with real servers
@ -4167,14 +4095,7 @@ func TestCleanLegacyServiceWithRealServers(t *testing.T) {
fp.ipvs.AddRealServer(v, r) fp.ipvs.AddRealServer(v, r)
} }
fp.netlinkHandle.EnsureDummyDevice(defaultDummyDevice) fp.cleanLegacyService(activeServices, currentServices)
activeBindAddrs := map[string]bool{"3.3.3.3": true}
currentBindAddrs := []string{"1.1.1.1", "2.2.2.2", "3.3.3.3"}
for i := range currentBindAddrs {
fp.netlinkHandle.EnsureAddressBind(currentBindAddrs[i], defaultDummyDevice)
}
fp.cleanLegacyService(activeServices, currentServices, map[string]bool{"1.1.1.1": true, "2.2.2.2": true})
remainingVirtualServers, _ := fp.ipvs.GetVirtualServers() remainingVirtualServers, _ := fp.ipvs.GetVirtualServers()
if len(remainingVirtualServers) != 1 { if len(remainingVirtualServers) != 1 {
t.Errorf("Expected number of remaining IPVS services after cleanup to be %v. Got %v", 1, len(remainingVirtualServers)) t.Errorf("Expected number of remaining IPVS services after cleanup to be %v. Got %v", 1, len(remainingVirtualServers))
@ -4185,23 +4106,6 @@ func TestCleanLegacyServiceWithRealServers(t *testing.T) {
t.Logf("expected virtual server: %v", currentServices["ipvs0"]) t.Logf("expected virtual server: %v", currentServices["ipvs0"])
t.Errorf("unexpected IPVS service") t.Errorf("unexpected IPVS service")
} }
remainingAddrs, _ := fp.netlinkHandle.ListBindAddress(defaultDummyDevice)
if len(remainingAddrs) != 1 {
t.Errorf("Expected number of remaining bound addrs after cleanup to be %v. Got %v", 1, len(remainingAddrs))
}
// check that address is "3.3.3.3"
remainingAddrsMap := make(map[string]bool)
for _, a := range remainingAddrs {
if netutils.ParseIPSloppy(a).To4() == nil {
continue
}
remainingAddrsMap[a] = true
}
if !reflect.DeepEqual(activeBindAddrs, remainingAddrsMap) {
t.Errorf("Expected remainingAddrsMap %v, got %v", activeBindAddrs, remainingAddrsMap)
}
} }
func TestCleanLegacyRealServersExcludeCIDRs(t *testing.T) { func TestCleanLegacyRealServersExcludeCIDRs(t *testing.T) {
@ -4245,11 +4149,7 @@ func TestCleanLegacyRealServersExcludeCIDRs(t *testing.T) {
fp.netlinkHandle.EnsureAddressBind("4.4.4.4", defaultDummyDevice) fp.netlinkHandle.EnsureAddressBind("4.4.4.4", defaultDummyDevice)
fp.cleanLegacyService( fp.cleanLegacyService(nil, map[string]*utilipvs.VirtualServer{"ipvs0": vs})
map[string]bool{},
map[string]*utilipvs.VirtualServer{"ipvs0": vs},
map[string]bool{"4.4.4.4": true},
)
fp.gracefuldeleteManager.tryDeleteRs() fp.gracefuldeleteManager.tryDeleteRs()
@ -4265,11 +4165,11 @@ func TestCleanLegacyService6(t *testing.T) {
ipvs := ipvstest.NewFake() ipvs := ipvstest.NewFake()
ipset := ipsettest.NewFake(testIPSetVersion) ipset := ipsettest.NewFake(testIPSetVersion)
excludeCIDRs, _ := netutils.ParseCIDRs([]string{"3000::/64", "4000::/64"}) excludeCIDRs, _ := netutils.ParseCIDRs([]string{"3000::/64", "4000::/64"})
fp := NewFakeProxier(ipt, ipvs, ipset, nil, excludeCIDRs, v1.IPv4Protocol) fp := NewFakeProxier(ipt, ipvs, ipset, nil, excludeCIDRs, v1.IPv6Protocol)
fp.nodeIP = netutils.ParseIPSloppy("::1") fp.nodeIP = netutils.ParseIPSloppy("::1")
// All ipvs services that were processed in the latest sync loop. // All ipvs services that were processed in the latest sync loop.
activeServices := map[string]bool{"ipvs0": true, "ipvs1": true} activeServices := sets.New("ipvs0", "ipvs1")
// All ipvs services in the system. // All ipvs services in the system.
currentServices := map[string]*utilipvs.VirtualServer{ currentServices := map[string]*utilipvs.VirtualServer{
// Created by kube-proxy. // Created by kube-proxy.
@ -4325,15 +4225,7 @@ func TestCleanLegacyService6(t *testing.T) {
fp.ipvs.AddVirtualServer(currentServices[v]) fp.ipvs.AddVirtualServer(currentServices[v])
} }
fp.netlinkHandle.EnsureDummyDevice(defaultDummyDevice) fp.cleanLegacyService(activeServices, currentServices)
activeBindAddrs := map[string]bool{"1000::1": true, "1000::2": true, "3000::1": true, "4000::1": true}
// This is ipv6-only so ipv4 addresses should be ignored
currentBindAddrs := []string{"1000::1", "1000::2", "3000::1", "4000::1", "5000::1", "1000::6", "1.1.1.1", "2.2.2.2"}
for i := range currentBindAddrs {
fp.netlinkHandle.EnsureAddressBind(currentBindAddrs[i], defaultDummyDevice)
}
fp.cleanLegacyService(activeServices, currentServices, map[string]bool{"5000::1": true, "1000::6": true})
// ipvs4 and ipvs5 should have been cleaned. // ipvs4 and ipvs5 should have been cleaned.
remainingVirtualServers, _ := fp.ipvs.GetVirtualServers() remainingVirtualServers, _ := fp.ipvs.GetVirtualServers()
if len(remainingVirtualServers) != 4 { if len(remainingVirtualServers) != 4 {
@ -4348,24 +4240,6 @@ func TestCleanLegacyService6(t *testing.T) {
t.Errorf("Expected ipvs5 to be removed after cleanup. It still remains") t.Errorf("Expected ipvs5 to be removed after cleanup. It still remains")
} }
} }
// Addresses 5000::1 and 1000::6 should not be bound any more, but the ipv4 addresses should remain
remainingAddrs, _ := fp.netlinkHandle.ListBindAddress(defaultDummyDevice)
if len(remainingAddrs) != 6 {
t.Errorf("Expected number of remaining bound addrs after cleanup to be %v. Got %v", 6, len(remainingAddrs))
}
// check that address "1000::1", "1000::2", "3000::1", "4000::1" are still bound, ignore ipv4 addresses
remainingAddrsMap := make(map[string]bool)
for _, a := range remainingAddrs {
if netutils.ParseIPSloppy(a).To4() != nil {
continue
}
remainingAddrsMap[a] = true
}
if !reflect.DeepEqual(activeBindAddrs, remainingAddrsMap) {
t.Errorf("Expected remainingAddrsMap %v, got %v", activeBindAddrs, remainingAddrsMap)
}
} }
func TestMultiPortServiceBindAddr(t *testing.T) { func TestMultiPortServiceBindAddr(t *testing.T) {
@ -6011,7 +5885,7 @@ func TestNoEndpointsMetric(t *testing.T) {
ipt := iptablestest.NewFake() ipt := iptablestest.NewFake()
ipvs := ipvstest.NewFake() ipvs := ipvstest.NewFake()
ipset := ipsettest.NewFake(testIPSetVersion) ipset := ipsettest.NewFake(testIPSetVersion)
fp := NewFakeProxier(ipt, ipvs, ipset, []net.IP{netutils.ParseIPSloppy("10.0.0.1")}, nil, v1.IPv4Protocol) fp := NewFakeProxier(ipt, ipvs, ipset, []string{"10.0.0.1"}, nil, v1.IPv4Protocol)
fp.servicesSynced = true fp.servicesSynced = true
// fp.endpointsSynced = true // fp.endpointsSynced = true
fp.endpointSlicesSynced = true fp.endpointSlicesSynced = true

View File

@ -33,9 +33,10 @@ type FakeNetlinkHandle struct {
} }
// NewFakeNetlinkHandle will create a new FakeNetlinkHandle // NewFakeNetlinkHandle will create a new FakeNetlinkHandle
func NewFakeNetlinkHandle() *FakeNetlinkHandle { func NewFakeNetlinkHandle(isIPv6 bool) *FakeNetlinkHandle {
fake := &FakeNetlinkHandle{ fake := &FakeNetlinkHandle{
localAddresses: make(map[string][]string), localAddresses: make(map[string][]string),
IsIPv6: isIPv6,
} }
return fake return fake
} }
@ -115,8 +116,8 @@ func (h *FakeNetlinkHandle) ListBindAddress(devName string) ([]string, error) {
} }
// GetLocalAddresses is a mock implementation // GetLocalAddresses is a mock implementation
func (h *FakeNetlinkHandle) GetLocalAddresses(dev string) (sets.String, error) { func (h *FakeNetlinkHandle) GetLocalAddresses(dev string) (sets.Set[string], error) {
res := sets.NewString() res := sets.New[string]()
// list all addresses from a given network interface. // list all addresses from a given network interface.
for _, addr := range h.localAddresses[dev] { for _, addr := range h.localAddresses[dev] {
if h.isValidForSet(addr) { if h.isValidForSet(addr) {
@ -125,8 +126,8 @@ func (h *FakeNetlinkHandle) GetLocalAddresses(dev string) (sets.String, error) {
} }
return res, nil return res, nil
} }
func (h *FakeNetlinkHandle) GetAllLocalAddresses() (sets.String, error) { func (h *FakeNetlinkHandle) GetAllLocalAddresses() (sets.Set[string], error) {
res := sets.NewString() res := sets.New[string]()
// List all addresses from all available network interfaces. // List all addresses from all available network interfaces.
for linkName := range h.localAddresses { for linkName := range h.localAddresses {
// list all addresses from a given network interface. // list all addresses from a given network interface.

View File

@ -17,33 +17,36 @@ limitations under the License.
package testing package testing
import ( import (
"reflect"
"testing" "testing"
"k8s.io/apimachinery/pkg/util/sets" "k8s.io/apimachinery/pkg/util/sets"
"k8s.io/kubernetes/pkg/proxy/ipvs"
) )
func TestSetGetLocalAddresses(t *testing.T) { func TestSetGetLocalAddresses(t *testing.T) {
fake := NewFakeNetlinkHandle() fake := NewFakeNetlinkHandle(false)
_ = ipvs.NetLinkHandle(fake) // Ensure that the interface is honored
fake.SetLocalAddresses("eth0", "1.2.3.4") fake.SetLocalAddresses("eth0", "1.2.3.4")
expected := sets.NewString("1.2.3.4") var expected, addr sets.Set[string]
addr, _ := fake.GetLocalAddresses("eth0") expected = sets.New("1.2.3.4")
if !reflect.DeepEqual(expected, addr) { addr, _ = fake.GetLocalAddresses("eth0")
if !addr.Equal(expected) {
t.Errorf("Unexpected mismatch, expected: %v, got: %v", expected, addr) t.Errorf("Unexpected mismatch, expected: %v, got: %v", expected, addr)
} }
list, _ := fake.GetAllLocalAddresses() addr, _ = fake.GetAllLocalAddresses()
if !reflect.DeepEqual(expected, list) { if !addr.Equal(expected) {
t.Errorf("Unexpected mismatch, expected: %v, got: %v", expected, list) t.Errorf("Unexpected mismatch, expected: %v, got: %v", expected, addr)
} }
fake.SetLocalAddresses("lo", "127.0.0.1") fake.SetLocalAddresses("lo", "127.0.0.1")
expected = sets.NewString() expected = nil
addr, _ = fake.GetLocalAddresses("lo") addr, _ = fake.GetLocalAddresses("lo")
if !reflect.DeepEqual(expected, addr) { if !addr.Equal(expected) {
t.Errorf("Unexpected mismatch, expected: %v, got: %v", expected, addr) t.Errorf("Unexpected mismatch, expected: %v, got: %v", expected, addr)
} }
list, _ = fake.GetAllLocalAddresses() fake.SetLocalAddresses("kube-ipvs0", "4.3.2.1")
expected = sets.NewString("1.2.3.4") addr, _ = fake.GetAllLocalAddresses()
if !reflect.DeepEqual(expected, list) { expected = sets.New("1.2.3.4", "4.3.2.1")
t.Errorf("Unexpected mismatch, expected: %v, got: %v", expected, list) if !addr.Equal(expected) {
t.Errorf("Unexpected mismatch, expected: %v, got: %v", expected, addr)
} }
} }