Skip to content

Commit e97b0e6

Browse files
authored
fix: custom backend weight not set properly (#6468)
Signed-off-by: bitliu <bitliu@tencent.com>
1 parent c23b7ff commit e97b0e6

9 files changed

+97
-26
lines changed

internal/gatewayapi/route.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1427,7 +1427,11 @@ func (t *Translator) processDestination(name string, backendRefContext BackendRe
14271427
if t.isCustomBackendResource(backendRef.Group, KindDerefOr(backendRef.Kind, resource.KindService)) {
14281428
// Add the custom backend resource to ExtensionRefFilters so it can be processed by the extension system
14291429
unstructuredRef = t.processBackendExtensions(backendRef.BackendObjectReference, backendNamespace, resources)
1430-
return nil, unstructuredRef, nil
1430+
return &ir.DestinationSetting{
1431+
Name: name,
1432+
Weight: &weight,
1433+
IsCustomBackend: true,
1434+
}, unstructuredRef, nil
14311435
}
14321436
}
14331437

internal/gatewayapi/testdata/extensions/httproute-with-custom-backend-invalid-apiversion.out.yaml

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,16 @@ xdsIR:
130130
mergeSlashes: true
131131
port: 10080
132132
routes:
133-
- directResponse:
134-
statusCode: 500
133+
- destination:
134+
metadata:
135+
kind: HTTPRoute
136+
name: httproute-1
137+
namespace: default
138+
name: httproute/default/httproute-1/rule/1
139+
settings:
140+
- isCustomBackend: true
141+
name: httproute/default/httproute-1/rule/1/backend/0
142+
weight: 1
135143
hostname: gateway.envoyproxy.io
136144
isHTTP2: false
137145
metadata:
@@ -143,8 +151,16 @@ xdsIR:
143151
distinct: false
144152
name: ""
145153
prefix: /lambda
146-
- directResponse:
147-
statusCode: 500
154+
- destination:
155+
metadata:
156+
kind: HTTPRoute
157+
name: httproute-1
158+
namespace: default
159+
name: httproute/default/httproute-1/rule/0
160+
settings:
161+
- isCustomBackend: true
162+
name: httproute/default/httproute-1/rule/0/backend/0
163+
weight: 1
148164
hostname: gateway.envoyproxy.io
149165
isHTTP2: false
150166
metadata:

internal/gatewayapi/testdata/extensions/httproute-with-custom-backend-mixed-multiple.out.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,12 @@ xdsIR:
160160
name: httproute/default/httproute-1/rule/0/backend/0
161161
protocol: HTTP
162162
weight: 1
163+
- isCustomBackend: true
164+
name: httproute/default/httproute-1/rule/0/backend/1
165+
weight: 1
166+
- isCustomBackend: true
167+
name: httproute/default/httproute-1/rule/0/backend/2
168+
weight: 1
163169
extensionRefs:
164170
- object:
165171
apiVersion: storage.example.io/v1alpha1
@@ -199,6 +205,12 @@ xdsIR:
199205
namespace: default
200206
name: httproute/default/httproute-1/rule/1
201207
settings:
208+
- isCustomBackend: true
209+
name: httproute/default/httproute-1/rule/1/backend/0
210+
weight: 1
211+
- isCustomBackend: true
212+
name: httproute/default/httproute-1/rule/1/backend/1
213+
weight: 1
202214
- addressType: IP
203215
endpoints:
204216
- host: 7.7.7.7

internal/gatewayapi/testdata/extensions/httproute-with-custom-backend-mixed.out.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,9 @@ xdsIR:
152152
name: httproute/default/httproute-1/rule/0/backend/0
153153
protocol: HTTP
154154
weight: 1
155+
- isCustomBackend: true
156+
name: httproute/default/httproute-1/rule/0/backend/1
157+
weight: 1
155158
extensionRefs:
156159
- object:
157160
apiVersion: storage.example.io/v1alpha1
@@ -181,6 +184,9 @@ xdsIR:
181184
namespace: default
182185
name: httproute/default/httproute-1/rule/1
183186
settings:
187+
- isCustomBackend: true
188+
name: httproute/default/httproute-1/rule/1/backend/0
189+
weight: 1
184190
- addressType: IP
185191
endpoints:
186192
- host: 7.7.7.7

internal/gatewayapi/testdata/extensions/httproute-with-custom-backend-multiple.out.yaml

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,19 @@ xdsIR:
138138
mergeSlashes: true
139139
port: 10080
140140
routes:
141-
- directResponse:
142-
statusCode: 500
141+
- destination:
142+
metadata:
143+
kind: HTTPRoute
144+
name: httproute-1
145+
namespace: default
146+
name: httproute/default/httproute-1/rule/0
147+
settings:
148+
- isCustomBackend: true
149+
name: httproute/default/httproute-1/rule/0/backend/0
150+
weight: 1
151+
- isCustomBackend: true
152+
name: httproute/default/httproute-1/rule/0/backend/1
153+
weight: 1
143154
extensionRefs:
144155
- object:
145156
apiVersion: storage.example.io/v1alpha1
@@ -172,8 +183,19 @@ xdsIR:
172183
distinct: false
173184
name: ""
174185
prefix: /service
175-
- directResponse:
176-
statusCode: 500
186+
- destination:
187+
metadata:
188+
kind: HTTPRoute
189+
name: httproute-1
190+
namespace: default
191+
name: httproute/default/httproute-1/rule/1
192+
settings:
193+
- isCustomBackend: true
194+
name: httproute/default/httproute-1/rule/1/backend/0
195+
weight: 1
196+
- isCustomBackend: true
197+
name: httproute/default/httproute-1/rule/1/backend/1
198+
weight: 1
177199
extensionRefs:
178200
- object:
179201
apiVersion: storage.example.io/v1alpha1

internal/gatewayapi/testdata/extensions/httproute-with-custom-backend.out.yaml

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,16 @@ xdsIR:
130130
mergeSlashes: true
131131
port: 10080
132132
routes:
133-
- directResponse:
134-
statusCode: 500
133+
- destination:
134+
metadata:
135+
kind: HTTPRoute
136+
name: httproute-1
137+
namespace: default
138+
name: httproute/default/httproute-1/rule/1
139+
settings:
140+
- isCustomBackend: true
141+
name: httproute/default/httproute-1/rule/1/backend/0
142+
weight: 1
135143
extensionRefs:
136144
- object:
137145
apiVersion: compute.example.io/v1alpha1
@@ -154,8 +162,16 @@ xdsIR:
154162
distinct: false
155163
name: ""
156164
prefix: /lambda
157-
- directResponse:
158-
statusCode: 500
165+
- destination:
166+
metadata:
167+
kind: HTTPRoute
168+
name: httproute-1
169+
namespace: default
170+
name: httproute/default/httproute-1/rule/0
171+
settings:
172+
- isCustomBackend: true
173+
name: httproute/default/httproute-1/rule/0/backend/0
174+
weight: 1
159175
extensionRefs:
160176
- object:
161177
apiVersion: storage.example.io/v1alpha1

internal/ir/xds.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1600,6 +1600,8 @@ func (r *RouteDestination) ToBackendWeights() *BackendWeights {
16001600
switch {
16011601
case s.IsDynamicResolver: // Dynamic resolver has no endpoints
16021602
w.Valid += *s.Weight
1603+
case s.IsCustomBackend: // Custom backends has no endpoints
1604+
w.Valid += *s.Weight
16031605
case len(s.Endpoints) > 0:
16041606
w.Valid += *s.Weight
16051607
default:
@@ -1620,6 +1622,9 @@ type DestinationSetting struct {
16201622
// A dynamic resolver is a destination that is resolved dynamically using the request's host header.
16211623
IsDynamicResolver bool `json:"isDynamicResolver,omitempty" yaml:"isDynamicResolver,omitempty"`
16221624

1625+
// IsCustomBackend specifies whether the destination is a custom backend.
1626+
IsCustomBackend bool `json:"isCustomBackend,omitempty" yaml:"isCustomBackend,omitempty"`
1627+
16231628
// Weight associated with this destination,
16241629
// invalid endpoints are represents with a
16251630
// non-zero weight with an empty endpoints list

internal/provider/kubernetes/controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -657,7 +657,7 @@ func (r *gatewayAPIReconciler) processBackendRefs(ctx context.Context, gwcResour
657657
}
658658
if !resourceMappings.allAssociatedBackendRefExtensionFilters.Has(key) {
659659
resourceMappings.allAssociatedBackendRefExtensionFilters.Insert(key)
660-
if extRefFilter, exists := resourceMappings.extensionRefFilters[key]; !exists {
660+
if extRefFilter, exists := resourceMappings.extensionRefFilters[key]; exists {
661661
resourceMappings.extensionRefFilters[key] = extRefFilter
662662
gwcResource.ExtensionRefFilters = append(gwcResource.ExtensionRefFilters, extRefFilter)
663663
r.log.Info("added custom backend resource to resource tree",

internal/provider/kubernetes/controller_test.go

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -318,18 +318,8 @@ func TestProcessBackendRefsWithCustomBackends(t *testing.T) {
318318

319319
// Call the function under test
320320
require.NoError(t, r.processBackendRefs(ctx, gwcResource, resourceMappings))
321-
322-
// Verify results
323-
// Note: Due to a bug in the current implementation (line 542 in controller.go),
324-
// custom backends are not properly added to ExtensionRefFilters when they exist
325-
// in the extensionRefFilters map. The logic should be `exists` instead of `!exists`.
326-
// For now, we test the current (buggy) behavior.
327-
if tc.name == "skip non-custom backends" {
328-
require.Len(t, gwcResource.ExtensionRefFilters, tc.expectedExtFiltersCount)
329-
} else {
330-
// Current buggy behavior: custom backends are not added to ExtensionRefFilters
331-
require.Empty(t, gwcResource.ExtensionRefFilters)
332-
}
321+
// Compare the results
322+
require.Len(t, gwcResource.ExtensionRefFilters, tc.expectedExtFiltersCount)
333323

334324
for _, expectedNS := range tc.expectedNamespaces {
335325
require.True(t, resourceMappings.allAssociatedNamespaces.Has(expectedNS))

0 commit comments

Comments
 (0)