Skip to content

Commit 4386d8b

Browse files
committed
only consider accepted gateway routes if the condition generation matches the current one
1 parent 5372915 commit 4386d8b

File tree

2 files changed

+78
-5
lines changed

2 files changed

+78
-5
lines changed

source/gateway.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ func (c *gatewayRouteResolver) resolve(rt gatewayRoute) (map[string]endpoint.Tar
293293
return nil, err
294294
}
295295
hostTargets := make(map[string]endpoint.Targets)
296-
296+
currentGeneration := rt.Metadata().Generation
297297
meta := rt.Metadata()
298298
for _, rps := range rt.RouteStatus().Parents {
299299
// Confirm the Parent is the standard Gateway kind.
@@ -316,8 +316,8 @@ func (c *gatewayRouteResolver) resolve(rt gatewayRoute) (map[string]endpoint.Tar
316316
log.Debugf("Gateway %s/%s does not match %s %s/%s", namespace, ref.Name, c.src.gwName, meta.Namespace, meta.Name)
317317
continue
318318
}
319-
// Confirm the Gateway has accepted the Route.
320-
if !gwRouteIsAccepted(rps.Conditions) {
319+
// Confirm the Gateway has accepted the Route, and that the generation matches.
320+
if !gwRouteIsAccepted(rps.Conditions, currentGeneration) {
321321
log.Debugf("Gateway %s/%s has not accepted %s %s/%s", namespace, ref.Name, c.src.rtKind, meta.Namespace, meta.Name)
322322
continue
323323
}
@@ -458,9 +458,9 @@ func (c *gatewayRouteResolver) routeIsAllowed(gw *v1beta1.Gateway, lis *v1.Liste
458458
return false
459459
}
460460

461-
func gwRouteIsAccepted(conds []metav1.Condition) bool {
461+
func gwRouteIsAccepted(conds []metav1.Condition, currentGeneration int64) bool {
462462
for _, c := range conds {
463-
if v1.RouteConditionType(c.Type) == v1.RouteConditionAccepted {
463+
if v1.RouteConditionType(c.Type) == v1.RouteConditionAccepted && c.ObservedGeneration == currentGeneration {
464464
return c.Status == metav1.ConditionTrue
465465
}
466466
}

source/gateway_test.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"strings"
2121
"testing"
2222

23+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2324
v1 "sigs.k8s.io/gateway-api/apis/v1"
2425
)
2526

@@ -245,3 +246,75 @@ func TestIsDNS1123Domain(t *testing.T) {
245246
})
246247
}
247248
}
249+
250+
func TestGwRouteIsAccepted(t *testing.T) {
251+
tests := []struct {
252+
desc string
253+
conditions []metav1.Condition
254+
currentGeneration int64
255+
want bool
256+
}{
257+
{
258+
desc: "accepted condition with matching generation",
259+
conditions: []metav1.Condition{
260+
{
261+
Type: string(v1.RouteConditionAccepted),
262+
Status: metav1.ConditionTrue,
263+
ObservedGeneration: 1,
264+
},
265+
},
266+
currentGeneration: 1,
267+
want: true,
268+
},
269+
{
270+
desc: "accepted condition with different generation",
271+
conditions: []metav1.Condition{
272+
{
273+
Type: string(v1.RouteConditionAccepted),
274+
Status: metav1.ConditionTrue,
275+
ObservedGeneration: 1,
276+
},
277+
},
278+
currentGeneration: 2,
279+
want: false,
280+
},
281+
{
282+
desc: "accepted condition with false status",
283+
conditions: []metav1.Condition{
284+
{
285+
Type: string(v1.RouteConditionAccepted),
286+
Status: metav1.ConditionFalse,
287+
ObservedGeneration: 1,
288+
},
289+
},
290+
currentGeneration: 1,
291+
want: false,
292+
},
293+
{
294+
desc: "no accepted condition",
295+
conditions: []metav1.Condition{
296+
{
297+
Type: "OtherCondition",
298+
Status: metav1.ConditionTrue,
299+
ObservedGeneration: 1,
300+
},
301+
},
302+
currentGeneration: 1,
303+
want: false,
304+
},
305+
{
306+
desc: "empty conditions",
307+
conditions: []metav1.Condition{},
308+
currentGeneration: 1,
309+
want: false,
310+
},
311+
}
312+
313+
for _, tt := range tests {
314+
t.Run(tt.desc, func(t *testing.T) {
315+
if got := gwRouteIsAccepted(tt.conditions, tt.currentGeneration); got != tt.want {
316+
t.Errorf("gwRouteIsAccepted() = %v, want %v", got, tt.want)
317+
}
318+
})
319+
}
320+
}

0 commit comments

Comments
 (0)