Skip to content

Commit

Permalink
fix: allow ipv4-mapped ipv6 addresses
Browse files Browse the repository at this point in the history
Signed-off-by: Steven Kreitzer <[email protected]>
  • Loading branch information
buroa committed Dec 21, 2024
1 parent fe2924b commit 22072b2
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 18 deletions.
27 changes: 9 additions & 18 deletions source/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"context"
"fmt"
"math"
"net"
"net/netip"
"reflect"
"strconv"
"strings"
Expand Down Expand Up @@ -255,12 +255,15 @@ 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 or AAAA for IPs and type CNAME for everything else.
func suitableType(target string) string {
if net.ParseIP(target) != nil && net.ParseIP(target).To4() != nil {
return endpoint.RecordTypeA
} else if net.ParseIP(target) != nil && net.ParseIP(target).To16() != nil {
return endpoint.RecordTypeAAAA
netIP, err := netip.ParseAddr(target)
if err == nil {
if netIP.Is4() {
return endpoint.RecordTypeA
} else if netIP.Is6() {
return endpoint.RecordTypeAAAA
}
}
return endpoint.RecordTypeCNAME
}
Expand All @@ -276,14 +279,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)
Expand Down Expand Up @@ -387,9 +384,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
}
1 change: 1 addition & 0 deletions source/source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
} {
Expand Down

0 comments on commit 22072b2

Please sign in to comment.