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
|
|
|
|
2023-02-08 21:52:12 +00:00
|
|
|
"github.com/hashicorp/consul/envoyextensions/xdscommon"
|
|
|
|
|
2021-04-29 18:54:05 +00:00
|
|
|
"github.com/hashicorp/consul/agent/proxycfg"
|
|
|
|
)
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
2023-02-03 06:18:10 +00:00
|
|
|
ProxyFeatures xdscommon.SupportedProxyFeatures
|
2021-04-29 18:54:05 +00:00
|
|
|
}
|
|
|
|
|
2023-02-03 06:18:10 +00:00
|
|
|
func NewResourceGenerator(
|
2021-04-29 18:54:05 +00:00
|
|
|
logger hclog.Logger,
|
|
|
|
cfgFetcher ConfigFetcher,
|
|
|
|
incrementalXDS bool,
|
|
|
|
) *ResourceGenerator {
|
|
|
|
return &ResourceGenerator{
|
|
|
|
Logger: logger,
|
|
|
|
CfgFetcher: cfgFetcher,
|
|
|
|
IncrementalXDS: incrementalXDS,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-03 06:18:10 +00:00
|
|
|
func (g *ResourceGenerator) AllResourcesFromSnapshot(cfgSnap *proxycfg.ConfigSnapshot) (map[string][]proto.Message, error) {
|
2021-04-29 18:54:05 +00:00
|
|
|
all := make(map[string][]proto.Message)
|
2023-02-08 21:52:12 +00:00
|
|
|
// TODO Add xdscommon.SecretType
|
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)
|
2023-02-08 21:52:12 +00:00
|
|
|
case xdscommon.SecretType:
|
|
|
|
return g.secretsFromSnapshot(cfgSnap)
|
2021-04-29 18:54:05 +00:00
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("unknown typeUrl: %s", typeUrl)
|
|
|
|
}
|
|
|
|
}
|