From b5e0c10e5859c9e6c26aad0e2d6d3a5741af8eb1 Mon Sep 17 00:00:00 2001 From: Dingkang Li Date: Fri, 17 May 2024 07:11:44 +0800 Subject: [PATCH] fix: duplicated xroutes are added to gatewayapi.Resources (#3282) fix duplicated xroutes Signed-off-by: Dingkang Li (cherry picked from commit 32c6876f0bc825a9a1b46c499d4d898b2f65a0c4) Signed-off-by: Arko Dasgupta --- internal/provider/kubernetes/controller.go | 15 ++++++++++ internal/provider/kubernetes/routes.go | 35 ++++++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/internal/provider/kubernetes/controller.go b/internal/provider/kubernetes/controller.go index b3da2a1f176..415ed7dfa05 100644 --- a/internal/provider/kubernetes/controller.go +++ b/internal/provider/kubernetes/controller.go @@ -114,6 +114,16 @@ func newGatewayAPIController(mgr manager.Manager, cfg *config.Server, su status. type resourceMappings struct { // Map for storing namespaces for Route, Service and Gateway objects. allAssociatedNamespaces map[string]struct{} + // Map for storing TLSRoutes' NamespacedNames attaching to various Gateway objects. + allAssociatedTLSRoutes map[string]struct{} + // Map for storing HTTPRoutes' NamespacedNames attaching to various Gateway objects. + allAssociatedHTTPRoutes map[string]struct{} + // Map for storing GRPCRoutes' NamespacedNames attaching to various Gateway objects. + allAssociatedGRPCRoutes map[string]struct{} + // Map for storing TCPRoutes' NamespacedNames attaching to various Gateway objects. + allAssociatedTCPRoutes map[string]struct{} + // Map for storing UDPRoutes' NamespacedNames attaching to various Gateway objects. + allAssociatedUDPRoutes map[string]struct{} // Map for storing backendRefs' NamespaceNames referred by various Route objects. allAssociatedBackendRefs map[gwapiv1.BackendObjectReference]struct{} // extensionRefFilters is a map of filters managed by an extension. @@ -125,6 +135,11 @@ type resourceMappings struct { func newResourceMapping() *resourceMappings { return &resourceMappings{ allAssociatedNamespaces: map[string]struct{}{}, + allAssociatedTLSRoutes: map[string]struct{}{}, + allAssociatedHTTPRoutes: map[string]struct{}{}, + allAssociatedGRPCRoutes: map[string]struct{}{}, + allAssociatedTCPRoutes: map[string]struct{}{}, + allAssociatedUDPRoutes: map[string]struct{}{}, allAssociatedBackendRefs: map[gwapiv1.BackendObjectReference]struct{}{}, extensionRefFilters: map[types.NamespacedName]unstructured.Unstructured{}, } diff --git a/internal/provider/kubernetes/routes.go b/internal/provider/kubernetes/routes.go index 20d690f574a..cb094b0189b 100644 --- a/internal/provider/kubernetes/routes.go +++ b/internal/provider/kubernetes/routes.go @@ -42,6 +42,12 @@ func (r *gatewayAPIReconciler) processTLSRoutes(ctx context.Context, gatewayName continue } } + + if _, ok := resourceMap.allAssociatedTLSRoutes[utils.NamespacedName(&tlsRoute).String()]; ok { + r.log.Info("current TLSRoute has been processed already", "namespace", tlsRoute.Namespace, "name", tlsRoute.Name) + continue + } + r.log.Info("processing TLSRoute", "namespace", tlsRoute.Namespace, "name", tlsRoute.Name) for _, rule := range tlsRoute.Spec.Rules { @@ -81,6 +87,7 @@ func (r *gatewayAPIReconciler) processTLSRoutes(ctx context.Context, gatewayName } resourceMap.allAssociatedNamespaces[tlsRoute.Namespace] = struct{}{} + resourceMap.allAssociatedTLSRoutes[utils.NamespacedName(&tlsRoute).String()] = struct{}{} // Discard Status to reduce memory consumption in watchable // It will be recomputed by the gateway-api layer tlsRoute.Status = gwapiv1a2.TLSRouteStatus{} @@ -113,6 +120,12 @@ func (r *gatewayAPIReconciler) processGRPCRoutes(ctx context.Context, gatewayNam continue } } + + if _, ok := resourceMap.allAssociatedGRPCRoutes[utils.NamespacedName(&grpcRoute).String()]; ok { + r.log.Info("current GRPCRoute has been processed already", "namespace", grpcRoute.Namespace, "name", grpcRoute.Name) + continue + } + r.log.Info("processing GRPCRoute", "namespace", grpcRoute.Namespace, "name", grpcRoute.Name) for _, rule := range grpcRoute.Spec.Rules { @@ -191,6 +204,7 @@ func (r *gatewayAPIReconciler) processGRPCRoutes(ctx context.Context, gatewayNam } resourceMap.allAssociatedNamespaces[grpcRoute.Namespace] = struct{}{} + resourceMap.allAssociatedGRPCRoutes[utils.NamespacedName(&grpcRoute).String()] = struct{}{} // Discard Status to reduce memory consumption in watchable // It will be recomputed by the gateway-api layer grpcRoute.Status = gwapiv1a2.GRPCRouteStatus{} @@ -232,6 +246,12 @@ func (r *gatewayAPIReconciler) processHTTPRoutes(ctx context.Context, gatewayNam continue } } + + if _, ok := resourceMap.allAssociatedHTTPRoutes[utils.NamespacedName(&httpRoute).String()]; ok { + r.log.Info("current HTTPRoute has been processed already", "namespace", httpRoute.Namespace, "name", httpRoute.Name) + continue + } + r.log.Info("processing HTTPRoute", "namespace", httpRoute.Namespace, "name", httpRoute.Name) for _, rule := range httpRoute.Spec.Rules { @@ -364,6 +384,7 @@ func (r *gatewayAPIReconciler) processHTTPRoutes(ctx context.Context, gatewayNam } resourceMap.allAssociatedNamespaces[httpRoute.Namespace] = struct{}{} + resourceMap.allAssociatedHTTPRoutes[utils.NamespacedName(&httpRoute).String()] = struct{}{} // Discard Status to reduce memory consumption in watchable // It will be recomputed by the gateway-api layer httpRoute.Status = gwapiv1.HTTPRouteStatus{} @@ -395,6 +416,12 @@ func (r *gatewayAPIReconciler) processTCPRoutes(ctx context.Context, gatewayName continue } } + + if _, ok := resourceMap.allAssociatedTCPRoutes[utils.NamespacedName(&tcpRoute).String()]; ok { + r.log.Info("current TCPRoute has been processed already", "namespace", tcpRoute.Namespace, "name", tcpRoute.Name) + continue + } + r.log.Info("processing TCPRoute", "namespace", tcpRoute.Namespace, "name", tcpRoute.Name) for _, rule := range tcpRoute.Spec.Rules { @@ -434,6 +461,7 @@ func (r *gatewayAPIReconciler) processTCPRoutes(ctx context.Context, gatewayName } resourceMap.allAssociatedNamespaces[tcpRoute.Namespace] = struct{}{} + resourceMap.allAssociatedTCPRoutes[utils.NamespacedName(&tcpRoute).String()] = struct{}{} // Discard Status to reduce memory consumption in watchable // It will be recomputed by the gateway-api layer tcpRoute.Status = gwapiv1a2.TCPRouteStatus{} @@ -465,6 +493,12 @@ func (r *gatewayAPIReconciler) processUDPRoutes(ctx context.Context, gatewayName continue } } + + if _, ok := resourceMap.allAssociatedUDPRoutes[utils.NamespacedName(&udpRoute).String()]; ok { + r.log.Info("current UDPRoute has been processed already", "namespace", udpRoute.Namespace, "name", udpRoute.Name) + continue + } + r.log.Info("processing UDPRoute", "namespace", udpRoute.Namespace, "name", udpRoute.Name) for _, rule := range udpRoute.Spec.Rules { @@ -504,6 +538,7 @@ func (r *gatewayAPIReconciler) processUDPRoutes(ctx context.Context, gatewayName } resourceMap.allAssociatedNamespaces[udpRoute.Namespace] = struct{}{} + resourceMap.allAssociatedUDPRoutes[utils.NamespacedName(&udpRoute).String()] = struct{}{} // Discard Status to reduce memory consumption in watchable // It will be recomputed by the gateway-api layer udpRoute.Status = gwapiv1a2.UDPRouteStatus{}