Skip to content

Commit

Permalink
feat: support FQDN address type for EndpointSlice (#2138)
Browse files Browse the repository at this point in the history
* initial implementation of supporting FQDN address type for EndpointSlice

Signed-off-by: sh2 <shawnhxh@outlook.com>

* add support to endpointslice fqdn address type with simple approach

Signed-off-by: sh2 <shawnhxh@outlook.com>

* fix ci unit test

Signed-off-by: sh2 <shawnhxh@outlook.com>

* resolve comments

Signed-off-by: sh2 <shawnhxh@outlook.com>

* add addrtype check and refactor condition message

Signed-off-by: sh2 <shawnhxh@outlook.com>

---------

Signed-off-by: sh2 <shawnhxh@outlook.com>
Co-authored-by: zirain <zirain2009@gmail.com>
  • Loading branch information
shawnh2 and zirain authored Dec 6, 2023
1 parent ebc18d8 commit a89d95e
Show file tree
Hide file tree
Showing 111 changed files with 1,451 additions and 190 deletions.
2 changes: 1 addition & 1 deletion internal/gatewayapi/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func (r *Resources) GetSecret(namespace, name string) *v1.Secret {
}

func (r *Resources) GetEndpointSlicesForBackend(svcNamespace, svcName string, backendKind string) []*discoveryv1.EndpointSlice {
endpointSlices := []*discoveryv1.EndpointSlice{}
var endpointSlices []*discoveryv1.EndpointSlice
for _, endpointSlice := range r.EndpointSlices {
var backendSelectorLabel string
switch backendKind {
Expand Down
101 changes: 79 additions & 22 deletions internal/gatewayapi/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,14 @@ func (t *Translator) processHTTPRouteRules(httpRoute *HTTPRouteContext, parentRe
// a unique Xds IR HTTPRoute per match.
var ruleRoutes = t.processHTTPRouteRule(httpRoute, ruleIdx, httpFiltersContext, rule)

dstAddrTypeMap := make(map[ir.DestinationAddressType]int)

for _, backendRef := range rule.BackendRefs {
ds, backendWeight := t.processDestination(backendRef.BackendRef, parentRef, httpRoute, resources)
if !t.EndpointRoutingDisabled && ds != nil && len(ds.Endpoints) > 0 && ds.AddressType != nil {
dstAddrTypeMap[*ds.AddressType]++
}

for _, route := range ruleRoutes {
// If the route already has a direct response or redirect configured, then it was from a filter so skip
// processing any destinations for this route.
Expand All @@ -163,14 +169,22 @@ func (t *Translator) processHTTPRouteRules(httpRoute *HTTPRouteContext, parentRe
}
route.Destination.Settings = append(route.Destination.Settings, ds)
route.BackendWeights.Valid += backendWeight

} else {
route.BackendWeights.Invalid += backendWeight
}
}
}
}

// TODO: support mixed endpointslice address type between backendRefs
if !t.EndpointRoutingDisabled && len(dstAddrTypeMap) > 1 {
parentRef.SetCondition(httpRoute,
gwapiv1.RouteConditionResolvedRefs,
metav1.ConditionFalse,
gwapiv1a1.RouteReasonResolvedRefs,
"Mixed endpointslice address type between backendRefs is not supported")
}

// If the route has no valid backends then just use a direct response and don't fuss with weighted responses
for _, ruleRoute := range ruleRoutes {
if ruleRoute.BackendWeights.Invalid > 0 && ruleRoute.Destination == nil {
Expand Down Expand Up @@ -980,7 +994,10 @@ func (t *Translator) processDestination(backendRef gwapiv1.BackendRef,
return nil, weight
}

var endpoints []*ir.DestinationEndpoint
var (
endpoints []*ir.DestinationEndpoint
addrType *ir.DestinationAddressType
)
protocol := inspectAppProtocolByRouteKind(routeType)
switch KindDerefOr(backendRef.Kind, KindService) {
case KindServiceImport:
Expand All @@ -992,9 +1009,10 @@ func (t *Translator) processDestination(backendRef gwapiv1.BackendRef,
break
}
}

if !t.EndpointRoutingDisabled {
endpointSlices := resources.GetEndpointSlicesForBackend(backendNamespace, string(backendRef.Name), KindDerefOr(backendRef.Kind, KindService))
endpoints = getIREndpointsFromEndpointSlice(endpointSlices, servicePort.Name, servicePort.Protocol)
endpoints, addrType = getIREndpointsFromEndpointSlices(endpointSlices, servicePort.Name, servicePort.Protocol)
} else {
backendIps := resources.GetServiceImport(backendNamespace, string(backendRef.Name)).Spec.IPs
for _, ip := range backendIps {
Expand Down Expand Up @@ -1023,7 +1041,7 @@ func (t *Translator) processDestination(backendRef gwapiv1.BackendRef,
// Route to endpoints by default
if !t.EndpointRoutingDisabled {
endpointSlices := resources.GetEndpointSlicesForBackend(backendNamespace, string(backendRef.Name), KindDerefOr(backendRef.Kind, KindService))
endpoints = getIREndpointsFromEndpointSlice(endpointSlices, servicePort.Name, servicePort.Protocol)
endpoints, addrType = getIREndpointsFromEndpointSlices(endpointSlices, servicePort.Name, servicePort.Protocol)
} else {
// Fall back to Service ClusterIP routing
ep := ir.NewDestEndpoint(
Expand All @@ -1033,10 +1051,20 @@ func (t *Translator) processDestination(backendRef gwapiv1.BackendRef,
}
}

// TODO: support mixed endpointslice address type for the same backendRef
if !t.EndpointRoutingDisabled && addrType != nil && *addrType == ir.MIXED {
parentRef.SetCondition(route,
gwapiv1.RouteConditionResolvedRefs,
metav1.ConditionFalse,
gwapiv1a1.RouteReasonResolvedRefs,
"Mixed endpointslice address type for the same backendRef is not supported")
}

ds = &ir.DestinationSetting{
Weight: &weight,
Protocol: protocol,
Endpoints: endpoints,
Weight: &weight,
Protocol: protocol,
Endpoints: endpoints,
AddressType: addrType,
}
return ds, weight
}
Expand Down Expand Up @@ -1137,22 +1165,51 @@ func (t *Translator) processAllowedListenersForParentRefs(routeContext RouteCont
return relevantRoute
}

func getIREndpointsFromEndpointSlice(endpointSlices []*discoveryv1.EndpointSlice, portName string, portProtocol corev1.Protocol) []*ir.DestinationEndpoint {
endpoints := []*ir.DestinationEndpoint{}
func getIREndpointsFromEndpointSlices(endpointSlices []*discoveryv1.EndpointSlice, portName string, portProtocol corev1.Protocol) ([]*ir.DestinationEndpoint, *ir.DestinationAddressType) {
var (
dstEndpoints []*ir.DestinationEndpoint
dstAddrType *ir.DestinationAddressType
)

addrTypeMap := make(map[ir.DestinationAddressType]int)
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 == portName &&
*endpointPort.Protocol == portProtocol &&
*endpoint.Conditions.Ready {
for _, address := range endpoint.Addresses {
ep := ir.NewDestEndpoint(
address,
uint32(*endpointPort.Port))
endpoints = append(endpoints, ep)
}
if endpointSlice.AddressType == discoveryv1.AddressTypeFQDN {
addrTypeMap[ir.FQDN]++
} else {
addrTypeMap[ir.IP]++
}
endpoints := getIREndpointsFromEndpointSlice(endpointSlice, portName, portProtocol)
dstEndpoints = append(dstEndpoints, endpoints...)
}

for addrTypeState, addrTypeCounts := range addrTypeMap {
if addrTypeCounts == len(endpointSlices) {
dstAddrType = ptr.To(addrTypeState)
break
}
}

if len(addrTypeMap) > 0 && dstAddrType == nil {
dstAddrType = ptr.To(ir.MIXED)
}

return dstEndpoints, dstAddrType
}

func getIREndpointsFromEndpointSlice(endpointSlice *discoveryv1.EndpointSlice, portName string, portProtocol corev1.Protocol) []*ir.DestinationEndpoint {
var endpoints []*ir.DestinationEndpoint
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 == portName &&
*endpointPort.Protocol == portProtocol &&
*endpoint.Conditions.Ready {
for _, address := range endpoint.Addresses {
ep := ir.NewDestEndpoint(
address,
uint32(*endpointPort.Port))
endpoints = append(endpoints, ep)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,8 @@ xdsIR:
destination:
name: grpcroute/default/grpcroute-1/rule/0
settings:
- endpoints:
- addressType: IP
endpoints:
- host: 7.7.7.7
port: 8080
protocol: GRPC
Expand All @@ -356,7 +357,8 @@ xdsIR:
destination:
name: httproute/default/httproute-2/rule/0
settings:
- endpoints:
- addressType: IP
endpoints:
- host: 7.7.7.7
port: 8080
protocol: HTTP
Expand All @@ -377,7 +379,8 @@ xdsIR:
destination:
name: httproute/default/httproute-1/rule/0
settings:
- endpoints:
- addressType: IP
endpoints:
- host: 7.7.7.7
port: 8080
protocol: HTTP
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,8 @@ xdsIR:
destination:
name: grpcroute/default/grpcroute-1/rule/0
settings:
- endpoints:
- addressType: IP
endpoints:
- host: 7.7.7.7
port: 8080
protocol: GRPC
Expand All @@ -270,7 +271,8 @@ xdsIR:
destination:
name: httproute/default/httproute-1/rule/0
settings:
- endpoints:
- addressType: IP
endpoints:
- host: 7.7.7.7
port: 8080
protocol: HTTP
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,8 @@ xdsIR:
destination:
name: grpcroute/default/grpcroute-1/rule/0
settings:
- endpoints:
- addressType: IP
endpoints:
- host: 7.7.7.7
port: 8080
protocol: GRPC
Expand Down Expand Up @@ -300,7 +301,8 @@ xdsIR:
destination:
name: httproute/default/httproute-1/rule/0
settings:
- endpoints:
- addressType: IP
endpoints:
- host: 7.7.7.7
port: 8080
protocol: HTTP
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,8 @@ xdsIR:
destination:
name: httproute/default/httproute-1/rule/0
settings:
- endpoints:
- addressType: IP
endpoints:
- host: 7.7.7.7
port: 8080
protocol: HTTP
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,8 @@ xdsIR:
destination:
name: httproute/envoy-gateway/httproute-1/rule/0
settings:
- endpoints:
- addressType: IP
endpoints:
- host: 7.7.7.7
port: 8080
protocol: HTTP
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,8 @@ xdsIR:
destination:
name: httproute/default/httproute-1/rule/0
settings:
- endpoints:
- addressType: IP
endpoints:
- host: 7.7.7.7
port: 8080
protocol: HTTP
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,8 @@ xdsIR:
destination:
name: httproute/default/httproute-1/rule/0
settings:
- endpoints:
- addressType: IP
endpoints:
- host: 7.7.7.7
port: 8080
protocol: HTTP
Expand All @@ -201,7 +202,8 @@ xdsIR:
destination:
name: tlsroute/default/tlsroute-1/rule/-1
settings:
- endpoints:
- addressType: IP
endpoints:
- host: 7.7.7.7
port: 8080
protocol: HTTPS
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ xdsIR:
destination:
name: httproute/default/httproute-1/rule/0
settings:
- endpoints:
- addressType: IP
endpoints:
- host: 7.7.7.7
port: 8080
protocol: HTTP
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ xdsIR:
destination:
name: httproute/default/httproute-1/rule/0
settings:
- endpoints:
- addressType: IP
endpoints:
- host: 7.7.7.7
port: 8080
protocol: HTTP
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,8 @@ xdsIR:
destination:
name: httproute/default/httproute-1/rule/0
settings:
- endpoints:
- addressType: IP
endpoints:
- host: 7.7.7.7
port: 8080
protocol: HTTP
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,8 @@ xdsIR:
destination:
name: httproute/default/httproute-1/rule/0
settings:
- endpoints:
- addressType: IP
endpoints:
- host: 7.7.7.7
port: 8080
protocol: HTTP
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@ xdsIR:
destination:
name: tcproute/default/tcproute-1/rule/-1
settings:
- endpoints:
- addressType: IP
endpoints:
- host: 7.7.7.7
port: 8163
protocol: TCP
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@ xdsIR:
destination:
name: udproute/default/udproute-1/rule/-1
settings:
- endpoints:
- addressType: IP
endpoints:
- host: 7.7.7.7
port: 8162
protocol: UDP
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,8 @@ xdsIR:
destination:
name: httproute/default/httproute-1/rule/0
settings:
- endpoints:
- addressType: IP
endpoints:
- host: 7.7.7.7
port: 8080
protocol: HTTP
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,8 @@ xdsIR:
destination:
name: tcproute/default/tcproute-1/rule/-1
settings:
- endpoints:
- addressType: IP
endpoints:
- host: 7.7.7.7
port: 8163
protocol: TCP
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,8 @@ xdsIR:
destination:
name: udproute/default/udproute-1/rule/-1
settings:
- endpoints:
- addressType: IP
endpoints:
- host: 7.7.7.7
port: 8162
protocol: UDP
Expand Down
Loading

0 comments on commit a89d95e

Please sign in to comment.