-
Notifications
You must be signed in to change notification settings - Fork 118
Run webhook validation rules inside NKG control plane #388
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 all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
f9ec7b6
Run webhook validation
pleshakov fd79ca2
Fix alignment
pleshakov 95caaa9
Fix alignment 2
pleshakov f130a9d
Improve recorder name
pleshakov 54b5de5
Reword a comment about not validating GatewayClass
pleshakov afcfcf2
Improve reconcile
pleshakov cdde573
Shorten the assertion
pleshakov 08a7dcb
Move validation error handling
pleshakov c96415f
Revert recorder name
pleshakov 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
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
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
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
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,27 @@ | ||
package manager | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
|
||
"k8s.io/apimachinery/pkg/util/validation/field" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
|
||
"github.com/nginxinc/nginx-kubernetes-gateway/internal/reconciler" | ||
) | ||
|
||
// createValidator creates a reconciler.ValidatorFunc from a function that validates a resource of type R. | ||
func createValidator[R client.Object](validate func(R) field.ErrorList) reconciler.ValidatorFunc { | ||
return func(obj client.Object) error { | ||
if obj == nil { | ||
panic(errors.New("obj is nil")) | ||
} | ||
|
||
r, ok := obj.(R) | ||
if !ok { | ||
panic(fmt.Errorf("obj type mismatch: got %T, expected %T", obj, r)) | ||
} | ||
|
||
return validate(r).ToAggregate() | ||
} | ||
} |
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,77 @@ | ||
package manager | ||
|
||
import ( | ||
"testing" | ||
|
||
. "github.com/onsi/gomega" | ||
"k8s.io/apimachinery/pkg/util/validation/field" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/gateway-api/apis/v1beta1" | ||
) | ||
|
||
func TestCreateTypedValidator(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
obj client.Object | ||
errorList field.ErrorList | ||
expectPanic bool | ||
expectErr bool | ||
}{ | ||
{ | ||
obj: &v1beta1.HTTPRoute{}, | ||
errorList: field.ErrorList{}, | ||
expectPanic: false, | ||
expectErr: false, | ||
name: "no errors", | ||
}, | ||
{ | ||
obj: &v1beta1.HTTPRoute{}, | ||
errorList: []*field.Error{{Detail: "test"}}, | ||
expectPanic: false, | ||
expectErr: true, | ||
name: "one error", | ||
}, | ||
{ | ||
obj: nil, | ||
errorList: field.ErrorList{}, | ||
expectPanic: true, | ||
expectErr: false, | ||
name: "nil object", | ||
}, | ||
{ | ||
obj: &v1beta1.Gateway{}, | ||
errorList: field.ErrorList{}, | ||
expectPanic: true, | ||
expectErr: false, | ||
name: "wrong object type", | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
g := NewGomegaWithT(t) | ||
|
||
v := createValidator(createValidateHTTPRouteThatReturns(test.errorList)) | ||
|
||
if test.expectPanic { | ||
g.Expect(func() { _ = v(test.obj) }).To(Panic()) | ||
return | ||
} | ||
|
||
result := v(test.obj) | ||
|
||
if test.expectErr { | ||
g.Expect(result).ToNot(BeNil()) | ||
return | ||
} | ||
|
||
g.Expect(result).To(BeNil()) | ||
}) | ||
} | ||
} | ||
|
||
func createValidateHTTPRouteThatReturns(errorList field.ErrorList) func(*v1beta1.HTTPRoute) field.ErrorList { | ||
return func(*v1beta1.HTTPRoute) field.ErrorList { | ||
return errorList | ||
} | ||
} |
Oops, something went wrong.
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.