Skip to content

Commit

Permalink
querier: do A lookups after SRV lookups for store discovery (#865)
Browse files Browse the repository at this point in the history
* do A lookups after SRV lookups for store discovery

* add missing trailing period

* re-add old behaviour under dnssrvnoa+
  • Loading branch information
mjd95 authored and bwplotka committed Mar 8, 2019
1 parent b4d233e commit 432785e
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 6 deletions.
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,
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

0 comments on commit 432785e

Please sign in to comment.