Merge pull request #12556 from hashicorp/wire-up-serverless-patcher

Create and wire up the serverless patcher
This commit is contained in:
Eric Haberkorn 2022-03-15 14:05:40 -04:00 committed by GitHub
commit 90ecd7b5fe
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 407 additions and 207 deletions

View file

@ -707,6 +707,7 @@ func (a *Agent) listenAndServeGRPC() error {
xdsServer := xds.NewServer( xdsServer := xds.NewServer(
a.logger.Named(logging.Envoy), a.logger.Named(logging.Envoy),
a.config.ConnectServerlessPluginEnabled,
a.proxyConfig, a.proxyConfig,
func(id string) (acl.Authorizer, error) { func(id string) (acl.Authorizer, error) {
return a.delegate.ResolveTokenAndDefaultMeta(id, nil, nil) return a.delegate.ResolveTokenAndDefaultMeta(id, nil, nil)

View file

@ -6,6 +6,7 @@ import (
"io/ioutil" "io/ioutil"
"path" "path"
"path/filepath" "path/filepath"
"runtime"
"sync" "sync"
"sync/atomic" "sync/atomic"
"time" "time"
@ -874,9 +875,14 @@ func (ct *ControllableCacheType) RegisterOptions() cache.RegisterOptions {
func golden(t testing.T, name string) string { func golden(t testing.T, name string) string {
t.Helper() t.Helper()
golden := filepath.Join("../xds/testdata", name+".golden") golden := filepath.Join(projectRoot(), "../", "/xds/testdata", name+".golden")
expected, err := ioutil.ReadFile(golden) expected, err := ioutil.ReadFile(golden)
require.NoError(t, err) require.NoError(t, err)
return string(expected) return string(expected)
} }
func projectRoot() string {
_, base, _, _ := runtime.Caller(0)
return filepath.Dir(base)
}

View file

@ -641,3 +641,15 @@ func TestConfigSnapshotTerminatingGatewayIgnoreExtraResolvers(t testing.T) *Conf
}, },
}) })
} }
func TestConfigSnapshotTerminatingGatewayWithServiceDefaultsMeta(t testing.T) *ConfigSnapshot {
web := structs.NewServiceName("web", nil)
return TestConfigSnapshotTerminatingGateway(t, true, nil, []agentcache.UpdateEvent{
{
CorrelationID: serviceConfigIDPrefix + web.String(),
Result: &structs.ServiceConfigResponse{
Meta: map[string]string{"a": "b"},
},
},
})
}

View file

@ -23,6 +23,7 @@ import (
"github.com/hashicorp/consul/agent/proxycfg" "github.com/hashicorp/consul/agent/proxycfg"
"github.com/hashicorp/consul/agent/structs" "github.com/hashicorp/consul/agent/structs"
"github.com/hashicorp/consul/agent/xds/serverlessplugin"
"github.com/hashicorp/consul/agent/xds/xdscommon" "github.com/hashicorp/consul/agent/xds/xdscommon"
"github.com/hashicorp/consul/logging" "github.com/hashicorp/consul/logging"
) )
@ -212,6 +213,14 @@ func (s *Server) processDelta(stream ADSDeltaStream, reqCh <-chan *envoy_discove
s.ResourceMapMutateFn(newResourceMap) s.ResourceMapMutateFn(newResourceMap)
} }
if s.serverlessPluginEnabled {
newResourceMap, err = serverlessplugin.MutateIndexedResources(newResourceMap, xdscommon.MakePluginConfiguration(cfgSnap))
if err != nil {
generator.Logger.Warn("failed to patch xDS resources in the serverless plugin", "err", err)
}
}
if err := populateChildIndexMap(newResourceMap); err != nil { if err := populateChildIndexMap(newResourceMap); err != nil {
return status.Errorf(codes.Unavailable, "failed to index xDS resource versions: %v", err) return status.Errorf(codes.Unavailable, "failed to index xDS resource versions: %v", err)
} }

View file

