Skip to content

Commit

Permalink
EndpointSlice to IR Route Destinations
Browse files Browse the repository at this point in the history
Relates to envoyproxy#1256

Signed-off-by: Arko Dasgupta <[email protected]>
  • Loading branch information
arkodg committed Sep 26, 2023
1 parent 34d477f commit f901043
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 21 deletions.
11 changes: 11 additions & 0 deletions internal/gatewayapi/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,14 @@ func (r *Resources) GetSecret(namespace, name string) *v1.Secret {

return nil
}

func (r *Resources) GetEndpointSlicesForService(svcNamespace, svcName string) []*discoveryv1.EndpointSlice {
endpointSlices := []*discoveryv1.EndpointSlice{}
for _, endpointSlice := range r.EndpointSlices {
if svcNamespace == endpointSlice.Namespace &&
endpointSlice.GetLabels()[discoveryv1.LabelServiceName] == svcName {
endpointSlices = append(endpointSlices, endpointSlice)
}
}
return endpointSlices
}
58 changes: 37 additions & 21 deletions internal/gatewayapi/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"fmt"
"strings"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/gateway-api/apis/v1alpha2"
"sigs.k8s.io/gateway-api/apis/v1beta1"
Expand Down Expand Up @@ -980,30 +981,45 @@ func (t *Translator) processDestEndpoints(backendRef v1beta1.BackendRef,
return nil, weight
}

var backendIps []string
switch KindDerefOr(backendRef.Kind, KindService) {
case KindServiceImport:
backendIps = resources.GetServiceImport(backendNamespace, string(backendRef.Name)).Spec.IPs
case KindService:
backendIps = []string{resources.GetService(backendNamespace, string(backendRef.Name)).Spec.ClusterIP}
var servicePort corev1.ServicePort
for _, port := range service.Spec.Ports {
if port.Port == int32(*backendRef.Port) {
servicePort = port
break
}
}

for _, ip := range backendIps {
var ep *ir.DestinationEndpoint
// Weights are not relevant for TCP and UDP Routes
if routeType == KindTCPRoute || routeType == KindUDPRoute {
ep = ir.NewDestEndpoint(
ip,
uint32(*backendRef.Port))
} else {
ep = ir.NewDestEndpointWithWeight(
ip,
uint32(*backendRef.Port),
weight)
}
endpoints = append(endpoints, ep)
endpointSlices := resources.GetEndpointSlicesForService(backendNamespace, string(backendRef.Name))

for _, endpointSlice := range endpointSlices {
for _, endpoint := range endpointSlice.Endpoints {
for _, endpointPort := range endpointSlice.Ports {
// Check if the endpoint port matches the service port
// and if endpoint is Ready
if *endpointPort.Name == servicePort.Name &&
*endpointPort.Protocol == servicePort.Protocol &&
*endpoint.Conditions.Ready {
for _, address := range endpoint.Addresses {
var dest *ir.RouteDestination
// Weights are not relevant for TCP and UDP Routes
if routeType == KindTCPRoute ||
routeType == KindUDPRoute {
dest = ir.NewRouteDest(
address,
uint32(servicePort.TargetPort.IntVal))
} else {
dest = ir.NewRouteDestWithWeight(
address,
uint32(servicePort.TargetPort.IntVal),
weight)
}
destinations = append(destinations, dest)
}
}
}
}
}
return endpoints, weight
return destinations, weight
}

// processAllowedListenersForParentRefs finds out if the route attaches to one of our
Expand Down

0 comments on commit f901043

Please sign in to comment.