Refactor disco chain prioritize by locality structs (#17696)

This includes prioritize by localities on disco chain targets rather than
resolvers, allowing different targets within the same partition to have
different policies.
This commit is contained in:
Eric Haberkorn 2023-06-13 11:03:30 -04:00 committed by GitHub
parent bba5cd8455
commit 0a1efe73f3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 33 additions and 24 deletions

View File

@ -1009,19 +1009,25 @@ RESOLVE_AGAIN:
Type: structs.DiscoveryGraphNodeTypeResolver,
Name: target.ID,
Resolver: &structs.DiscoveryResolver{
Default: resolver.IsDefault(),
Target: target.ID,
ConnectTimeout: connectTimeout,
RequestTimeout: resolver.RequestTimeout,
PrioritizeByLocality: resolver.PrioritizeByLocality.ToDiscovery(),
Default: resolver.IsDefault(),
Target: target.ID,
ConnectTimeout: connectTimeout,
RequestTimeout: resolver.RequestTimeout,
},
LoadBalancer: resolver.LoadBalancer,
}
// Merge default values from the proxy defaults
proxyDefault := c.entries.GetProxyDefaults(targetID.PartitionOrDefault())
if proxyDefault != nil && node.Resolver.PrioritizeByLocality == nil {
node.Resolver.PrioritizeByLocality = proxyDefault.PrioritizeByLocality.ToDiscovery()
// Only set PrioritizeByLocality for targets in the same partition.
if target.Partition == c.evaluateInPartition && target.Peer == "" {
if resolver.PrioritizeByLocality != nil {
target.PrioritizeByLocality = resolver.PrioritizeByLocality.ToDiscovery()
}
if target.PrioritizeByLocality == nil && proxyDefault != nil {
target.PrioritizeByLocality = proxyDefault.PrioritizeByLocality.ToDiscovery()
}
}
target.Subset = resolver.Subsets[target.ServiceSubset]

View File

@ -3301,6 +3301,7 @@ func newTarget(opts structs.DiscoveryTargetOpts, modFn func(t *structs.Discovery
t.SNI = connect.TargetSNI(t, "trustdomain.consul")
t.Name = t.SNI
t.ConnectTimeout = 5 * time.Second // default
t.PrioritizeByLocality = opts.PrioritizeByLocality
if modFn != nil {
modFn(t)
}

View File

@ -121,12 +121,11 @@ func (s *DiscoveryGraphNode) MapKey() string {
// compiled form of ServiceResolverConfigEntry
type DiscoveryResolver struct {
Default bool `json:",omitempty"`
ConnectTimeout time.Duration `json:",omitempty"`
RequestTimeout time.Duration `json:",omitempty"`
Target string `json:",omitempty"`
Failover *DiscoveryFailover `json:",omitempty"`
PrioritizeByLocality *DiscoveryPrioritizeByLocality `json:",omitempty"`
Default bool `json:",omitempty"`
ConnectTimeout time.Duration `json:",omitempty"`
RequestTimeout time.Duration `json:",omitempty"`
Target string `json:",omitempty"`
Failover *DiscoveryFailover `json:",omitempty"`
}
func (r *DiscoveryResolver) MarshalJSON() ([]byte, error) {
@ -238,6 +237,8 @@ type DiscoveryTarget struct {
// balancer objects. This has a structure similar to SNI, but will not be
// affected by SNI customizations.
Name string `json:",omitempty"`
PrioritizeByLocality *DiscoveryPrioritizeByLocality `json:",omitempty"`
}
func (t *DiscoveryTarget) MarshalJSON() ([]byte, error) {
@ -277,12 +278,13 @@ func (t *DiscoveryTarget) UnmarshalJSON(data []byte) error {
}
type DiscoveryTargetOpts struct {
Service string
ServiceSubset string
Namespace string
Partition string
Datacenter string
Peer string
Service string
ServiceSubset string
Namespace string
Partition string
Datacenter string
Peer string
PrioritizeByLocality *DiscoveryPrioritizeByLocality
}
func MergeDiscoveryTargetOpts(opts ...DiscoveryTargetOpts) DiscoveryTargetOpts {

View File

@ -129,6 +129,10 @@ func (o *CompiledDiscoveryChain) DeepCopy() *CompiledDiscoveryChain {
cp_Targets_v2.Locality = new(Locality)
*cp_Targets_v2.Locality = *v2.Locality
}
if v2.PrioritizeByLocality != nil {
cp_Targets_v2.PrioritizeByLocality = new(DiscoveryPrioritizeByLocality)
*cp_Targets_v2.PrioritizeByLocality = *v2.PrioritizeByLocality
}
}
cp.Targets[k2] = cp_Targets_v2
}
@ -240,10 +244,6 @@ func (o *DiscoveryResolver) DeepCopy() *DiscoveryResolver {
if o.Failover != nil {
cp.Failover = o.Failover.DeepCopy()
}
if o.PrioritizeByLocality != nil {
cp.PrioritizeByLocality = new(DiscoveryPrioritizeByLocality)
*cp.PrioritizeByLocality = *o.PrioritizeByLocality
}
return &cp
}