From 1ad804caea9ce5e6faedae657e7d128e8461c957 Mon Sep 17 00:00:00 2001 From: Tyler Holinka <1685504+tholinka@users.noreply.github.com> Date: Sat, 3 May 2025 04:26:54 -0500 Subject: [PATCH] fix: add unit tests for generation check --- source/gateway.go | 4 ++- source/gateway_httproute_test.go | 57 ++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 1 deletion(-) diff --git a/source/gateway.go b/source/gateway.go index 8a78bd2f8..6c07e5b8c 100644 --- a/source/gateway.go +++ b/source/gateway.go @@ -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 } diff --git a/source/gateway_httproute_test.go b/source/gateway_httproute_test.go index 19f009fb7..2d068ae6a 100644 --- a/source/gateway_httproute_test.go +++ b/source/gateway_httproute_test.go @@ -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{