-
Notifications
You must be signed in to change notification settings - Fork 9
[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
Conversation
sgirones
commented
Oct 2, 2017
- Get a list of hosts from a DNS name
- Use a DNS name for configuring Rabbit and Nats
90de812
to
b1863e4
Compare
discovery/main.go
Outdated
import ( | ||
"fmt" | ||
|
||
"github.com/miekg/dns" |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
messaging/nats.go
Outdated
Servers []string `mapstructure:"servers"` | ||
LogsSubject string `mapstructure:"log_subject"` | ||
TLS *tls.Config `mapstructure:"tls_conf"` | ||
ServiceDNSName string `mapstructure:"service_dnsname"` |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
messaging/nats.go
Outdated
@@ -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 != "" { |
There was a problem hiding this comment.
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)
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
9b7a3c2
to
8fce4cb
Compare
8fce4cb
to
bc46ca2
Compare
bc46ca2
to
5ddd599
Compare
discovery/main.go
Outdated
Port uint16 | ||
} | ||
|
||
func DiscoverEndpoints(serviceDNS string) ([]Endpoint, error) { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
messaging/nats.go
Outdated
Servers []string `mapstructure:"servers"` | ||
LogsSubject string `mapstructure:"log_subject"` | ||
TLS *tls.Config `mapstructure:"tls_conf"` | ||
DiscoveryName string `mapstructure:"discovery_name"` |
There was a problem hiding this comment.
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
.
messaging/nats.go
Outdated
@@ -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{} |
There was a problem hiding this comment.
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
messaging/nats.go
Outdated
natsUrls = append(natsUrls, fmt.Sprintf("nats://%s:%d", endpoint.Name, endpoint.Port)) | ||
} | ||
|
||
return strings.Join(natsUrls, ","), nil |
There was a problem hiding this comment.
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
messaging/nats.go
Outdated
@@ -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() |
There was a problem hiding this comment.
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...)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah, I like that
messaging/rabbit.go
Outdated
@@ -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 { |
There was a problem hiding this comment.
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.
messaging/rabbit.go
Outdated
@@ -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 |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 = ...
messaging/rabbit.go
Outdated
conn, err := DialToRabbit(config.Servers, config.TLS, log) | ||
servers := config.Servers | ||
if config.DiscoveryName != "" { | ||
servers, err = discoverRabbitServers(config.DiscoveryName) |
There was a problem hiding this comment.
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