@@ -26,6 +26,7 @@ import (
26
26
"time"
27
27
28
28
"github.com/nginxinc/kubernetes-ingress/pkg/apis/dos/v1beta1"
29
+ "golang.org/x/exp/maps"
29
30
30
31
"github.com/nginxinc/kubernetes-ingress/internal/k8s/appprotect"
31
32
"github.com/nginxinc/kubernetes-ingress/internal/k8s/appprotectcommon"
@@ -3596,46 +3597,66 @@ func (lbc *LoadBalancerController) getEndpointsForServiceWithSubselector(targetP
3596
3597
return endps , nil
3597
3598
}
3598
3599
3600
+ // selectEndpointSlicesForPort returns EndpointSlices that match given targetPort.
3601
+ func selectEndpointSlicesForPort (targetPort int32 , esx []discovery_v1.EndpointSlice ) []discovery_v1.EndpointSlice {
3602
+ eps := make ([]discovery_v1.EndpointSlice , 0 , len (esx ))
3603
+ for _ , es := range esx {
3604
+ for _ , p := range es .Ports {
3605
+ if p .Port == nil {
3606
+ continue
3607
+ }
3608
+ if * p .Port == targetPort {
3609
+ eps = append (eps , es )
3610
+ }
3611
+ }
3612
+ }
3613
+ return eps
3614
+ }
3615
+
3616
+ // filterReadyEndpoinsFrom returns ready Endpoints from given EndpointSlices.
3617
+ func filterReadyEndpointsFrom (esx []discovery_v1.EndpointSlice ) []discovery_v1.Endpoint {
3618
+ epx := make ([]discovery_v1.Endpoint , 0 , len (esx ))
3619
+ for _ , es := range esx {
3620
+ for _ , e := range es .Endpoints {
3621
+ if e .Conditions .Ready == nil {
3622
+ continue
3623
+ }
3624
+ if * e .Conditions .Ready {
3625
+ epx = append (epx , e )
3626
+ }
3627
+ }
3628
+ }
3629
+ return epx
3630
+ }
3631
+
3599
3632
func getEndpointsFromEndpointSlicesForSubselectedPods (targetPort int32 , pods []* api_v1.Pod , svcEndpointSlices []discovery_v1.EndpointSlice ) (podEndpoints []podEndpoint ) {
3600
- endpointSet := make (map [podEndpoint ]struct {})
3601
- for _ , pod := range pods {
3602
- for _ , endpointSlice := range svcEndpointSlices {
3603
- for _ , port := range endpointSlice .Ports {
3604
- if * port .Port != targetPort {
3605
- continue
3606
- }
3607
- for _ , endpoint := range endpointSlice .Endpoints {
3608
- if ! * endpoint .Conditions .Ready {
3609
- continue
3610
- }
3611
- for _ , address := range endpoint .Addresses {
3612
- if pod .Status .PodIP == address {
3613
- addr := ipv6SafeAddrPort (pod .Status .PodIP , targetPort )
3614
- ownerType , ownerName := getPodOwnerTypeAndName (pod )
3615
- podEndpoint := podEndpoint {
3616
- Address : addr ,
3617
- PodName : getPodName (endpoint .TargetRef ),
3618
- MeshPodOwner : configs.MeshPodOwner {
3619
- OwnerType : ownerType ,
3620
- OwnerName : ownerName ,
3621
- },
3622
- }
3623
- endpointSet [podEndpoint ] = struct {}{}
3624
- podEndpoints = append (podEndpoints , podEndpoint )
3633
+ // Match ready endpoints IP ddresses with Pod's IP. If they match create a new podEnpoint.
3634
+ makePodEndpoints := func (pods []* api_v1.Pod , endpoints []discovery_v1.Endpoint ) []podEndpoint {
3635
+ endpointSet := make (map [podEndpoint ]struct {})
3636
+
3637
+ for _ , pod := range pods {
3638
+ for _ , endpoint := range endpoints {
3639
+ for _ , address := range endpoint .Addresses {
3640
+ if pod .Status .PodIP == address {
3641
+ addr := ipv6SafeAddrPort (pod .Status .PodIP , targetPort )
3642
+ ownerType , ownerName := getPodOwnerTypeAndName (pod )
3643
+ podEndpoint := podEndpoint {
3644
+ Address : addr ,
3645
+ PodName : getPodName (endpoint .TargetRef ),
3646
+ MeshPodOwner : configs.MeshPodOwner {
3647
+ OwnerType : ownerType ,
3648
+ OwnerName : ownerName ,
3649
+ },
3625
3650
}
3651
+ endpointSet [podEndpoint ] = struct {}{}
3626
3652
}
3627
3653
}
3628
3654
}
3629
3655
}
3656
+ return maps .Keys (endpointSet )
3630
3657
}
3631
- if len (endpointSet ) == 0 {
3632
- return nil
3633
- }
3634
- endpoints := make ([]podEndpoint , 0 , len (endpointSet ))
3635
- for ep := range endpointSet {
3636
- endpoints = append (endpoints , ep )
3637
- }
3638
- return endpoints
3658
+
3659
+ return makePodEndpoints (pods , filterReadyEndpointsFrom (selectEndpointSlicesForPort (targetPort , svcEndpointSlices )))
3639
3660
}
3640
3661
3641
3662
func ipv6SafeAddrPort (addr string , port int32 ) string {
@@ -3754,38 +3775,31 @@ func (lbc *LoadBalancerController) getEndpointsForPortFromEndpointSlices(endpoin
3754
3775
return nil , fmt .Errorf ("no port %v in service %s" , backendPort , svc .Name )
3755
3776
}
3756
3777
3757
- endpointSet := make (map [podEndpoint ]struct {})
3758
- for _ , endpointSlice := range endpointSlices {
3759
- for _ , endpointSlicePort := range endpointSlice .Ports {
3760
- if * endpointSlicePort .Port == targetPort {
3761
- for _ , endpoint := range endpointSlice .Endpoints {
3762
- if ! * endpoint .Conditions .Ready {
3763
- continue
3764
- }
3765
- for _ , endpointAddress := range endpoint .Addresses {
3766
- address := ipv6SafeAddrPort (endpointAddress , * endpointSlicePort .Port )
3767
- podEndpoint := podEndpoint {
3768
- Address : address ,
3769
- }
3770
- if endpoint .TargetRef != nil {
3771
- parentType , parentName := lbc .getPodOwnerTypeAndNameFromAddress (endpoint .TargetRef .Namespace , endpoint .TargetRef .Name )
3772
- podEndpoint .OwnerType = parentType
3773
- podEndpoint .OwnerName = parentName
3774
- podEndpoint .PodName = endpoint .TargetRef .Name
3775
- }
3776
- endpointSet [podEndpoint ] = struct {}{}
3777
- }
3778
+ makePodEndpoints := func (port int32 , epx []discovery_v1.Endpoint ) []podEndpoint {
3779
+ endpointSet := make (map [podEndpoint ]struct {})
3780
+
3781
+ for _ , ep := range epx {
3782
+ for _ , addr := range ep .Addresses {
3783
+ address := ipv6SafeAddrPort (addr , port )
3784
+ podEndpoint := podEndpoint {
3785
+ Address : address ,
3786
+ }
3787
+ if ep .TargetRef != nil {
3788
+ parentType , parentName := lbc .getPodOwnerTypeAndNameFromAddress (ep .TargetRef .Namespace , ep .TargetRef .Name )
3789
+ podEndpoint .OwnerType = parentType
3790
+ podEndpoint .OwnerName = parentName
3791
+ podEndpoint .PodName = ep .TargetRef .Name
3778
3792
}
3793
+ endpointSet [podEndpoint ] = struct {}{}
3779
3794
}
3780
3795
}
3796
+ return maps .Keys (endpointSet )
3781
3797
}
3782
- if len (endpointSet ) == 0 {
3798
+
3799
+ endpoints := makePodEndpoints (targetPort , filterReadyEndpointsFrom (selectEndpointSlicesForPort (targetPort , endpointSlices )))
3800
+ if len (endpoints ) == 0 {
3783
3801
return nil , fmt .Errorf ("no endpointslices for target port %v in service %s" , targetPort , svc .Name )
3784
3802
}
3785
- endpoints := make ([]podEndpoint , 0 , len (endpointSet ))
3786
- for ep := range endpointSet {
3787
- endpoints = append (endpoints , ep )
3788
- }
3789
3803
return endpoints , nil
3790
3804
}
3791
3805
0 commit comments