@ -2,6 +2,7 @@ package xds
import ( import (
"errors" "errors"
"fmt"
"strings" "strings"
"sync/atomic" "sync/atomic"
"testing" "testing"
@ -27,11 +28,14 @@ import (
// Stick to very straightforward stuff in xds_protocol_helpers_test.go. // Stick to very straightforward stuff in xds_protocol_helpers_test.go.
func TestServer_DeltaAggregatedResources_v3_BasicProtocol_TCP(t *testing.T) { func TestServer_DeltaAggregatedResources_v3_BasicProtocol_TCP(t *testing.T) {
for _, serverlessPluginEnabled := range []bool{false, true} {
t.Run(fmt.Sprintf("serverless patcher: %t", serverlessPluginEnabled), func(t *testing.T) {
aclResolve := func(id string) (acl.Authorizer, error) { aclResolve := func(id string) (acl.Authorizer, error) {
// Allow all // Allow all
return acl.RootAuthorizer("manage"), nil return acl.RootAuthorizer("manage"), nil
} }
scenario := newTestServerDeltaScenario(t, aclResolve, "web-sidecar-proxy", "", 0) scenario := newTestServerDeltaScenario(t, aclResolve, "web-sidecar-proxy", "", 0, serverlessPluginEnabled)
mgr, errCh, envoy := scenario.mgr, scenario.errCh, scenario.envoy mgr, errCh, envoy := scenario.mgr, scenario.errCh, scenario.envoy
sid := structs.NewServiceID("web-sidecar-proxy", nil) sid := structs.NewServiceID("web-sidecar-proxy", nil)
@ -224,6 +228,8 @@ func TestServer_DeltaAggregatedResources_v3_BasicProtocol_TCP(t *testing.T) {
case <-time.After(50 * time.Millisecond): case <-time.After(50 * time.Millisecond):
t.Fatalf("timed out waiting for handler to finish") t.Fatalf("timed out waiting for handler to finish")
} }
})
}
} }
func TestServer_DeltaAggregatedResources_v3_NackLoop(t *testing.T) { func TestServer_DeltaAggregatedResources_v3_NackLoop(t *testing.T) {
@ -231,7 +237,7 @@ func TestServer_DeltaAggregatedResources_v3_NackLoop(t *testing.T) {
// Allow all // Allow all
return acl.RootAuthorizer("manage"), nil return acl.RootAuthorizer("manage"), nil
} }
scenario := newTestServerDeltaScenario(t, aclResolve, "web-sidecar-proxy", "", 0) scenario := newTestServerDeltaScenario(t, aclResolve, "web-sidecar-proxy", "", 0, false)
mgr, errCh, envoy := scenario.mgr, scenario.errCh, scenario.envoy mgr, errCh, envoy := scenario.mgr, scenario.errCh, scenario.envoy
sid := structs.NewServiceID("web-sidecar-proxy", nil) sid := structs.NewServiceID("web-sidecar-proxy", nil)
@ -362,7 +368,7 @@ func TestServer_DeltaAggregatedResources_v3_BasicProtocol_HTTP2(t *testing.T) {
// Allow all // Allow all
return acl.RootAuthorizer("manage"), nil return acl.RootAuthorizer("manage"), nil
} }
scenario := newTestServerDeltaScenario(t, aclResolve, "web-sidecar-proxy", "", 0) scenario := newTestServerDeltaScenario(t, aclResolve, "web-sidecar-proxy", "", 0, false)
mgr, errCh, envoy := scenario.mgr, scenario.errCh, scenario.envoy mgr, errCh, envoy := scenario.mgr, scenario.errCh, scenario.envoy
sid := structs.NewServiceID("web-sidecar-proxy", nil) sid := structs.NewServiceID("web-sidecar-proxy", nil)
@ -515,7 +521,7 @@ func TestServer_DeltaAggregatedResources_v3_SlowEndpointPopulation(t *testing.T)
// Allow all // Allow all
return acl.RootAuthorizer("manage"), nil return acl.RootAuthorizer("manage"), nil
} }
scenario := newTestServerDeltaScenario(t, aclResolve, "web-sidecar-proxy", "", 0) scenario := newTestServerDeltaScenario(t, aclResolve, "web-sidecar-proxy", "", 0, false)
server, mgr, errCh, envoy := scenario.server, scenario.mgr, scenario.errCh, scenario.envoy server, mgr, errCh, envoy := scenario.server, scenario.mgr, scenario.errCh, scenario.envoy
// This mutateFn causes any endpoint with a name containing "geo-cache" to be // This mutateFn causes any endpoint with a name containing "geo-cache" to be
@ -658,7 +664,7 @@ func TestServer_DeltaAggregatedResources_v3_GetAllClusterAfterConsulRestarted(t
// Allow all // Allow all
return acl.RootAuthorizer("manage"), nil return acl.RootAuthorizer("manage"), nil
} }
scenario := newTestServerDeltaScenario(t, aclResolve, "web-sidecar-proxy", "", 0) scenario := newTestServerDeltaScenario(t, aclResolve, "web-sidecar-proxy", "", 0, false)
_, mgr, errCh, envoy := scenario.server, scenario.mgr, scenario.errCh, scenario.envoy _, mgr, errCh, envoy := scenario.server, scenario.mgr, scenario.errCh, scenario.envoy
envoy.EnvoyVersion = "1.18.0" envoy.EnvoyVersion = "1.18.0"
@ -722,7 +728,7 @@ func TestServer_DeltaAggregatedResources_v3_BasicProtocol_TCP_clusterChangesImpa
// Allow all // Allow all
return acl.RootAuthorizer("manage"), nil return acl.RootAuthorizer("manage"), nil
} }
scenario := newTestServerDeltaScenario(t, aclResolve, "web-sidecar-proxy", "", 0) scenario := newTestServerDeltaScenario(t, aclResolve, "web-sidecar-proxy", "", 0, false)
mgr, errCh, envoy := scenario.mgr, scenario.errCh, scenario.envoy mgr, errCh, envoy := scenario.mgr, scenario.errCh, scenario.envoy
sid := structs.NewServiceID("web-sidecar-proxy", nil) sid := structs.NewServiceID("web-sidecar-proxy", nil)
@ -858,7 +864,7 @@ func TestServer_DeltaAggregatedResources_v3_BasicProtocol_HTTP2_RDS_listenerChan
// Allow all // Allow all
return acl.RootAuthorizer("manage"), nil return acl.RootAuthorizer("manage"), nil
} }
scenario := newTestServerDeltaScenario(t, aclResolve, "web-sidecar-proxy", "", 0) scenario := newTestServerDeltaScenario(t, aclResolve, "web-sidecar-proxy", "", 0, false)
mgr, errCh, envoy := scenario.mgr, scenario.errCh, scenario.envoy mgr, errCh, envoy := scenario.mgr, scenario.errCh, scenario.envoy
sid := structs.NewServiceID("web-sidecar-proxy", nil) sid := structs.NewServiceID("web-sidecar-proxy", nil)
@ -1118,7 +1124,7 @@ func TestServer_DeltaAggregatedResources_v3_ACLEnforcement(t *testing.T) {
return acl.NewPolicyAuthorizerWithDefaults(acl.RootAuthorizer("deny"), []*acl.Policy{policy}, nil) return acl.NewPolicyAuthorizerWithDefaults(acl.RootAuthorizer("deny"), []*acl.Policy{policy}, nil)
} }
scenario := newTestServerDeltaScenario(t, aclResolve, "web-sidecar-proxy", tt.token, 0) scenario := newTestServerDeltaScenario(t, aclResolve, "web-sidecar-proxy", tt.token, 0, false)
mgr, errCh, envoy := scenario.mgr, scenario.errCh, scenario.envoy mgr, errCh, envoy := scenario.mgr, scenario.errCh, scenario.envoy
sid := structs.NewServiceID("web-sidecar-proxy", nil) sid := structs.NewServiceID("web-sidecar-proxy", nil)
@ -1195,6 +1201,7 @@ func TestServer_DeltaAggregatedResources_v3_ACLTokenDeleted_StreamTerminatedDuri
} }
scenario := newTestServerDeltaScenario(t, aclResolve, "web-sidecar-proxy", token, scenario := newTestServerDeltaScenario(t, aclResolve, "web-sidecar-proxy", token,
100*time.Millisecond, // Make this short. 100*time.Millisecond, // Make this short.
false,
) )
mgr, errCh, envoy := scenario.mgr, scenario.errCh, scenario.envoy mgr, errCh, envoy := scenario.mgr, scenario.errCh, scenario.envoy
@ -1293,6 +1300,7 @@ func TestServer_DeltaAggregatedResources_v3_ACLTokenDeleted_StreamTerminatedInBa
} }
scenario := newTestServerDeltaScenario(t, aclResolve, "web-sidecar-proxy", token, scenario := newTestServerDeltaScenario(t, aclResolve, "web-sidecar-proxy", token,
100*time.Millisecond, // Make this short. 100*time.Millisecond, // Make this short.
false,
) )
mgr, errCh, envoy := scenario.mgr, scenario.errCh, scenario.envoy mgr, errCh, envoy := scenario.mgr, scenario.errCh, scenario.envoy
@ -1373,7 +1381,7 @@ func TestServer_DeltaAggregatedResources_v3_IngressEmptyResponse(t *testing.T) {
// Allow all // Allow all
return acl.RootAuthorizer("manage"), nil return acl.RootAuthorizer("manage"), nil
} }
scenario := newTestServerDeltaScenario(t, aclResolve, "ingress-gateway", "", 0) scenario := newTestServerDeltaScenario(t, aclResolve, "ingress-gateway", "", 0, false)
mgr, errCh, envoy := scenario.mgr, scenario.errCh, scenario.envoy mgr, errCh, envoy := scenario.mgr, scenario.errCh, scenario.envoy
sid := structs.NewServiceID("ingress-gateway", nil) sid := structs.NewServiceID("ingress-gateway", nil)

