2023-03-28 18:39:22 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
2023-08-11 13:12:13 +00:00
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
2023-03-28 18:39:22 +00:00
|
|
|
|
2021-04-29 18:54:05 +00:00
|
|
|
package xds
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2023-08-15 18:57:07 +00:00
|
|
|
"github.com/hashicorp/consul/agent/xds/configfetcher"
|
2021-04-29 18:54:05 +00:00
|
|
|
"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
|
2023-08-15 18:57:07 +00:00
|
|
|
CfgFetcher configfetcher.ConfigFetcher
|
2021-04-29 18:54:05 +00:00
|
|
|
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,
|
2023-08-15 18:57:07 +00:00
|
|
|
cfgFetcher configfetcher.ConfigFetcher,
|
2021-04-29 18:54:05 +00:00
|
|
|
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-17 17:46:03 +00:00
|
|
|
for _, typeUrl := range []string{xdscommon.ListenerType, xdscommon.RouteType, xdscommon.ClusterType, xdscommon.EndpointType, xdscommon.SecretType} {
|
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)
|
|
|
|
}
|
|
|
|
}
|