From f1179cd43af0ac670ea71b53bf5c7f52243ac04e Mon Sep 17 00:00:00 2001 From: Tim Gross Date: Fri, 17 Feb 2023 20:06:53 +0000 Subject: [PATCH] backport of commit 0bf6e65cfb5fc809baf58898e039e88fa592eb4c --- .changelog/16217.txt | 3 +++ client/client.go | 30 ++++++++++++++++++------------ 2 files changed, 21 insertions(+), 12 deletions(-) create mode 100644 .changelog/16217.txt diff --git a/.changelog/16217.txt b/.changelog/16217.txt new file mode 100644 index 00000000000..0ebbc293b23 --- /dev/null +++ b/.changelog/16217.txt @@ -0,0 +1,3 @@ +```release-note:bug +client: Fixed a bug where clients used the serf advertise address to connect to servers when using Consul auto-discovery +``` diff --git a/client/client.go b/client/client.go index da1390501b5..45473875f4c 100644 --- a/client/client.go +++ b/client/client.go @@ -2944,23 +2944,29 @@ DISCOLOOP: mErr.Errors = append(mErr.Errors, err) continue } - var peers []string - if err := c.connPool.RPC(region, addr, "Status.Peers", rpcargs, &peers); err != nil { + + // Query the members from the region that Consul gave us, and + // extract the client-advertise RPC address from each member + var membersResp structs.ServerMembersResponse + if err := c.connPool.RPC(region, addr, "Status.Members", rpcargs, &membersResp); err != nil { mErr.Errors = append(mErr.Errors, err) continue } - - // Successfully received the Server peers list of the correct - // region - for _, p := range peers { - addr, err := net.ResolveTCPAddr("tcp", p) - if err != nil { - mErr.Errors = append(mErr.Errors, err) - continue + for _, member := range membersResp.Members { + if addrTag, ok := member.Tags["rpc_addr"]; ok { + if portTag, ok := member.Tags["port"]; ok { + addr, err := net.ResolveTCPAddr("tcp", + fmt.Sprintf("%s:%s", addrTag, portTag)) + if err != nil { + mErr.Errors = append(mErr.Errors, err) + continue + } + srv := &servers.Server{Addr: addr} + nomadServers = append(nomadServers, srv) + } } - srv := &servers.Server{Addr: addr} - nomadServers = append(nomadServers, srv) } + if len(nomadServers) > 0 { break DISCOLOOP }