View file

@ -132,6 +132,7 @@ type Server struct {
ResourceMapMutateFn func(resourceMap *xdscommon.IndexedResources) ResourceMapMutateFn func(resourceMap *xdscommon.IndexedResources)
activeStreams *activeStreamCounters activeStreams *activeStreamCounters
serverlessPluginEnabled bool
} }
// activeStreamCounters simply encapsulates two counters accessed atomically to // activeStreamCounters simply encapsulates two counters accessed atomically to
@ -166,6 +167,7 @@ func (c *activeStreamCounters) Increment(xdsVersion string) func() {
func NewServer( func NewServer(
logger hclog.Logger, logger hclog.Logger,
serverlessPluginEnabled bool,
cfgMgr ConfigManager, cfgMgr ConfigManager,
resolveToken ACLResolverFunc, resolveToken ACLResolverFunc,
checkFetcher HTTPCheckFetcher, checkFetcher HTTPCheckFetcher,
@ -179,6 +181,7 @@ func NewServer(
CfgFetcher: cfgFetcher, CfgFetcher: cfgFetcher,
AuthCheckFrequency: DefaultAuthCheckFrequency, AuthCheckFrequency: DefaultAuthCheckFrequency,
activeStreams: &activeStreamCounters{}, activeStreams: &activeStreamCounters{},
serverlessPluginEnabled: serverlessPluginEnabled,
} }
} }

