Skip to content

Commit

Permalink
dedup code
Browse files Browse the repository at this point in the history
Signed-off-by: Karol Szwaj <[email protected]>
  • Loading branch information
cnvergence committed Feb 26, 2024
1 parent f9d33bb commit c76954d
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 56 deletions.
35 changes: 15 additions & 20 deletions internal/gatewayapi/contexts.go
Original file line number Diff line number Diff line change
Expand Up @@ -418,26 +418,21 @@ func (r *RouteParentContext) HasCondition(route RouteContext, condType gwapiv1.R
return false
}

// BackendRefContext represents a generic BackendRef object that can reference Gateway objects.
type BackendRefContext struct {
HTTPBackendRef *gwapiv1.HTTPBackendRef
GRPCBackendRef *v1alpha2.GRPCBackendRef
BackendRef *gwapiv1.BackendRef
}
// BackendRefContext represents a generic BackendRef object that can reference different BackendRef objects.
type BackendRefContext any

func GetBackendRef(b BackendRefContext) *gwapiv1.BackendRef {
rv := reflect.ValueOf(b)
br := rv.FieldByName("BackendRef")
if br.IsValid() {
backendRef := br.Interface().(gwapiv1.BackendRef)
return &backendRef

// GetBackendRef returns the BackendRef object from BackendRefContext matching route Kind.
func (b *BackendRefContext) GetBackendRef(routeKind gwapiv1.Kind) *gwapiv1.BackendRef {
switch routeKind {
case KindHTTPRoute:
if b.HTTPBackendRef != nil {
return &b.HTTPBackendRef.BackendRef
}
case KindGRPCRoute:
if b.GRPCBackendRef != nil {
return &b.GRPCBackendRef.BackendRef
}
default:
return b.BackendRef
}
return nil
backendRef := b.(gwapiv1.BackendRef)
return &backendRef
}

func GetFilters(b BackendRefContext) any {
return reflect.ValueOf(b).FieldByName("Filters")

Check warning on line 437 in internal/gatewayapi/contexts.go

View check run for this annotation

Codecov / codecov/patch

internal/gatewayapi/contexts.go#L436-L437

Added lines #L436 - L437 were not covered by tests
}
17 changes: 7 additions & 10 deletions internal/gatewayapi/filters.go
Original file line number Diff line number Diff line change
Expand Up @@ -704,25 +704,22 @@ func (t *Translator) processRequestMirrorFilter(

// Wrap the filter's BackendObjectReference into a BackendRef so we can use existing tooling to check it
weight := int32(1)
mirrorBackendRef := gwapiv1.BackendRef{
BackendObjectReference: mirrorBackend,
Weight: &weight,
}

backendRefContext := BackendRefContext{
HTTPBackendRef: &gwapiv1.HTTPBackendRef{
BackendRef: mirrorBackendRef,
mirrorBackendRef := gwapiv1.HTTPBackendRef{
BackendRef: gwapiv1.BackendRef{
BackendObjectReference: mirrorBackend,
Weight: &weight,
},
}

// This sets the status on the HTTPRoute, should the usage be changed so that the status message reflects that the backendRef is from the filter?
filterNs := filterContext.Route.GetNamespace()
serviceNamespace := NamespaceDerefOr(mirrorBackend.Namespace, filterNs)
if !t.validateBackendRef(backendRefContext, filterContext.ParentRef, filterContext.Route,
if !t.validateBackendRef(mirrorBackendRef, filterContext.ParentRef, filterContext.Route,
resources, serviceNamespace, KindHTTPRoute) {
return
}

ds, _ := t.processDestination(backendRefContext, filterContext.ParentRef, filterContext.Route, resources)
ds, _ := t.processDestination(mirrorBackendRef, filterContext.ParentRef, filterContext.Route, resources)

newMirror := &ir.RouteDestination{
Name: fmt.Sprintf("%s-mirror-%d", irRouteDestinationName(filterContext.Route, filterContext.RuleIdx), filterIdx),
Expand Down
29 changes: 6 additions & 23 deletions internal/gatewayapi/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,11 +174,7 @@ func (t *Translator) processHTTPRouteRules(httpRoute *HTTPRouteContext, parentRe

for _, backendRef := range rule.BackendRefs {
backendRef := backendRef
backendRefContext := BackendRefContext{
HTTPBackendRef: &backendRef,
}

ds, backendWeight := t.processDestination(backendRefContext, parentRef, httpRoute, resources)
ds, backendWeight := t.processDestination(backendRef, parentRef, httpRoute, resources)
if !t.EndpointRoutingDisabled && ds != nil && len(ds.Endpoints) > 0 && ds.AddressType != nil {
dstAddrTypeMap[*ds.AddressType]++
}
Expand Down Expand Up @@ -474,11 +470,7 @@ func (t *Translator) processGRPCRouteRules(grpcRoute *GRPCRouteContext, parentRe

for _, backendRef := range rule.BackendRefs {
backendRef := backendRef
backendRefContext := BackendRefContext{
GRPCBackendRef: &backendRef,
}

ds, backendWeight := t.processDestination(backendRefContext, parentRef, grpcRoute, resources)
ds, backendWeight := t.processDestination(backendRef, parentRef, grpcRoute, resources)
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 Down Expand Up @@ -732,10 +724,7 @@ func (t *Translator) processTLSRouteParentRefs(tlsRoute *TLSRouteContext, resour
for _, rule := range tlsRoute.Spec.Rules {
for _, backendRef := range rule.BackendRefs {
backendRef := backendRef
backendRefContext := BackendRefContext{
BackendRef: &backendRef,
}
ds, _ := t.processDestination(backendRefContext, parentRef, tlsRoute, resources)
ds, _ := t.processDestination(backendRef, parentRef, tlsRoute, resources)
if ds != nil {
destSettings = append(destSettings, ds)
}
Expand Down Expand Up @@ -873,10 +862,7 @@ func (t *Translator) processUDPRouteParentRefs(udpRoute *UDPRouteContext, resour
}

backendRef := udpRoute.Spec.Rules[0].BackendRefs[0]
backendRefContext := BackendRefContext{
BackendRef: &backendRef,
}
ds, _ := t.processDestination(backendRefContext, parentRef, udpRoute, resources)
ds, _ := t.processDestination(backendRef, parentRef, udpRoute, resources)
// Skip further processing if route destination is not valid
if ds == nil || len(ds.Endpoints) == 0 {
continue
Expand Down Expand Up @@ -1008,10 +994,7 @@ func (t *Translator) processTCPRouteParentRefs(tcpRoute *TCPRouteContext, resour
}

backendRef := tcpRoute.Spec.Rules[0].BackendRefs[0]
backendRefContext := BackendRefContext{
BackendRef: &backendRef,
}
ds, _ := t.processDestination(backendRefContext, parentRef, tcpRoute, resources)
ds, _ := t.processDestination(backendRef, parentRef, tcpRoute, resources)
// Skip further processing if route destination is not valid
if ds == nil || len(ds.Endpoints) == 0 {
continue
Expand Down Expand Up @@ -1093,7 +1076,7 @@ func (t *Translator) processDestination(backendRefContext BackendRefContext,
resources *Resources) (ds *ir.DestinationSetting, backendWeight uint32) {
routeType := GetRouteType(route)
weight := uint32(1)
backendRef := backendRefContext.GetBackendRef(routeType)
backendRef := GetBackendRef(backendRefContext)
if backendRef.Weight != nil {
weight = uint32(*backendRef.Weight)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ xdsIR:
directResponse:
statusCode: 500
hostname: '*'
isHTTP2: false
name: httproute/default/httproute-1/rule/0/match/0/*
pathMatch:
distinct: false
Expand Down
8 changes: 5 additions & 3 deletions internal/gatewayapi/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func (t *Translator) validateBackendRef(backendRefContext BackendRefContext, par
if !t.validateBackendRefFilters(backendRefContext, parentRef, route, routeKind) {
return false
}
backendRef := backendRefContext.GetBackendRef(routeKind)
backendRef := GetBackendRef(backendRefContext)

if !t.validateBackendRefGroup(backendRef, parentRef, route) {
return false
Expand Down Expand Up @@ -89,9 +89,11 @@ func (t *Translator) validateBackendRefFilters(backendRef BackendRefContext, par
var filtersLen int
switch routeKind {
case KindHTTPRoute:
filtersLen = len(backendRef.HTTPBackendRef.Filters)
br := backendRef.(gwapiv1.HTTPBackendRef)
filtersLen = len(br.Filters)
case KindGRPCRoute:
filtersLen = len(backendRef.GRPCBackendRef.Filters)
br := backendRef.(gwapiv1a2.GRPCBackendRef)
filtersLen = len(br.Filters)
default:
return true
}
Expand Down

0 comments on commit c76954d

Please sign in to comment.