2021-04-29 18:54:05 +00:00
|
|
|
package xds
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/hashicorp/go-hclog"
|
2023-01-11 14:39:10 +00:00
|
|
|
"google.golang.org/protobuf/proto"
|
2021-04-29 18:54:05 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/consul/agent/proxycfg"
|
2022-03-08 19:37:24 +00:00
|
|
|
"github.com/hashicorp/consul/agent/xds/xdscommon"
|
2021-04-29 18:54:05 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// ResourceGenerator is associated with a single gRPC stream and creates xDS
|
|
|
|
// resources for a single client.
|
|
|
|
type ResourceGenerator struct {
|
|
|
|
Logger hclog.Logger
|
|
|
|
CfgFetcher ConfigFetcher
|
|
|
|
IncrementalXDS bool
|
|
|
|
|
|
|
|
ProxyFeatures supportedProxyFeatures
|
|
|
|
}
|
|
|
|
|
|
|
|
func newResourceGenerator(
|
|
|
|
logger hclog.Logger,
|
|
|
|
cfgFetcher ConfigFetcher,
|
|
|
|
incrementalXDS bool,
|
|
|
|
) *ResourceGenerator {
|
|
|
|
return &ResourceGenerator{
|
|
|
|
Logger: logger,
|
|
|
|
CfgFetcher: cfgFetcher,
|
|
|
|
IncrementalXDS: incrementalXDS,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *ResourceGenerator) allResourcesFromSnapshot(cfgSnap *proxycfg.ConfigSnapshot) (map[string][]proto.Message, error) {
|
|
|
|
all := make(map[string][]proto.Message)
|
2022-03-08 19:37:24 +00:00
|
|
|
for _, typeUrl := range []string{xdscommon.ListenerType, xdscommon.RouteType, xdscommon.ClusterType, xdscommon.EndpointType} {
|
2021-04-29 18:54:05 +00:00
|
|
|
res, err := g.resourcesFromSnapshot(typeUrl, cfgSnap)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to generate xDS resources for %q: %v", typeUrl, err)
|
|
|
|
}
|
|
|
|
all[typeUrl] = res
|
|
|
|
}
|
|
|
|
return all, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *ResourceGenerator) resourcesFromSnapshot(typeUrl string, cfgSnap *proxycfg.ConfigSnapshot) ([]proto.Message, error) {
|
|
|
|
switch typeUrl {
|
2022-03-08 19:37:24 +00:00
|
|
|
case xdscommon.ListenerType:
|
2021-04-29 18:54:05 +00:00
|
|
|
return g.listenersFromSnapshot(cfgSnap)
|
2022-03-08 19:37:24 +00:00
|
|
|
case xdscommon.RouteType:
|
2021-04-29 18:54:05 +00:00
|
|
|
return g.routesFromSnapshot(cfgSnap)
|
2022-03-08 19:37:24 +00:00
|
|
|
case xdscommon.ClusterType:
|
2021-04-29 18:54:05 +00:00
|
|
|
return g.clustersFromSnapshot(cfgSnap)
|
2022-03-08 19:37:24 +00:00
|
|
|
case xdscommon.EndpointType:
|
2021-04-29 18:54:05 +00:00
|
|
|
return g.endpointsFromSnapshot(cfgSnap)
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("unknown typeUrl: %s", typeUrl)
|
|
|
|
}
|
|
|
|
}
|