View file

@ -0,0 +1,9 @@
package serverlessplugin
import (
"github.com/hashicorp/consul/agent/xds/xdscommon"
)
func MutateIndexedResources(resources *xdscommon.IndexedResources, config xdscommon.PluginConfiguration) (*xdscommon.IndexedResources, error) {
return resources, nil
}

View file

@ -131,6 +131,7 @@ func newTestServerDeltaScenario(
proxyID string, proxyID string,
token string, token string,
authCheckFrequency time.Duration, authCheckFrequency time.Duration,
serverlessPluginEnabled bool,
) *testServerScenario { ) *testServerScenario {
mgr := newTestManager(t) mgr := newTestManager(t)
envoy := NewTestEnvoy(t, proxyID, token) envoy := NewTestEnvoy(t, proxyID, token)
@ -151,6 +152,7 @@ func newTestServerDeltaScenario(
s := NewServer( s := NewServer(
testutil.Logger(t), testutil.Logger(t),
serverlessPluginEnabled,
mgr, mgr,
resolveToken, resolveToken,
nil, /*checkFetcher HTTPCheckFetcher*/ nil, /*checkFetcher HTTPCheckFetcher*/

View file

@ -2,6 +2,11 @@ package xdscommon
import ( import (
"github.com/golang/protobuf/proto" "github.com/golang/protobuf/proto"
"github.com/hashicorp/consul/agent/connect"
"github.com/hashicorp/consul/agent/proxycfg"
"github.com/hashicorp/consul/agent/structs"
"github.com/hashicorp/consul/api"
) )
const ( const (
@ -47,3 +52,77 @@ func EmptyIndexedResources() *IndexedResources {
}, },
} }
} }
type ServiceConfig struct {
// Kind identifies the final proxy kind that will make the request to the
// destination service.
Kind api.ServiceKind
Meta map[string]string
}
// PluginConfiguration is passed into Envoy plugins. It should depend on the
// API client rather than the structs package because the API client is meant
// to be public.
type PluginConfiguration struct {
// ServiceConfigs is a mapping from service names to the data Envoy plugins
// need to override the default Envoy configurations.
ServiceConfigs map[api.CompoundServiceName]ServiceConfig
// SNIToServiceName is a mapping from SNIs to service names. This allows
// Envoy plugins to easily convert from an SNI Envoy resource name to the
// associated service's CompoundServiceName
SNIToServiceName map[string]api.CompoundServiceName
// EnvoyIDToServiceName is a mapping from EnvoyIDs to service names. This allows
// Envoy plugins to easily convert from an EnvoyID Envoy resource name to the
// associated service's CompoundServiceName
EnvoyIDToServiceName map[string]api.CompoundServiceName
// Kind is mode the local Envoy proxy is running in
Kind api.ServiceKind
}
// MakePluginConfiguration generates the configuration that will be sent to
// Envoy plugins.
func MakePluginConfiguration(cfgSnap *proxycfg.ConfigSnapshot) PluginConfiguration {
serviceConfigs := make(map[api.CompoundServiceName]ServiceConfig)
sniMappings := make(map[string]api.CompoundServiceName)
envoyIDMappings := make(map[string]api.CompoundServiceName)
trustDomain := ""
if cfgSnap.Roots != nil {
trustDomain = cfgSnap.Roots.TrustDomain
}
switch cfgSnap.Kind {
case structs.ServiceKindTerminatingGateway:
for svc, c := range cfgSnap.TerminatingGateway.ServiceConfigs {
compoundServiceName := serviceNameToCompoundServiceName(svc)
serviceConfigs[compoundServiceName] = ServiceConfig{
Meta: c.Meta,
Kind: api.ServiceKindTerminatingGateway,
}
sni := connect.ServiceSNI(svc.Name, "", svc.NamespaceOrDefault(), svc.PartitionOrDefault(), cfgSnap.Datacenter, trustDomain)
sniMappings[sni] = compoundServiceName
envoyID := proxycfg.NewUpstreamIDFromServiceName(svc)
envoyIDMappings[envoyID.EnvoyID()] = compoundServiceName
}
}
return PluginConfiguration{
ServiceConfigs: serviceConfigs,
SNIToServiceName: sniMappings,
EnvoyIDToServiceName: envoyIDMappings,
Kind: api.ServiceKind(cfgSnap.Kind),
}
}
func serviceNameToCompoundServiceName(svc structs.ServiceName) api.CompoundServiceName {
return api.CompoundServiceName{
Name: svc.Name,
Partition: svc.PartitionOrDefault(),
Namespace: svc.NamespaceOrDefault(),
}
}

View file

@ -0,0 +1,71 @@
//go:build !consulent
// +build !consulent
package xdscommon
import (
"testing"
"github.com/stretchr/testify/require"
"github.com/hashicorp/consul/agent/proxycfg"
"github.com/hashicorp/consul/api"
)
func TestMakePluginConfiguration_TerminatingGateway(t *testing.T) {
snap := proxycfg.TestConfigSnapshotTerminatingGatewayWithServiceDefaultsMeta(t)
webService := api.CompoundServiceName{
Name: "web",
Namespace: "default",
Partition: "default",
}
dbService := api.CompoundServiceName{
Name: "db",
Namespace: "default",
Partition: "default",
}
cacheService := api.CompoundServiceName{
Name: "cache",
Namespace: "default",
Partition: "default",
}
apiService := api.CompoundServiceName{
Name: "api",
Namespace: "default",
Partition: "default",
}
expected := PluginConfiguration{
Kind: api.ServiceKindTerminatingGateway,
ServiceConfigs: map[api.CompoundServiceName]ServiceConfig{
webService: {
Kind: api.ServiceKindTerminatingGateway,
Meta: map[string]string{"a": "b"},
},
apiService: {
Kind: api.ServiceKindTerminatingGateway,
},
cacheService: {
Kind: api.ServiceKindTerminatingGateway,
},
dbService: {
Kind: api.ServiceKindTerminatingGateway,
},
},
SNIToServiceName: map[string]api.CompoundServiceName{
"api.default.dc1.internal.11111111-2222-3333-4444-555555555555.consul": apiService,
"cache.default.dc1.internal.11111111-2222-3333-4444-555555555555.consul": cacheService,
"db.default.dc1.internal.11111111-2222-3333-4444-555555555555.consul": dbService,
"web.default.dc1.internal.11111111-2222-3333-4444-555555555555.consul": webService,
},
EnvoyIDToServiceName: map[string]api.CompoundServiceName{
"web": webService,
"db": dbService,
"cache": cacheService,
"api": apiService,
},
}
require.Equal(t, expected, MakePluginConfiguration(snap))
}