From ef877449ce3916ae3620a1ca0f11d63232159cbe Mon Sep 17 00:00:00 2001 From: freddygv Date: Wed, 2 Sep 2020 15:49:03 -0600 Subject: [PATCH] Move valid policies to pkg level --- agent/structs/config_entry_discoverychain.go | 35 +++++++++++--------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/agent/structs/config_entry_discoverychain.go b/agent/structs/config_entry_discoverychain.go index 9fc2550c01..d0cd7e46d0 100644 --- a/agent/structs/config_entry_discoverychain.go +++ b/agent/structs/config_entry_discoverychain.go @@ -30,6 +30,23 @@ const ( HashPolicyQueryParam = "query_parameter" ) +var ( + validLBPolicies = map[string]bool{ + "": true, + LBPolicyRandom: true, + LBPolicyRoundRobin: true, + LBPolicyLeastRequest: true, + LBPolicyRingHash: true, + LBPolicyMaglev: true, + } + + validHashPolicies = map[string]bool{ + HashPolicyHeader: true, + HashPolicyCookie: true, + HashPolicyQueryParam: true, + } +) + // ServiceRouterConfigEntry defines L7 (e.g. http) routing rules for a named // service exposed in Connect. // @@ -828,15 +845,7 @@ func (e *ServiceResolverConfigEntry) Validate() error { if e.LoadBalancer != nil && e.LoadBalancer.EnvoyConfig != nil { ec := e.LoadBalancer.EnvoyConfig - validPolicies := map[string]bool{ - "": true, - LBPolicyRandom: true, - LBPolicyRoundRobin: true, - LBPolicyLeastRequest: true, - LBPolicyRingHash: true, - LBPolicyMaglev: true, - } - if ok := validPolicies[ec.Policy]; !ok { + if ok := validLBPolicies[ec.Policy]; !ok { return fmt.Errorf("Bad LoadBalancer policy: %q is not supported", ec.Policy) } @@ -853,15 +862,11 @@ func (e *ServiceResolverConfigEntry) Validate() error { "HashPolicies specified for non-hash-based Policy: %q", ec.Policy) } - validFields := map[string]bool{ - HashPolicyHeader: true, - HashPolicyCookie: true, - HashPolicyQueryParam: true, - } for i, hp := range ec.HashPolicies { - if ok := validFields[hp.Field]; hp.Field != "" && !ok { + if ok := validHashPolicies[hp.Field]; hp.Field != "" && !ok { return fmt.Errorf("Bad LoadBalancer HashPolicy[%d]: %q is not a supported field", i, hp.Field) } + if hp.SourceIP && hp.Field != "" { return fmt.Errorf("Bad LoadBalancer HashPolicy[%d]: "+ "A single hash policy cannot hash both a source address and a %q", i, hp.Field)