Skip to content

Commit 4e7f0c9

Browse files
committed
First phase - Implement ZoneAware Lb Config
Signed-off-by: Jukie <[email protected]>
1 parent 4140f61 commit 4e7f0c9

15 files changed

+472
-69
lines changed

internal/gatewayapi/route.go

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1418,7 +1418,7 @@ func (t *Translator) processDestination(name string, backendRefContext BackendRe
14181418
ds = t.processServiceDestinationSetting(name, backendRef.BackendObjectReference, backendNamespace, protocol, resources, envoyProxy)
14191419
svc := resources.GetService(backendNamespace, string(backendRef.Name))
14201420
ds.IPFamily = getServiceIPFamily(svc)
1421-
ds.ZoneAwareRouting = processZoneAwareRouting(svc)
1421+
ds.PreferLocal = processPreferLocalZone(svc)
14221422

14231423
case egv1a1.KindBackend:
14241424
ds = t.processBackendDestinationSetting(name, backendRef.BackendObjectReference, backendNamespace, protocol, resources)
@@ -1591,12 +1591,12 @@ func (t *Translator) processServiceDestinationSetting(
15911591
}
15921592

15931593
return &ir.DestinationSetting{
1594-
Name: name,
1595-
Protocol: protocol,
1596-
Endpoints: endpoints,
1597-
AddressType: addrType,
1598-
ZoneAwareRouting: processZoneAwareRouting(service),
1599-
Metadata: buildResourceMetadata(service, ptr.To(gwapiv1.SectionName(strconv.Itoa(int(*backendRef.Port))))),
1594+
Name: name,
1595+
Protocol: protocol,
1596+
Endpoints: endpoints,
1597+
AddressType: addrType,
1598+
PreferLocal: processPreferLocalZone(service),
1599+
Metadata: buildResourceMetadata(service, ptr.To(gwapiv1.SectionName(strconv.Itoa(int(*backendRef.Port))))),
16001600
}
16011601
}
16021602

@@ -1616,14 +1616,17 @@ func getBackendFilters(routeType gwapiv1.Kind, backendRefContext BackendRefConte
16161616
return nil
16171617
}
16181618

