Skip to content

Commit

Permalink
Add support for DNS SRV lookups
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt Hamann committed Nov 6, 2017
1 parent 2df0ea0 commit b6a0751
Show file tree
Hide file tree
Showing 8 changed files with 137 additions and 5 deletions.
13 changes: 11 additions & 2 deletions glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions glide.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,8 @@ import:
subpackages:
- websocket
- package: github.com/hashicorp/go-version
- package: github.com/benschw/srv-lb
subpackages:
- dns
- lb
- package: github.com/miekg/dns
41 changes: 41 additions & 0 deletions resolver/dns.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package resolver

import (
"strings"

"github.com/benschw/srv-lb/lb"
)

// DNSConfig accepts an address and optional load balancer config
type DNSConfig struct {
Addr string
LbCfg *lb.Config
}

// ResolveSrvAddr returns a load-balanced host:port based on DNS SRV lookup (or `addr` if not a hostname)
func ResolveSrvAddr(dnsCfg DNSConfig) (string, error) {
addr := dnsCfg.Addr

if v := strings.Split(addr, ":"); len(v) < 2 {
var cfg *lb.Config
var err error
if dnsCfg.LbCfg == nil {
cfg, err = lb.DefaultConfig()

if err != nil {
return addr, err
}
} else {
cfg = dnsCfg.LbCfg
}

l := lb.New(cfg, addr)
resolvAddr, err := l.Next()

if err == nil {
addr = resolvAddr.String()
}
}

return addr, nil
}
29 changes: 29 additions & 0 deletions resolver/dns_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package resolver

import (
"testing"

"github.com/benschw/srv-lb/lb"
)

func TestSrvLookup(t *testing.T) {
lbCfg, _ := lb.DefaultConfig()
lbCfg.Strategy = MockStrategy
dnsCfg := DNSConfig{Addr: "foo.example.com", LbCfg: lbCfg}

addr, _ := ResolveSrvAddr(dnsCfg)

if addr != "1.2.3.4:1234" {
t.Error("expected address string of 1.2.3.4:1234, got:", addr)
}
}

func TestIpPassthrough(t *testing.T) {
dnsCfg := DNSConfig{Addr: "10.0.0.1:1234"}

addr, _ := ResolveSrvAddr(dnsCfg)

if addr != "10.0.0.1:1234" {
t.Error("expected output to equal input (10.0.0.1:1234), got:", addr)
}
}
30 changes: 30 additions & 0 deletions resolver/lb_mock_strategy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package resolver

import (
"github.com/benschw/srv-lb/dns"
"github.com/benschw/srv-lb/lb"
)

// MockStrategy is used for testing DNS load balancing
const MockStrategy lb.StrategyType = "mock"

// New creates a new instance of the load balancer
func New(lib dns.Lookup) lb.GenericLoadBalancer {
lb := new(MockClb)
lb.dnsLib = lib
return lb
}

// MockClb contains the dnslib
type MockClb struct {
dnsLib dns.Lookup
}

// Next gets the next server in the available nodes
func (lb *MockClb) Next(name string) (dns.Address, error) {
return dns.Address{Address: "1.2.3.4", Port: 1234}, nil
}

func init() {
lb.RegisterStrategy(MockStrategy, New)
}
8 changes: 7 additions & 1 deletion transports/tcp/tcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"net"

"github.com/gliderlabs/logspout/adapters/raw"
"github.com/gliderlabs/logspout/resolver"
"github.com/gliderlabs/logspout/router"
)

Expand All @@ -21,7 +22,12 @@ func rawTCPAdapter(route *router.Route) (router.LogAdapter, error) {
type tcpTransport int

func (t *tcpTransport) Dial(addr string, options map[string]string) (net.Conn, error) {
raddr, err := net.ResolveTCPAddr("tcp", addr)
daddr, err := resolver.ResolveSrvAddr(resolver.DNSConfig{Addr: addr})
if err != nil {
return nil, err
}

raddr, err := net.ResolveTCPAddr("tcp", daddr)
if err != nil {
return nil, err
}
Expand Down
8 changes: 7 additions & 1 deletion transports/tls/tls.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"net"

"github.com/gliderlabs/logspout/adapters/raw"
"github.com/gliderlabs/logspout/resolver"
"github.com/gliderlabs/logspout/router"
)

Expand All @@ -22,7 +23,12 @@ func rawTLSAdapter(route *router.Route) (router.LogAdapter, error) {
type tlsTransport int

func (t *tlsTransport) Dial(addr string, options map[string]string) (net.Conn, error) {
conn, err := tls.Dial("tcp", addr, nil)
daddr, err := resolver.ResolveSrvAddr(resolver.DNSConfig{Addr: addr})
if err != nil {
return nil, err
}

conn, err := tls.Dial("tcp", daddr, nil)
if err != nil {
return nil, err
}
Expand Down
8 changes: 7 additions & 1 deletion transports/udp/udp.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"net"

"github.com/gliderlabs/logspout/adapters/raw"
"github.com/gliderlabs/logspout/resolver"
"github.com/gliderlabs/logspout/router"
)

Expand All @@ -26,7 +27,12 @@ func rawUDPAdapter(route *router.Route) (router.LogAdapter, error) {
type udpTransport int

func (t *udpTransport) Dial(addr string, options map[string]string) (net.Conn, error) {
raddr, err := net.ResolveUDPAddr("udp", addr)
daddr, err := resolver.ResolveSrvAddr(resolver.DNSConfig{Addr: addr})
if err != nil {
return nil, err
}

raddr, err := net.ResolveUDPAddr("udp", daddr)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit b6a0751

Please sign in to comment.