2023-03-28 22:48:58 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
2023-08-11 13:12:13 +00:00
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
2023-03-28 22:48:58 +00:00
|
|
|
|
2022-03-08 19:37:24 +00:00
|
|
|
package xdscommon
|
|
|
|
|
|
|
|
import (
|
2023-02-03 06:18:10 +00:00
|
|
|
envoy_cluster_v3 "github.com/envoyproxy/go-control-plane/envoy/config/cluster/v3"
|
|
|
|
envoy_endpoint_v3 "github.com/envoyproxy/go-control-plane/envoy/config/endpoint/v3"
|
|
|
|
envoy_listener_v3 "github.com/envoyproxy/go-control-plane/envoy/config/listener/v3"
|
|
|
|
envoy_route_v3 "github.com/envoyproxy/go-control-plane/envoy/config/route/v3"
|
2024-04-15 20:45:05 +00:00
|
|
|
envoy_tls_v3 "github.com/envoyproxy/go-control-plane/envoy/extensions/transport_sockets/tls/v3"
|
2023-02-03 06:18:10 +00:00
|
|
|
"github.com/hashicorp/go-hclog"
|
2023-01-11 14:39:10 +00:00
|
|
|
"google.golang.org/protobuf/proto"
|
2022-03-08 19:37:24 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2023-01-06 17:13:40 +00:00
|
|
|
// PublicListenerName is the name we give the public listener in Envoy config.
|
|
|
|
PublicListenerName = "public_listener"
|
|
|
|
|
2023-01-30 21:35:26 +00:00
|
|
|
// OutboundListenerName is the name we give the outbound Envoy listener when transparent proxy mode is enabled.
|
|
|
|
OutboundListenerName = "outbound_listener"
|
|
|
|
|
2023-01-06 17:13:40 +00:00
|
|
|
// LocalAppClusterName is the name we give the local application "cluster" in
|
|
|
|
// Envoy config. Note that all cluster names may collide with service names
|
|
|
|
// since we want cluster names and service names to match to enable nice
|
|
|
|
// metrics correlation without massaging prefixes on cluster names.
|
|
|
|
//
|
|
|
|
// We should probably make this more unlikely to collide however changing it
|
|
|
|
// potentially breaks upgrade compatibility without restarting all Envoy's as
|
|
|
|
// it will no longer match their existing cluster name. Changing this will
|
|
|
|
// affect metrics output so could break dashboards (for local app traffic).
|
|
|
|
//
|
|
|
|
// We should probably just make it configurable if anyone actually has
|
|
|
|
// services named "local_app" in the future.
|
|
|
|
LocalAppClusterName = "local_app"
|
2023-01-27 19:43:16 +00:00
|
|
|
|
2023-01-30 21:35:26 +00:00
|
|
|
// Resource types in xDS v3. These are copied from
|
|
|
|
// envoyproxy/go-control-plane/pkg/resource/v3/resource.go since we don't need any of
|
|
|
|
// the rest of that package.
|
|
|
|
apiTypePrefix = "type.googleapis.com/"
|
2022-03-08 19:37:24 +00:00
|
|
|
|
2023-01-30 21:35:26 +00:00
|
|
|
// EndpointType is the TypeURL for Endpoint discovery responses.
|
|
|
|
EndpointType = apiTypePrefix + "envoy.config.endpoint.v3.ClusterLoadAssignment"
|
|
|
|
|
|
|
|
// ClusterType is the TypeURL for Cluster discovery responses.
|
|
|
|
ClusterType = apiTypePrefix + "envoy.config.cluster.v3.Cluster"
|
|
|
|
|
|
|
|
// RouteType is the TypeURL for Route discovery responses.
|
|
|
|
RouteType = apiTypePrefix + "envoy.config.route.v3.RouteConfiguration"
|
|
|
|
|
|
|
|
// ListenerType is the TypeURL for Listener discovery responses.
|
|
|
|
ListenerType = apiTypePrefix + "envoy.config.listener.v3.Listener"
|
2023-02-08 21:52:12 +00:00
|
|
|
|
|
|
|
// SecretType is the TypeURL for Secret discovery responses.
|
|
|
|
SecretType = apiTypePrefix + "envoy.extensions.transport_sockets.tls.v3.Secret"
|
2023-05-30 18:53:42 +00:00
|
|
|
|
|
|
|
FailoverClusterNamePrefix = "failover-target~"
|
2023-09-19 00:26:13 +00:00
|
|
|
|
|
|
|
// BlackHoleClusterName is the cluster we use for black-holing traffic for cases when a workload
|
|
|
|
// has no inbound ports to route to.
|
|
|
|
BlackHoleClusterName = "black-hole-cluster"
|
2023-01-30 21:35:26 +00:00
|
|
|
)
|
2023-01-06 17:13:40 +00:00
|
|
|
|
2022-03-08 19:37:24 +00:00
|
|
|
type IndexedResources struct {
|
|
|
|
// Index is a map of typeURL => resourceName => resource
|
|
|
|
Index map[string]map[string]proto.Message
|
|
|
|
|
|
|
|
// ChildIndex is a map of typeURL => parentResourceName => list of
|
|
|
|
// childResourceNames. This only applies if the child and parent do not
|
|
|
|
// share a name.
|
|
|
|
ChildIndex map[string]map[string][]string
|
|
|
|
}
|
|
|
|
|
2023-07-31 19:24:33 +00:00
|
|
|
// Clone makes a deep copy of the IndexedResources value at the given pointer and
|
|
|
|
// returns a pointer to the copy.
|
|
|
|
func Clone(i *IndexedResources) *IndexedResources {
|
|
|
|
if i == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
iCopy := EmptyIndexedResources()
|
|
|
|
for typeURL, typeMap := range i.Index {
|
|
|
|
for name, msg := range typeMap {
|
|
|
|
clone := proto.Clone(msg)
|
|
|
|
iCopy.Index[typeURL][name] = clone
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for typeURL, parentMap := range i.ChildIndex {
|
|
|
|
for name, childName := range parentMap {
|
|
|
|
iCopy.ChildIndex[typeURL][name] = childName
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return iCopy
|
|
|
|
}
|
|
|
|
|
2023-02-03 06:18:10 +00:00
|
|
|
func IndexResources(logger hclog.Logger, resources map[string][]proto.Message) *IndexedResources {
|
|
|
|
data := EmptyIndexedResources()
|
|
|
|
|
|
|
|
for typeURL, typeRes := range resources {
|
|
|
|
for _, res := range typeRes {
|
|
|
|
name := GetResourceName(res)
|
|
|
|
if name == "" {
|
|
|
|
logger.Warn("skipping unexpected xDS type found in delta snapshot", "typeURL", typeURL)
|
|
|
|
} else {
|
|
|
|
data.Index[typeURL][name] = res
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return data
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetResourceName(res proto.Message) string {
|
2024-04-15 20:45:05 +00:00
|
|
|
// NOTE: this only covers types that we currently care about for LDS/RDS/CDS/EDS/SDS
|
2023-02-03 06:18:10 +00:00
|
|
|
switch x := res.(type) {
|
|
|
|
case *envoy_listener_v3.Listener: // LDS
|
|
|
|
return x.Name
|
|
|
|
case *envoy_route_v3.RouteConfiguration: // RDS
|
|
|
|
return x.Name
|
|
|
|
case *envoy_cluster_v3.Cluster: // CDS
|
|
|
|
return x.Name
|
|
|
|
case *envoy_endpoint_v3.ClusterLoadAssignment: // EDS
|
|
|
|
return x.ClusterName
|
2024-04-15 20:45:05 +00:00
|
|
|
case *envoy_tls_v3.Secret: // SDS
|
|
|
|
return x.Name
|
2023-02-03 06:18:10 +00:00
|
|
|
default:
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-08 19:37:24 +00:00
|
|
|
func EmptyIndexedResources() *IndexedResources {
|
|
|
|
return &IndexedResources{
|
|
|
|
Index: map[string]map[string]proto.Message{
|
|
|
|
ListenerType: make(map[string]proto.Message),
|
|
|
|
RouteType: make(map[string]proto.Message),
|
|
|
|
ClusterType: make(map[string]proto.Message),
|
|
|
|
EndpointType: make(map[string]proto.Message),
|
2024-04-15 20:45:05 +00:00
|
|
|
SecretType: make(map[string]proto.Message),
|
2022-03-08 19:37:24 +00:00
|
|
|
},
|
|
|
|
ChildIndex: map[string]map[string][]string{
|
|
|
|
ListenerType: make(map[string][]string),
|
|
|
|
ClusterType: make(map[string][]string),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|