From 4b0771d026d35cf0c843b6f621414e939e3c6180 Mon Sep 17 00:00:00 2001 From: KevFan Date: Fri, 16 Aug 2024 12:32:11 +0100 Subject: [PATCH] fixup: expand service ports as targetables only once Signed-off-by: KevFan --- machinery/gateway_api_topology.go | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/machinery/gateway_api_topology.go b/machinery/gateway_api_topology.go index fd755c9..78a1262 100644 --- a/machinery/gateway_api_topology.go +++ b/machinery/gateway_api_topology.go @@ -236,8 +236,6 @@ func NewGatewayAPITopology(options ...GatewayAPITopologyOptionsFunc) *Topology { opts = append(opts, WithLinks(LinkHTTPRouteToHTTPRouteRuleFunc())) // HTTPRoute -> HTTPRouteRule if o.ExpandServicePorts { - servicePorts := lo.FlatMap(o.Services, ServicePortsFromBackendFunc) - opts = append(opts, WithTargetables(servicePorts...)) opts = append(opts, WithLinks( LinkHTTPRouteRuleToServicePortFunc(httpRouteRules), // HTTPRouteRule -> ServicePort LinkHTTPRouteRuleToServiceFunc(httpRouteRules, true), // HTTPRouteRule -> Service @@ -262,8 +260,6 @@ func NewGatewayAPITopology(options ...GatewayAPITopologyOptionsFunc) *Topology { opts = append(opts, WithLinks(LinkGRPCRouteToGRPCRouteRuleFunc())) // GRPCRoute -> GRPCRouteRule if o.ExpandServicePorts { - servicePorts := lo.FlatMap(o.Services, ServicePortsFromBackendFunc) - opts = append(opts, WithTargetables(servicePorts...)) opts = append(opts, WithLinks( LinkGRPCRouteRuleToServicePortFunc(grpcRouteRules), // GRPCRouteRule -> ServicePort LinkGRPCRouteRuleToServiceFunc(grpcRouteRules, true), // GRPCRouteRule -> Service @@ -288,8 +284,6 @@ func NewGatewayAPITopology(options ...GatewayAPITopologyOptionsFunc) *Topology { opts = append(opts, WithLinks(LinkTCPRouteToTCPRouteRuleFunc())) // TCPRoute - TCPRouteRules if o.ExpandServicePorts { - servicePorts := lo.FlatMap(o.Services, ServicePortsFromBackendFunc) - opts = append(opts, WithTargetables(servicePorts...)) opts = append(opts, WithLinks( LinkTCPRouteRuleToServicePortFunc(tcpRouteRules), // TCPRouteRule -> ServicePort LinkTCPRouteRuleToServiceFunc(tcpRouteRules, true), // TCPRoute -> service @@ -314,8 +308,6 @@ func NewGatewayAPITopology(options ...GatewayAPITopologyOptionsFunc) *Topology { opts = append(opts, WithLinks(LinkTLSRouteToTLSRouteRuleFunc())) if o.ExpandServicePorts { - servicePorts := lo.FlatMap(o.Services, ServicePortsFromBackendFunc) - opts = append(opts, WithTargetables(servicePorts...)) opts = append(opts, WithLinks( LinkTLSRouteRuleToServicePortFunc(tlsRouteRules), // TLSRouteRule -> ServicePort LinkTLSRouteRuleToServiceFunc(tlsRouteRules, true), // TLSRouteRule -> Service @@ -340,8 +332,6 @@ func NewGatewayAPITopology(options ...GatewayAPITopologyOptionsFunc) *Topology { opts = append(opts, WithLinks(LinkUDPRouteToUDPRouteRuleFunc())) if o.ExpandServicePorts { - servicePorts := lo.FlatMap(o.Services, ServicePortsFromBackendFunc) - opts = append(opts, WithTargetables(servicePorts...)) opts = append(opts, WithLinks( LinkUDPRouteRuleToServicePortFunc(udpRouteRules), // UDPRouteRule -> ServicePort LinkUDPRouteRuleToServiceFunc(udpRouteRules, true), // UDPRouteRule -> Service @@ -361,6 +351,8 @@ func NewGatewayAPITopology(options ...GatewayAPITopologyOptionsFunc) *Topology { } if o.ExpandServicePorts { + servicePorts := lo.FlatMap(o.Services, ServicePortsFromServiceFunc) + opts = append(opts, WithTargetables(servicePorts...)) opts = append(opts, WithLinks(LinkServiceToServicePortFunc())) // Service -> ServicePort } @@ -432,8 +424,8 @@ func UDPRouteRulesFromUDPRouteFunc(udpRoute *UDPRoute, _ int) []*UDPRouteRule { }) } -// ServicePortsFromBackendFunc returns a list of targetable service ports from a targetable Service. -func ServicePortsFromBackendFunc(service *Service, _ int) []*ServicePort { +// ServicePortsFromServiceFunc returns a list of targetable service ports from a targetable Service. +func ServicePortsFromServiceFunc(service *Service, _ int) []*ServicePort { return lo.Map(service.Spec.Ports, func(port core.ServicePort, _ int) *ServicePort { return &ServicePort{ ServicePort: &port,