From 449d27b00ea822dabe6be42a7ee72cabebee3001 Mon Sep 17 00:00:00 2001 From: Steven Kreitzer Date: Wed, 11 Dec 2024 10:05:49 -0600 Subject: [PATCH] fix: allow ipv4-mapped ipv6 addresses Signed-off-by: Steven Kreitzer --- source/source.go | 21 +++++---------------- source/source_test.go | 1 + 2 files changed, 6 insertions(+), 16 deletions(-) diff --git a/source/source.go b/source/source.go index 132f40dcd9..012cf82739 100644 --- a/source/source.go +++ b/source/source.go @@ -21,7 +21,7 @@ import ( "context" "fmt" "math" - "net" + "net/netip" "reflect" "strconv" "strings" @@ -255,11 +255,12 @@ func getTargetsFromTargetAnnotation(annotations map[string]string) endpoint.Targ } // suitableType returns the DNS resource record type suitable for the target. -// In this case type A for IPs and type CNAME for everything else. +// In this case type A/AAAA for IPs and type CNAME for everything else. func suitableType(target string) string { - if net.ParseIP(target) != nil && net.ParseIP(target).To4() != nil { + netIP, err := netip.ParseAddr(target) + if err == nil && netIP.Is4() { return endpoint.RecordTypeA - } else if net.ParseIP(target) != nil && net.ParseIP(target).To16() != nil { + } else if err == nil && netIP.Is6() { return endpoint.RecordTypeAAAA } return endpoint.RecordTypeCNAME @@ -276,14 +277,8 @@ func endpointsForHostname(hostname string, targets endpoint.Targets, ttl endpoin for _, t := range targets { switch suitableType(t) { case endpoint.RecordTypeA: - if isIPv6String(t) { - continue - } aTargets = append(aTargets, t) case endpoint.RecordTypeAAAA: - if !isIPv6String(t) { - continue - } aaaaTargets = append(aaaaTargets, t) default: cnameTargets = append(cnameTargets, t) @@ -387,9 +382,3 @@ func waitForDynamicCacheSync(ctx context.Context, factory dynamicInformerFactory } return nil } - -// isIPv6String returns if ip is IPv6. -func isIPv6String(ip string) bool { - netIP := net.ParseIP(ip) - return netIP != nil && netIP.To4() == nil -} diff --git a/source/source_test.go b/source/source_test.go index 417a3c14e5..17a3c4a7fb 100644 --- a/source/source_test.go +++ b/source/source_test.go @@ -85,6 +85,7 @@ func TestSuitableType(t *testing.T) { }{ {"8.8.8.8", "", "A"}, {"2001:db8::1", "", "AAAA"}, + {"::ffff:c0a8:101", "", "AAAA"}, {"foo.example.org", "", "CNAME"}, {"bar.eu-central-1.elb.amazonaws.com", "", "CNAME"}, } {