2023-08-15 18:57:07 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
2023-08-17 18:43:21 +00:00
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
2023-08-15 18:57:07 +00:00
|
|
|
|
|
|
|
package xdsv2
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2023-08-29 21:39:29 +00:00
|
|
|
|
2023-08-15 18:57:07 +00:00
|
|
|
"github.com/hashicorp/go-hclog"
|
|
|
|
"google.golang.org/protobuf/proto"
|
|
|
|
|
|
|
|
"github.com/hashicorp/consul/envoyextensions/xdscommon"
|
2023-08-29 21:39:29 +00:00
|
|
|
proxytracker "github.com/hashicorp/consul/internal/mesh/proxy-tracker"
|
2023-08-15 18:57:07 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// ResourceGenerator is associated with a single gRPC stream and creates xDS
|
|
|
|
// resources for a single client.
|
|
|
|
type ResourceGenerator struct {
|
|
|
|
Logger hclog.Logger
|
|
|
|
ProxyFeatures xdscommon.SupportedProxyFeatures
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewResourceGenerator(
|
|
|
|
logger hclog.Logger,
|
|
|
|
) *ResourceGenerator {
|
|
|
|
return &ResourceGenerator{
|
|
|
|
Logger: logger,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type ProxyResources struct {
|
2023-08-29 15:15:34 +00:00
|
|
|
proxyState *proxytracker.ProxyState
|
2023-08-15 18:57:07 +00:00
|
|
|
envoyResources map[string][]proto.Message
|
|
|
|
}
|
|
|
|
|
2023-08-29 15:15:34 +00:00
|
|
|
func (g *ResourceGenerator) AllResourcesFromIR(proxyState *proxytracker.ProxyState) (map[string][]proto.Message, error) {
|
2023-08-15 18:57:07 +00:00
|
|
|
pr := &ProxyResources{
|
|
|
|
proxyState: proxyState,
|
|
|
|
envoyResources: make(map[string][]proto.Message),
|
|
|
|
}
|
|
|
|
err := pr.generateXDSResources()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to generate xDS resources for ProxyState: %v", err)
|
|
|
|
}
|
|
|
|
return pr.envoyResources, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pr *ProxyResources) generateXDSResources() error {
|
2023-08-17 21:04:53 +00:00
|
|
|
listeners, err := pr.makeXDSListeners()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2023-08-15 18:57:07 +00:00
|
|
|
}
|
|
|
|
|
2023-08-17 19:55:54 +00:00
|
|
|
pr.envoyResources[xdscommon.ListenerType] = listeners
|
|
|
|
|
2023-08-17 18:43:21 +00:00
|
|
|
clusters, err := pr.makeXDSClusters()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-08-15 18:57:07 +00:00
|
|
|
pr.envoyResources[xdscommon.ClusterType] = clusters
|
2023-08-17 19:55:54 +00:00
|
|
|
|
|
|
|
endpoints, err := pr.makeXDSEndpoints()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-08-15 18:57:07 +00:00
|
|
|
pr.envoyResources[xdscommon.EndpointType] = endpoints
|
|
|
|
|
2023-08-17 21:04:53 +00:00
|
|
|
routes, err := pr.makeXDSRoutes()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-08-17 19:55:54 +00:00
|
|
|
pr.envoyResources[xdscommon.RouteType] = routes
|
|
|
|
|
2023-08-15 18:57:07 +00:00
|
|
|
return nil
|
|
|
|
}
|