Skip to content

Commit 9d80e75

Browse files
authored
Merge pull request kubernetes#71911 from Nordix/issue-70113-2
Only handle addresses of the own ip family
2 parents 3ed107f + 39dc41d commit 9d80e75

File tree

2 files changed

+123
-7
lines changed

2 files changed

+123
-7
lines changed

pkg/proxy/ipvs/proxier.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1679,7 +1679,12 @@ func (proxier *Proxier) cleanLegacyService(activeServices map[string]bool, curre
16791679

16801680
func (proxier *Proxier) getLegacyBindAddr(activeBindAddrs map[string]bool, currentBindAddrs []string) map[string]bool {
16811681
legacyAddrs := make(map[string]bool)
1682+
isIpv6 := utilnet.IsIPv6(proxier.nodeIP)
16821683
for _, addr := range currentBindAddrs {
1684+
addrIsIpv6 := utilnet.IsIPv6(net.ParseIP(addr))
1685+
if addrIsIpv6 && !isIpv6 || !addrIsIpv6 && isIpv6 {
1686+
continue
1687+
}
16831688
if _, ok := activeBindAddrs[addr]; !ok {
16841689
legacyAddrs[addr] = true
16851690
}

pkg/proxy/ipvs/proxier_test.go

Lines changed: 118 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2869,7 +2869,8 @@ func TestCleanLegacyService(t *testing.T) {
28692869

28702870
fp.netlinkHandle.EnsureDummyDevice(DefaultDummyDevice)
28712871
activeBindAddrs := map[string]bool{"1.1.1.1": true, "2.2.2.2": true, "3.3.3.3": true, "4.4.4.4": true}
2872-
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"}
2872+
// This is ipv4-only so ipv6 addresses should be ignored
2873+
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"}
28732874
for i := range currentBindAddrs {
28742875
fp.netlinkHandle.EnsureAddressBind(currentBindAddrs[i], DefaultDummyDevice)
28752876
}
@@ -2890,15 +2891,125 @@ func TestCleanLegacyService(t *testing.T) {
28902891
}
28912892
}
28922893

2893-
// Addresses 5.5.5.5 and 6.6.6.6 should not be bound any more
2894+
// Addresses 5.5.5.5 and 6.6.6.6 should not be bound any more, but the ipv6 addresses should remain
28942895
remainingAddrs, _ := fp.netlinkHandle.ListBindAddress(DefaultDummyDevice)
2895-
if len(remainingAddrs) != 4 {
2896-
t.Errorf("Expected number of remaining bound addrs after cleanup to be %v. Got %v", 4, len(remainingAddrs))
2896+
if len(remainingAddrs) != 6 {
2897+
t.Errorf("Expected number of remaining bound addrs after cleanup to be %v. Got %v", 6, len(remainingAddrs))
28972898
}
2898-
// check that address "1.1.1.1", "2.2.2.2", "3.3.3.3", "4.4.4.4" are still bound
2899+
// check that address "1.1.1.1", "2.2.2.2", "3.3.3.3", "4.4.4.4" are bound, ignore ipv6 addresses
28992900
remainingAddrsMap := make(map[string]bool)
2900-
for i := range remainingAddrs {
2901-
remainingAddrsMap[remainingAddrs[i]] = true
2901+
for _, a := range remainingAddrs {
2902+
if net.ParseIP(a).To4() == nil {
2903+
continue
2904+
}
2905+
remainingAddrsMap[a] = true
2906+
}
2907+
if !reflect.DeepEqual(activeBindAddrs, remainingAddrsMap) {
2908+
t.Errorf("Expected remainingAddrsMap %v, got %v", activeBindAddrs, remainingAddrsMap)
2909+
}
2910+
2911+
}
2912+
2913+
func TestCleanLegacyService6(t *testing.T) {
2914+
ipt := iptablestest.NewFake()
2915+
ipvs := ipvstest.NewFake()
2916+
ipset := ipsettest.NewFake(testIPSetVersion)
2917+
fp := NewFakeProxier(ipt, ipvs, ipset, nil, []string{"3000::/64", "4000::/64"})
2918+
fp.nodeIP = net.ParseIP("::1")
2919+
2920+
// All ipvs services that were processed in the latest sync loop.
2921+
activeServices := map[string]bool{"ipvs0": true, "ipvs1": true}
2922+
// All ipvs services in the system.
2923+
currentServices := map[string]*utilipvs.VirtualServer{
2924+
// Created by kube-proxy.
2925+
"ipvs0": {
2926+
Address: net.ParseIP("1000::1"),
2927+
Protocol: string(v1.ProtocolUDP),
2928+
Port: 53,
2929+
Scheduler: "rr",
2930+
Flags: utilipvs.FlagHashed,
2931+
},
2932+
// Created by kube-proxy.
2933+
"ipvs1": {
2934+
Address: net.ParseIP("1000::2"),
2935+
Protocol: string(v1.ProtocolUDP),
2936+
Port: 54,
2937+
Scheduler: "rr",
2938+
Flags: utilipvs.FlagHashed,
2939+
},
2940+
// Created by an external party.
2941+
"ipvs2": {
2942+
Address: net.ParseIP("3000::1"),
2943+
Protocol: string(v1.ProtocolUDP),
2944+
Port: 55,
2945+
Scheduler: "rr",
2946+
Flags: utilipvs.FlagHashed,
2947+
},
2948+
// Created by an external party.
2949+
"ipvs3": {
2950+
Address: net.ParseIP("4000::1"),
2951+
Protocol: string(v1.ProtocolUDP),
2952+
Port: 56,
2953+
Scheduler: "rr",
2954+
Flags: utilipvs.FlagHashed,
2955+
},
2956+
// Created by an external party.
2957+
"ipvs4": {
2958+
Address: net.ParseIP("5000::1"),
2959+
Protocol: string(v1.ProtocolUDP),
2960+
Port: 57,
2961+
Scheduler: "rr",
2962+
Flags: utilipvs.FlagHashed,
2963+
},
2964+
// Created by kube-proxy, but now stale.
2965+
"ipvs5": {
2966+
Address: net.ParseIP("1000::6"),
2967+
Protocol: string(v1.ProtocolUDP),
2968+
Port: 58,
2969+
Scheduler: "rr",
2970+
Flags: utilipvs.FlagHashed,
2971+
},
2972+
}
2973+
for v := range currentServices {
2974+
fp.ipvs.AddVirtualServer(currentServices[v])
2975+
}
2976+
2977+
fp.netlinkHandle.EnsureDummyDevice(DefaultDummyDevice)
2978+
activeBindAddrs := map[string]bool{"1000::1": true, "1000::2": true, "3000::1": true, "4000::1": true}
2979+
// This is ipv6-only so ipv4 addresses should be ignored
2980+
currentBindAddrs := []string{"1000::1", "1000::2", "3000::1", "4000::1", "5000::1", "1000::6", "1.1.1.1", "2.2.2.2"}
2981+
for i := range currentBindAddrs {
2982+
fp.netlinkHandle.EnsureAddressBind(currentBindAddrs[i], DefaultDummyDevice)
2983+
}
2984+
2985+
fp.cleanLegacyService(activeServices, currentServices, map[string]bool{"5000::1": true, "1000::6": true})
2986+
// ipvs4 and ipvs5 should have been cleaned.
2987+
remainingVirtualServers, _ := fp.ipvs.GetVirtualServers()
2988+
if len(remainingVirtualServers) != 4 {
2989+
t.Errorf("Expected number of remaining IPVS services after cleanup to be %v. Got %v", 4, len(remainingVirtualServers))
2990+
}
2991+
for _, vs := range remainingVirtualServers {
2992+
// Checking that ipvs4 and ipvs5 were removed.
2993+
if vs.Port == 57 {
2994+
t.Errorf("Expected ipvs4 to be removed after cleanup. It still remains")
2995+
}
2996+
if vs.Port == 58 {
2997+
t.Errorf("Expected ipvs5 to be removed after cleanup. It still remains")
2998+
}
2999+
}
3000+
3001+
// Addresses 5000::1 and 1000::6 should not be bound any more, but the ipv4 addresses should remain
3002+
remainingAddrs, _ := fp.netlinkHandle.ListBindAddress(DefaultDummyDevice)
3003+
if len(remainingAddrs) != 6 {
3004+
t.Errorf("Expected number of remaining bound addrs after cleanup to be %v. Got %v", 6, len(remainingAddrs))
3005+
}
3006+
// check that address "1000::1", "1000::2", "3000::1", "4000::1" are still bound, ignore ipv4 addresses
3007+
remainingAddrsMap := make(map[string]bool)
3008+
for _, a := range remainingAddrs {
3009+
if net.ParseIP(a).To4() != nil {
3010+
continue
3011+
}
3012+
remainingAddrsMap[a] = true
29023013
}
29033014
if !reflect.DeepEqual(activeBindAddrs, remainingAddrsMap) {
29043015
t.Errorf("Expected remainingAddrsMap %v, got %v", activeBindAddrs, remainingAddrsMap)

0 commit comments

Comments
 (0)