Skip to content

Commit

Permalink
refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
cnvergence committed May 20, 2024
1 parent e042b4b commit 2781416
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 23 deletions.
3 changes: 1 addition & 2 deletions internal/gatewayapi/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -1239,14 +1239,13 @@ func (t *Translator) processDestination(backendRefContext BackendRefContext,
}

func getBackendFilters(routeType gwapiv1.Kind, backendRefContext BackendRefContext) (backendFilters any) {
filters := GetFilters(backendRefContext)
switch routeType {
case KindHTTPRoute:
filters := GetFilters(backendRefContext)
if len(filters.([]gwapiv1.HTTPRouteFilter)) > 0 {
return filters.([]gwapiv1.HTTPRouteFilter)
}
case KindGRPCRoute:
filters := GetFilters(backendRefContext)
if len(filters.([]gwapiv1.GRPCRouteFilter)) > 0 {
return filters.([]gwapiv1.GRPCRouteFilter)

Check warning on line 1250 in internal/gatewayapi/route.go

View check run for this annotation

Codecov / codecov/patch

internal/gatewayapi/route.go#L1250

Added line #L1250 was not covered by tests
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ httpRoutes:
status: "True"
type: Accepted
- lastTransitionTime: null
message: Specific filter is not supported within BackendRef
message: Specific filter is not supported within BackendRef, only RequestHeaderModifier
and ResponseHeaderModifier are supported
reason: UnsupportedRefValue
status: "False"
type: ResolvedRefs
Expand Down
41 changes: 21 additions & 20 deletions internal/gatewayapi/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,39 +94,40 @@ func (t *Translator) validateBackendRefKind(backendRef *gwapiv1a2.BackendRef, pa
}

func (t *Translator) validateBackendRefFilters(backendRef BackendRefContext, parentRef *RouteParentContext, route RouteContext, routeKind gwapiv1.Kind) bool {
filters := GetFilters(backendRef)
var unsupportedFilters bool

switch routeKind {
case KindHTTPRoute:
filters := GetFilters(backendRef).([]gwapiv1.HTTPRouteFilter)
for _, filter := range filters {
for _, filter := range filters.([]gwapiv1.HTTPRouteFilter) {
if filter.Type != gwapiv1.HTTPRouteFilterRequestHeaderModifier && filter.Type != gwapiv1.HTTPRouteFilterResponseHeaderModifier {
t.setRouteStatusCondition(route, parentRef, "UnsupportedRefValue", "Specific filter is not supported within BackendRef")
return false
unsupportedFilters = true
}
}
case KindGRPCRoute:
filters := GetFilters(backendRef).([]gwapiv1.GRPCRouteFilter)
for _, filter := range filters {
for _, filter := range filters.([]gwapiv1.GRPCRouteFilter) {
if filter.Type != gwapiv1.GRPCRouteFilterRequestHeaderModifier && filter.Type != gwapiv1.GRPCRouteFilterResponseHeaderModifier {
t.setRouteStatusCondition(route, parentRef, "UnsupportedRefValue", "Specific filter is not supported within BackendRef")
return false
unsupportedFilters = true

Check warning on line 110 in internal/gatewayapi/validate.go

View check run for this annotation

Codecov / codecov/patch

internal/gatewayapi/validate.go#L109-L110

Added lines #L109 - L110 were not covered by tests
}
}
default:
return true
}
return true
}

func (t *Translator) setRouteStatusCondition(route RouteContext, parentRef *RouteParentContext, reason gwapiv1.RouteConditionReason, message string) {
routeStatus := GetRouteStatus(route)
status.SetRouteStatusCondition(routeStatus,
parentRef.routeParentStatusIdx,
route.GetGeneration(),
gwapiv1.RouteConditionResolvedRefs,
metav1.ConditionFalse,
reason,
message,
)
if unsupportedFilters {
routeStatus := GetRouteStatus(route)
status.SetRouteStatusCondition(routeStatus,
parentRef.routeParentStatusIdx,
route.GetGeneration(),
gwapiv1.RouteConditionResolvedRefs,
metav1.ConditionFalse,
"UnsupportedRefValue",
"Specific filter is not supported within BackendRef, only RequestHeaderModifier and ResponseHeaderModifier are supported",
)
return false
}

return true
}

func (t *Translator) validateBackendNamespace(backendRef *gwapiv1a2.BackendRef, parentRef *RouteParentContext, route RouteContext,
Expand Down

0 comments on commit 2781416

Please sign in to comment.