Skip to content

Commit

Permalink
webhook: Allow only one instance of each HTTPRouteFilterType
Browse files Browse the repository at this point in the history
Signed-off-by: Christopher M. Luciano <cmluciano@us.ibm.com>
  • Loading branch information
Christopher M. Luciano committed Dec 29, 2020
1 parent d8aed04 commit c67eaf3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
18 changes: 16 additions & 2 deletions pkg/admission/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,24 @@ func ValidateHTTPRoute(httpRoute v1alpha1.HTTPRoute) (bool, string, error) {
if len(httpRoute.Spec.Rules) == 0 {
return true, "", nil
}
numReqHeaderMod := 0
numReqMirror := 0
numExtRef := 0
for _, rule := range httpRoute.Spec.Rules {
if len(rule.Filters) != 0 {
if len(rule.Filters) > 1 {
return false, "HTTPRules cannot contain more than one HTTPFilter", nil
for _, filter := range rule.Filters {
switch filter.Type {
case v1alpha1.HTTPRouteFilterRequestHeaderModifier:
numReqHeaderMod++
case v1alpha1.HTTPRouteFilterRequestMirror:
numReqMirror++
case v1alpha1.HTTPRouteFilterExtensionRef:
numExtRef++
}
}
// Multiple filters are allowed but only 1 of each HTTPRouteFilterType
if numReqHeaderMod > 1 || numReqMirror > 1 || numExtRef > 1 {
return false, "HTTPRules cannot contain more than instance of each HTTPRouteFilterType", nil
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/admission/validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ func TestSAValidator_ValidateHTTPRoute(t *testing.T) {
wantErr: false,
},
{
name: "valid httpRoute with 2 filters",
name: "invalid httpRoute with 2 filters",
hRoute: v1alpha1.HTTPRoute{
Spec: v1alpha1.HTTPRouteSpec{
Rules: []v1alpha1.HTTPRouteRule{
Expand Down Expand Up @@ -204,7 +204,7 @@ func TestSAValidator_ValidateHTTPRoute(t *testing.T) {
},
},
wantOK: false,
wantMessage: "HTTPRules cannot contain more than one HTTPFilter",
wantMessage: "HTTPRules cannot contain more than instance of each HTTPRouteFilterType",
wantErr: false,
},
}
Expand Down

0 comments on commit c67eaf3

Please sign in to comment.