-
Notifications
You must be signed in to change notification settings - Fork 594
feat: support DNS SD URLs #5034
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,6 +6,7 @@ import ( | |
| "fmt" | ||
| "net" | ||
| "strconv" | ||
| "strings" | ||
|
|
||
| "github.com/go-kit/log" | ||
| "github.com/samber/lo" | ||
|
|
@@ -29,7 +30,9 @@ func newWithJoinPeers(opts Options) DiscoverFn { | |
| defer span.End() | ||
|
|
||
| // Use these resolvers in order to resolve the provided addresses into a form that can be used by clustering. | ||
| // NOTE: dnsSDURLResolver should be above other DNS resolvers. | ||
| resolvers := []addressResolver{ | ||
| dnsSDURLResolver(opts, ctx), | ||
| ipResolver(opts.Logger), | ||
| dnsAResolver(opts, ctx), | ||
| dnsSRVResolver(opts, ctx), | ||
|
|
@@ -128,6 +131,7 @@ func dnsSRVResolver(opts Options, ctx context.Context) addressResolver { | |
| if srvLookup == nil { | ||
| srvLookup = net.LookupSRV | ||
| } | ||
|
|
||
| return dnsResolver(opts, ctx, "SRV", func(addr string) ([]string, error) { | ||
| _, addresses, err := srvLookup("", "", addr) | ||
| result := make([]string, 0, len(addresses)) | ||
|
|
@@ -138,6 +142,57 @@ func dnsSRVResolver(opts Options, ctx context.Context) addressResolver { | |
| }) | ||
| } | ||
|
|
||
| const ( | ||
| dnsSchemeDNS = "dns+" | ||
| dnsSchemeDNSSRV = "dnssrv+" | ||
| dnsSchemeDNSSRVNOA = "dnssrvnoa+" | ||
| ) | ||
|
|
||
| // dnsSDURLResolver handles DNS-SD URLs which explicitly states what DNS query should be used for host resolve. | ||
| // | ||
| // Example: `dnssrv+_memcached._tcp.memcached.namespace.svc.cluster.local` | ||
| // | ||
| // Resolver rejects any non-URL values. | ||
| func dnsSDURLResolver(opts Options, ctx context.Context) addressResolver { | ||
| srvLookup := opts.lookupSRVFn | ||
| if srvLookup == nil { | ||
| srvLookup = net.LookupSRV | ||
| } | ||
|
|
||
| return func(addr string) ([]string, error) { | ||
| var ( | ||
| nextAddr string | ||
| nextResolver addressResolver | ||
| ) | ||
|
|
||
| switch { | ||
| case strings.HasPrefix(addr, dnsSchemeDNS): | ||
| nextAddr = addr[len(dnsSchemeDNS):] | ||
| nextResolver = dnsAResolver(opts, ctx) | ||
| case strings.HasPrefix(addr, dnsSchemeDNSSRV): | ||
| nextAddr = addr[len(dnsSchemeDNSSRV):] | ||
| nextResolver = dnsSRVResolver(opts, ctx) | ||
| case strings.HasPrefix(addr, dnsSchemeDNSSRVNOA): | ||
| nextAddr = addr[len(dnsSchemeDNSSRVNOA):] | ||
| nextResolver = dnsResolver(opts, ctx, "SRVNOA", func(addr string) ([]string, error) { | ||
| // NOTE: the only difference between SRVNOA and SRV, as SRV request should do N+1 query for A/AAAA. | ||
| _, addresses, err := srvLookup("", "", addr) | ||
| result := make([]string, 0, len(addresses)) | ||
| for _, a := range addresses { | ||
| result = append(result, a.Target) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there anything in spec about randomizing the order here? for clustering purposes, it may be useful to have the addresses in random order :)
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @thampiotr IIRC spec doesn't mention this, but some DNS servers return items in a random order - this was the case with Consul, and Alloy worked perfectly fine with that during my test run. Do you know if Loki and Tempo do any explicit randomization? |
||
| } | ||
| return result, err | ||
| }) | ||
|
|
||
| default: | ||
| // skip and pass control to a next resolver. | ||
| return nil, nil | ||
| } | ||
|
|
||
| return nextResolver(nextAddr) | ||
| } | ||
| } | ||
|
|
||
| func dnsResolver(opts Options, ctx context.Context, recordType string, dnsLookupFn func(string) ([]string, error)) addressResolver { | ||
| return func(addr string) ([]string, error) { | ||
| _, span := opts.Tracer.Tracer("").Start( | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.