@@ -18,6 +18,7 @@ package ipam
1818
1919import (
2020 "context"
21+ "net"
2122 "reflect"
2223 "strings"
2324
@@ -87,6 +88,27 @@ func (m *IPPoolManager) UnsetFinalizer() {
8788 )
8889}
8990
91+ // ipEqual compares two IP addresses by their parsed values to handle non-canonical
92+ // IPv6 formats (e.g., ::0005 vs ::5). Returns false if either address fails to parse,
93+ // logging a warning to aid in diagnosing configuration issues.
94+ func (m * IPPoolManager ) ipEqual (a , b ipamv1.IPAddressStr ) bool {
95+ ipA := net .ParseIP (string (a ))
96+ ipB := net .ParseIP (string (b ))
97+ if ipA == nil {
98+ if a != "" {
99+ m .Log .Info ("Failed to parse IP address" , "address" , a )
100+ }
101+ return false
102+ }
103+ if ipB == nil {
104+ if b != "" {
105+ m .Log .Info ("Failed to parse IP address" , "address" , b )
106+ }
107+ return false
108+ }
109+ return ipA .Equal (ipB )
110+ }
111+
90112func (m * IPPoolManager ) SetClusterOwnerRef (cluster * clusterv1beta1.Cluster ) error {
91113 if cluster == nil {
92114 return errors .New ("Missing cluster" )
@@ -436,7 +458,7 @@ func (m *IPPoolManager) allocateAddress(addressClaim *ipamv1.IPClaim,
436458 isRequestedIPAllocated := false
437459
438460 // Conflict-case, claim is preAllocated but has requested different IP
439- if requestedIP != "" && ipPreAllocated && requestedIP != preAllocatedAddress {
461+ if requestedIP != "" && ipPreAllocated && ! m . ipEqual ( requestedIP , preAllocatedAddress ) {
440462 addressClaim .Status .ErrorMessage = ptr .To ("PreAllocation and requested ip address are conflicting" )
441463 return "" , 0 , nil , []ipamv1.IPAddressStr {}, errors .New ("PreAllocation and requested ip address are conflicting" )
442464 }
@@ -453,15 +475,15 @@ func (m *IPPoolManager) allocateAddress(addressClaim *ipamv1.IPClaim,
453475 }
454476 index ++
455477 // Check if requestedIP is present and matches the current address
456- if requestedIP != "" && allocatedAddress != requestedIP {
478+ if requestedIP != "" && ! m . ipEqual ( allocatedAddress , requestedIP ) {
457479 continue
458480 }
459- if requestedIP != "" && allocatedAddress == requestedIP {
481+ if requestedIP != "" && m . ipEqual ( allocatedAddress , requestedIP ) {
460482 isRequestedIPAllocated = true
461483 }
462484 // We have a pre-allocated ip, we just need to ensure that it matches the current address
463485 // if it does not, continue and try the next address
464- if ipPreAllocated && allocatedAddress != preAllocatedAddress {
486+ if ipPreAllocated && ! m . ipEqual ( allocatedAddress , preAllocatedAddress ) {
465487 continue
466488 }
467489 // Here the two addresses match, so we continue with that one
@@ -527,7 +549,7 @@ func (m *IPPoolManager) capiAllocateAddress(addressClaim *capipamv1beta1.IPAddre
527549 isRequestedIPAllocated := false
528550
529551 // Conflict-case, claim is preAllocated but has requested different IP
530- if requestedIP != "" && ipPreAllocated && requestedIP != preAllocatedAddress {
552+ if requestedIP != "" && ipPreAllocated && ! m . ipEqual ( requestedIP , preAllocatedAddress ) {
531553 conditions := clusterv1beta1.Conditions {}
532554 conditions = append (conditions , clusterv1beta1.Condition {
533555 Type : "ErrorMessage" ,
@@ -553,15 +575,15 @@ func (m *IPPoolManager) capiAllocateAddress(addressClaim *capipamv1beta1.IPAddre
553575 }
554576 index ++
555577 // Check if requestedIP is present and matches the current address
556- if requestedIP != "" && allocatedAddress != requestedIP {
578+ if requestedIP != "" && ! m . ipEqual ( allocatedAddress , requestedIP ) {
557579 continue
558580 }
559- if requestedIP != "" && allocatedAddress == requestedIP {
581+ if requestedIP != "" && m . ipEqual ( allocatedAddress , requestedIP ) {
560582 isRequestedIPAllocated = true
561583 }
562584 // We have a pre-allocated ip, we just need to ensure that it matches the current address
563585 // if it does not, continue and try the next address
564- if ipPreAllocated && allocatedAddress != preAllocatedAddress {
586+ if ipPreAllocated && ! m . ipEqual ( allocatedAddress , preAllocatedAddress ) {
565587 continue
566588 }
567589 // Here the two addresses match, so we continue with that one
0 commit comments