Revert EnvoyConfig nesting

This commit is contained in:
freddygv 2020-09-11 09:21:43 -06:00
parent 403a180430
commit eab90ea9fa
12 changed files with 388 additions and 492 deletions

View File

@ -746,7 +746,7 @@ func (c *compiler) getSplitterNode(sid structs.ServiceID) (*structs.DiscoveryGra
// We cannot apply multiple hash policies to a splitter node's route action.
// Therefore, we attach the first hash-based load balancer config we encounter.
if !hasLB {
if lb := node.LoadBalancer; lb != nil && lb.EnvoyConfig != nil && lb.EnvoyConfig.IsHashBased() {
if lb := node.LoadBalancer; lb != nil && lb.IsHashBased() {
splitNode.LoadBalancer = node.LoadBalancer
hasLB = true
}

View File

@ -1763,15 +1763,13 @@ func testcase_AllBellsAndWhistles() compileTestCase {
"qa": {Filter: "ServiceMeta.env == qa"},
},
LoadBalancer: &structs.LoadBalancer{
EnvoyConfig: &structs.EnvoyLBConfig{
Policy: "ring_hash",
RingHashConfig: &structs.RingHashConfig{
MaximumRingSize: 100,
},
HashPolicies: []structs.HashPolicy{
{
SourceIP: true,
},
Policy: "ring_hash",
RingHashConfig: &structs.RingHashConfig{
MaximumRingSize: 100,
},
HashPolicies: []structs.HashPolicy{
{
SourceIP: true,
},
},
},
@ -1837,15 +1835,13 @@ func testcase_AllBellsAndWhistles() compileTestCase {
},
},
LoadBalancer: &structs.LoadBalancer{
EnvoyConfig: &structs.EnvoyLBConfig{
Policy: "ring_hash",
RingHashConfig: &structs.RingHashConfig{
MaximumRingSize: 100,
},
HashPolicies: []structs.HashPolicy{
{
SourceIP: true,
},
Policy: "ring_hash",
RingHashConfig: &structs.RingHashConfig{
MaximumRingSize: 100,
},
HashPolicies: []structs.HashPolicy{
{
SourceIP: true,
},
},
},
@ -1858,15 +1854,13 @@ func testcase_AllBellsAndWhistles() compileTestCase {
Target: "prod.redirected.default.dc1",
},
LoadBalancer: &structs.LoadBalancer{
EnvoyConfig: &structs.EnvoyLBConfig{
Policy: "ring_hash",
RingHashConfig: &structs.RingHashConfig{
MaximumRingSize: 100,
},
HashPolicies: []structs.HashPolicy{
{
SourceIP: true,
},
Policy: "ring_hash",
RingHashConfig: &structs.RingHashConfig{
MaximumRingSize: 100,
},
HashPolicies: []structs.HashPolicy{
{
SourceIP: true,
},
},
},
@ -2283,11 +2277,9 @@ func testcase_LBSplitterAndResolver() compileTestCase {
Kind: "service-resolver",
Name: "foo",
LoadBalancer: &structs.LoadBalancer{
EnvoyConfig: &structs.EnvoyLBConfig{
Policy: "least_request",
LeastRequestConfig: &structs.LeastRequestConfig{
ChoiceCount: 3,
},
Policy: "least_request",
LeastRequestConfig: &structs.LeastRequestConfig{
ChoiceCount: 3,
},
},
},
@ -2295,15 +2287,13 @@ func testcase_LBSplitterAndResolver() compileTestCase {
Kind: "service-resolver",
Name: "bar",
LoadBalancer: &structs.LoadBalancer{
EnvoyConfig: &structs.EnvoyLBConfig{
Policy: "ring_hash",
RingHashConfig: &structs.RingHashConfig{
MaximumRingSize: 101,
},
HashPolicies: []structs.HashPolicy{
{
SourceIP: true,
},
Policy: "ring_hash",
RingHashConfig: &structs.RingHashConfig{
MaximumRingSize: 101,
},
HashPolicies: []structs.HashPolicy{
{
SourceIP: true,
},
},
},
@ -2312,18 +2302,16 @@ func testcase_LBSplitterAndResolver() compileTestCase {
Kind: "service-resolver",
Name: "baz",
LoadBalancer: &structs.LoadBalancer{
EnvoyConfig: &structs.EnvoyLBConfig{
Policy: "maglev",
HashPolicies: []structs.HashPolicy{
{
Field: "cookie",
FieldValue: "chocolate-chip",
CookieConfig: &structs.CookieConfig{
TTL: 2 * time.Minute,
Path: "/bowl",
},
Terminal: true,
Policy: "maglev",
HashPolicies: []structs.HashPolicy{
{
Field: "cookie",
FieldValue: "chocolate-chip",
CookieConfig: &structs.CookieConfig{
TTL: 2 * time.Minute,
Path: "/bowl",
},
Terminal: true,
},
},
},
@ -2354,15 +2342,13 @@ func testcase_LBSplitterAndResolver() compileTestCase {
// The LB config from bar is attached because splitters only care about hash-based policies,
// and it's the config from bar not baz because we pick the first one we encounter in the Splits.
LoadBalancer: &structs.LoadBalancer{
EnvoyConfig: &structs.EnvoyLBConfig{
Policy: "ring_hash",
RingHashConfig: &structs.RingHashConfig{
MaximumRingSize: 101,
},
HashPolicies: []structs.HashPolicy{
{
SourceIP: true,
},
Policy: "ring_hash",
RingHashConfig: &structs.RingHashConfig{
MaximumRingSize: 101,
},
HashPolicies: []structs.HashPolicy{
{
SourceIP: true,
},
},
},
@ -2377,11 +2363,9 @@ func testcase_LBSplitterAndResolver() compileTestCase {
Target: "foo.default.dc1",
},
LoadBalancer: &structs.LoadBalancer{
EnvoyConfig: &structs.EnvoyLBConfig{
Policy: "least_request",
LeastRequestConfig: &structs.LeastRequestConfig{
ChoiceCount: 3,
},
Policy: "least_request",
LeastRequestConfig: &structs.LeastRequestConfig{
ChoiceCount: 3,
},
},
},
@ -2394,15 +2378,13 @@ func testcase_LBSplitterAndResolver() compileTestCase {
Target: "bar.default.dc1",
},
LoadBalancer: &structs.LoadBalancer{
EnvoyConfig: &structs.EnvoyLBConfig{
Policy: "ring_hash",
RingHashConfig: &structs.RingHashConfig{
MaximumRingSize: 101,
},
HashPolicies: []structs.HashPolicy{
{
SourceIP: true,
},
Policy: "ring_hash",
RingHashConfig: &structs.RingHashConfig{
MaximumRingSize: 101,
},
HashPolicies: []structs.HashPolicy{
{
SourceIP: true,
},
},
},
@ -2416,18 +2398,16 @@ func testcase_LBSplitterAndResolver() compileTestCase {
Target: "baz.default.dc1",
},
LoadBalancer: &structs.LoadBalancer{
EnvoyConfig: &structs.EnvoyLBConfig{
Policy: "maglev",
HashPolicies: []structs.HashPolicy{
{
Field: "cookie",
FieldValue: "chocolate-chip",
CookieConfig: &structs.CookieConfig{
TTL: 2 * time.Minute,
Path: "/bowl",
},
Terminal: true,
Policy: "maglev",
HashPolicies: []structs.HashPolicy{
{
Field: "cookie",
FieldValue: "chocolate-chip",
CookieConfig: &structs.CookieConfig{
TTL: 2 * time.Minute,
Path: "/bowl",
},
Terminal: true,
},
},
},
@ -2453,15 +2433,13 @@ func testcase_LBResolver() compileTestCase {
Kind: "service-resolver",
Name: "main",
LoadBalancer: &structs.LoadBalancer{
EnvoyConfig: &structs.EnvoyLBConfig{
Policy: "ring_hash",
RingHashConfig: &structs.RingHashConfig{
MaximumRingSize: 101,
},
HashPolicies: []structs.HashPolicy{
{
SourceIP: true,
},
Policy: "ring_hash",
RingHashConfig: &structs.RingHashConfig{
MaximumRingSize: 101,
},
HashPolicies: []structs.HashPolicy{
{
SourceIP: true,
},
},
},
@ -2481,15 +2459,13 @@ func testcase_LBResolver() compileTestCase {
Target: "main.default.dc1",
},
LoadBalancer: &structs.LoadBalancer{
EnvoyConfig: &structs.EnvoyLBConfig{
Policy: "ring_hash",
RingHashConfig: &structs.RingHashConfig{
MaximumRingSize: 101,
},
HashPolicies: []structs.HashPolicy{
{
SourceIP: true,
},
Policy: "ring_hash",
RingHashConfig: &structs.RingHashConfig{
MaximumRingSize: 101,
},
HashPolicies: []structs.HashPolicy{
{
SourceIP: true,
},
},
},

View File

@ -1275,26 +1275,24 @@ func setupTestVariationConfigEntriesAndSnapshot(
Kind: structs.ServiceResolver,
Name: "db",
LoadBalancer: &structs.LoadBalancer{
EnvoyConfig: &structs.EnvoyLBConfig{
Policy: "ring_hash",
RingHashConfig: &structs.RingHashConfig{
MinimumRingSize: 20,
MaximumRingSize: 30,
Policy: "ring_hash",
RingHashConfig: &structs.RingHashConfig{
MinimumRingSize: 20,
MaximumRingSize: 30,
},
HashPolicies: []structs.HashPolicy{
{
Field: "cookie",
FieldValue: "chocolate-chip",
Terminal: true,
},
HashPolicies: []structs.HashPolicy{
{
Field: "cookie",
FieldValue: "chocolate-chip",
Terminal: true,
},
{
Field: "header",
FieldValue: "x-user-id",
},
{
SourceIP: true,
Terminal: true,
},
{
Field: "header",
FieldValue: "x-user-id",
},
{
SourceIP: true,
Terminal: true,
},
},
},

View File

@ -843,27 +843,27 @@ func (e *ServiceResolverConfigEntry) Validate() error {
return fmt.Errorf("Bad ConnectTimeout '%s', must be >= 0", e.ConnectTimeout)
}
if e.LoadBalancer != nil && e.LoadBalancer.EnvoyConfig != nil {
ec := e.LoadBalancer.EnvoyConfig
if e.LoadBalancer != nil {
lb := e.LoadBalancer
if ok := validLBPolicies[ec.Policy]; !ok {
return fmt.Errorf("Bad LoadBalancer policy: %q is not supported", ec.Policy)
if ok := validLBPolicies[lb.Policy]; !ok {
return fmt.Errorf("Bad LoadBalancer policy: %q is not supported", lb.Policy)
}
if ec.Policy != LBPolicyRingHash && ec.RingHashConfig != nil {
if lb.Policy != LBPolicyRingHash && lb.RingHashConfig != nil {
return fmt.Errorf("Bad LoadBalancer configuration. "+
"RingHashConfig specified for incompatible load balancing policy %q", ec.Policy)
"RingHashConfig specified for incompatible load balancing policy %q", lb.Policy)
}
if ec.Policy != LBPolicyLeastRequest && ec.LeastRequestConfig != nil {
if lb.Policy != LBPolicyLeastRequest && lb.LeastRequestConfig != nil {
return fmt.Errorf("Bad LoadBalancer configuration. "+
"LeastRequestConfig specified for incompatible load balancing policy %q", ec.Policy)
"LeastRequestConfig specified for incompatible load balancing policy %q", lb.Policy)
}
if !ec.IsHashBased() && len(ec.HashPolicies) > 0 {
if !lb.IsHashBased() && len(lb.HashPolicies) > 0 {
return fmt.Errorf("Bad LoadBalancer configuration: "+
"HashPolicies specified for non-hash-based Policy: %q", ec.Policy)
"HashPolicies specified for non-hash-based Policy: %q", lb.Policy)
}
for i, hp := range ec.HashPolicies {
for i, hp := range lb.HashPolicies {
if ok := validHashPolicies[hp.Field]; hp.Field != "" && !ok {
return fmt.Errorf("Bad LoadBalancer HashPolicy[%d]: %q is not a supported field", i, hp.Field)
}
@ -1027,14 +1027,6 @@ type ServiceResolverFailover struct {
// LoadBalancer determines the load balancing policy and configuration for services
// issuing requests to this upstream service.
type LoadBalancer struct {
// EnvoyConfig contains Envoy-specific load balancing configuration for this upstream
EnvoyConfig *EnvoyLBConfig `json:",omitempty" alias:"envoy_config"`
// OpaqueConfig contains load balancing configuration opaque to Consul for 3rd party proxies
OpaqueConfig string `json:",omitempty" alias:"opaque_config"`
}
type EnvoyLBConfig struct {
// Policy is the load balancing policy used to select a host
Policy string `json:",omitempty"`
@ -1102,12 +1094,12 @@ type CookieConfig struct {
Path string `json:",omitempty"`
}
func (ec *EnvoyLBConfig) IsHashBased() bool {
if ec == nil {
func (lb *LoadBalancer) IsHashBased() bool {
if lb == nil {
return false
}
switch ec.Policy {
switch lb.Policy {
case LBPolicyMaglev, LBPolicyRingHash:
return true
default:

View File

@ -555,7 +555,7 @@ func TestServiceResolverConfigEntry_LoadBalancer(t *testing.T) {
Kind: ServiceResolver,
Name: "test",
LoadBalancer: &LoadBalancer{
EnvoyConfig: &EnvoyLBConfig{Policy: ""},
Policy: "",
},
},
},
@ -565,7 +565,7 @@ func TestServiceResolverConfigEntry_LoadBalancer(t *testing.T) {
Kind: ServiceResolver,
Name: "test",
LoadBalancer: &LoadBalancer{
EnvoyConfig: &EnvoyLBConfig{Policy: LBPolicyRandom},
Policy: LBPolicyRandom,
},
},
},
@ -575,7 +575,7 @@ func TestServiceResolverConfigEntry_LoadBalancer(t *testing.T) {
Kind: ServiceResolver,
Name: "test",
LoadBalancer: &LoadBalancer{
EnvoyConfig: &EnvoyLBConfig{Policy: "fake-policy"},
Policy: "fake-policy",
},
},
validateErr: `"fake-policy" is not supported`,
@ -586,10 +586,8 @@ func TestServiceResolverConfigEntry_LoadBalancer(t *testing.T) {
Kind: ServiceResolver,
Name: "test",
LoadBalancer: &LoadBalancer{
EnvoyConfig: &EnvoyLBConfig{
Policy: LBPolicyRingHash,
LeastRequestConfig: &LeastRequestConfig{ChoiceCount: 10},
},
Policy: LBPolicyRingHash,
LeastRequestConfig: &LeastRequestConfig{ChoiceCount: 10},
},
},
validateErr: `LeastRequestConfig specified for incompatible load balancing policy`,
@ -600,10 +598,8 @@ func TestServiceResolverConfigEntry_LoadBalancer(t *testing.T) {
Kind: ServiceResolver,
Name: "test",
LoadBalancer: &LoadBalancer{
EnvoyConfig: &EnvoyLBConfig{
Policy: LBPolicyLeastRequest,
RingHashConfig: &RingHashConfig{MinimumRingSize: 1024},
},
Policy: LBPolicyLeastRequest,
RingHashConfig: &RingHashConfig{MinimumRingSize: 1024},
},
},
validateErr: `RingHashConfig specified for incompatible load balancing policy`,
@ -614,10 +610,8 @@ func TestServiceResolverConfigEntry_LoadBalancer(t *testing.T) {
Kind: ServiceResolver,
Name: "test",
LoadBalancer: &LoadBalancer{
EnvoyConfig: &EnvoyLBConfig{
Policy: LBPolicyRingHash,
RingHashConfig: &RingHashConfig{MinimumRingSize: 1024},
},
Policy: LBPolicyRingHash,
RingHashConfig: &RingHashConfig{MinimumRingSize: 1024},
},
},
},
@ -627,10 +621,8 @@ func TestServiceResolverConfigEntry_LoadBalancer(t *testing.T) {
Kind: ServiceResolver,
Name: "test",
LoadBalancer: &LoadBalancer{
EnvoyConfig: &EnvoyLBConfig{
Policy: LBPolicyLeastRequest,
LeastRequestConfig: &LeastRequestConfig{ChoiceCount: 2},
},
Policy: LBPolicyLeastRequest,
LeastRequestConfig: &LeastRequestConfig{ChoiceCount: 2},
},
},
},
@ -640,11 +632,11 @@ func TestServiceResolverConfigEntry_LoadBalancer(t *testing.T) {
Kind: ServiceResolver,
Name: "test",
LoadBalancer: &LoadBalancer{
EnvoyConfig: &EnvoyLBConfig{Policy: ""},
Policy: "",
},
},
check: func(t *testing.T, entry *ServiceResolverConfigEntry) {
require.Equal(t, "", entry.LoadBalancer.EnvoyConfig.Policy)
require.Equal(t, "", entry.LoadBalancer.Policy)
},
},
{
@ -653,12 +645,10 @@ func TestServiceResolverConfigEntry_LoadBalancer(t *testing.T) {
Kind: ServiceResolver,
Name: "test",
LoadBalancer: &LoadBalancer{
EnvoyConfig: &EnvoyLBConfig{
Policy: "",
HashPolicies: []HashPolicy{
{
SourceIP: true,
},
Policy: "",
HashPolicies: []HashPolicy{
{
SourceIP: true,
},
},
},
@ -671,16 +661,14 @@ func TestServiceResolverConfigEntry_LoadBalancer(t *testing.T) {
Kind: ServiceResolver,
Name: "test",
LoadBalancer: &LoadBalancer{
EnvoyConfig: &EnvoyLBConfig{
Policy: LBPolicyMaglev,
HashPolicies: []HashPolicy{
{
Field: HashPolicyHeader,
FieldValue: "x-user-id",
CookieConfig: &CookieConfig{
TTL: 10 * time.Second,
Path: "/root",
},
Policy: LBPolicyMaglev,
HashPolicies: []HashPolicy{
{
Field: HashPolicyHeader,
FieldValue: "x-user-id",
CookieConfig: &CookieConfig{
TTL: 10 * time.Second,
Path: "/root",
},
},
},
@ -694,16 +682,14 @@ func TestServiceResolverConfigEntry_LoadBalancer(t *testing.T) {
Kind: ServiceResolver,
Name: "test",
LoadBalancer: &LoadBalancer{
EnvoyConfig: &EnvoyLBConfig{
Policy: LBPolicyMaglev,
HashPolicies: []HashPolicy{
{
Field: HashPolicyCookie,
FieldValue: "good-cookie",
CookieConfig: &CookieConfig{
TTL: 10 * time.Second,
Path: "/oven",
},
Policy: LBPolicyMaglev,
HashPolicies: []HashPolicy{
{
Field: HashPolicyCookie,
FieldValue: "good-cookie",
CookieConfig: &CookieConfig{
TTL: 10 * time.Second,
Path: "/oven",
},
},
},
@ -716,13 +702,11 @@ func TestServiceResolverConfigEntry_LoadBalancer(t *testing.T) {
Kind: ServiceResolver,
Name: "test",
LoadBalancer: &LoadBalancer{
EnvoyConfig: &EnvoyLBConfig{
Policy: LBPolicyMaglev,
HashPolicies: []HashPolicy{
{
Field: "header",
FieldValue: "X-Consul-Token",
},
Policy: LBPolicyMaglev,
HashPolicies: []HashPolicy{
{
Field: "header",
FieldValue: "X-Consul-Token",
},
},
},
@ -734,12 +718,10 @@ func TestServiceResolverConfigEntry_LoadBalancer(t *testing.T) {
Kind: ServiceResolver,
Name: "test",
LoadBalancer: &LoadBalancer{
EnvoyConfig: &EnvoyLBConfig{
Policy: LBPolicyMaglev,
HashPolicies: []HashPolicy{
{
Field: "fake-field",
},
Policy: LBPolicyMaglev,
HashPolicies: []HashPolicy{
{
Field: "fake-field",
},
},
},
@ -752,13 +734,11 @@ func TestServiceResolverConfigEntry_LoadBalancer(t *testing.T) {
Kind: ServiceResolver,
Name: "test",
LoadBalancer: &LoadBalancer{
EnvoyConfig: &EnvoyLBConfig{
Policy: LBPolicyMaglev,
HashPolicies: []HashPolicy{
{
Field: "header",
SourceIP: true,
},
Policy: LBPolicyMaglev,
HashPolicies: []HashPolicy{
{
Field: "header",
SourceIP: true,
},
},
},
@ -771,13 +751,11 @@ func TestServiceResolverConfigEntry_LoadBalancer(t *testing.T) {
Kind: ServiceResolver,
Name: "test",
LoadBalancer: &LoadBalancer{
EnvoyConfig: &EnvoyLBConfig{
Policy: LBPolicyMaglev,
HashPolicies: []HashPolicy{
{
FieldValue: "X-Consul-Token",
SourceIP: true,
},
Policy: LBPolicyMaglev,
HashPolicies: []HashPolicy{
{
FieldValue: "X-Consul-Token",
SourceIP: true,
},
},
},
@ -790,12 +768,10 @@ func TestServiceResolverConfigEntry_LoadBalancer(t *testing.T) {
Kind: ServiceResolver,
Name: "test",
LoadBalancer: &LoadBalancer{
EnvoyConfig: &EnvoyLBConfig{
Policy: LBPolicyMaglev,
HashPolicies: []HashPolicy{
{
Field: "header",
},
Policy: LBPolicyMaglev,
HashPolicies: []HashPolicy{
{
Field: "header",
},
},
},
@ -808,12 +784,10 @@ func TestServiceResolverConfigEntry_LoadBalancer(t *testing.T) {
Kind: ServiceResolver,
Name: "test",
LoadBalancer: &LoadBalancer{
EnvoyConfig: &EnvoyLBConfig{
Policy: LBPolicyMaglev,
HashPolicies: []HashPolicy{
{
FieldValue: "my-cookie",
},
Policy: LBPolicyMaglev,
HashPolicies: []HashPolicy{
{
FieldValue: "my-cookie",
},
},
},
@ -826,19 +800,17 @@ func TestServiceResolverConfigEntry_LoadBalancer(t *testing.T) {
Kind: ServiceResolver,
Name: "test",
LoadBalancer: &LoadBalancer{
EnvoyConfig: &EnvoyLBConfig{
Policy: LBPolicyRingHash,
RingHashConfig: &RingHashConfig{MaximumRingSize: 10, MinimumRingSize: 2},
HashPolicies: []HashPolicy{
{
Field: "cookie",
FieldValue: "my-cookie",
},
{
Field: "header",
FieldValue: "alt-header",
Terminal: true,
},
Policy: LBPolicyRingHash,
RingHashConfig: &RingHashConfig{MaximumRingSize: 10, MinimumRingSize: 2},
HashPolicies: []HashPolicy{
{
Field: "cookie",
FieldValue: "my-cookie",
},
{
Field: "header",
FieldValue: "alt-header",
Terminal: true,
},
},
},
@ -850,10 +822,8 @@ func TestServiceResolverConfigEntry_LoadBalancer(t *testing.T) {
Kind: ServiceResolver,
Name: "test",
LoadBalancer: &LoadBalancer{
EnvoyConfig: &EnvoyLBConfig{
Policy: LBPolicyLeastRequest,
LeastRequestConfig: &LeastRequestConfig{ChoiceCount: 20},
},
Policy: LBPolicyLeastRequest,
LeastRequestConfig: &LeastRequestConfig{ChoiceCount: 20},
},
},
},

View File

@ -206,14 +206,14 @@ func (s *Server) makeGatewayServiceClusters(cfgSnap *proxycfg.ConfigSnapshot) ([
clusterName := connect.ServiceSNI(svc.Name, "", svc.NamespaceOrDefault(), cfgSnap.Datacenter, cfgSnap.Roots.TrustDomain)
resolver, hasResolver := resolvers[svc]
var loadBalancer *structs.EnvoyLBConfig
var loadBalancer *structs.LoadBalancer
if !hasResolver {
// Use a zero value resolver with no timeout and no subsets
resolver = &structs.ServiceResolverConfigEntry{}
}
if resolver.LoadBalancer != nil {
loadBalancer = resolver.LoadBalancer.EnvoyConfig
loadBalancer = resolver.LoadBalancer
}
// When making service clusters we only pass endpoints with hostnames if the kind is a terminating gateway
@ -514,9 +514,9 @@ func (s *Server) makeUpstreamClustersForDiscoveryChain(
OutlierDetection: cfg.PassiveHealthCheck.AsOutlierDetection(),
}
var lb *structs.EnvoyLBConfig
var lb *structs.LoadBalancer
if node.LoadBalancer != nil {
lb = node.LoadBalancer.EnvoyConfig
lb = node.LoadBalancer
}
if err := injectLBToCluster(lb, c); err != nil {
return nil, fmt.Errorf("failed to apply load balancer configuration to cluster %q: %v", clusterName, err)
@ -595,7 +595,7 @@ func makeClusterFromUserConfig(configJSON string) (*envoy.Cluster, error) {
return &c, err
}
// No @type so try decoding as a straight listener.
// No @type so try decoding as a straight cluster.
err := jsonpb.UnmarshalString(configJSON, &c)
return &c, err
}
@ -783,7 +783,7 @@ func makeLbEndpoint(addr string, port int, health envoycore.HealthStatus, weight
}
}
func injectLBToCluster(ec *structs.EnvoyLBConfig, c *envoy.Cluster) error {
func injectLBToCluster(ec *structs.LoadBalancer, c *envoy.Cluster) error {
if ec == nil {
return nil
}

View File

@ -368,11 +368,9 @@ func TestClustersFromSnapshot(t *testing.T) {
},
},
LoadBalancer: &structs.LoadBalancer{
EnvoyConfig: &structs.EnvoyLBConfig{
Policy: "least_request",
LeastRequestConfig: &structs.LeastRequestConfig{
ChoiceCount: 5,
},
Policy: "least_request",
LeastRequestConfig: &structs.LeastRequestConfig{
ChoiceCount: 5,
},
},
},
@ -397,12 +395,10 @@ func TestClustersFromSnapshot(t *testing.T) {
},
},
LoadBalancer: &structs.LoadBalancer{
EnvoyConfig: &structs.EnvoyLBConfig{
Policy: "ring_hash",
RingHashConfig: &structs.RingHashConfig{
MinimumRingSize: 20,
MaximumRingSize: 50,
},
Policy: "ring_hash",
RingHashConfig: &structs.RingHashConfig{
MinimumRingSize: 20,
MaximumRingSize: 50,
},
},
},
@ -625,12 +621,10 @@ func TestClustersFromSnapshot(t *testing.T) {
},
},
LoadBalancer: &structs.LoadBalancer{
EnvoyConfig: &structs.EnvoyLBConfig{
Policy: "ring_hash",
RingHashConfig: &structs.RingHashConfig{
MinimumRingSize: 20,
MaximumRingSize: 50,
},
Policy: "ring_hash",
RingHashConfig: &structs.RingHashConfig{
MinimumRingSize: 20,
MaximumRingSize: 50,
},
},
},
@ -862,40 +856,40 @@ func setupTLSRootsAndLeaf(t *testing.T, snap *proxycfg.ConfigSnapshot) {
func TestEnvoyLBConfig_InjectToCluster(t *testing.T) {
var tests = []struct {
name string
lb *structs.EnvoyLBConfig
lb *structs.LoadBalancer
expected envoy.Cluster
}{
{
name: "skip empty",
lb: &structs.EnvoyLBConfig{
lb: &structs.LoadBalancer{
Policy: "",
},
expected: envoy.Cluster{},
},
{
name: "round robin",
lb: &structs.EnvoyLBConfig{
lb: &structs.LoadBalancer{
Policy: structs.LBPolicyRoundRobin,
},
expected: envoy.Cluster{LbPolicy: envoy.Cluster_ROUND_ROBIN},
},
{
name: "random",
lb: &structs.EnvoyLBConfig{
lb: &structs.LoadBalancer{
Policy: structs.LBPolicyRandom,
},
expected: envoy.Cluster{LbPolicy: envoy.Cluster_RANDOM},
},
{
name: "maglev",
lb: &structs.EnvoyLBConfig{
lb: &structs.LoadBalancer{
Policy: structs.LBPolicyMaglev,
},
expected: envoy.Cluster{LbPolicy: envoy.Cluster_MAGLEV},
},
{
name: "ring_hash",
lb: &structs.EnvoyLBConfig{
lb: &structs.LoadBalancer{
Policy: structs.LBPolicyRingHash,
RingHashConfig: &structs.RingHashConfig{
MinimumRingSize: 3,
@ -914,7 +908,7 @@ func TestEnvoyLBConfig_InjectToCluster(t *testing.T) {
},
{
name: "least_request",
lb: &structs.EnvoyLBConfig{
lb: &structs.LoadBalancer{
Policy: "least_request",
LeastRequestConfig: &structs.LeastRequestConfig{
ChoiceCount: 3,

View File

@ -65,9 +65,9 @@ func (s *Server) routesFromSnapshotTerminatingGateway(_ connectionInfo, cfgSnap
resolver = &structs.ServiceResolverConfigEntry{}
}
var lb *structs.EnvoyLBConfig
var lb *structs.LoadBalancer
if resolver.LoadBalancer != nil {
lb = resolver.LoadBalancer.EnvoyConfig
lb = resolver.LoadBalancer
}
route, err := makeNamedDefaultRouteWithLB(clusterName, lb)
if err != nil {
@ -91,7 +91,7 @@ func (s *Server) routesFromSnapshotTerminatingGateway(_ connectionInfo, cfgSnap
return resources, nil
}
func makeNamedDefaultRouteWithLB(clusterName string, lb *structs.EnvoyLBConfig) (*envoy.RouteConfiguration, error) {
func makeNamedDefaultRouteWithLB(clusterName string, lb *structs.LoadBalancer) (*envoy.RouteConfiguration, error) {
action := makeRouteActionFromName(clusterName)
if err := injectLBToRouteAction(lb, action.Route); err != nil {
@ -262,7 +262,7 @@ func makeUpstreamRouteForDiscoveryChain(
return nil, fmt.Errorf("missing first node in compiled discovery chain for: %s", chain.ServiceName)
}
var lb *structs.EnvoyLBConfig
var lb *structs.LoadBalancer
switch startNode.Type {
case structs.DiscoveryGraphNodeTypeRouter:
@ -278,7 +278,7 @@ func makeUpstreamRouteForDiscoveryChain(
nextNode := chain.Nodes[discoveryRoute.NextNode]
if nextNode.LoadBalancer != nil {
lb = nextNode.LoadBalancer.EnvoyConfig
lb = nextNode.LoadBalancer
}
switch nextNode.Type {
@ -351,7 +351,7 @@ func makeUpstreamRouteForDiscoveryChain(
}
if startNode.LoadBalancer != nil {
lb = startNode.LoadBalancer.EnvoyConfig
lb = startNode.LoadBalancer
}
if err := injectLBToRouteAction(lb, routeAction.Route); err != nil {
return nil, fmt.Errorf("failed to apply load balancer configuration to route action: %v", err)
@ -368,7 +368,7 @@ func makeUpstreamRouteForDiscoveryChain(
routeAction := makeRouteActionForChainCluster(startNode.Resolver.Target, chain)
if startNode.LoadBalancer != nil {
lb = startNode.LoadBalancer.EnvoyConfig
lb = startNode.LoadBalancer
}
if err := injectLBToRouteAction(lb, routeAction.Route); err != nil {
return nil, fmt.Errorf("failed to apply load balancer configuration to route action: %v", err)
@ -577,13 +577,13 @@ func makeRouteActionForSplitter(splits []*structs.DiscoverySplit, chain *structs
}, nil
}
func injectLBToRouteAction(ec *structs.EnvoyLBConfig, action *envoyroute.RouteAction) error {
if ec == nil || !ec.IsHashBased() {
func injectLBToRouteAction(lb *structs.LoadBalancer, action *envoyroute.RouteAction) error {
if lb == nil || !lb.IsHashBased() {
return nil
}
result := make([]*envoyroute.RouteAction_HashPolicy, 0, len(ec.HashPolicies))
for _, policy := range ec.HashPolicies {
result := make([]*envoyroute.RouteAction_HashPolicy, 0, len(lb.HashPolicies))
for _, policy := range lb.HashPolicies {
if policy.SourceIP {
result = append(result, &envoyroute.RouteAction_HashPolicy{
PolicySpecifier: &envoyroute.RouteAction_HashPolicy_ConnectionProperties_{

View File

@ -206,26 +206,24 @@ func TestRoutesFromSnapshot(t *testing.T) {
},
},
LoadBalancer: &structs.LoadBalancer{
EnvoyConfig: &structs.EnvoyLBConfig{
Policy: "ring_hash",
RingHashConfig: &structs.RingHashConfig{
MinimumRingSize: 20,
MaximumRingSize: 50,
Policy: "ring_hash",
RingHashConfig: &structs.RingHashConfig{
MinimumRingSize: 20,
MaximumRingSize: 50,
},
HashPolicies: []structs.HashPolicy{
{
Field: structs.HashPolicyCookie,
FieldValue: "chocolate-chip",
Terminal: true,
},
HashPolicies: []structs.HashPolicy{
{
Field: structs.HashPolicyCookie,
FieldValue: "chocolate-chip",
Terminal: true,
},
{
Field: structs.HashPolicyHeader,
FieldValue: "x-user-id",
},
{
SourceIP: true,
Terminal: true,
},
{
Field: structs.HashPolicyHeader,
FieldValue: "x-user-id",
},
{
SourceIP: true,
Terminal: true,
},
},
},
@ -291,12 +289,12 @@ func TestRoutesFromSnapshot(t *testing.T) {
func TestEnvoyLBConfig_InjectToRouteAction(t *testing.T) {
var tests = []struct {
name string
lb *structs.EnvoyLBConfig
lb *structs.LoadBalancer
expected envoyroute.RouteAction
}{
{
name: "empty",
lb: &structs.EnvoyLBConfig{
lb: &structs.LoadBalancer{
Policy: "",
},
// we only modify route actions for hash-based LB policies
@ -304,7 +302,7 @@ func TestEnvoyLBConfig_InjectToRouteAction(t *testing.T) {
},
{
name: "least request",
lb: &structs.EnvoyLBConfig{
lb: &structs.LoadBalancer{
Policy: structs.LBPolicyLeastRequest,
LeastRequestConfig: &structs.LeastRequestConfig{
ChoiceCount: 3,
@ -315,7 +313,7 @@ func TestEnvoyLBConfig_InjectToRouteAction(t *testing.T) {
},
{
name: "headers",
lb: &structs.EnvoyLBConfig{
lb: &structs.LoadBalancer{
Policy: "ring_hash",
RingHashConfig: &structs.RingHashConfig{
MinimumRingSize: 3,
@ -344,7 +342,7 @@ func TestEnvoyLBConfig_InjectToRouteAction(t *testing.T) {
},
{
name: "cookies",
lb: &structs.EnvoyLBConfig{
lb: &structs.LoadBalancer{
Policy: structs.LBPolicyMaglev,
HashPolicies: []structs.HashPolicy{
{
@ -380,7 +378,7 @@ func TestEnvoyLBConfig_InjectToRouteAction(t *testing.T) {
},
{
name: "source addr",
lb: &structs.EnvoyLBConfig{
lb: &structs.LoadBalancer{
Policy: structs.LBPolicyMaglev,
HashPolicies: []structs.HashPolicy{
{
@ -404,7 +402,7 @@ func TestEnvoyLBConfig_InjectToRouteAction(t *testing.T) {
},
{
name: "kitchen sink",
lb: &structs.EnvoyLBConfig{
lb: &structs.LoadBalancer{
Policy: structs.LBPolicyMaglev,
HashPolicies: []structs.HashPolicy{
{

View File

@ -209,14 +209,6 @@ type ServiceResolverFailover struct {
// LoadBalancer determines the load balancing policy and configuration for services
// issuing requests to this upstream service.
type LoadBalancer struct {
// EnvoyConfig contains Envoy-specific load balancing configuration for this upstream
EnvoyConfig *EnvoyLBConfig `json:",omitempty" alias:"envoy_config"`
// OpaqueConfig contains load balancing configuration opaque to Consul for 3rd party proxies
OpaqueConfig string `json:",omitempty" alias:"opaque_config"`
}
type EnvoyLBConfig struct {
// Policy is the load balancing policy used to select a host
Policy string `json:",omitempty"`

View File

@ -294,10 +294,8 @@ func TestAPI_ConfigEntry_ServiceResolver_LoadBalancer(t *testing.T) {
Name: "test-least-req",
Namespace: defaultNamespace,
LoadBalancer: &LoadBalancer{
EnvoyConfig: &EnvoyLBConfig{
Policy: "least_request",
LeastRequestConfig: &LeastRequestConfig{ChoiceCount: 10},
},
Policy: "least_request",
LeastRequestConfig: &LeastRequestConfig{ChoiceCount: 10},
},
},
verify: verifyResolver,
@ -309,30 +307,28 @@ func TestAPI_ConfigEntry_ServiceResolver_LoadBalancer(t *testing.T) {
Name: "test-ring-hash",
Namespace: defaultNamespace,
LoadBalancer: &LoadBalancer{
EnvoyConfig: &EnvoyLBConfig{
Policy: "ring_hash",
RingHashConfig: &RingHashConfig{
MinimumRingSize: 1024 * 2,
MaximumRingSize: 1024 * 4,
Policy: "ring_hash",
RingHashConfig: &RingHashConfig{
MinimumRingSize: 1024 * 2,
MaximumRingSize: 1024 * 4,
},
HashPolicies: []HashPolicy{
{
Field: "header",
FieldValue: "my-session-header",
Terminal: true,
},
HashPolicies: []HashPolicy{
{
Field: "header",
FieldValue: "my-session-header",
Terminal: true,
},
{
Field: "cookie",
FieldValue: "oreo",
CookieConfig: &CookieConfig{
Path: "/tray",
TTL: 20 * time.Millisecond,
},
},
{
SourceIP: true,
{
Field: "cookie",
FieldValue: "oreo",
CookieConfig: &CookieConfig{
Path: "/tray",
TTL: 20 * time.Millisecond,
},
},
{
SourceIP: true,
},
},
},
},

View File

@ -1172,62 +1172,58 @@ func TestParseConfigEntry(t *testing.T) {
kind = "service-resolver"
name = "main"
load_balancer = {
envoy_config = {
policy = "ring_hash"
ring_hash_config = {
minimum_ring_size = 1
maximum_ring_size = 2
}
hash_policies = [
{
field = "cookie"
field_value = "good-cookie"
cookie_config = {
ttl = "1s"
path = "/oven"
}
terminal = true
},
{
field = "header"
field_value = "x-user-id"
},
{
source_ip = true
}
]
policy = "ring_hash"
ring_hash_config = {
minimum_ring_size = 1
maximum_ring_size = 2
}
hash_policies = [
{
field = "cookie"
field_value = "good-cookie"
cookie_config = {
ttl = "1s"
path = "/oven"
}
terminal = true
},
{
field = "header"
field_value = "x-user-id"
},
{
source_ip = true
}
]
}
`,
camel: `
Kind = "service-resolver"
Name = "main"
LoadBalancer = {
EnvoyConfig = {
Policy = "ring_hash"
RingHashConfig = {
MinimumRingSize = 1
MaximumRingSize = 2
}
HashPolicies = [
{
Field = "cookie"
FieldValue = "good-cookie"
CookieConfig = {
TTL = "1s"
Path = "/oven"
}
Terminal = true
},
{
Field = "header"
FieldValue = "x-user-id"
},
{
SourceIP = true
}
]
Policy = "ring_hash"
RingHashConfig = {
MinimumRingSize = 1
MaximumRingSize = 2
}
HashPolicies = [
{
Field = "cookie"
FieldValue = "good-cookie"
CookieConfig = {
TTL = "1s"
Path = "/oven"
}
Terminal = true
},
{
Field = "header"
FieldValue = "x-user-id"
},
{
SourceIP = true
}
]
}
`,
snakeJSON: `
@ -1235,31 +1231,29 @@ func TestParseConfigEntry(t *testing.T) {
"kind": "service-resolver",
"name": "main",
"load_balancer": {
"envoy_config": {
"policy": "ring_hash",
"ring_hash_config": {
"minimum_ring_size": 1,
"maximum_ring_size": 2
"policy": "ring_hash",
"ring_hash_config": {
"minimum_ring_size": 1,
"maximum_ring_size": 2
},
"hash_policies": [
{
"field": "cookie",
"field_value": "good-cookie",
"cookie_config": {
"ttl": "1s",
"path": "/oven"
},
"terminal": true
},
"hash_policies": [
{
"field": "cookie",
"field_value": "good-cookie",
"cookie_config": {
"ttl": "1s",
"path": "/oven"
},
"terminal": true
},
{
"field": "header",
"field_value": "x-user-id"
},
{
"source_ip": true
}
]
}
{
"field": "header",
"field_value": "x-user-id"
},
{
"source_ip": true
}
]
}
}
`,
@ -1268,31 +1262,29 @@ func TestParseConfigEntry(t *testing.T) {
"Kind": "service-resolver",
"Name": "main",
"LoadBalancer": {
"EnvoyConfig": {
"Policy": "ring_hash",
"RingHashConfig": {
"MinimumRingSize": 1,
"MaximumRingSize": 2
"Policy": "ring_hash",
"RingHashConfig": {
"MinimumRingSize": 1,
"MaximumRingSize": 2
},
"HashPolicies": [
{
"Field": "cookie",
"FieldValue": "good-cookie",
"CookieConfig": {
"TTL": "1s",
"Path": "/oven"
},
"Terminal": true
},
"HashPolicies": [
{
"Field": "cookie",
"FieldValue": "good-cookie",
"CookieConfig": {
"TTL": "1s",
"Path": "/oven"
},
"Terminal": true
},
{
"Field": "header",
"FieldValue": "x-user-id"
},
{
"SourceIP": true
}
]
}
{
"Field": "header",
"FieldValue": "x-user-id"
},
{
"SourceIP": true
}
]
}
}
`,
@ -1300,29 +1292,27 @@ func TestParseConfigEntry(t *testing.T) {
Kind: "service-resolver",
Name: "main",
LoadBalancer: &api.LoadBalancer{
EnvoyConfig: &api.EnvoyLBConfig{
Policy: structs.LBPolicyRingHash,
RingHashConfig: &api.RingHashConfig{
MinimumRingSize: 1,
MaximumRingSize: 2,
Policy: structs.LBPolicyRingHash,
RingHashConfig: &api.RingHashConfig{
MinimumRingSize: 1,
MaximumRingSize: 2,
},
HashPolicies: []api.HashPolicy{
{
Field: structs.HashPolicyCookie,
FieldValue: "good-cookie",
CookieConfig: &api.CookieConfig{
TTL: 1 * time.Second,
Path: "/oven",
},
Terminal: true,
},
HashPolicies: []api.HashPolicy{
{
Field: structs.HashPolicyCookie,
FieldValue: "good-cookie",
CookieConfig: &api.CookieConfig{
TTL: 1 * time.Second,
Path: "/oven",
},
Terminal: true,
},
{
Field: structs.HashPolicyHeader,
FieldValue: "x-user-id",
},
{
SourceIP: true,
},
{
Field: structs.HashPolicyHeader,
FieldValue: "x-user-id",
},
{
SourceIP: true,
},
},
},
@ -1334,11 +1324,9 @@ func TestParseConfigEntry(t *testing.T) {
kind = "service-resolver"
name = "main"
load_balancer = {
envoy_config = {
policy = "least_request"
least_request_config = {
choice_count = 2
}
policy = "least_request"
least_request_config = {
choice_count = 2
}
}
`,
@ -1346,11 +1334,9 @@ func TestParseConfigEntry(t *testing.T) {
Kind = "service-resolver"
Name = "main"
LoadBalancer = {
EnvoyConfig = {
Policy = "least_request"
LeastRequestConfig = {
ChoiceCount = 2
}
Policy = "least_request"
LeastRequestConfig = {
ChoiceCount = 2
}
}
`,
@ -1359,11 +1345,9 @@ func TestParseConfigEntry(t *testing.T) {
"kind": "service-resolver",
"name": "main",
"load_balancer": {
"envoy_config": {
"policy": "least_request",
"least_request_config": {
"choice_count": 2
}
"policy": "least_request",
"least_request_config": {
"choice_count": 2
}
}
}
@ -1373,11 +1357,9 @@ func TestParseConfigEntry(t *testing.T) {
"Kind": "service-resolver",
"Name": "main",
"LoadBalancer": {
"EnvoyConfig": {
"Policy": "least_request",
"LeastRequestConfig": {
"ChoiceCount": 2
}
"Policy": "least_request",
"LeastRequestConfig": {
"ChoiceCount": 2
}
}
}
@ -1386,11 +1368,9 @@ func TestParseConfigEntry(t *testing.T) {
Kind: "service-resolver",
Name: "main",
LoadBalancer: &api.LoadBalancer{
EnvoyConfig: &api.EnvoyLBConfig{
Policy: structs.LBPolicyLeastRequest,
LeastRequestConfig: &api.LeastRequestConfig{
ChoiceCount: 2,
},
Policy: structs.LBPolicyLeastRequest,
LeastRequestConfig: &api.LeastRequestConfig{
ChoiceCount: 2,
},
},
},