Skip to content

Commit bd37ec4

Browse files
shoekstraM00nF1sh
authored andcommitted
Make isELBV2TargetInELBVPC more efficient
Don't need to check vpc.CidrBlock as it is part of vpc.CidrBlockAssociationSet. isIPinCIDR is now also a bit more robust in checking if an IP is part of a CIDR. Signed-off-by: Stephen Hoekstra <[email protected]>
1 parent e1c8af9 commit bd37ec4

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

pkg/targetgroupbinding/resource_manager.go

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -490,24 +490,27 @@ func isELBV2TargetGroupNotFoundError(err error) bool {
490490
}
491491

492492
func isELBV2TargetInELBVPC(podIP string, vpc *ec2sdk.Vpc) bool {
493-
// Check if the pod IP is in the primary VPC CIDR.
494-
if isIPinCIDR(podIP, *vpc.CidrBlock) {
495-
return true
496-
}
497-
498-
// Check if the pod IP is from a secondary CIDR block.
493+
// Check if the pod IP is found in a VPC CIDR block.
499494
for _, v := range vpc.CidrBlockAssociationSet {
500-
if isIPinCIDR(podIP, *v.CidrBlock) {
495+
if isIPinCIDR(podIP, awssdk.StringValue(v.CidrBlock)) {
501496
return true
502497
}
503498
}
504499

505-
// Cannot find pod IP in a VPC CIDR.
500+
// Cannot find pod IP in a VPC CIDR block.
506501
return false
507502
}
508503

509504
func isIPinCIDR(ipAddr, cidrBlock string) bool {
510-
_, cidr, _ := net.ParseCIDR(cidrBlock)
505+
_, cidr, err := net.ParseCIDR(cidrBlock)
506+
if err != nil {
507+
return false
508+
}
509+
511510
ip := net.ParseIP(ipAddr)
511+
if ip == nil {
512+
return false
513+
}
514+
512515
return cidr.Contains(ip)
513516
}

0 commit comments

Comments
 (0)