From f56810132ff6227d0aed34de8974587f5a6874e9 Mon Sep 17 00:00:00 2001 From: "Chris S. Kim" Date: Wed, 13 Jul 2022 16:12:01 -0400 Subject: [PATCH] Check if an upstream is implicit from either intentions or peered services --- agent/proxycfg/snapshot.go | 6 ++++++ agent/proxycfg/upstreams.go | 3 ++- agent/xds/clusters.go | 8 ++++---- agent/xds/endpoints.go | 8 ++++---- agent/xds/listeners.go | 8 ++++---- agent/xds/routes.go | 3 ++- 6 files changed, 22 insertions(+), 14 deletions(-) diff --git a/agent/proxycfg/snapshot.go b/agent/proxycfg/snapshot.go index 3835c2a01b..6a02aad1e6 100644 --- a/agent/proxycfg/snapshot.go +++ b/agent/proxycfg/snapshot.go @@ -170,6 +170,12 @@ func (c *configSnapshotConnectProxy) isEmpty() bool { len(c.PeerUpstreamEndpointsUseHostnames) == 0 } +func (c *configSnapshotConnectProxy) IsImplicitUpstream(uid UpstreamID) bool { + _, intentionImplicit := c.IntentionUpstreams[uid] + _, peeringImplicit := c.PeeredUpstreams[uid] + return intentionImplicit || peeringImplicit +} + type configSnapshotTerminatingGateway struct { MeshConfig *structs.MeshConfigEntry MeshConfigSet bool diff --git a/agent/proxycfg/upstreams.go b/agent/proxycfg/upstreams.go index 4f41d7908e..a47510543c 100644 --- a/agent/proxycfg/upstreams.go +++ b/agent/proxycfg/upstreams.go @@ -71,7 +71,8 @@ func (s *handlerUpstreams) handleUpdateUpstreams(ctx context.Context, u UpdateEv case structs.ServiceKindConnectProxy: explicit := snap.ConnectProxy.UpstreamConfig[uid].HasLocalPortOrSocket() - if _, implicit := snap.ConnectProxy.IntentionUpstreams[uid]; !implicit && !explicit { + implicit := snap.ConnectProxy.IsImplicitUpstream(uid) + if !implicit && !explicit { // Discovery chain is not associated with a known explicit or implicit upstream so it is purged/skipped. // The associated watch was likely cancelled. delete(upstreamsSnapshot.DiscoveryChain, uid) diff --git a/agent/xds/clusters.go b/agent/xds/clusters.go index 0d5c147762..b4f4eea39b 100644 --- a/agent/xds/clusters.go +++ b/agent/xds/clusters.go @@ -98,7 +98,8 @@ func (s *ResourceGenerator) clustersFromSnapshotConnectProxy(cfgSnap *proxycfg.C upstream := cfgSnap.ConnectProxy.UpstreamConfig[uid] explicit := upstream.HasLocalPortOrSocket() - if _, implicit := cfgSnap.ConnectProxy.IntentionUpstreams[uid]; !implicit && !explicit { + implicit := cfgSnap.ConnectProxy.IsImplicitUpstream(uid) + if !implicit && !explicit { // Discovery chain is not associated with a known explicit or implicit upstream so it is skipped. continue } @@ -129,13 +130,12 @@ func (s *ResourceGenerator) clustersFromSnapshotConnectProxy(cfgSnap *proxycfg.C // NOTE: Any time we skip an upstream below we MUST also skip that same // upstream in endpoints.go so that the sets of endpoints generated matches // the sets of clusters. - // - // TODO(peering): make this work for tproxy for _, uid := range cfgSnap.ConnectProxy.PeeredUpstreamIDs() { upstreamCfg := cfgSnap.ConnectProxy.UpstreamConfig[uid] explicit := upstreamCfg.HasLocalPortOrSocket() - if _, implicit := cfgSnap.ConnectProxy.IntentionUpstreams[uid]; !implicit && !explicit { + implicit := cfgSnap.ConnectProxy.IsImplicitUpstream(uid) + if !implicit && !explicit { // Not associated with a known explicit or implicit upstream so it is skipped. continue } diff --git a/agent/xds/endpoints.go b/agent/xds/endpoints.go index d305a85bdf..2538914dd2 100644 --- a/agent/xds/endpoints.go +++ b/agent/xds/endpoints.go @@ -56,7 +56,8 @@ func (s *ResourceGenerator) endpointsFromSnapshotConnectProxy(cfgSnap *proxycfg. upstream := cfgSnap.ConnectProxy.UpstreamConfig[uid] explicit := upstream.HasLocalPortOrSocket() - if _, implicit := cfgSnap.ConnectProxy.IntentionUpstreams[uid]; !implicit && !explicit { + implicit := cfgSnap.ConnectProxy.IsImplicitUpstream(uid) + if !implicit && !explicit { // Discovery chain is not associated with a known explicit or implicit upstream so it is skipped. continue } @@ -84,13 +85,12 @@ func (s *ResourceGenerator) endpointsFromSnapshotConnectProxy(cfgSnap *proxycfg. // NOTE: Any time we skip an upstream below we MUST also skip that same // upstream in clusters.go so that the sets of endpoints generated matches // the sets of clusters. - // - // TODO(peering): make this work for tproxy for _, uid := range cfgSnap.ConnectProxy.PeeredUpstreamIDs() { upstreamCfg := cfgSnap.ConnectProxy.UpstreamConfig[uid] explicit := upstreamCfg.HasLocalPortOrSocket() - if _, implicit := cfgSnap.ConnectProxy.IntentionUpstreams[uid]; !implicit && !explicit { + implicit := cfgSnap.ConnectProxy.IsImplicitUpstream(uid) + if !implicit && !explicit { // Not associated with a known explicit or implicit upstream so it is skipped. continue } diff --git a/agent/xds/listeners.go b/agent/xds/listeners.go index 1bf8e4e6c4..2f3650aa77 100644 --- a/agent/xds/listeners.go +++ b/agent/xds/listeners.go @@ -110,7 +110,8 @@ func (s *ResourceGenerator) listenersFromSnapshotConnectProxy(cfgSnap *proxycfg. upstreamCfg := cfgSnap.ConnectProxy.UpstreamConfig[uid] explicit := upstreamCfg.HasLocalPortOrSocket() - if _, implicit := cfgSnap.ConnectProxy.IntentionUpstreams[uid]; !implicit && !explicit { + implicit := cfgSnap.ConnectProxy.IsImplicitUpstream(uid) + if !implicit && !explicit { // Discovery chain is not associated with a known explicit or implicit upstream so it is skipped. continue } @@ -228,13 +229,12 @@ func (s *ResourceGenerator) listenersFromSnapshotConnectProxy(cfgSnap *proxycfg. // Looping over explicit upstreams is only needed for cross-peer because // they do not have discovery chains. - // - // TODO(peering): make this work for tproxy for _, uid := range cfgSnap.ConnectProxy.PeeredUpstreamIDs() { upstreamCfg := cfgSnap.ConnectProxy.UpstreamConfig[uid] explicit := upstreamCfg.HasLocalPortOrSocket() - if _, implicit := cfgSnap.ConnectProxy.IntentionUpstreams[uid]; !implicit && !explicit { + implicit := cfgSnap.ConnectProxy.IsImplicitUpstream(uid) + if !implicit && !explicit { // Not associated with a known explicit or implicit upstream so it is skipped. continue } diff --git a/agent/xds/routes.go b/agent/xds/routes.go index 6f9e2214c1..6faa1fa674 100644 --- a/agent/xds/routes.go +++ b/agent/xds/routes.go @@ -50,7 +50,8 @@ func (s *ResourceGenerator) routesForConnectProxy(cfgSnap *proxycfg.ConfigSnapsh } explicit := cfgSnap.ConnectProxy.UpstreamConfig[uid].HasLocalPortOrSocket() - if _, implicit := cfgSnap.ConnectProxy.IntentionUpstreams[uid]; !implicit && !explicit { + implicit := cfgSnap.ConnectProxy.IsImplicitUpstream(uid) + if !implicit && !explicit { // Discovery chain is not associated with a known explicit or implicit upstream so it is skipped. continue }