Skip to content

Commit 707073d

Browse files
committed
Fixup #1 addressing review comments
1 parent 09d966c commit 707073d

File tree

5 files changed

+70
-60
lines changed

5 files changed

+70
-60
lines changed

pkg/proxy/iptables/proxier.go

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -294,14 +294,11 @@ func NewProxier(ipt utiliptables.Interface,
294294
ipFamily = v1.IPv6Protocol
295295
}
296296

297-
isCIDR := true
298-
ipFamilyIPMap := utilproxy.MapIPsToIPFamily(nodePortAddresses, isCIDR)
299-
nodePortAddresses = ipFamilyIPMap[ipFamily]
300-
for ipFam, ips := range ipFamilyIPMap {
301-
// Log the IPs not matching the ipFamily
302-
if ipFam != ipFamily && len(ips) > 0 {
303-
klog.Warningf("IP Family: %s, NodePortAddresses of wrong family; %s", ipFamily, strings.Join(ips, ","))
304-
}
297+
ipFamilyMap := utilproxy.MapCIDRsByIPFamily(nodePortAddresses)
298+
nodePortAddresses = ipFamilyMap[ipFamily]
299+
// Log the IPs not matching the ipFamily
300+
if ips, ok := ipFamilyMap[utilproxy.OtherIPFamily(ipFamily)]; ok && len(ips) > 0 {
301+
klog.Warningf("IP Family: %s, NodePortAddresses of wrong family; %s", ipFamily, strings.Join(ips, ","))
305302
}
306303

307304
proxier := &Proxier{
@@ -371,18 +368,17 @@ func NewDualStackProxier(
371368
nodePortAddresses []string,
372369
) (proxy.Provider, error) {
373370
// Create an ipv4 instance of the single-stack proxier
374-
isCIDR := true
375-
ipFamilyIPMap := utilproxy.MapIPsToIPFamily(nodePortAddresses, isCIDR)
371+
ipFamilyMap := utilproxy.MapCIDRsByIPFamily(nodePortAddresses)
376372
ipv4Proxier, err := NewProxier(ipt[0], sysctl,
377373
exec, syncPeriod, minSyncPeriod, masqueradeAll, masqueradeBit, localDetectors[0], hostname,
378-
nodeIP[0], recorder, healthzServer, ipFamilyIPMap[v1.IPv4Protocol])
374+
nodeIP[0], recorder, healthzServer, ipFamilyMap[v1.IPv4Protocol])
379375
if err != nil {
380376
return nil, fmt.Errorf("unable to create ipv4 proxier: %v", err)
381377
}
382378

383379
ipv6Proxier, err := NewProxier(ipt[1], sysctl,
384380
exec, syncPeriod, minSyncPeriod, masqueradeAll, masqueradeBit, localDetectors[1], hostname,
385-
nodeIP[1], recorder, healthzServer, ipFamilyIPMap[v1.IPv6Protocol])
381+
nodeIP[1], recorder, healthzServer, ipFamilyMap[v1.IPv6Protocol])
386382
if err != nil {
387383
return nil, fmt.Errorf("unable to create ipv6 proxier: %v", err)
388384
}

pkg/proxy/ipvs/proxier.go

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -450,15 +450,13 @@ func NewProxier(ipt utiliptables.Interface,
450450

451451
endpointSlicesEnabled := utilfeature.DefaultFeatureGate.Enabled(features.EndpointSliceProxying)
452452

453-
isCIDR := true
454-
ipFamilyIPMap := utilproxy.MapIPsToIPFamily(nodePortAddresses, isCIDR)
455-
nodePortAddresses = ipFamilyIPMap[ipFamily]
456-
for ipFam, ips := range ipFamilyIPMap {
457-
// Log the IPs not matching the ipFamily
458-
if ipFam != ipFamily && len(ips) > 0 {
459-
klog.Warningf("IP Family: %s, NodePortAddresses of wrong family; %s", ipFamily, strings.Join(ips, ","))
460-
}
453+
ipFamilyMap := utilproxy.MapCIDRsByIPFamily(nodePortAddresses)
454+
nodePortAddresses = ipFamilyMap[ipFamily]
455+
// Log the IPs not matching the ipFamily
456+
if ips, ok := ipFamilyMap[utilproxy.OtherIPFamily(ipFamily)]; ok && len(ips) > 0 {
457+
klog.Warningf("IP Family: %s, NodePortAddresses of wrong family; %s", ipFamily, strings.Join(ips, ","))
461458
}
459+
462460
proxier := &Proxier{
463461
ipFamily: ipFamily,
464462
portsMap: make(map[utilproxy.LocalPort]utilproxy.Closeable),
@@ -536,15 +534,14 @@ func NewDualStackProxier(
536534

537535
safeIpset := newSafeIpset(ipset)
538536

539-
isCIDR := true
540-
ipFamilyIPMap := utilproxy.MapIPsToIPFamily(nodePortAddresses, isCIDR)
537+
ipFamilyMap := utilproxy.MapCIDRsByIPFamily(nodePortAddresses)
541538

542539
// Create an ipv4 instance of the single-stack proxier
543540
ipv4Proxier, err := NewProxier(ipt[0], ipvs, safeIpset, sysctl,
544541
exec, syncPeriod, minSyncPeriod, filterCIDRs(false, excludeCIDRs), strictARP,
545542
tcpTimeout, tcpFinTimeout, udpTimeout, masqueradeAll, masqueradeBit,
546543
localDetectors[0], hostname, nodeIP[0],
547-
recorder, healthzServer, scheduler, ipFamilyIPMap[v1.IPv4Protocol], kernelHandler)
544+
recorder, healthzServer, scheduler, ipFamilyMap[v1.IPv4Protocol], kernelHandler)
548545
if err != nil {
549546
return nil, fmt.Errorf("unable to create ipv4 proxier: %v", err)
550547
}
@@ -553,7 +550,7 @@ func NewDualStackProxier(
553550
exec, syncPeriod, minSyncPeriod, filterCIDRs(true, excludeCIDRs), strictARP,
554551
tcpTimeout, tcpFinTimeout, udpTimeout, masqueradeAll, masqueradeBit,
555552
localDetectors[1], hostname, nodeIP[1],
556-
nil, nil, scheduler, ipFamilyIPMap[v1.IPv6Protocol], kernelHandler)
553+
nil, nil, scheduler, ipFamilyMap[v1.IPv6Protocol], kernelHandler)
557554
if err != nil {
558555
return nil, fmt.Errorf("unable to create ipv6 proxier: %v", err)
559556
}

pkg/proxy/service.go

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -156,25 +156,19 @@ func (sct *ServiceChangeTracker) newBaseServiceInfo(port *v1.ServicePort, servic
156156
// services, this is actually expected. Hence we downgraded from reporting by events
157157
// to just log lines with high verbosity
158158

159-
isCIDR := false
160-
ipFamilyIPMap := utilproxy.MapIPsToIPFamily(service.Spec.ExternalIPs, isCIDR)
161-
info.externalIPs = ipFamilyIPMap[sct.ipFamily]
162-
163-
for ipFam, ips := range ipFamilyIPMap {
164-
// Log the IPs not matching the ipFamily
165-
if ipFam != sct.ipFamily && len(ips) > 0 {
166-
klog.V(4).Infof("service change tracker(%v) ignored the following external IPs(%s) for service %v/%v as they don't match IPFamily", sct.ipFamily, strings.Join(ips, ","), service.Namespace, service.Name)
167-
}
159+
ipFamilyMap := utilproxy.MapIPsByIPFamily(service.Spec.ExternalIPs)
160+
info.externalIPs = ipFamilyMap[sct.ipFamily]
161+
162+
// Log the IPs not matching the ipFamily
163+
if ips, ok := ipFamilyMap[utilproxy.OtherIPFamily(sct.ipFamily)]; ok && len(ips) > 0 {
164+
klog.V(4).Infof("service change tracker(%v) ignored the following external IPs(%s) for service %v/%v as they don't match IPFamily", sct.ipFamily, strings.Join(ips, ","), service.Namespace, service.Name)
168165
}
169166

170-
isCIDR = true
171-
ipFamilyCIDRMap := utilproxy.MapIPsToIPFamily(loadBalancerSourceRanges, isCIDR)
172-
info.loadBalancerSourceRanges = ipFamilyCIDRMap[sct.ipFamily]
173-
for ipFam, cidrs := range ipFamilyCIDRMap {
174-
// Log the CIDRs not matching the ipFamily
175-
if ipFam != sct.ipFamily && len(cidrs) > 0 {
176-
klog.V(4).Infof("service change tracker(%v) ignored the following load balancer source ranges(%s) for service %v/%v as they don't match IPFamily", sct.ipFamily, strings.Join(cidrs, ","), service.Namespace, service.Name)
177-
}
167+
ipFamilyMap = utilproxy.MapCIDRsByIPFamily(loadBalancerSourceRanges)
168+
info.loadBalancerSourceRanges = ipFamilyMap[sct.ipFamily]
169+
// Log the CIDRs not matching the ipFamily
170+
if cidrs, ok := ipFamilyMap[utilproxy.OtherIPFamily(sct.ipFamily)]; ok && len(cidrs) > 0 {
171+
klog.V(4).Infof("service change tracker(%v) ignored the following load balancer source ranges(%s) for service %v/%v as they don't match IPFamily", sct.ipFamily, strings.Join(cidrs, ","), service.Namespace, service.Name)
178172
}
179173

180174
// Obtain Load Balancer Ingress IPs
@@ -184,17 +178,14 @@ func (sct *ServiceChangeTracker) newBaseServiceInfo(port *v1.ServicePort, servic
184178
}
185179

186180
if len(ips) > 0 {
187-
isCIDR = false
188-
ipFamilyIPMap = utilproxy.MapIPsToIPFamily(ips, isCIDR)
181+
ipFamilyMap = utilproxy.MapIPsByIPFamily(ips)
189182

190-
for ipFam, ipList := range ipFamilyIPMap {
191-
if ipFam != sct.ipFamily && len(ipList) > 0 {
192-
klog.V(4).Infof("service change tracker(%v) ignored the following load balancer(%s) ingress ips for service %v/%v as they don't match IPFamily", sct.ipFamily, strings.Join(ipList, ","), service.Namespace, service.Name)
183+
if ipList, ok := ipFamilyMap[utilproxy.OtherIPFamily(sct.ipFamily)]; ok && len(ipList) > 0 {
184+
klog.V(4).Infof("service change tracker(%v) ignored the following load balancer(%s) ingress ips for service %v/%v as they don't match IPFamily", sct.ipFamily, strings.Join(ipList, ","), service.Namespace, service.Name)
193185

194-
}
195186
}
196187
// Create the LoadBalancerStatus with the filtered IPs
197-
for _, ip := range ipFamilyIPMap[sct.ipFamily] {
188+
for _, ip := range ipFamilyMap[sct.ipFamily] {
198189
info.loadBalancerStatus.Ingress = append(info.loadBalancerStatus.Ingress, v1.LoadBalancerIngress{IP: ip})
199190
}
200191
}

pkg/proxy/util/utils.go

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -255,23 +255,50 @@ func LogAndEmitIncorrectIPVersionEvent(recorder record.EventRecorder, fieldName,
255255
}
256256
}
257257

258-
// MapIPsToIPFamily maps a slice of IPs to their respective IP families (v4 or v6)
259-
func MapIPsToIPFamily(ipStrings []string, isCIDR bool) map[v1.IPFamily][]string {
258+
// MapIPsByIPFamily maps a slice of IPs to their respective IP families (v4 or v6)
259+
func MapIPsByIPFamily(ipStrings []string) map[v1.IPFamily][]string {
260260
ipFamilyMap := map[v1.IPFamily][]string{}
261261
for _, ip := range ipStrings {
262-
ipFamily := getIPFamilyFromIP(ip, isCIDR)
263-
ipFamilyMap[ipFamily] = append(ipFamilyMap[ipFamily], ip)
262+
// Handle only the valid IPs
263+
if net.ParseIP(ip) != nil {
264+
ipFamily := getIPFamilyFromIP(ip)
265+
ipFamilyMap[ipFamily] = append(ipFamilyMap[ipFamily], ip)
266+
}
267+
}
268+
return ipFamilyMap
269+
}
270+
271+
// MapCIDRsByIPFamily maps a slice of IPs to their respective IP families (v4 or v6)
272+
func MapCIDRsByIPFamily(ipStrings []string) map[v1.IPFamily][]string {
273+
ipFamilyMap := map[v1.IPFamily][]string{}
274+
for _, cidr := range ipStrings {
275+
// Handle only the valid CIDRs
276+
if _, _, err := net.ParseCIDR(cidr); err == nil {
277+
ipFamily := getIPFamilyFromCIDR(cidr)
278+
ipFamilyMap[ipFamily] = append(ipFamilyMap[ipFamily], cidr)
279+
}
264280
}
265281
return ipFamilyMap
266282
}
267283

268-
func getIPFamilyFromIP(ip string, isCIDR bool) v1.IPFamily {
269-
conditionFunc := utilnet.IsIPv6String
270-
if isCIDR {
271-
conditionFunc = utilnet.IsIPv6CIDRString
284+
func getIPFamilyFromIP(ip string) v1.IPFamily {
285+
if utilnet.IsIPv6String(ip) {
286+
return v1.IPv6Protocol
272287
}
288+
return v1.IPv4Protocol
289+
}
290+
291+
// OtherIPFamily returns the other ip family
292+
func OtherIPFamily(ipFamily v1.IPFamily) v1.IPFamily {
293+
if ipFamily == v1.IPv6Protocol {
294+
return v1.IPv4Protocol
295+
}
296+
297+
return v1.IPv6Protocol
298+
}
273299

274-
if conditionFunc(ip) {
300+
func getIPFamilyFromCIDR(ip string) v1.IPFamily {
301+
if utilnet.IsIPv6CIDRString(ip) {
275302
return v1.IPv6Protocol
276303
}
277304
return v1.IPv4Protocol

pkg/proxy/util/utils_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -567,7 +567,7 @@ func TestShuffleStrings(t *testing.T) {
567567
}
568568
}
569569

570-
func TestMapIPsToIPFamily(t *testing.T) {
570+
func TestMapIPsByIPFamily(t *testing.T) {
571571
testCases := []struct {
572572
desc string
573573
ipString []string
@@ -657,8 +657,7 @@ func TestMapIPsToIPFamily(t *testing.T) {
657657
otherIPFamily = v1.IPv4Protocol
658658
}
659659

660-
isCIDR := false
661-
ipMap := MapIPsToIPFamily(testcase.ipString, isCIDR)
660+
ipMap := MapIPsByIPFamily(testcase.ipString)
662661

663662
if !reflect.DeepEqual(testcase.expectCorrect, ipMap[ipFamily]) {
664663
t.Errorf("Test %v failed: expected %v, got %v", testcase.desc, testcase.expectCorrect, ipMap[ipFamily])

0 commit comments

Comments
 (0)