1619-
func processZoneAwareRouting(svc *corev1.Service) *ir.ZoneAwareRouting {
1619+
func processPreferLocalZone(svc *corev1.Service) *ir.PreferLocalZone {
16201620
if svc == nil {
16211621
return nil
16221622
}
16231623

16241624
if trafficDist := svc.Spec.TrafficDistribution; trafficDist != nil {
1625-
return &ir.ZoneAwareRouting{
1626-
MinSize: 1,
1625+
return &ir.PreferLocalZone{
1626+
MinEndpointsThreshold: ptr.To[uint64](1),
1627+
Force: &ir.ForceLocalZone{
1628+
MinEndpointsInZoneThreshold: ptr.To[uint32](1),
1629+
},
16271630
}
16281631
}
16291632

@@ -1632,8 +1635,11 @@ func processZoneAwareRouting(svc *corev1.Service) *ir.ZoneAwareRouting {
16321635
// https://kubernetes.io/docs/concepts/services-networking/topology-aware-routing/#enabling-topology-aware-routing
16331636
// https://github.com/kubernetes/kubernetes/blob/9d9e1afdf78bce0a517cc22557457f942040ca19/staging/src/k8s.io/endpointslice/utils.go#L355-L368
16341637
if val, ok := svc.Annotations[corev1.AnnotationTopologyMode]; ok && val == "Auto" || val == "auto" {
1635-
return &ir.ZoneAwareRouting{
1636-
MinSize: 3,
1638+
return &ir.PreferLocalZone{
1639+
MinEndpointsThreshold: ptr.To[uint64](3),
1640+
Force: &ir.ForceLocalZone{
1641+
MinEndpointsInZoneThreshold: ptr.To[uint32](3),
1642+
},
16371643
}
16381644
}
16391645

internal/gatewayapi/testdata/httproute-with-enable-zone-discovery.out.yaml

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -157,10 +157,12 @@ xdsIR:
157157
namespace: default
158158
sectionName: "8080"
159159
name: httproute/default/httproute-1/rule/1/backend/0
160+
preferLocal:
161+
force:
162+
minEndpointsInZoneThreshold: 1
163+
minEndpointsThreshold: 1
160164
protocol: HTTP
161165
weight: 1
162-
zoneAwareRouting:
163-
minSize: 1
164166
headerMatches:
165167
- distinct: false
166168
exact: bar
@@ -195,10 +197,12 @@ xdsIR:
195197
namespace: default
196198
sectionName: "8080"
197199
name: httproute/default/httproute-1/rule/0/backend/0
200+
preferLocal:
201+
force:
202+
minEndpointsInZoneThreshold: 1
203+
minEndpointsThreshold: 1
198204
protocol: HTTP
199205
weight: 1
200-
zoneAwareRouting:
201-
minSize: 1
202206
hostname: '*'
203207
isHTTP2: false
204208
metadata:

internal/ir/xds.go

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -813,6 +813,16 @@ func (h *HTTPRoute) GetRetry() *Retry {
813813
return nil
814814
}
815815

816+
func (h *HTTPRoute) NeedsClusterPerSetting() bool {
817+
if h.Traffic != nil &&
818+
h.Traffic.LoadBalancer != nil &&
819+
h.Traffic.LoadBalancer.ZoneAware != nil &&
820+
h.Traffic.LoadBalancer.ZoneAware.PreferLocal != nil {
821+
return true
822+
}
823+
return h.Destination.NeedsClusterPerSetting()
824+
}
825+
816826
// DNS contains configuration options for DNS resolution.
817827
// +k8s:deepcopy-gen=true
818828
type DNS struct {
@@ -1552,7 +1562,7 @@ func (r *RouteDestination) Validate() error {
15521562
func (r *RouteDestination) NeedsClusterPerSetting() bool {
15531563
return r.HasMixedEndpoints() ||
15541564
r.HasFiltersInSettings() ||
1555-
(len(r.Settings) > 1 && r.HasZoneAwareRouting())
1565+
(len(r.Settings) > 1 && r.HasPreferLocalZone())
15561566
}
15571567

15581568
// HasMixedEndpoints returns true if the RouteDestination has endpoints of multiple types
@@ -1577,10 +1587,10 @@ func (r *RouteDestination) HasFiltersInSettings() bool {
15771587
return false
15781588
}
15791589

1580-
// HasZoneAwareRouting returns true if any setting in the destination has ZoneAwareRoutingEnabled set
1581-
func (r *RouteDestination) HasZoneAwareRouting() bool {
1590+
// HasPreferLocalZone returns true if any setting in the destination has PreferLocalZone set
1591+
func (r *RouteDestination) HasPreferLocalZone() bool {
15821592
for _, setting := range r.Settings {
1583-
if setting.ZoneAwareRouting != nil {
1593+
if setting.PreferLocal != nil {
15841594
return true
15851595
}
15861596
}
@@ -1643,11 +1653,9 @@ type DestinationSetting struct {
16431653
IPFamily *egv1a1.IPFamily `json:"ipFamily,omitempty" yaml:"ipFamily,omitempty"`
16441654
TLS *TLSUpstreamConfig `json:"tls,omitempty" yaml:"tls,omitempty"`
16451655
Filters *DestinationFilters `json:"filters,omitempty" yaml:"filters,omitempty"`
1646-
// ZoneAwareRouting specifies whether to enable Zone Aware Routing for this destination's endpoints.
1656+
// PreferLocal specifies whether to enable Zone Aware Routing for this destination's endpoints.
16471657
// This is derived from the backend service and depends on having Kubernetes Topology Aware Routing or Traffic Distribution enabled.
1648-
//
1649-
// +optional
1650-
ZoneAwareRouting *ZoneAwareRouting `json:"zoneAwareRouting,omitempty" yaml:"zoneAwareRouting,omitempty"`
1658+
PreferLocal *PreferLocalZone `json:"preferLocal,omitempty" yaml:"preferLocal,omitempty"`
16511659
// Metadata is used to enrich envoy route metadata with user and provider-specific information
16521660
// The primary metadata for DestinationSettings comes from the Backend resource reference in BackendRef
16531661
Metadata *ResourceMetadata `json:"metadata,omitempty" yaml:"metadata,omitempty"`
@@ -2490,6 +2498,8 @@ type LoadBalancer struct {
24902498
Random *Random `json:"random,omitempty" yaml:"random,omitempty"`
24912499
// ConsistentHash load balancer policy
24922500
ConsistentHash *ConsistentHash `json:"consistentHash,omitempty" yaml:"consistentHash,omitempty"`
2501+
// ZoneAware defines the configuration related to the distribution of requests between locality zones.
2502+
ZoneAware *ZoneAware `json:"zoneAware,omitempty" yaml:"zoneAware,omitempty"`
24932503
}
24942504

24952505
// Validate the fields within the LoadBalancer structure
@@ -3162,8 +3172,28 @@ type RequestBuffer struct {
31623172
Limit resource.Quantity `json:"limit" yaml:"limit"`
31633173
}
31643174

3165-
// ZoneAwareRouting holds the zone aware routing configuration
3175+
// ZoneAware defines the configuration related to the distribution of requests between locality zones.
3176+
// +k8s:deepcopy-gen=true
3177+
type ZoneAware struct {
3178+
// PreferLocal configures zone-aware routing to prefer sending traffic to the local locality zone.
3179+
PreferLocal *PreferLocalZone `json:"preferLocal,omitempty" yaml:"preferLocal,omitempty"`
3180+
}
3181+
3182+
// PreferLocalZone configures zone-aware routing to prefer sending traffic to the local locality zone.
3183+
// +k8s:deepcopy-gen=true
3184+
type PreferLocalZone struct {
3185+
// ForceLocalZone defines override configuration for forcing all traffic to stay within the local zone instead of the default behavior
3186+
// which maintains equal distribution among upstream endpoints while sending as much traffic as possible locally.
3187+
Force *ForceLocalZone `json:"force,omitempty" yaml:"force,omitempty"`
3188+
// MinEndpointsThreshold is the minimum number of total upstream endpoints across all zones required to enable zone-aware routing.
3189+
MinEndpointsThreshold *uint64 `json:"minEndpointsThreshold,omitempty" yaml:"minEndpointsThreshold,omitempty"`
3190+
}
3191+
3192+
// ForceLocalZone defines override configuration for forcing all traffic to stay within the local zone instead of the default behavior
3193+
// which maintains equal distribution among upstream endpoints while sending as much traffic as possible locally.
31663194
// +k8s:deepcopy-gen=true
3167-
type ZoneAwareRouting struct {
3168-
MinSize int `json:"minSize" yaml:"minSize"`
3195+
type ForceLocalZone struct {
3196+
// MinEndpointsInZoneThreshold is the minimum number of upstream endpoints in the local zone required to honor the forceLocalZone
3197+
// override. This is useful for protecting zones with fewer endpoints.
3198+
MinEndpointsInZoneThreshold *uint32 `json:"minEndpointsInZoneThreshold,omitempty" yaml:"minEndpointsInZoneThreshold,omitempty"`
31693199
}

internal/ir/xds_test.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1124,8 +1124,10 @@ func TestRouteDestination_NeedsClusterPerSetting(t *testing.T) {
11241124
Port: 8080,
11251125
},
11261126
},
1127-
AddressType: ptr.To(FQDN),
1128-
ZoneAwareRouting: &ZoneAwareRouting{MinSize: 1},
1127+
AddressType: ptr.To(FQDN),
1128+
PreferLocal: &PreferLocalZone{
1129+
Force: &ForceLocalZone{MinEndpointsInZoneThreshold: ptr.To[uint32](1)},
1130+
},
11291131
},
11301132
{
11311133
Endpoints: []*DestinationEndpoint{
@@ -1152,8 +1154,10 @@ func TestRouteDestination_NeedsClusterPerSetting(t *testing.T) {
11521154
Port: 8080,
11531155
},
11541156
},
1155-
AddressType: ptr.To(FQDN),
1156-
ZoneAwareRouting: &ZoneAwareRouting{MinSize: 1},
1157+
AddressType: ptr.To(FQDN),
1158+
PreferLocal: &PreferLocalZone{
1159+
Force: &ForceLocalZone{MinEndpointsInZoneThreshold: ptr.To[uint32](1)},
1160+
},
11571161
},
11581162
},
11591163
},

internal/ir/zz_generated.deepcopy.go

Lines changed: 63 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)