From bbe46e95221e3cc65e80fba75b5524a84b13ac00 Mon Sep 17 00:00:00 2001 From: freddygv Date: Thu, 28 Oct 2021 18:41:58 -0600 Subject: [PATCH] Update locality check in xds --- agent/xds/clusters.go | 14 +++++++++++--- agent/xds/endpoints.go | 16 ++++++++-------- agent/xds/endpoints_test.go | 2 +- 3 files changed, 20 insertions(+), 12 deletions(-) diff --git a/agent/xds/clusters.go b/agent/xds/clusters.go index 0bcd66fb93..0910eed769 100644 --- a/agent/xds/clusters.go +++ b/agent/xds/clusters.go @@ -209,14 +209,14 @@ func (s *ResourceGenerator) clustersFromSnapshotMeshGateway(cfgSnap *proxycfg.Co // Generate the remote clusters for _, key := range keys { - if key.Matches(cfgSnap.Datacenter, cfgSnap.ProxyID.PartitionOrEmpty()) { + if key.Matches(cfgSnap.Datacenter, cfgSnap.ProxyID.PartitionOrDefault()) { continue // skip local } opts := gatewayClusterOpts{ name: connect.GatewaySNI(key.Datacenter, key.Partition, cfgSnap.Roots.TrustDomain), hostnameEndpoints: cfgSnap.MeshGateway.HostnameDatacenters[key.String()], - isRemote: key.Datacenter != cfgSnap.Datacenter, + isRemote: true, } cluster := s.makeGatewayCluster(cfgSnap, opts) clusters = append(clusters, cluster) @@ -238,7 +238,7 @@ func (s *ResourceGenerator) clustersFromSnapshotMeshGateway(cfgSnap *proxycfg.Co opts := gatewayClusterOpts{ name: cfgSnap.ServerSNIFn(key.Datacenter, ""), hostnameEndpoints: hostnameEndpoints, - isRemote: key.Datacenter != cfgSnap.Datacenter, + isRemote: !key.Matches(cfgSnap.Datacenter, cfgSnap.ProxyID.PartitionOrDefault()), } cluster := s.makeGatewayCluster(cfgSnap, opts) clusters = append(clusters, cluster) @@ -299,10 +299,17 @@ func (s *ResourceGenerator) makeGatewayServiceClusters( hostnameEndpoints = cfgSnap.TerminatingGateway.HostnameServices[svc] } + localKey := proxycfg.GatewayKey{Partition: cfgSnap.ProxyID.PartitionOrDefault(), Datacenter: cfgSnap.Datacenter} + var isRemote bool + if len(services[svc]) > 0 { + isRemote = !localKey.Matches(services[svc][0].Node.Datacenter, services[svc][0].Node.PartitionOrDefault()) + } + opts := gatewayClusterOpts{ name: clusterName, hostnameEndpoints: hostnameEndpoints, connectTimeout: resolver.ConnectTimeout, + isRemote: isRemote, } cluster := s.makeGatewayCluster(cfgSnap, opts) @@ -323,6 +330,7 @@ func (s *ResourceGenerator) makeGatewayServiceClusters( hostnameEndpoints: subsetHostnameEndpoints, onlyPassing: subset.OnlyPassing, connectTimeout: resolver.ConnectTimeout, + isRemote: isRemote, } cluster := s.makeGatewayCluster(cfgSnap, opts) diff --git a/agent/xds/endpoints.go b/agent/xds/endpoints.go index 073a7ac320..9c9a02b2f4 100644 --- a/agent/xds/endpoints.go +++ b/agent/xds/endpoints.go @@ -79,7 +79,7 @@ func (s *ResourceGenerator) endpointsFromSnapshotConnectProxy(cfgSnap *proxycfg. []loadAssignmentEndpointGroup{ {Endpoints: endpoints}, }, - cfgSnap.Datacenter, + proxycfg.GatewayKey{Datacenter: cfgSnap.Datacenter, Partition: cfgSnap.ProxyID.PartitionOrDefault()}, ) resources = append(resources, la) } @@ -114,7 +114,7 @@ func (s *ResourceGenerator) endpointsFromSnapshotMeshGateway(cfgSnap *proxycfg.C resources := make([]proto.Message, 0, len(keys)+len(cfgSnap.MeshGateway.ServiceGroups)) for _, key := range keys { - if key.Matches(cfgSnap.Datacenter, cfgSnap.ProxyID.PartitionOrEmpty()) { + if key.Matches(cfgSnap.Datacenter, cfgSnap.ProxyID.PartitionOrDefault()) { continue // skip local } // Also skip gateways with a hostname as their address. EDS cannot resolve hostnames, @@ -140,7 +140,7 @@ func (s *ResourceGenerator) endpointsFromSnapshotMeshGateway(cfgSnap *proxycfg.C []loadAssignmentEndpointGroup{ {Endpoints: endpoints}, }, - cfgSnap.Datacenter, + proxycfg.GatewayKey{Datacenter: cfgSnap.Datacenter, Partition: cfgSnap.ProxyID.PartitionOrDefault()}, ) resources = append(resources, la) } @@ -155,7 +155,7 @@ func (s *ResourceGenerator) endpointsFromSnapshotMeshGateway(cfgSnap *proxycfg.C []loadAssignmentEndpointGroup{ {Endpoints: endpoints}, }, - cfgSnap.Datacenter, + proxycfg.GatewayKey{Datacenter: cfgSnap.Datacenter, Partition: cfgSnap.ProxyID.PartitionOrDefault()}, ) resources = append(resources, la) } @@ -256,7 +256,7 @@ func (s *ResourceGenerator) endpointsFromServicesAndResolvers( la := makeLoadAssignment( clusterName, groups, - cfgSnap.Datacenter, + proxycfg.GatewayKey{Datacenter: cfgSnap.Datacenter, Partition: cfgSnap.ProxyID.PartitionOrDefault()}, ) resources = append(resources, la) } @@ -427,7 +427,7 @@ func (s *ResourceGenerator) endpointsFromDiscoveryChain( la := makeLoadAssignment( clusterName, endpointGroups, - gatewayKey.Datacenter, + gatewayKey, ) resources = append(resources, la) } @@ -441,7 +441,7 @@ type loadAssignmentEndpointGroup struct { OverrideHealth envoy_core_v3.HealthStatus } -func makeLoadAssignment(clusterName string, endpointGroups []loadAssignmentEndpointGroup, localDatacenter string) *envoy_endpoint_v3.ClusterLoadAssignment { +func makeLoadAssignment(clusterName string, endpointGroups []loadAssignmentEndpointGroup, localKey proxycfg.GatewayKey) *envoy_endpoint_v3.ClusterLoadAssignment { cla := &envoy_endpoint_v3.ClusterLoadAssignment{ ClusterName: clusterName, Endpoints: make([]*envoy_endpoint_v3.LocalityLbEndpoints, 0, len(endpointGroups)), @@ -461,7 +461,7 @@ func makeLoadAssignment(clusterName string, endpointGroups []loadAssignmentEndpo for _, ep := range endpoints { // TODO (mesh-gateway) - should we respect the translate_wan_addrs configuration here or just always use the wan for cross-dc? - addr, port := ep.BestAddress(localDatacenter != ep.Node.Datacenter) + addr, port := ep.BestAddress(!localKey.Matches(ep.Node.Datacenter, ep.Node.PartitionOrDefault())) healthStatus, weight := calculateEndpointHealthAndWeight(ep, endpointGroup.OnlyPassing) if endpointGroup.OverrideHealth != envoy_core_v3.HealthStatus_UNKNOWN { diff --git a/agent/xds/endpoints_test.go b/agent/xds/endpoints_test.go index c52012c8e3..e9a982be0f 100644 --- a/agent/xds/endpoints_test.go +++ b/agent/xds/endpoints_test.go @@ -209,7 +209,7 @@ func Test_makeLoadAssignment(t *testing.T) { got := makeLoadAssignment( tt.clusterName, tt.endpoints, - "dc1", + proxycfg.GatewayKey{Datacenter: "dc1"}, ) require.Equal(t, tt.want, got) })