Skip to content

Commit 11ca783

Browse files
authored
Merge pull request #1 from tholinka/fix-gateway-maybe
fix: add unit tests for generation check
2 parents 219e19f + 1ad804c commit 11ca783

File tree

2 files changed

+60
-1
lines changed

2 files changed

+60
-1
lines changed

source/gateway.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,9 @@ func (c *gatewayRouteResolver) resolve(rt gatewayRoute) (map[string]endpoint.Tar
309309
ref := rps.ParentRef
310310

311311
namespace := strVal((*string)(ref.Namespace), meta.Namespace)
312-
if rps.Conditions[0].ObservedGeneration != meta.Generation {
312+
313+
// Ensure that the parent reference is for the current generation
314+
if len(rps.Conditions) > 0 && rps.Conditions[0].ObservedGeneration != meta.Generation {
313315
log.Debugf("Ignoring parent %s/%s of %s/%s as generation %d does not match current generation %d", namespace, ref.Name, meta.Namespace, meta.Name, rps.Conditions[0].ObservedGeneration, meta.Generation)
314316
continue
315317
}

source/gateway_httproute_test.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,23 @@ func gwRouteStatus(refs ...v1.ParentReference) v1.RouteStatus {
7070
return v
7171
}
7272

73+
func omWithGeneration(meta metav1.ObjectMeta, generation int64) metav1.ObjectMeta {
74+
meta.Generation = generation
75+
return meta
76+
}
77+
78+
func rsWithGeneration(routeStatus v1.HTTPRouteStatus, generation ...int64) v1.HTTPRouteStatus {
79+
for i, parent := range routeStatus.Parents {
80+
if len(generation) <= i {
81+
break
82+
}
83+
84+
parent.Conditions[0].ObservedGeneration = generation[i]
85+
}
86+
87+
return routeStatus
88+
}
89+
7390
func gwParentRef(namespace, name string, options ...gwParentRefOption) v1.ParentReference {
7491
group := v1.Group("gateway.networking.k8s.io")
7592
kind := v1.Kind("Gateway")
@@ -192,6 +209,46 @@ func TestGatewayHTTPRouteSourceEndpoints(t *testing.T) {
192209
"level=debug msg=\"Gateway gateway-namespace/not-gateway-name does not match gateway-name route-namespace/test\"",
193210
},
194211
},
212+
{
213+
title: "GatewayNameOldGeneration",
214+
config: Config{
215+
GatewayName: "gateway-name",
216+
},
217+
namespaces: namespaces("gateway-namespace", "route-namespace"),
218+
gateways: []*v1beta1.Gateway{
219+
{
220+
ObjectMeta: omWithGeneration(objectMeta("gateway-namespace", "gateway-name"), 2),
221+
Spec: v1.GatewaySpec{
222+
Listeners: []v1.Listener{{
223+
Protocol: v1.HTTPProtocolType,
224+
AllowedRoutes: allowAllNamespaces,
225+
}},
226+
},
227+
Status: gatewayStatus("1.2.3.4"),
228+
},
229+
},
230+
routes: []*v1beta1.HTTPRoute{{
231+
ObjectMeta: omWithGeneration(objectMeta("route-namespace", "old-test"), 5),
232+
Spec: v1.HTTPRouteSpec{
233+
Hostnames: hostnames("test.example.internal"),
234+
CommonRouteSpec: v1.CommonRouteSpec{
235+
ParentRefs: []v1.ParentReference{
236+
gwParentRef("gateway-namespace", "gateway-name"),
237+
},
238+
},
239+
},
240+
Status: rsWithGeneration(httpRouteStatus( // The route was previously attached to a different gateway
241+
gwParentRef("gateway-namespace", "gateway-name"),
242+
gwParentRef("gateway-namespace", "gateway-name"),
243+
), 5, 4),
244+
}},
245+
endpoints: []*endpoint.Endpoint{
246+
newTestEndpoint("test.example.internal", "A", "1.2.3.4"),
247+
},
248+
logExpectations: []string{
249+
"level=debug msg=\"Ignoring parent gateway-namespace/gateway-name of route-namespace/old-test as generation 4 does not match current generation 5\"",
250+
},
251+
},
195252
{
196253
title: "GatewayNamespace",
197254
config: Config{

0 commit comments

Comments
 (0)