@@ -266,13 +266,16 @@ func (t *Translator) fillHTTPRoutePolicies(routes []*adctypes.Route, policies []
266266 }
267267}
268268
269- func (t * Translator ) translateEndpointSlice (weight int , endpointSlices []discoveryv1.EndpointSlice ) adctypes.UpstreamNodes {
269+ func (t * Translator ) translateEndpointSlice (portName * string , weight int , endpointSlices []discoveryv1.EndpointSlice ) adctypes.UpstreamNodes {
270270 var nodes adctypes.UpstreamNodes
271271 if len (endpointSlices ) == 0 {
272272 return nodes
273273 }
274274 for _ , endpointSlice := range endpointSlices {
275275 for _ , port := range endpointSlice .Ports {
276+ if portName != nil && ! ptr .Equal (portName , port .Name ) {
277+ continue
278+ }
276279 for _ , endpoint := range endpointSlice .Endpoints {
277280 for _ , addr := range endpoint .Addresses {
278281 node := adctypes.UpstreamNode {
@@ -283,21 +286,16 @@ func (t *Translator) translateEndpointSlice(weight int, endpointSlices []discove
283286 nodes = append (nodes , node )
284287 }
285288 }
289+ if portName != nil {
290+ break
291+ }
286292 }
287293 }
288294
289295 return nodes
290296}
291297
292298func (t * Translator ) translateBackendRef (tctx * provider.TranslateContext , ref gatewayv1.BackendRef ) adctypes.UpstreamNodes {
293- weight := 1
294- port := 80
295- if ref .Weight != nil {
296- weight = int (* ref .Weight )
297- }
298- if ref .Port != nil {
299- port = int (* ref .Port )
300- }
301299 key := types.NamespacedName {
302300 Namespace : string (* ref .Namespace ),
303301 Name : string (ref .Name ),
@@ -307,7 +305,16 @@ func (t *Translator) translateBackendRef(tctx *provider.TranslateContext, ref ga
307305 return adctypes.UpstreamNodes {}
308306 }
309307
308+ weight := 1
309+ if ref .Weight != nil {
310+ weight = int (* ref .Weight )
311+ }
312+
310313 if service .Spec .Type == corev1 .ServiceTypeExternalName {
314+ port := 80
315+ if ref .Port != nil {
316+ port = int (* ref .Port )
317+ }
311318 return adctypes.UpstreamNodes {
312319 {
313320 Host : service .Spec .ExternalName ,
@@ -316,8 +323,22 @@ func (t *Translator) translateBackendRef(tctx *provider.TranslateContext, ref ga
316323 },
317324 }
318325 }
326+
327+ var portName * string
328+ if ref .Port != nil {
329+ for _ , p := range service .Spec .Ports {
330+ if int (p .Port ) == int (* ref .Port ) {
331+ portName = ptr .To (p .Name )
332+ break
333+ }
334+ }
335+ if portName == nil {
336+ return adctypes.UpstreamNodes {}
337+ }
338+ }
339+
319340 endpointSlices := tctx .EndpointSlices [key ]
320- return t .translateEndpointSlice (weight , endpointSlices )
341+ return t .translateEndpointSlice (portName , weight , endpointSlices )
321342}
322343
323344func (t * Translator ) TranslateHTTPRoute (tctx * provider.TranslateContext , httpRoute * gatewayv1.HTTPRoute ) (* TranslateResult , error ) {
0 commit comments