fix: allow ipv4-mapped ipv6 addresses

Signed-off-by: Steven Kreitzer <skre@skre.me>
This commit is contained in:
Steven Kreitzer 2024-12-11 10:05:49 -06:00
parent fe2924b227
commit 449d27b00e
No known key found for this signature in database
GPG Key ID: 76F902141A05C232
2 changed files with 6 additions and 16 deletions

View File

@ -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
}

View File

@ -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"},
} {