2022-03-31 20:24:46 +00:00
|
|
|
package serverlessplugin
|
|
|
|
|
|
|
|
import (
|
|
|
|
envoy_cluster_v3 "github.com/envoyproxy/go-control-plane/envoy/config/cluster/v3"
|
|
|
|
envoy_listener_v3 "github.com/envoyproxy/go-control-plane/envoy/config/listener/v3"
|
2022-04-01 14:32:38 +00:00
|
|
|
envoy_route_v3 "github.com/envoyproxy/go-control-plane/envoy/config/route/v3"
|
2022-03-31 20:24:46 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/consul/agent/xds/xdscommon"
|
|
|
|
"github.com/hashicorp/consul/api"
|
|
|
|
)
|
|
|
|
|
|
|
|
// patcher is the interface that each serverless integration must implement. It
|
|
|
|
// is responsible for modifying the xDS structures based on only the state of
|
|
|
|
// the patcher.
|
|
|
|
type patcher interface {
|
|
|
|
// CanPatch determines if the patcher can mutate resources for the given api.ServiceKind
|
|
|
|
CanPatch(api.ServiceKind) bool
|
|
|
|
|
2022-04-01 14:32:38 +00:00
|
|
|
// patchRoute patches a route to include the custom Envoy configuration
|
|
|
|
// PatchCluster patches a cluster to include the custom Envoy configuration
|
|
|
|
// required to integrate with the serverless integration.
|
|
|
|
PatchRoute(*envoy_route_v3.RouteConfiguration) (*envoy_route_v3.RouteConfiguration, bool, error)
|
|
|
|
|
2022-03-31 20:24:46 +00:00
|
|
|
// PatchCluster patches a cluster to include the custom Envoy configuration
|
|
|
|
// required to integrate with the serverless integration.
|
|
|
|
PatchCluster(*envoy_cluster_v3.Cluster) (*envoy_cluster_v3.Cluster, bool, error)
|
|
|
|
|
|
|
|
// PatchFilter patches an Envoy filter to include the custom Envoy
|
|
|
|
// configuration required to integrate with the serverless integration.
|
|
|
|
PatchFilter(*envoy_listener_v3.Filter) (*envoy_listener_v3.Filter, bool, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
type patchers map[api.CompoundServiceName]patcher
|
|
|
|
|
2022-05-05 20:39:39 +00:00
|
|
|
// getPatcherBySNI gets the patcher for the associated SNI.
|
|
|
|
func getPatcherBySNI(config xdscommon.PluginConfiguration, sni string) patcher {
|
|
|
|
serviceName, ok := config.SNIToServiceName[sni]
|
2022-03-31 20:24:46 +00:00
|
|
|
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-05-05 20:39:39 +00:00
|
|
|
serviceConfig, ok := config.ServiceConfigs[serviceName]
|
|
|
|
if !ok {
|
2022-03-31 20:24:46 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-05-05 20:39:39 +00:00
|
|
|
p := makePatcher(serviceConfig)
|
|
|
|
if p == nil || !p.CanPatch(config.Kind) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return p
|
2022-03-31 20:24:46 +00:00
|
|
|
}
|
|
|
|
|
2022-05-05 20:39:39 +00:00
|
|
|
// getPatcherByEnvoyID gets the patcher for the associated envoy id.
|
|
|
|
func getPatcherByEnvoyID(config xdscommon.PluginConfiguration, envoyID string) patcher {
|
|
|
|
serviceName, ok := config.EnvoyIDToServiceName[envoyID]
|
2022-03-31 20:24:46 +00:00
|
|
|
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
serviceConfig, ok := config.ServiceConfigs[serviceName]
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
p := makePatcher(serviceConfig)
|
2022-05-05 20:39:39 +00:00
|
|
|
if p == nil || !p.CanPatch(config.Kind) {
|
2022-03-31 20:24:46 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
|
|
|
func makePatcher(serviceConfig xdscommon.ServiceConfig) patcher {
|
|
|
|
for _, constructor := range patchConstructors {
|
|
|
|
patcher, ok := constructor(serviceConfig)
|
|
|
|
if ok {
|
|
|
|
return patcher
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// patchConstructor is used to construct patchers based on
|
|
|
|
// xdscommon.ServiceConfig. This function contains all of the logic around
|
|
|
|
// turning Meta data into the patcher.
|
|
|
|
type patchConstructor func(xdscommon.ServiceConfig) (patcher, bool)
|
|
|
|
|
|
|
|
// patchConstructors contains all patchers that getPatchers tries to create.
|
|
|
|
var patchConstructors = []patchConstructor{makeLambdaPatcher}
|