Skip to content

Commit

Permalink
fixup! add peer locality to discovery chains
Browse files Browse the repository at this point in the history
  • Loading branch information
erichaberkorn committed Mar 10, 2023
1 parent 045e0ab commit 4331622
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 20 deletions.
10 changes: 10 additions & 0 deletions agent/configentry/discoverychain.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,16 @@ func (e *DiscoveryChainSet) AddProxyDefaults(entries ...*structs.ProxyConfigEntr
}
}

// AddPeers adds cluster peers. Convenience function for testing.
func (e *DiscoveryChainSet) AddPeers(entries ...*pbpeering.Peering) {
if e.Peers == nil {
e.Peers = make(map[string]*pbpeering.Peering)
}
for _, entry := range entries {
e.Peers[entry.Name] = entry
}
}

// AddEntries adds generic configs. Convenience function for testing. Panics on
// operator error.
func (e *DiscoveryChainSet) AddEntries(entries ...structs.ConfigEntry) {
Expand Down
3 changes: 2 additions & 1 deletion agent/consul/discoverychain/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -737,8 +737,9 @@ func (c *compiler) newTarget(opts structs.DiscoveryTargetOpts) *structs.Discover
// Use the same representation for the name. This will NOT be overridden
// later.
t.Name = t.SNI
} else {
peer := c.entries.Peers[opts.Peer]
if peer != nil && peer.Remote != nil && peer.Remote.Locality != nil {
if peer != nil && peer.Remote != nil {
t.Locality = pbpeering.LocalityToStructs(peer.Remote.Locality)
}
}
Expand Down
31 changes: 31 additions & 0 deletions agent/consul/discoverychain/compile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"github.com/hashicorp/consul/agent/configentry"
"github.com/hashicorp/consul/agent/connect"
"github.com/hashicorp/consul/agent/structs"
"github.com/hashicorp/consul/proto/private/pbcommon"
"github.com/hashicorp/consul/proto/private/pbpeering"
)

type compileTestCase struct {
Expand Down Expand Up @@ -1578,12 +1580,25 @@ func testcase_Failover_Targets() compileTestCase {
{Datacenter: "dc3"},
{Service: "new-main"},
{Peer: "cluster-01"},
{Peer: "cluster-02"},
},
},
},
},
)

entries.AddPeers(
&pbpeering.Peering{
Name: "cluster-01",
Remote: &pbpeering.RemoteInfo{
Locality: &pbcommon.Locality{
Region: "us-west-1",
Zone: "us-west-1a",
},
},
},
)

expect := &structs.CompiledDiscoveryChain{
Protocol: "tcp",
StartNode: "resolver:main.default.default.dc1",
Expand All @@ -1599,6 +1614,7 @@ func testcase_Failover_Targets() compileTestCase {
"main.default.default.dc3",
"new-main.default.default.dc1",
"main.default.default.external.cluster-01",
"main.default.default.external.cluster-02",
},
},
},
Expand Down Expand Up @@ -1626,6 +1642,21 @@ func testcase_Failover_Targets() compileTestCase {
"main.default.default.external.cluster-01": newTarget(structs.DiscoveryTargetOpts{
Service: "main",
Peer: "cluster-01",
}, func(t *structs.DiscoveryTarget) {
t.SNI = ""
t.Name = ""
t.Datacenter = ""
t.MeshGateway = structs.MeshGatewayConfig{
Mode: structs.MeshGatewayModeRemote,
}
t.Locality = &structs.Locality{
Region: "us-west-1",
Zone: "us-west-1a",
}
}),
"main.default.default.external.cluster-02": newTarget(structs.DiscoveryTargetOpts{
Service: "main",
Peer: "cluster-02",
}, func(t *structs.DiscoveryTarget) {
t.SNI = ""
t.Name = ""
Expand Down
8 changes: 1 addition & 7 deletions agent/consul/state/config_entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package state
import (
"errors"
"fmt"
"strings"

memdb "github.com/hashicorp/go-memdb"

Expand Down Expand Up @@ -1444,7 +1443,7 @@ func readDiscoveryChainConfigEntriesTxn(
peerEntMeta := structs.DefaultEnterpriseMetaInPartition(entMeta.PartitionOrDefault())
for peerName := range todoPeers {
q := Query{
Value: strings.ToLower(peerName),
Value: peerName,
EnterpriseMeta: *peerEntMeta,
}
idx, entry, err := peeringReadTxn(tx, ws, q)
Expand All @@ -1455,11 +1454,6 @@ func readDiscoveryChainConfigEntriesTxn(
maxIdx = idx
}

if entry == nil {
res.Peers[peerName] = nil
continue
}

res.Peers[peerName] = entry
}

Expand Down
2 changes: 1 addition & 1 deletion agent/consul/state/config_entry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2067,7 +2067,7 @@ func TestStore_ReadDiscoveryChainConfigEntries_SubsetSplit(t *testing.T) {
require.Len(t, entrySet.Services, 1)
}

func TestStore_ReadDiscoveryChainConfigEntries_PeerLocality(t *testing.T) {
func TestStore_ReadDiscoveryChainConfigEntries_FetchPeers(t *testing.T) {
s := testConfigStateStore(t)

entries := []structs.ConfigEntry{
Expand Down
13 changes: 2 additions & 11 deletions agent/structs/config_entry_discoverychain.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/hashicorp/consul/acl"
"github.com/hashicorp/consul/agent/cache"
"github.com/hashicorp/consul/lib"
"github.com/hashicorp/consul/lib/maps"
)

const (
Expand Down Expand Up @@ -888,17 +889,7 @@ func (e *ServiceResolverConfigEntry) RelatedPeers() []string {
}
}

if len(peers) == 0 {
return nil
}

out := make([]string, 0, len(peers))
for svc := range peers {
out = append(out, svc)
}
sort.Strings(out)

return out
return maps.SliceOfKeys(peers)
}

func (e *ServiceResolverConfigEntry) MarshalJSON() ([]byte, error) {
Expand Down

0 comments on commit 4331622

Please sign in to comment.