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 449d27b
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 16 deletions.
21 changes: 5 additions & 16 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,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
Expand All @@ -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)
Expand Down Expand Up @@ -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
}
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 449d27b

Please sign in to comment.