Store GatewayKey in proxycfg snapshot for re-use

This commit is contained in:
freddygv 2021-10-28 18:47:42 -06:00
parent bbe46e9522
commit 90ce897456
8 changed files with 50 additions and 39 deletions

View File

@ -239,6 +239,7 @@ func TestManager_BasicLifecycle(t *testing.T) {
IntentionsSet: true, IntentionsSet: true,
}, },
Datacenter: "dc1", Datacenter: "dc1",
Locality: GatewayKey{Datacenter: "dc1", Partition: structs.PartitionOrDefault("")},
}, },
}, },
{ {
@ -296,6 +297,7 @@ func TestManager_BasicLifecycle(t *testing.T) {
IntentionsSet: true, IntentionsSet: true,
}, },
Datacenter: "dc1", Datacenter: "dc1",
Locality: GatewayKey{Datacenter: "dc1", Partition: structs.PartitionOrDefault("")},
}, },
}, },
} }

View File

@ -144,7 +144,7 @@ func (s *handlerMeshGateway) handleUpdate(ctx context.Context, u cache.UpdateEve
for dc, nodes := range dcIndexedNodes.DatacenterNodes { for dc, nodes := range dcIndexedNodes.DatacenterNodes {
snap.MeshGateway.HostnameDatacenters[dc] = hostnameEndpoints( snap.MeshGateway.HostnameDatacenters[dc] = hostnameEndpoints(
s.logger.Named(logging.MeshGateway), s.logger.Named(logging.MeshGateway),
GatewayKey{Partition: snap.ProxyID.PartitionOrDefault(), Datacenter: snap.Datacenter}, snap.Locality,
nodes, nodes,
) )
} }
@ -327,7 +327,7 @@ func (s *handlerMeshGateway) handleUpdate(ctx context.Context, u cache.UpdateEve
snap.MeshGateway.GatewayGroups[key] = resp.Nodes snap.MeshGateway.GatewayGroups[key] = resp.Nodes
snap.MeshGateway.HostnameDatacenters[key] = hostnameEndpoints( snap.MeshGateway.HostnameDatacenters[key] = hostnameEndpoints(
s.logger.Named(logging.MeshGateway), s.logger.Named(logging.MeshGateway),
GatewayKey{Partition: snap.ProxyID.PartitionOrDefault(), Datacenter: snap.Datacenter}, snap.Locality,
resp.Nodes, resp.Nodes,
) )
} }

View File

@ -408,6 +408,7 @@ type ConfigSnapshot struct {
Proxy structs.ConnectProxyConfig Proxy structs.ConnectProxyConfig
Datacenter string Datacenter string
IntentionDefaultAllow bool IntentionDefaultAllow bool
Locality GatewayKey
ServerSNIFn ServerSNIFunc ServerSNIFn ServerSNIFunc
Roots *structs.IndexedCARoots Roots *structs.IndexedCARoots

View File

@ -254,6 +254,7 @@ func newConfigSnapshotFromServiceInstance(s serviceInstance, config stateConfig)
TaggedAddresses: s.taggedAddresses, TaggedAddresses: s.taggedAddresses,
Proxy: s.proxyCfg, Proxy: s.proxyCfg,
Datacenter: config.source.Datacenter, Datacenter: config.source.Datacenter,
Locality: GatewayKey{Datacenter: config.source.Datacenter, Partition: s.proxyID.PartitionOrDefault()},
ServerSNIFn: config.serverSNIFn, ServerSNIFn: config.serverSNIFn,
IntentionDefaultAllow: config.intentionDefaultAllow, IntentionDefaultAllow: config.intentionDefaultAllow,
} }

View File

@ -286,7 +286,7 @@ func (s *handlerTerminatingGateway) handleUpdate(ctx context.Context, u cache.Up
snap.TerminatingGateway.ServiceGroups[sn] = resp.Nodes snap.TerminatingGateway.ServiceGroups[sn] = resp.Nodes
snap.TerminatingGateway.HostnameServices[sn] = hostnameEndpoints( snap.TerminatingGateway.HostnameServices[sn] = hostnameEndpoints(
s.logger, s.logger,
GatewayKey{Partition: snap.ProxyID.PartitionOrDefault(), Datacenter: snap.Datacenter}, snap.Locality,
resp.Nodes, resp.Nodes,
) )
} }

View File

