generated from kubernetes/kubernetes-template-project
-
Notifications
You must be signed in to change notification settings - Fork 617
Webhook: validate at least one of GRPCRoute.Rules.Matches.{Method,Service} is supplied #1490
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| /* | ||
| Copyright 2022 The Kubernetes Authors. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package validation | ||
|
|
||
| import ( | ||
| "k8s.io/apimachinery/pkg/util/validation/field" | ||
|
|
||
| gatewayv1a2 "sigs.k8s.io/gateway-api/apis/v1alpha2" | ||
| ) | ||
|
|
||
| // ValidateGRPCRoute validates GRPCRoute according to the Gateway API specification. | ||
| // For additional details of the GRPCRoute spec, refer to: | ||
| // https://gateway-api.sigs.k8s.io/v1alpha2/references/spec/#gateway.networking.k8s.io/v1alpha2.GRPCRoute | ||
| func ValidateGRPCRoute(route *gatewayv1a2.GRPCRoute) field.ErrorList { | ||
| return validateGRPCRouteSpec(&route.Spec, field.NewPath("spec")) | ||
| } | ||
|
|
||
| // validateRouteSpec validates that required fields of spec are set according to the | ||
| // Gateway API specification. | ||
| func validateGRPCRouteSpec(spec *gatewayv1a2.GRPCRouteSpec, path *field.Path) field.ErrorList { | ||
| var errs field.ErrorList | ||
| errs = append(errs, validateGRPCRouteRules(spec.Rules, path.Child("rules"))...) | ||
| return errs | ||
| } | ||
|
|
||
| // validateGRPCRouteRules validates whether required fields of rules are set according | ||
| // to the Gateway API specification. | ||
| func validateGRPCRouteRules(rules []gatewayv1a2.GRPCRouteRule, path *field.Path) field.ErrorList { | ||
| var errs field.ErrorList | ||
| for i, rule := range rules { | ||
| errs = append(errs, validateRuleMatches(rule.Matches, path.Index(i).Child("matches"))...) | ||
| } | ||
| return errs | ||
| } | ||
|
|
||
| // validateRuleMatches validates that at least one of the fields Service and Method of | ||
| // GRPCMethodMatch to be specified | ||
| func validateRuleMatches(matches []gatewayv1a2.GRPCRouteMatch, path *field.Path) field.ErrorList { | ||
| var errs field.ErrorList | ||
| for i, m := range matches { | ||
| if m.Method != nil && m.Method.Service == nil && m.Method.Method == nil { | ||
| errs = append(errs, field.Required(path.Index(i).Child("methods"), "should have at least one of fields Service and Method")) | ||
| return errs | ||
| } | ||
| } | ||
| return errs | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| /* | ||
| Copyright 2022 The Kubernetes Authors. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package validation | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "k8s.io/apimachinery/pkg/util/validation/field" | ||
|
|
||
| gatewayv1a2 "sigs.k8s.io/gateway-api/apis/v1alpha2" | ||
| ) | ||
|
|
||
| func TestValidateGRPCRoute(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| service := "foo" | ||
| method := "login" | ||
|
|
||
| tests := []struct { | ||
| name string | ||
| rules []gatewayv1a2.GRPCRouteRule | ||
| errs field.ErrorList | ||
| }{ | ||
| { | ||
| name: "valid GRPCRoute with 1 service in GRPCMethodMatch field", | ||
| rules: []gatewayv1a2.GRPCRouteRule{ | ||
| { | ||
| Matches: []gatewayv1a2.GRPCRouteMatch{ | ||
| { | ||
| Method: &gatewayv1a2.GRPCMethodMatch{ | ||
| Service: &service, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| { | ||
| name: "valid GRPCRoute with 1 method in GRPCMethodMatch field", | ||
| rules: []gatewayv1a2.GRPCRouteRule{ | ||
| { | ||
| Matches: []gatewayv1a2.GRPCRouteMatch{ | ||
| { | ||
| Method: &gatewayv1a2.GRPCMethodMatch{ | ||
| Method: &method, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| { | ||
| name: "invalid GRPCRoute missing service or method in GRPCMethodMatch field", | ||
| rules: []gatewayv1a2.GRPCRouteRule{ | ||
| { | ||
| Matches: []gatewayv1a2.GRPCRouteMatch{ | ||
| { | ||
| Method: &gatewayv1a2.GRPCMethodMatch{ | ||
| Service: nil, | ||
| Method: nil, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| errs: field.ErrorList{ | ||
| { | ||
| Type: field.ErrorTypeRequired, | ||
| Field: "spec.rules[0].matches[0].methods", | ||
| Detail: "should have at least one of fields Service and Method", | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| for _, tc := range tests { | ||
| tc := tc | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| route := gatewayv1a2.GRPCRoute{Spec: gatewayv1a2.GRPCRouteSpec{Rules: tc.rules}} | ||
| errs := ValidateGRPCRoute(&route) | ||
| if len(errs) != len(tc.errs) { | ||
| t.Errorf("got %d errors, want %d errors: %s", len(errs), len(tc.errs), errs) | ||
| t.FailNow() | ||
| } | ||
| for i := 0; i < len(errs); i++ { | ||
| realErr := errs[i].Error() | ||
| expectedErr := tc.errs[i].Error() | ||
| if realErr != expectedErr { | ||
| t.Errorf("expect error message: %s, but got: %s", expectedErr, realErr) | ||
| t.FailNow() | ||
| } | ||
| } | ||
| }) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.