Skip to content

Commit

Permalink
switch to filtering addresses by region
Browse files Browse the repository at this point in the history
  • Loading branch information
tgross committed Mar 15, 2023
1 parent 21d9a4a commit 094d3ba
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 34 deletions.
47 changes: 14 additions & 33 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ import (
vaultapi "github.com/hashicorp/vault/api"
"github.com/shirou/gopsutil/v3/host"
"golang.org/x/exp/maps"
"golang.org/x/exp/slices"
)

const (
Expand Down Expand Up @@ -2906,14 +2907,8 @@ func (c *Client) consulDiscoveryImpl() error {
dcs = dcs[0:helper.Min(len(dcs), datacenterQueryLimit)]
}

// Query for servers in this client's region only. Note this has to be an
// unauthenticated request because we haven't registered yet.
// Query for servers in this client's region only
region := c.Region()
rpcargs := structs.GenericRequest{
QueryOptions: structs.QueryOptions{
Region: region,
},
}

serviceName := c.GetConfig().ConsulConfig.ServerServiceName
var mErr multierror.Error
Expand All @@ -2934,41 +2929,27 @@ DISCOLOOP:
}

for _, s := range consulServices {
port := strconv.Itoa(s.ServicePort)
addrstr := s.ServiceAddress
if addrstr == "" {
addrstr = s.Address
}
addr, err := net.ResolveTCPAddr("tcp", net.JoinHostPort(addrstr, port))
if err != nil {
mErr.Errors = append(mErr.Errors, err)
continue
}

srv := &servers.Server{Addr: addr}
nomadServers = append(nomadServers, srv)

// Query the client-advertise RPC addresses from the region that
// Consul gave us
var resp *structs.RPCServersResponse
if err := c.connPool.RPC(region, addr, "Status.RPCServers", rpcargs, &resp); err != nil {
mErr.Errors = append(mErr.Errors, err)
continue
}
for _, member := range resp.Addresses {
addr, err := net.ResolveTCPAddr("tcp", member)
if slices.Contains(s.ServiceTags, region) {
port := strconv.Itoa(s.ServicePort)
addrstr := s.ServiceAddress
if addrstr == "" {
addrstr = s.Address
}
addr, err := net.ResolveTCPAddr("tcp", net.JoinHostPort(addrstr, port))
if err != nil {
mErr.Errors = append(mErr.Errors, err)
continue
}

srv := &servers.Server{Addr: addr}
nomadServers = append(nomadServers, srv)
}
}

if len(nomadServers) > 0 {
break DISCOLOOP
}
if len(nomadServers) > 0 {
break DISCOLOOP
}

}
if len(nomadServers) == 0 {
if len(mErr.Errors) > 0 {
Expand Down
3 changes: 2 additions & 1 deletion command/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -891,7 +891,8 @@ func (a *Agent) setupServer() error {
rpcServ := &structs.Service{
Name: a.config.Consul.ServerServiceName,
PortLabel: a.config.AdvertiseAddrs.RPC,
Tags: append([]string{consul.ServiceTagRPC}, a.config.Consul.Tags...),
Tags: append([]string{consul.ServiceTagRPC, a.config.Region},
a.config.Consul.Tags...),
Checks: []*structs.ServiceCheck{
{
Name: a.config.Consul.ServerRPCCheckName,
Expand Down

0 comments on commit 094d3ba

Please sign in to comment.