Skip to content

Commit

Permalink
Feature: Fix miekgdns resolver LookupSRV function to work with CNAME …
Browse files Browse the repository at this point in the history
…records too (#5716)

* feat: cname handling in lookupsrv

Signed-off-by: Atharva Shinde <atharvashinde179@gmail.com>

* deleted: redundant var

Signed-off-by: Atharva Shinde <atharvashinde179@gmail.com>

* Update CHANGELOG.md

Signed-off-by: Atharva Shinde <atharvashinde179@gmail.com>

* Update CHANGELOG.md

Signed-off-by: Atharva Shinde <atharvashinde179@gmail.com>

Signed-off-by: Atharva Shinde <atharvashinde179@gmail.com>
Signed-off-by: Matej Gera <38492574+matej-g@users.noreply.github.com>
Co-authored-by: Matej Gera <38492574+matej-g@users.noreply.github.com>
  • Loading branch information
Atharva-Shinde and matej-g committed Oct 31, 2022
1 parent 0e0f4fb commit 7968915
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ We use *breaking :warning:* to mark changes that are not backward compatible (re

### Changed

- [#5716](https://github.com/thanos-io/thanos/pull/5716) DNS: Fix miekgdns resolver LookupSRV to work with CNAME records.

### Removed

- [#5824](https://github.com/thanos-io/thanos/pull/5824) Mixin: Remove noisy `ThanosReceiveTrafficBelowThreshold` alert.
Expand Down
15 changes: 15 additions & 0 deletions pkg/discovery/dns/miekgdns/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ type Resolver struct {
}

func (r *Resolver) LookupSRV(ctx context.Context, service, proto, name string) (cname string, addrs []*net.SRV, err error) {
return r.lookupSRV(service, proto, name, 1, 8)
}

func (r *Resolver) lookupSRV(service, proto, name string, currIteration, maxIterations int) (cname string, addrs []*net.SRV, err error) {
// We want to protect from infinite loops when resolving DNS records recursively.
if currIteration > maxIterations {
return "", nil, errors.Errorf("maximum number of recursive iterations reached (%d)", maxIterations)
}
var target string
if service == "" && proto == "" {
target = name
Expand All @@ -41,6 +49,13 @@ func (r *Resolver) LookupSRV(ctx context.Context, service, proto, name string) (
Priority: addr.Priority,
Port: addr.Port,
})
case *dns.CNAME:
// Recursively resolve it.
_, resp, err := r.lookupSRV("", "", addr.Target, currIteration+1, maxIterations)
if err != nil {
return "", nil, errors.Wrapf(err, "recursively resolve %s", addr.Target)
}
addrs = append(addrs, resp...)
default:
return "", nil, errors.Errorf("invalid SRV response record %s", record)
}
Expand Down

0 comments on commit 7968915

Please sign in to comment.