Merge pull request #15065 from hashicorp/gh-15051-advertise_addr_wan

This commit is contained in:
Freddy 2022-10-19 18:10:56 -06:00 committed by GitHub
commit 678f8b8876
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 40 additions and 4 deletions

3
.changelog/15065.txt Normal file
View File

@ -0,0 +1,3 @@
```release-note:bug
peering: fix the error of wan address isn't taken by the peering token.
```

View File

@ -205,23 +205,33 @@ func meshGatewayAdresses(state *state.Store, ws memdb.WatchSet, wan bool) ([]str
return addrs, nil return addrs, nil
} }
func parseNodeAddr(node *structs.ServiceNode) string {
// Prefer the wan address
if v, ok := node.TaggedAddresses[structs.TaggedAddressWAN]; ok {
return v
}
return node.Address
}
func serverAddresses(state *state.Store) ([]string, error) { func serverAddresses(state *state.Store) ([]string, error) {
_, nodes, err := state.ServiceNodes(nil, "consul", structs.DefaultEnterpriseMetaInDefaultPartition(), structs.DefaultPeerKeyword) _, nodes, err := state.ServiceNodes(nil, structs.ConsulServiceName, structs.DefaultEnterpriseMetaInDefaultPartition(), structs.DefaultPeerKeyword)
if err != nil { if err != nil {
return nil, err return nil, err
} }
var addrs []string var addrs []string
for _, node := range nodes { for _, node := range nodes {
addr := parseNodeAddr(node)
// Prefer the TLS port if it is defined. // Prefer the TLS port if it is defined.
grpcPortStr := node.ServiceMeta["grpc_tls_port"] grpcPortStr := node.ServiceMeta["grpc_tls_port"]
if v, err := strconv.Atoi(grpcPortStr); err == nil && v > 0 { if v, err := strconv.Atoi(grpcPortStr); err == nil && v > 0 {
addrs = append(addrs, node.Address+":"+grpcPortStr) addrs = append(addrs, addr+":"+grpcPortStr)
continue continue
} }
// Fallback to the standard port if TLS is not defined. // Fallback to the standard port if TLS is not defined.
grpcPortStr = node.ServiceMeta["grpc_port"] grpcPortStr = node.ServiceMeta["grpc_port"]
if v, err := strconv.Atoi(grpcPortStr); err == nil && v > 0 { if v, err := strconv.Atoi(grpcPortStr); err == nil && v > 0 {
addrs = append(addrs, node.Address+":"+grpcPortStr) addrs = append(addrs, addr+":"+grpcPortStr)
continue continue
} }
// Skip node if neither defined. // Skip node if neither defined.

View File

@ -9,6 +9,7 @@ import (
gogrpc "google.golang.org/grpc" gogrpc "google.golang.org/grpc"
"github.com/hashicorp/consul/acl"
"github.com/hashicorp/consul/agent/connect" "github.com/hashicorp/consul/agent/connect"
"github.com/hashicorp/consul/agent/consul/state" "github.com/hashicorp/consul/agent/consul/state"
"github.com/hashicorp/consul/agent/pool" "github.com/hashicorp/consul/agent/pool"
@ -99,6 +100,28 @@ func TestPeeringBackend_GetLocalServerAddresses(t *testing.T) {
require.Equal(t, []string{expect}, addrs) require.Equal(t, []string{expect}, addrs)
}) })
testutil.RunStep(t, "prefer WAN address for servers", func(t *testing.T) {
req := structs.RegisterRequest{
Datacenter: cfg.Datacenter,
Node: cfg.NodeName,
ID: cfg.NodeID,
Address: "127.0.0.1",
EnterpriseMeta: *acl.DefaultEnterpriseMeta(),
// Add a tagged WAN address to the server registration
TaggedAddresses: map[string]string{
structs.TaggedAddressWAN: "3.4.5.6",
},
}
require.NoError(t, srv.fsm.State().EnsureRegistration(200, &req))
addrs, err := backend.GetLocalServerAddresses()
require.NoError(t, err)
expect := fmt.Sprintf("3.4.5.6:%d", srv.config.GRPCTLSPort)
require.Equal(t, []string{expect}, addrs)
})
testutil.RunStep(t, "existence of mesh config entry is not enough to peer through gateways", func(t *testing.T) { testutil.RunStep(t, "existence of mesh config entry is not enough to peer through gateways", func(t *testing.T) {
mesh := structs.MeshConfigEntry{ mesh := structs.MeshConfigEntry{
// Enable unrelated config. // Enable unrelated config.
@ -112,7 +135,7 @@ func TestPeeringBackend_GetLocalServerAddresses(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
// Still expect server address because PeerThroughMeshGateways was not enabled. // Still expect server address because PeerThroughMeshGateways was not enabled.
expect := fmt.Sprintf("127.0.0.1:%d", srv.config.GRPCTLSPort) expect := fmt.Sprintf("3.4.5.6:%d", srv.config.GRPCTLSPort)
require.Equal(t, []string{expect}, addrs) require.Equal(t, []string{expect}, addrs)
}) })