fix: add unit tests for generation check

This commit is contained in:
Tyler Holinka 2025-05-03 04:26:54 -05:00
parent 219e19fee7
commit 1ad804caea
No known key found for this signature in database
GPG Key ID: 2A07CAF8E0981A2D
2 changed files with 60 additions and 1 deletions

View File

@ -309,7 +309,9 @@ func (c *gatewayRouteResolver) resolve(rt gatewayRoute) (map[string]endpoint.Tar
ref := rps.ParentRef
namespace := strVal((*string)(ref.Namespace), meta.Namespace)
if rps.Conditions[0].ObservedGeneration != meta.Generation {
// Ensure that the parent reference is for the current generation
if len(rps.Conditions) > 0 && rps.Conditions[0].ObservedGeneration != meta.Generation {
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)
continue
}

View File

@ -70,6 +70,23 @@ func gwRouteStatus(refs ...v1.ParentReference) v1.RouteStatus {
return v
}
func omWithGeneration(meta metav1.ObjectMeta, generation int64) metav1.ObjectMeta {
meta.Generation = generation
return meta
}
func rsWithGeneration(routeStatus v1.HTTPRouteStatus, generation ...int64) v1.HTTPRouteStatus {
for i, parent := range routeStatus.Parents {
if len(generation) <= i {
break
}
parent.Conditions[0].ObservedGeneration = generation[i]
}
return routeStatus
}
func gwParentRef(namespace, name string, options ...gwParentRefOption) v1.ParentReference {
group := v1.Group("gateway.networking.k8s.io")
kind := v1.Kind("Gateway")
@ -192,6 +209,46 @@ func TestGatewayHTTPRouteSourceEndpoints(t *testing.T) {
"level=debug msg=\"Gateway gateway-namespace/not-gateway-name does not match gateway-name route-namespace/test\"",
},
},
{
title: "GatewayNameOldGeneration",
config: Config{
GatewayName: "gateway-name",
},
namespaces: namespaces("gateway-namespace", "route-namespace"),
gateways: []*v1beta1.Gateway{
{
ObjectMeta: omWithGeneration(objectMeta("gateway-namespace", "gateway-name"), 2),
Spec: v1.GatewaySpec{
Listeners: []v1.Listener{{
Protocol: v1.HTTPProtocolType,
AllowedRoutes: allowAllNamespaces,
}},
},
Status: gatewayStatus("1.2.3.4"),
},
},
routes: []*v1beta1.HTTPRoute{{
ObjectMeta: omWithGeneration(objectMeta("route-namespace", "old-test"), 5),
Spec: v1.HTTPRouteSpec{
Hostnames: hostnames("test.example.internal"),
CommonRouteSpec: v1.CommonRouteSpec{
ParentRefs: []v1.ParentReference{
gwParentRef("gateway-namespace", "gateway-name"),
},
},
},
Status: rsWithGeneration(httpRouteStatus( // The route was previously attached to a different gateway
gwParentRef("gateway-namespace", "gateway-name"),
gwParentRef("gateway-namespace", "gateway-name"),
), 5, 4),
}},
endpoints: []*endpoint.Endpoint{
newTestEndpoint("test.example.internal", "A", "1.2.3.4"),
},
logExpectations: []string{
"level=debug msg=\"Ignoring parent gateway-namespace/gateway-name of route-namespace/old-test as generation 4 does not match current generation 5\"",
},
},
{
title: "GatewayNamespace",
config: Config{