@@ -48,9 +48,9 @@ func ValidateHTTPRoute(route *gatewayv1a2.HTTPRoute) field.ErrorList {
48
48
func validateHTTPRouteSpec (spec * gatewayv1a2.HTTPRouteSpec , path * field.Path ) field.ErrorList {
49
49
var errs field.ErrorList
50
50
for i , rule := range spec .Rules {
51
- errs = append (errs , validateHTTPRouteFilters (rule .Filters , path .Child ("rules" ).Index (i ))... )
51
+ errs = append (errs , validateHTTPRouteFilters (rule .Filters , rule . Matches , path .Child ("rules" ).Index (i ))... )
52
52
for j , backendRef := range rule .BackendRefs {
53
- errs = append (errs , validateHTTPRouteFilters (backendRef .Filters , path .Child ("rules" ).Index (i ).Child ("backendsrefs" ).Index (j ))... )
53
+ errs = append (errs , validateHTTPRouteFilters (backendRef .Filters , rule . Matches , path .Child ("rules" ).Index (i ).Child ("backendsrefs" ).Index (j ))... )
54
54
}
55
55
for j , m := range rule .Matches {
56
56
if m .Path != nil {
@@ -90,17 +90,17 @@ func validateHTTPRouteBackendServicePorts(rules []gatewayv1a2.HTTPRouteRule, pat
90
90
91
91
// validateHTTPRouteFilters validates that a list of core and extended filters
92
92
// is used at most once and that the filter type matches its value
93
- func validateHTTPRouteFilters (filters []gatewayv1a2.HTTPRouteFilter , path * field.Path ) field.ErrorList {
93
+ func validateHTTPRouteFilters (filters []gatewayv1a2.HTTPRouteFilter , matches []gatewayv1a2. HTTPRouteMatch , path * field.Path ) field.ErrorList {
94
94
var errs field.ErrorList
95
95
counts := map [gatewayv1a2.HTTPRouteFilterType ]int {}
96
96
97
97
for i , filter := range filters {
98
98
counts [filter .Type ]++
99
99
if filter .RequestRedirect != nil && filter .RequestRedirect .Path != nil {
100
- errs = append (errs , validateHTTPPathModifier (* filter .RequestRedirect .Path , path .Index (i ).Child ("requestRedirect" , "path" ))... )
100
+ errs = append (errs , validateHTTPPathModifier (* filter .RequestRedirect .Path , matches , path .Index (i ).Child ("requestRedirect" , "path" ))... )
101
101
}
102
102
if filter .URLRewrite != nil && filter .URLRewrite .Path != nil {
103
- errs = append (errs , validateHTTPPathModifier (* filter .URLRewrite .Path , path .Index (i ).Child ("urlRewrite" , "path" ))... )
103
+ errs = append (errs , validateHTTPPathModifier (* filter .URLRewrite .Path , matches , path .Index (i ).Child ("urlRewrite" , "path" ))... )
104
104
}
105
105
errs = append (errs , validateHTTPRouteFilterTypeMatchesValue (filter , path .Index (i ))... )
106
106
}
@@ -198,7 +198,7 @@ func validateHTTPRouteFilterTypeMatchesValue(filter gatewayv1a2.HTTPRouteFilter,
198
198
199
199
// validateHTTPPathModifier validates that only the expected fields are set in a
200
200
// path modifier.
201
- func validateHTTPPathModifier (modifier gatewayv1a2.HTTPPathModifier , path * field.Path ) field.ErrorList {
201
+ func validateHTTPPathModifier (modifier gatewayv1a2.HTTPPathModifier , matches []gatewayv1a2. HTTPRouteMatch , path * field.Path ) field.ErrorList {
202
202
var errs field.ErrorList
203
203
if modifier .ReplaceFullPath != nil && modifier .Type != gatewayv1a2 .FullPathHTTPPathModifier {
204
204
errs = append (errs , field .Invalid (path , modifier .ReplaceFullPath , "must be nil if the HTTPRouteFilter.Type is not ReplaceFullPath" ))
@@ -212,5 +212,23 @@ func validateHTTPPathModifier(modifier gatewayv1a2.HTTPPathModifier, path *field
212
212
if modifier .ReplacePrefixMatch == nil && modifier .Type == gatewayv1a2 .PrefixMatchHTTPPathModifier {
213
213
errs = append (errs , field .Invalid (path , modifier .ReplacePrefixMatch , "must not be nil if the HTTPRouteFilter.Type is ReplacePrefixMatch" ))
214
214
}
215
+
216
+ if modifier .Type == gatewayv1a2 .PrefixMatchHTTPPathModifier && modifier .ReplacePrefixMatch != nil {
217
+ if ! hasExactlyOnePrefixMatch (matches ) {
218
+ errs = append (errs , field .Invalid (path , modifier .ReplacePrefixMatch , "exactly one PathPrefix match must be specified to use this path modifier" ))
219
+ }
220
+ }
215
221
return errs
216
222
}
223
+
224
+ func hasExactlyOnePrefixMatch (matches []gatewayv1a2.HTTPRouteMatch ) bool {
225
+ if len (matches ) != 1 || matches [0 ].Path == nil {
226
+ return false
227
+ }
228
+ pathMatchType := matches [0 ].Path .Type
229
+ if * pathMatchType != gatewayv1a2 .PathMatchPathPrefix {
230
+ return false
231
+ }
232
+
233
+ return true
234
+ }
0 commit comments