get-envoy-bootstrap-params: when v2 is enabled, use computed proxy configuration (#19175)

pull/19183/head
Iryna Shustava 2023-10-12 14:01:36 -06:00 committed by GitHub
parent 54a12ab3c9
commit 25283f0ec2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 39 additions and 89 deletions

View File

@ -12,9 +12,9 @@ import (
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/structpb"
"github.com/hashicorp/consul/internal/resource"
pbcatalog "github.com/hashicorp/consul/proto-public/pbcatalog/v2beta1"
pbmesh "github.com/hashicorp/consul/proto-public/pbmesh/v2beta1"
"github.com/hashicorp/consul/proto-public/pbresource"
@ -85,46 +85,33 @@ func (s *Server) GetEnvoyBootstrapParams(ctx context.Context, req *pbdataplane.G
return nil, err
}
// Get all proxy configurations for this workload. Currently we're only looking
// for proxy configurations in the same tenancy as the workload.
// todo (ishustava): we need to support wildcard proxy configurations as well.
computedProxyConfig, err := resource.GetDecodedResource[*pbmesh.ComputedProxyConfiguration](
ctx,
s.ResourceAPIClient,
resource.ReplaceType(pbmesh.ComputedProxyConfigurationType, workloadId))
proxyCfgList, err := s.ResourceAPIClient.List(ctx, &pbresource.ListRequest{
Tenancy: workloadRsp.Resource.Id.GetTenancy(),
Type: pbmesh.ProxyConfigurationType,
})
if err != nil {
logger.Error("Error looking up proxyConfiguration", "error", err)
logger.Error("Error looking up ComputedProxyConfiguration for this workload", "error", err)
return nil, err
}
// Collect and merge proxy configs.
// todo (ishustava): sorting and conflict resolution.
bootstrapCfg := &pbmesh.BootstrapConfig{}
dynamicCfg := &pbmesh.DynamicConfig{}
for _, cfgResource := range proxyCfgList.Resources {
var proxyCfg pbmesh.ProxyConfiguration
err = cfgResource.Data.UnmarshalTo(&proxyCfg)
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to parse proxy configuration data: %q", cfgResource.Id.Name)
}
if isWorkloadSelected(req.ProxyId, proxyCfg.Workloads) {
proto.Merge(bootstrapCfg, proxyCfg.BootstrapConfig)
proto.Merge(dynamicCfg, proxyCfg.DynamicConfig)
}
rsp := &pbdataplane.GetEnvoyBootstrapParamsResponse{
Identity: workload.Identity,
Partition: workloadRsp.Resource.Id.Tenancy.Partition,
Namespace: workloadRsp.Resource.Id.Tenancy.Namespace,
Datacenter: s.Datacenter,
NodeName: workload.NodeName,
}
accessLogs := makeAccessLogs(dynamicCfg.GetAccessLogs(), logger)
if computedProxyConfig != nil {
if computedProxyConfig.GetData().GetDynamicConfig() != nil {
rsp.AccessLogs = makeAccessLogs(computedProxyConfig.GetData().GetDynamicConfig().GetAccessLogs(), logger)
}
return &pbdataplane.GetEnvoyBootstrapParamsResponse{
Identity: workload.Identity,
Partition: workloadRsp.Resource.Id.Tenancy.Partition,
Namespace: workloadRsp.Resource.Id.Tenancy.Namespace,
BootstrapConfig: bootstrapCfg,
Datacenter: s.Datacenter,
NodeName: workload.NodeName,
AccessLogs: accessLogs,
}, nil
rsp.BootstrapConfig = computedProxyConfig.GetData().GetBootstrapConfig()
}
return rsp, nil
}
// The remainder of this file focuses on v1 implementation of this endpoint.
@ -215,19 +202,3 @@ func makeAccessLogs(logs structs.AccessLogs, logger hclog.Logger) []string {
return accessLogs
}
func isWorkloadSelected(name string, selector *pbcatalog.WorkloadSelector) bool {
for _, prefix := range selector.Prefixes {
if strings.Contains(name, prefix) {
return true
}
}
for _, selectorName := range selector.Names {
if name == selectorName {
return true
}
}
return false
}

View File

@ -5,7 +5,6 @@ package dataplane
import (
"context"
"fmt"
"testing"
"github.com/hashicorp/go-hclog"
@ -257,7 +256,7 @@ func TestGetEnvoyBootstrapParams_Success_EnableV2(t *testing.T) {
type testCase struct {
name string
workloadData *pbcatalog.Workload
proxyCfgs []*pbmesh.ProxyConfiguration
proxyCfg *pbmesh.ComputedProxyConfiguration
expBootstrapCfg *pbmesh.BootstrapConfig
expAccessLogs string
}
@ -294,13 +293,11 @@ func TestGetEnvoyBootstrapParams_Success_EnableV2(t *testing.T) {
WithTenancy(resource.DefaultNamespacedTenancy()).
Write(t, resourceClient)
// Create any proxy cfg resources.
for i, cfg := range tc.proxyCfgs {
resourcetest.Resource(pbmesh.ProxyConfigurationType, fmt.Sprintf("proxy-cfg-%d", i)).
WithData(t, cfg).
WithTenancy(resource.DefaultNamespacedTenancy()).
Write(t, resourceClient)
}
// Create computed proxy cfg resource.
resourcetest.Resource(pbmesh.ComputedProxyConfigurationType, workloadResource.Id.Name).
WithData(t, tc.proxyCfg).
WithTenancy(resource.DefaultNamespacedTenancy()).
Write(t, resourceClient)
req := &pbdataplane.GetEnvoyBootstrapParamsRequest{
ProxyId: workloadResource.Id.Name,
@ -347,7 +344,7 @@ func TestGetEnvoyBootstrapParams_Success_EnableV2(t *testing.T) {
workloadData: &pbcatalog.Workload{
Identity: testIdentity,
},
expBootstrapCfg: &pbmesh.BootstrapConfig{},
expBootstrapCfg: nil,
},
{
name: "workload with node",
@ -355,19 +352,16 @@ func TestGetEnvoyBootstrapParams_Success_EnableV2(t *testing.T) {
Identity: testIdentity,
NodeName: "test-node",
},
expBootstrapCfg: &pbmesh.BootstrapConfig{},
expBootstrapCfg: nil,
},
{
name: "single proxy configuration",
workloadData: &pbcatalog.Workload{
Identity: testIdentity,
},
proxyCfgs: []*pbmesh.ProxyConfiguration{
{
Workloads: &pbcatalog.WorkloadSelector{Names: []string{"test-workload"}},
BootstrapConfig: &pbmesh.BootstrapConfig{
DogstatsdUrl: "dogstats-url",
},
proxyCfg: &pbmesh.ComputedProxyConfiguration{
BootstrapConfig: &pbmesh.BootstrapConfig{
DogstatsdUrl: "dogstats-url",
},
},
expBootstrapCfg: &pbmesh.BootstrapConfig{
@ -379,30 +373,15 @@ func TestGetEnvoyBootstrapParams_Success_EnableV2(t *testing.T) {
workloadData: &pbcatalog.Workload{
Identity: testIdentity,
},
proxyCfgs: []*pbmesh.ProxyConfiguration{
{
Workloads: &pbcatalog.WorkloadSelector{Names: []string{"test-workload"}},
BootstrapConfig: &pbmesh.BootstrapConfig{
DogstatsdUrl: "dogstats-url",
},
proxyCfg: &pbmesh.ComputedProxyConfiguration{
BootstrapConfig: &pbmesh.BootstrapConfig{
DogstatsdUrl: "dogstats-url",
StatsdUrl: "stats-url",
},
{
Workloads: &pbcatalog.WorkloadSelector{Prefixes: []string{"test-"}},
BootstrapConfig: &pbmesh.BootstrapConfig{
StatsdUrl: "stats-url",
},
DynamicConfig: &pbmesh.DynamicConfig{
AccessLogs: &pbmesh.AccessLogsConfig{
Enabled: true,
JsonFormat: "{ \"custom_field\": \"%START_TIME%\" }",
},
},
},
{
Workloads: &pbcatalog.WorkloadSelector{Names: []string{"not-test-workload"}},
BootstrapConfig: &pbmesh.BootstrapConfig{
PrometheusBindAddr: "prom-addr",
DynamicConfig: &pbmesh.DynamicConfig{
AccessLogs: &pbmesh.AccessLogsConfig{
Enabled: true,
JsonFormat: "{ \"custom_field\": \"%START_TIME%\" }",
},
},
},