@@ -18,6 +18,7 @@ package ipam
1818
1919import (
2020 "context"
21+ "net"
2122 "reflect"
2223 "strings"
2324
@@ -86,6 +87,27 @@ func (m *IPPoolManager) UnsetFinalizer() {
8687 )
8788}
8889
90+ // ipEqual compares two IP addresses by their parsed values to handle non-canonical
91+ // IPv6 formats (e.g., ::0005 vs ::5). Returns false if either address fails to parse,
92+ // logging a warning to aid in diagnosing configuration issues.
93+ func (m * IPPoolManager ) ipEqual (a , b ipamv1.IPAddressStr ) bool {
94+ ipA := net .ParseIP (string (a ))
95+ ipB := net .ParseIP (string (b ))
96+ if ipA == nil {
97+ if a != "" {
98+ m .Log .Info ("Failed to parse IP address" , "address" , a )
99+ }
100+ return false
101+ }
102+ if ipB == nil {
103+ if b != "" {
104+ m .Log .Info ("Failed to parse IP address" , "address" , b )
105+ }
106+ return false
107+ }
108+ return ipA .Equal (ipB )
109+ }
110+
89111func (m * IPPoolManager ) SetClusterOwnerRef (cluster * clusterv1beta1.Cluster ) error {
90112 if cluster == nil {
91113 return errors .New ("Missing cluster" )
@@ -432,7 +454,7 @@ func (m *IPPoolManager) allocateAddress(addressClaim *ipamv1.IPClaim,
432454 isRequestedIPAllocated := false
433455
434456 // Conflict-case, claim is preAllocated but has requested different IP
435- if requestedIP != "" && ipPreAllocated && requestedIP != preAllocatedAddress {
457+ if requestedIP != "" && ipPreAllocated && ! m . ipEqual ( requestedIP , preAllocatedAddress ) {
436458 addressClaim .Status .ErrorMessage = ptr .To ("PreAllocation and requested ip address are conflicting" )
437459 return "" , 0 , nil , []ipamv1.IPAddressStr {}, errors .New ("PreAllocation and requested ip address are conflicting" )
438460 }
@@ -449,15 +471,15 @@ func (m *IPPoolManager) allocateAddress(addressClaim *ipamv1.IPClaim,
449471 }
450472 index ++
451473 // Check if requestedIP is present and matches the current address
452- if requestedIP != "" && allocatedAddress != requestedIP {
474+ if requestedIP != "" && ! m . ipEqual ( allocatedAddress , requestedIP ) {
453475 continue
454476 }
455- if requestedIP != "" && allocatedAddress == requestedIP {
477+ if requestedIP != "" && m . ipEqual ( allocatedAddress , requestedIP ) {
456478 isRequestedIPAllocated = true
457479 }
458480 // We have a pre-allocated ip, we just need to ensure that it matches the current address
459481 // if it does not, continue and try the next address
460- if ipPreAllocated && allocatedAddress != preAllocatedAddress {
482+ if ipPreAllocated && ! m . ipEqual ( allocatedAddress , preAllocatedAddress ) {
461483 continue
462484 }
463485 // Here the two addresses match, so we continue with that one
@@ -523,7 +545,7 @@ func (m *IPPoolManager) capiAllocateAddress(addressClaim *capipamv1beta1.IPAddre
523545 isRequestedIPAllocated := false
524546
525547 // Conflict-case, claim is preAllocated but has requested different IP
526- if requestedIP != "" && ipPreAllocated && requestedIP != preAllocatedAddress {
548+ if requestedIP != "" && ipPreAllocated && ! m . ipEqual ( requestedIP , preAllocatedAddress ) {
527549 conditions := clusterv1beta1.Conditions {}
528550 conditions = append (conditions , clusterv1beta1.Condition {
529551 Type : "ErrorMessage" ,
@@ -549,15 +571,15 @@ func (m *IPPoolManager) capiAllocateAddress(addressClaim *capipamv1beta1.IPAddre
549571 }
550572 index ++
551573 // Check if requestedIP is present and matches the current address
552- if requestedIP != "" && allocatedAddress != requestedIP {
574+ if requestedIP != "" && ! m . ipEqual ( allocatedAddress , requestedIP ) {
553575 continue
554576 }
555- if requestedIP != "" && allocatedAddress == requestedIP {
577+ if requestedIP != "" && m . ipEqual ( allocatedAddress , requestedIP ) {
556578 isRequestedIPAllocated = true
557579 }
558580 // We have a pre-allocated ip, we just need to ensure that it matches the current address
559581 // if it does not, continue and try the next address
560- if ipPreAllocated && allocatedAddress != preAllocatedAddress {
582+ if ipPreAllocated && ! m . ipEqual ( allocatedAddress , preAllocatedAddress ) {
561583 continue
562584 }
563585 // Here the two addresses match, so we continue with that one
0 commit comments