@ -13,6 +13,7 @@ import (
"github.com/mitchellh/go-testing-interface" "github.com/mitchellh/go-testing-interface"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"github.com/hashicorp/consul/acl"
"github.com/hashicorp/consul/agent/cache" "github.com/hashicorp/consul/agent/cache"
cachetype "github.com/hashicorp/consul/agent/cache-types" cachetype "github.com/hashicorp/consul/agent/cache-types"
"github.com/hashicorp/consul/agent/connect" "github.com/hashicorp/consul/agent/connect"
@ -674,6 +675,7 @@ func TestConfigSnapshot(t testing.T) *ConfigSnapshot {
upstreams := structs.TestUpstreams(t) upstreams := structs.TestUpstreams(t)
return &ConfigSnapshot{ return &ConfigSnapshot{
Locality: GatewayKey{Datacenter: "dc1", Partition: acl.DefaultPartitionName},
Kind: structs.ServiceKindConnectProxy, Kind: structs.ServiceKindConnectProxy,
Service: "web-sidecar-proxy", Service: "web-sidecar-proxy",
ProxyID: structs.NewServiceID("web-sidecar-proxy", nil), ProxyID: structs.NewServiceID("web-sidecar-proxy", nil),
@ -798,6 +800,7 @@ func testConfigSnapshotDiscoveryChain(t testing.T, variation string, additionalE
roots, leaf := TestCerts(t) roots, leaf := TestCerts(t)
snap := &ConfigSnapshot{ snap := &ConfigSnapshot{
Locality: GatewayKey{Datacenter: "dc1", Partition: acl.DefaultPartitionName},
Kind: structs.ServiceKindConnectProxy, Kind: structs.ServiceKindConnectProxy,
Service: "web-sidecar-proxy", Service: "web-sidecar-proxy",
ProxyID: structs.NewServiceID("web-sidecar-proxy", nil), ProxyID: structs.NewServiceID("web-sidecar-proxy", nil),
@ -1510,6 +1513,7 @@ func TestConfigSnapshotMeshGatewayNoServices(t testing.T) *ConfigSnapshot {
func testConfigSnapshotMeshGateway(t testing.T, populateServices bool, useFederationStates bool) *ConfigSnapshot { func testConfigSnapshotMeshGateway(t testing.T, populateServices bool, useFederationStates bool) *ConfigSnapshot {
roots, _ := TestCerts(t) roots, _ := TestCerts(t)
snap := &ConfigSnapshot{ snap := &ConfigSnapshot{
Locality: GatewayKey{Datacenter: "dc1", Partition: acl.DefaultPartitionName},
Kind: structs.ServiceKindMeshGateway, Kind: structs.ServiceKindMeshGateway,
Service: "mesh-gateway", Service: "mesh-gateway",
ProxyID: structs.NewServiceID("mesh-gateway", nil), ProxyID: structs.NewServiceID("mesh-gateway", nil),
@ -1721,6 +1725,7 @@ func testConfigSnapshotIngressGateway(
roots, leaf := TestCerts(t) roots, leaf := TestCerts(t)
snap := &ConfigSnapshot{ snap := &ConfigSnapshot{
Locality: GatewayKey{Datacenter: "dc1", Partition: acl.DefaultPartitionName},
Kind: structs.ServiceKindIngressGateway, Kind: structs.ServiceKindIngressGateway,
Service: "ingress-gateway", Service: "ingress-gateway",
ProxyID: structs.NewServiceID("ingress-gateway", nil), ProxyID: structs.NewServiceID("ingress-gateway", nil),
@ -1760,6 +1765,7 @@ func testConfigSnapshotIngressGateway(
func TestConfigSnapshotExposeConfig(t testing.T) *ConfigSnapshot { func TestConfigSnapshotExposeConfig(t testing.T) *ConfigSnapshot {
return &ConfigSnapshot{ return &ConfigSnapshot{
Locality: GatewayKey{Datacenter: "dc1", Partition: acl.DefaultPartitionName},
Kind: structs.ServiceKindConnectProxy, Kind: structs.ServiceKindConnectProxy,
Service: "web-proxy", Service: "web-proxy",
ProxyID: structs.NewServiceID("web-proxy", nil), ProxyID: structs.NewServiceID("web-proxy", nil),
@ -1801,6 +1807,7 @@ func testConfigSnapshotTerminatingGateway(t testing.T, populateServices bool) *C
roots, _ := TestCerts(t) roots, _ := TestCerts(t)
snap := &ConfigSnapshot{ snap := &ConfigSnapshot{
Locality: GatewayKey{Datacenter: "dc1", Partition: acl.DefaultPartitionName},
Kind: structs.ServiceKindTerminatingGateway, Kind: structs.ServiceKindTerminatingGateway,
Service: "terminating-gateway", Service: "terminating-gateway",
ProxyID: structs.NewServiceID("terminating-gateway", nil), ProxyID: structs.NewServiceID("terminating-gateway", nil),
@ -2035,6 +2042,7 @@ func testConfigSnapshotTerminatingGateway(t testing.T, populateServices bool) *C
func TestConfigSnapshotGRPCExposeHTTP1(t testing.T) *ConfigSnapshot { func TestConfigSnapshotGRPCExposeHTTP1(t testing.T) *ConfigSnapshot {
return &ConfigSnapshot{ return &ConfigSnapshot{
Locality: GatewayKey{Datacenter: "dc1", Partition: acl.DefaultPartitionName},
Kind: structs.ServiceKindConnectProxy, Kind: structs.ServiceKindConnectProxy,
Service: "grpc-proxy", Service: "grpc-proxy",
ProxyID: structs.NewServiceID("grpc-proxy", nil), ProxyID: structs.NewServiceID("grpc-proxy", nil),

View File

@ -299,10 +299,9 @@ func (s *ResourceGenerator) makeGatewayServiceClusters(
hostnameEndpoints = cfgSnap.TerminatingGateway.HostnameServices[svc] hostnameEndpoints = cfgSnap.TerminatingGateway.HostnameServices[svc]
} }
localKey := proxycfg.GatewayKey{Partition: cfgSnap.ProxyID.PartitionOrDefault(), Datacenter: cfgSnap.Datacenter}
var isRemote bool var isRemote bool
if len(services[svc]) > 0 { if len(services[svc]) > 0 {
isRemote = !localKey.Matches(services[svc][0].Node.Datacenter, services[svc][0].Node.PartitionOrDefault()) isRemote = !cfgSnap.Locality.Matches(services[svc][0].Node.Datacenter, services[svc][0].Node.PartitionOrDefault())
} }
opts := gatewayClusterOpts{ opts := gatewayClusterOpts{

View File

@ -51,7 +51,7 @@ func (s *ResourceGenerator) endpointsFromSnapshotConnectProxy(cfgSnap *proxycfg.
es := s.endpointsFromDiscoveryChain( es := s.endpointsFromDiscoveryChain(
id, id,
chain, chain,
proxycfg.GatewayKey{Datacenter: cfgSnap.Datacenter, Partition: cfgSnap.ProxyID.PartitionOrDefault()}, cfgSnap.Locality,
cfgSnap.ConnectProxy.UpstreamConfig[id], cfgSnap.ConnectProxy.UpstreamConfig[id],
cfgSnap.ConnectProxy.WatchedUpstreamEndpoints[id], cfgSnap.ConnectProxy.WatchedUpstreamEndpoints[id],
cfgSnap.ConnectProxy.WatchedGatewayEndpoints[id], cfgSnap.ConnectProxy.WatchedGatewayEndpoints[id],
@ -79,7 +79,7 @@ func (s *ResourceGenerator) endpointsFromSnapshotConnectProxy(cfgSnap *proxycfg.
[]loadAssignmentEndpointGroup{ []loadAssignmentEndpointGroup{
{Endpoints: endpoints}, {Endpoints: endpoints},
}, },
proxycfg.GatewayKey{Datacenter: cfgSnap.Datacenter, Partition: cfgSnap.ProxyID.PartitionOrDefault()}, cfgSnap.Locality,
) )
resources = append(resources, la) resources = append(resources, la)
} }
@ -140,7 +140,7 @@ func (s *ResourceGenerator) endpointsFromSnapshotMeshGateway(cfgSnap *proxycfg.C
[]loadAssignmentEndpointGroup{ []loadAssignmentEndpointGroup{
{Endpoints: endpoints}, {Endpoints: endpoints},
}, },
proxycfg.GatewayKey{Datacenter: cfgSnap.Datacenter, Partition: cfgSnap.ProxyID.PartitionOrDefault()}, cfgSnap.Locality,
) )
resources = append(resources, la) resources = append(resources, la)
} }
@ -155,7 +155,7 @@ func (s *ResourceGenerator) endpointsFromSnapshotMeshGateway(cfgSnap *proxycfg.C
[]loadAssignmentEndpointGroup{ []loadAssignmentEndpointGroup{
{Endpoints: endpoints}, {Endpoints: endpoints},
}, },
proxycfg.GatewayKey{Datacenter: cfgSnap.Datacenter, Partition: cfgSnap.ProxyID.PartitionOrDefault()}, cfgSnap.Locality,
) )
resources = append(resources, la) resources = append(resources, la)
} }
@ -256,7 +256,7 @@ func (s *ResourceGenerator) endpointsFromServicesAndResolvers(
la := makeLoadAssignment( la := makeLoadAssignment(
clusterName, clusterName,
groups, groups,
proxycfg.GatewayKey{Datacenter: cfgSnap.Datacenter, Partition: cfgSnap.ProxyID.PartitionOrDefault()}, cfgSnap.Locality,
) )
resources = append(resources, la) resources = append(resources, la)
} }