Skip to content

Commit 44fb358

Browse files
authored
Merge pull request #38 from netlify/salvador/add-dns-discovery-package
[discovery] Add DNS discovery methods. Rabbit and Nats config to support DNS discovery
2 parents 0445a47 + 31f5251 commit 44fb358

File tree

5 files changed

+89
-19
lines changed

5 files changed

+89
-19
lines changed

discovery/main.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package discovery
2+
3+
import (
4+
"net"
5+
)
6+
7+
type Endpoint struct {
8+
Name string
9+
Port uint16
10+
}
11+
12+
func DiscoverEndpoints(serviceDNS string) ([]*net.SRV, error) {
13+
_, remotes, err := net.LookupSRV("", "", serviceDNS)
14+
if err != nil {
15+
return nil, err
16+
}
17+
return remotes, nil
18+
}

glide.lock

Lines changed: 14 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

glide.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import:
77
- package: github.com/kelseyhightower/envconfig
88
version: v1.3.0
99
- package: github.com/nats-io/nats
10-
version: v1.2.2
10+
version: v1.3.0
1111
- package: github.com/pkg/errors
1212
version: v0.8.0
1313
- package: github.com/rybit/nats_logrus_hook

messaging/nats.go

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
11
package messaging
22

33
import (
4+
"fmt"
45
"strings"
56

67
"github.com/nats-io/nats"
78
"github.com/sirupsen/logrus"
89

910
"github.com/rybit/nats_logrus_hook"
1011

12+
"github.com/netlify/netlify-commons/discovery"
1113
"github.com/netlify/netlify-commons/tls"
1214
)
1315

1416
type NatsConfig struct {
15-
TLS *tls.Config `mapstructure:"tls_conf"`
16-
Servers []string `mapstructure:"servers"`
17-
LogsSubject string `mapstructure:"log_subject"`
17+
TLS *tls.Config `mapstructure:"tls_conf"`
18+
DiscoveryName string `split_words:"true" mapstructure:"discovery_name"`
19+
Servers []string `mapstructure:"servers"`
20+
LogsSubject string `mapstructure:"log_subject"`
1821
}
1922

2023
type MetricsConfig struct {
@@ -29,7 +32,6 @@ func (config *NatsConfig) ServerString() string {
2932

3033
func (config *NatsConfig) Fields() logrus.Fields {
3134
f := logrus.Fields{
32-
3335
"logs_subject": config.LogsSubject,
3436
"servers": strings.Join(config.Servers, ","),
3537
}
@@ -64,6 +66,14 @@ func ConfigureNatsConnection(config *NatsConfig, log *logrus.Entry) (*nats.Conn,
6466

6567
// ConnectToNats will do a TLS connection to the nats servers specified
6668
func ConnectToNats(config *NatsConfig, errHandler nats.ErrHandler) (*nats.Conn, error) {
69+
if config.DiscoveryName != "" {
70+
servers, err := discoverNatsURLs(config.DiscoveryName)
71+
if err != nil {
72+
return nil, err
73+
}
74+
config.Servers = servers
75+
}
76+
6777
options := []nats.Option{}
6878
if config.TLS != nil {
6979
tlsConfig, err := config.TLS.TLSConfig()
@@ -92,3 +102,18 @@ func ErrorHandler(log *logrus.Entry) nats.ErrHandler {
92102
}).Error("Error while consuming from " + sub.Subject)
93103
}
94104
}
105+
106+
func discoverNatsURLs(serviceName string) ([]string, error) {
107+
natsURLs := []string{}
108+
109+
endpoints, err := discovery.DiscoverEndpoints(serviceName)
110+
if err != nil {
111+
return nil, err
112+
}
113+
114+
for _, endpoint := range endpoints {
115+
natsURLs = append(natsURLs, fmt.Sprintf("nats://%s:%d", endpoint.Target, endpoint.Port))
116+
}
117+
118+
return natsURLs, nil
119+
}

messaging/rabbit.go

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"strings"
99
"time"
1010

11+
"github.com/netlify/netlify-commons/discovery"
1112
"github.com/netlify/netlify-commons/tls"
1213
"github.com/sirupsen/logrus"
1314
"github.com/streadway/amqp"
@@ -40,8 +41,9 @@ func (c *Consumer) Clone(queueName string, delivery *DeliveryDefinition) (*Consu
4041
}
4142

4243
type RabbitConfig struct {
43-
Servers []string `mapstructure:"servers"`
44-
TLS *tls.Config `mapstructure:"tls_conf"`
44+
Servers []string `mapstructure:"servers"`
45+
DiscoveryName string `split_words:"true" mapstructure:"discovery_name"`
46+
TLS *tls.Config `mapstructure:"tls_conf"`
4547

4648
ExchangeDefinition ExchangeDefinition `envconfig:"exchange" mapstructure:"exchange"`
4749
QueueDefinition QueueDefinition `envconfig:"queue" mapstructure:"queue"`
@@ -208,6 +210,14 @@ func ConnectToRabbit(config *RabbitConfig, log *logrus.Entry) (*Consumer, error)
208210
return nil, err
209211
}
210212

213+
if config.DiscoveryName != "" {
214+
servers, err := discoverRabbitServers(config.DiscoveryName)
215+
if err != nil {
216+
return nil, err
217+
}
218+
config.Servers = servers
219+
}
220+
211221
conn, err := DialToRabbit(config.Servers, config.TLS, log)
212222
if err != nil {
213223
return nil, err
@@ -394,6 +404,21 @@ func Consume(channel *amqp.Channel, deliveryDef *DeliveryDefinition) (<-chan amq
394404
)
395405
}
396406

407+
func discoverRabbitServers(serviceName string) ([]string, error) {
408+
rabbitUrls := []string{}
409+
410+
endpoints, err := discovery.DiscoverEndpoints(serviceName)
411+
if err != nil {
412+
return rabbitUrls, err
413+
}
414+
415+
for _, endpoint := range endpoints {
416+
rabbitUrls = append(rabbitUrls, fmt.Sprintf("%s:%d", endpoint.Target, endpoint.Port))
417+
}
418+
419+
return rabbitUrls, nil
420+
}
421+
397422
// ----------------------------------------------------------------------------
398423
// utils
399424
// ----------------------------------------------------------------------------

0 commit comments

Comments
 (0)