Skip to content

[discovery] Add DNS discovery methods. Rabbit and Nats config to support DNS discovery #38

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

Merged
merged 4 commits into from
Oct 4, 2017

Conversation

sgirones
Copy link
Contributor

@sgirones sgirones commented Oct 2, 2017

  • Get a list of hosts from a DNS name
  • Use a DNS name for configuring Rabbit and Nats

@sgirones sgirones changed the title [discovery] Add DNS discovery methods. Rabbit and Nats config support DNS discovery [discovery] Add DNS discovery methods. Rabbit and Nats config to support DNS discovery Oct 2, 2017
@sgirones sgirones force-pushed the salvador/add-dns-discovery-package branch from 90de812 to b1863e4 Compare October 2, 2017 15:59
import (
"fmt"

"github.com/miekg/dns"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there a reason to not use the builtin https://golang.org/pkg/net/#LookupSRV?

We aren't specifying a particular DNS server, just using the resolve.conf, meaning we gain nothing afaict

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TBH I googled around and found that library to be used in some places. I will take a look at your suggestion

Servers []string `mapstructure:"servers"`
LogsSubject string `mapstructure:"log_subject"`
TLS *tls.Config `mapstructure:"tls_conf"`
ServiceDNSName string `mapstructure:"service_dnsname"`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add an envconfig:"dns_name" so that we can access it by envvars easily? It is redundant to say "service_" b/c it is embedded in a nats config already

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wanted to add "service" to make it explicit that it was a "discovery" DNS name, not a simple DNS name pointing to a host

@@ -64,6 +66,23 @@ func ConfigureNatsConnection(config *NatsConfig, log *logrus.Entry) (*nats.Conn,

// ConnectToNats will do a TLS connection to the nats servers specified
func ConnectToNats(config *NatsConfig, errHandler nats.ErrHandler) (*nats.Conn, error) {
serversString := config.ServerString()

if config.ServiceDNSName != "" {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should pull this into a method.

if config.ServiceDNSName != "" {
  config.Servers = discoverNatsService(config.ServicDNSName)
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@sgirones sgirones force-pushed the salvador/add-dns-discovery-package branch from 9b7a3c2 to 8fce4cb Compare October 3, 2017 11:13
@sgirones sgirones force-pushed the salvador/add-dns-discovery-package branch from 8fce4cb to bc46ca2 Compare October 3, 2017 11:14
@sgirones sgirones force-pushed the salvador/add-dns-discovery-package branch from bc46ca2 to 5ddd599 Compare October 3, 2017 11:21
Port uint16
}

func DiscoverEndpoints(serviceDNS string) ([]Endpoint, error) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

personally, I'd do something like this

func DiscoverEndpoints(serviceName string) ([]Endpoint, error) {
  _, remotes, err := net.LookupSRV("", "", serviceName) 
  if err != nil {
    return nil, err
  }

  endpoints := []Endpoint{}
  for _, n := range remotes {
    endpoints = append(...)
  }
  return endpoints, nil
}

a little different, mostly around some spacing and where I'd declare endpoints. That is mostly style though.

I am not sure if we need the distinction though. I mean the remote structure is pretty close to the Endpoint in general. I don't think we are getting much by doing this in commons really.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Initially I wanted to add more methods to Endpoint, but I guess it is worthless right now

Servers []string `mapstructure:"servers"`
LogsSubject string `mapstructure:"log_subject"`
TLS *tls.Config `mapstructure:"tls_conf"`
DiscoveryName string `mapstructure:"discovery_name"`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add a split_words:"true" so that the envconfig would be something like WHATEVER_NATS_DISCOVERY_NAME.

@@ -92,3 +104,18 @@ func ErrorHandler(log *logrus.Entry) nats.ErrHandler {
}).Error("Error while consuming from " + sub.Subject)
}
}

func buildNatsServersString(serviceName string) (string, error) {
natsUrls := []string{}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/s/natsUrls/natsURLs/ ~ go uppercases any acronyms

natsUrls = append(natsUrls, fmt.Sprintf("nats://%s:%d", endpoint.Name, endpoint.Port))
}

return strings.Join(natsUrls, ","), nil
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would return the nats urls directly

@@ -64,6 +66,16 @@ func ConfigureNatsConnection(config *NatsConfig, log *logrus.Entry) (*nats.Conn,

// ConnectToNats will do a TLS connection to the nats servers specified
func ConnectToNats(config *NatsConfig, errHandler nats.ErrHandler) (*nats.Conn, error) {
var err error
serversString := config.ServerString()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

personally, I'd choose to do this after the discover block. Something like:

if config.DiscoveryName != "" {
  var err error
  config.Servers, err = discoverNatsURLs(config.DiscoveryName)
  if err != nil {
    return nil, errors.Wrapf(err, "Failed to lookup servers for service %s", config.DiscoveryName)
  }
}
...

return nats.Connect(config.ServerString(), options...)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, I like that

@@ -204,11 +206,20 @@ func ValidateRabbitConfigStruct(servers []string, exchange ExchangeDefinition, q

// ConnectToRabbit will open a TLS connection to rabbit mq
func ConnectToRabbit(config *RabbitConfig, log *logrus.Entry) (*Consumer, error) {
if err := ValidateRabbitConfig(config); err != nil {
var err error
if err = ValidateRabbitConfig(config); err != nil {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

personally, I keep var scoped down in if statements. It stops from accidentally using the wrong err b/c of shadowing.

@@ -204,11 +206,20 @@ func ValidateRabbitConfigStruct(servers []string, exchange ExchangeDefinition, q

// ConnectToRabbit will open a TLS connection to rabbit mq
func ConnectToRabbit(config *RabbitConfig, log *logrus.Entry) (*Consumer, error) {
if err := ValidateRabbitConfig(config); err != nil {
var err error
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

declare this in the if block you're using it in

Copy link
Contributor Author

@sgirones sgirones Oct 4, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem I found is, when I assign the result of discoverRabbitServers to servers and err, if I use :=, it fails because of:

servers declared and not used (build)

I guess I could have used a tmp var and then assign it to servers?

Declaring err outside the if, allowed me to do:

servers, err = ...

conn, err := DialToRabbit(config.Servers, config.TLS, log)
servers := config.Servers
if config.DiscoveryName != "" {
servers, err = discoverRabbitServers(config.DiscoveryName)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

again, I'd set the config.Servers this way when you inspect the object afterwards you don't have a difference from what it is connecting an what it says it is using

@calavera calavera merged commit 44fb358 into master Oct 4, 2017
@calavera calavera deleted the salvador/add-dns-discovery-package branch October 4, 2017 17:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants