Skip to content
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

querier: do A lookups after SRV lookups for store discovery #865

Merged
merged 3 commits into from
Mar 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions pkg/discovery/dns/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ import (
type QType string

const (
A = QType("dns")
SRV = QType("dnssrv")
A = QType("dns")
SRV = QType("dnssrv")
SRVNoA = QType("dnssrvnoa")
)

type Resolver interface {
Expand Down Expand Up @@ -72,6 +73,26 @@ func (s *dnsSD) Resolve(ctx context.Context, name string, qtype QType) ([]string
res = append(res, appendScheme(scheme, net.JoinHostPort(ip.String(), port)))
}
case SRV:
_, recs, err := s.resolver.LookupSRV(ctx, "", "", host)
if err != nil {
return nil, errors.Wrapf(err, "lookup SRV records %q", host)
}
for _, rec := range recs {
// Only use port from SRV record if no explicit port was specified.
resPort := port
if resPort == "" {
resPort = strconv.Itoa(int(rec.Port))
}
// Do A lookup for the domain in SRV answer.
resIPs, err := s.resolver.LookupIPAddr(ctx, rec.Target)
if err != nil {
return nil, errors.Wrapf(err, "look IP addresses %q", rec.Target)
}
for _, resIP := range resIPs {
res = append(res, appendScheme(scheme, net.JoinHostPort(resIP.String(), resPort)))
}
}
case SRVNoA:
_, recs, err := s.resolver.LookupSRV(ctx, "", "", host)
if err != nil {
return nil, errors.Wrapf(err, "lookup SRV records %q", host)
Expand Down
54 changes: 50 additions & 4 deletions pkg/discovery/dns/resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,57 @@ var (
resolver: &mockHostnameResolver{},
},
{
testName: "multiple srv records from srv lookup",
testName: "multiple SRV records from SRV lookup",
addr: "_test._tcp.mycompany.com",
qtype: SRV,
expectedResult: []string{"192.168.0.1:8080", "192.168.0.2:8081"},
expectedErr: nil,
resolver: &mockHostnameResolver{
resultSRVs: map[string][]*net.SRV{
"_test._tcp.mycompany.com": {
&net.SRV{Target: "alt1.mycompany.com.", Port: 8080},
&net.SRV{Target: "alt2.mycompany.com.", Port: 8081},
},
},
resultIPs: map[string][]net.IPAddr{
"alt1.mycompany.com.": {net.IPAddr{IP: net.ParseIP("192.168.0.1")}},
"alt2.mycompany.com.": {net.IPAddr{IP: net.ParseIP("192.168.0.2")}},
},
},
},
{
testName: "multiple SRV records from SRV lookup with specified port",
addr: "_test._tcp.mycompany.com:8082",
qtype: SRV,
mjd95 marked this conversation as resolved.
Show resolved Hide resolved
expectedResult: []string{"192.168.0.1:8082", "192.168.0.2:8082"},
expectedErr: nil,
resolver: &mockHostnameResolver{
resultSRVs: map[string][]*net.SRV{
"_test._tcp.mycompany.com": {
&net.SRV{Target: "alt1.mycompany.com.", Port: 8080},
&net.SRV{Target: "alt2.mycompany.com.", Port: 8081},
},
},
resultIPs: map[string][]net.IPAddr{
"alt1.mycompany.com.": {net.IPAddr{IP: net.ParseIP("192.168.0.1")}},
"alt2.mycompany.com.": {net.IPAddr{IP: net.ParseIP("192.168.0.2")}},
},
},
},
{
testName: "error from SRV resolver",
addr: "_test._tcp.mycompany.com",
qtype: SRV,
expectedResult: nil,
expectedErr: errors.Wrapf(errorFromResolver, "lookup SRV records \"_test._tcp.mycompany.com\""),
resolver: &mockHostnameResolver{err: errorFromResolver},
},
{
testName: "multiple SRV records from SRV no A lookup",
addr: "_test._tcp.mycompany.com",
qtype: SRVNoA,
expectedResult: []string{"192.168.0.1:8080", "192.168.0.2:8081"},
expectedErr: nil,
resolver: &mockHostnameResolver{
resultSRVs: map[string][]*net.SRV{
"_test._tcp.mycompany.com": {
Expand All @@ -92,9 +138,9 @@ var (
},
},
{
testName: "multiple srv records from srv lookup",
testName: "multiple SRV records from SRV no A lookup with specified port",
addr: "_test._tcp.mycompany.com:8082",
qtype: SRV,
qtype: SRVNoA,
expectedResult: []string{"192.168.0.1:8082", "192.168.0.2:8082"},
expectedErr: nil,
resolver: &mockHostnameResolver{
Expand All @@ -107,7 +153,7 @@ var (
},
},
{
testName: "error from SRV resolver",
testName: "error from SRV no A lookup",
addr: "_test._tcp.mycompany.com",
qtype: SRV,
expectedResult: nil,
Expand Down