-
Notifications
You must be signed in to change notification settings - Fork 1.2k
⚠️ admission responses with raw Status #1129
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
k8s-ci-robot
merged 7 commits into
kubernetes-sigs:master
from
ericabramov:admission-webhooks-status-response
Aug 27, 2020
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
1902414
:warning: admission responses with raw Status
2c49665
:warning: admission responses with raw Status
f61e953
:warning: admission responses with raw Status
1f5be8a
Adding tests for webhook/admission/validator.go
a76edb0
Fix tests (undo FDescribe)
47eafd8
Align test structure
9f235ae
Fix CR comments for tests
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,280 @@ | ||
| package admission | ||
|
|
||
| import ( | ||
| "context" | ||
| goerrors "errors" | ||
| "net/http" | ||
|
|
||
| . "github.com/onsi/ginkgo" | ||
| . "github.com/onsi/gomega" | ||
|
|
||
| "k8s.io/api/admission/v1beta1" | ||
| apierrs "k8s.io/apimachinery/pkg/api/errors" | ||
| v1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| "k8s.io/apimachinery/pkg/runtime" | ||
| "k8s.io/apimachinery/pkg/runtime/schema" | ||
| "k8s.io/client-go/kubernetes/scheme" | ||
| ) | ||
|
|
||
| var _ = Describe("validatingHandler", func() { | ||
| Describe("Handle", func() { | ||
| It("should return 200 in response when create succeeds", func() { | ||
|
|
||
| handler := createSucceedingValidatingHandler() | ||
|
|
||
| response := handler.Handle(context.TODO(), Request{ | ||
| AdmissionRequest: v1beta1.AdmissionRequest{ | ||
| Operation: v1beta1.Create, | ||
| Object: runtime.RawExtension{ | ||
| Raw: []byte("{}"), | ||
| Object: handler.validator, | ||
| }, | ||
| }, | ||
| }) | ||
| Expect(response.Allowed).Should(BeTrue()) | ||
| Expect(response.Result.Code).Should(Equal(int32(http.StatusOK))) | ||
| }) | ||
|
|
||
| It("should return 400 in response when create fails on decode", func() { | ||
| //TODO | ||
| }) | ||
|
|
||
| It("should return response built with the Status object when ValidateCreate returns APIStatus error", func() { | ||
|
|
||
| handler, expectedError := createValidatingHandlerWhichReturnsStatusError() | ||
|
|
||
| response := handler.Handle(context.TODO(), Request{ | ||
| AdmissionRequest: v1beta1.AdmissionRequest{ | ||
| Operation: v1beta1.Create, | ||
| Object: runtime.RawExtension{ | ||
| Raw: []byte("{}"), | ||
| Object: handler.validator, | ||
| }, | ||
| }, | ||
| }) | ||
| Expect(response.Allowed).Should(BeFalse()) | ||
|
|
||
| apiStatus, ok := expectedError.(apierrs.APIStatus) | ||
| Expect(ok).Should(BeTrue()) | ||
| Expect(response.Result.Code).Should(Equal(apiStatus.Status().Code)) | ||
| Expect(*response.Result).Should(Equal(apiStatus.Status())) | ||
|
|
||
| }) | ||
|
|
||
| It("should return 403 response when ValidateCreate returns non-APIStatus error", func() { | ||
|
|
||
| handler, expectedError := createValidatingHandlerWhichReturnsRegularError() | ||
|
|
||
| response := handler.Handle(context.TODO(), Request{ | ||
| AdmissionRequest: v1beta1.AdmissionRequest{ | ||
| Operation: v1beta1.Create, | ||
| Object: runtime.RawExtension{ | ||
| Raw: []byte("{}"), | ||
| Object: handler.validator, | ||
| }, | ||
| }, | ||
| }) | ||
| Expect(response.Allowed).Should(BeFalse()) | ||
| Expect(response.Result.Code).Should(Equal(int32(http.StatusForbidden))) | ||
| Expect(string(response.Result.Reason)).Should(Equal(expectedError.Error())) | ||
|
|
||
| }) | ||
|
|
||
| It("should return 200 in response when update succeeds", func() { | ||
|
|
||
| handler := createSucceedingValidatingHandler() | ||
|
|
||
| response := handler.Handle(context.TODO(), Request{ | ||
| AdmissionRequest: v1beta1.AdmissionRequest{ | ||
| Operation: v1beta1.Update, | ||
| Object: runtime.RawExtension{ | ||
| Raw: []byte("{}"), | ||
| Object: handler.validator, | ||
| }, | ||
| OldObject: runtime.RawExtension{ | ||
| Raw: []byte("{}"), | ||
| Object: handler.validator, | ||
| }, | ||
| }, | ||
| }) | ||
| Expect(response.Allowed).Should(BeTrue()) | ||
| Expect(response.Result.Code).Should(Equal(int32(http.StatusOK))) | ||
| }) | ||
|
|
||
| It("should return 400 in response when update fails on decoding new object", func() { | ||
ericabramov marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| //TODO | ||
| }) | ||
|
|
||
| It("should return 400 in response when update fails on decoding old object", func() { | ||
| //TODO | ||
| }) | ||
|
|
||
| It("should return response built with the Status object when ValidateUpdate returns APIStatus error", func() { | ||
|
|
||
| handler, expectedError := createValidatingHandlerWhichReturnsStatusError() | ||
|
|
||
| response := handler.Handle(context.TODO(), Request{ | ||
| AdmissionRequest: v1beta1.AdmissionRequest{ | ||
| Operation: v1beta1.Update, | ||
| Object: runtime.RawExtension{ | ||
| Raw: []byte("{}"), | ||
| Object: handler.validator, | ||
| }, | ||
| OldObject: runtime.RawExtension{ | ||
| Raw: []byte("{}"), | ||
| Object: handler.validator, | ||
| }, | ||
| }, | ||
| }) | ||
| Expect(response.Allowed).Should(BeFalse()) | ||
|
|
||
| apiStatus, ok := expectedError.(apierrs.APIStatus) | ||
| Expect(ok).Should(BeTrue()) | ||
| Expect(response.Result.Code).Should(Equal(apiStatus.Status().Code)) | ||
| Expect(*response.Result).Should(Equal(apiStatus.Status())) | ||
|
|
||
| }) | ||
|
|
||
| It("should return 403 response when ValidateUpdate returns non-APIStatus error", func() { | ||
|
|
||
| handler, expectedError := createValidatingHandlerWhichReturnsRegularError() | ||
|
|
||
| response := handler.Handle(context.TODO(), Request{ | ||
| AdmissionRequest: v1beta1.AdmissionRequest{ | ||
| Operation: v1beta1.Update, | ||
| Object: runtime.RawExtension{ | ||
| Raw: []byte("{}"), | ||
| Object: handler.validator, | ||
| }, | ||
| OldObject: runtime.RawExtension{ | ||
| Raw: []byte("{}"), | ||
| Object: handler.validator, | ||
| }, | ||
| }, | ||
| }) | ||
| Expect(response.Allowed).Should(BeFalse()) | ||
| Expect(response.Result.Code).Should(Equal(int32(http.StatusForbidden))) | ||
| Expect(string(response.Result.Reason)).Should(Equal(expectedError.Error())) | ||
|
|
||
| }) | ||
|
|
||
| It("should return 200 in response when delete succeeds", func() { | ||
|
|
||
| handler := createSucceedingValidatingHandler() | ||
|
|
||
| response := handler.Handle(context.TODO(), Request{ | ||
| AdmissionRequest: v1beta1.AdmissionRequest{ | ||
| Operation: v1beta1.Delete, | ||
| OldObject: runtime.RawExtension{ | ||
| Raw: []byte("{}"), | ||
| Object: handler.validator, | ||
| }, | ||
| }, | ||
| }) | ||
| Expect(response.Allowed).Should(BeTrue()) | ||
| Expect(response.Result.Code).Should(Equal(int32(http.StatusOK))) | ||
| }) | ||
|
|
||
| It("should return 400 in response when delete fails on decode", func() { | ||
| //TODO | ||
| }) | ||
|
|
||
| It("should return response built with the Status object when ValidateDelete returns APIStatus error", func() { | ||
|
|
||
| handler, expectedError := createValidatingHandlerWhichReturnsStatusError() | ||
|
|
||
| response := handler.Handle(context.TODO(), Request{ | ||
| AdmissionRequest: v1beta1.AdmissionRequest{ | ||
| Operation: v1beta1.Delete, | ||
| OldObject: runtime.RawExtension{ | ||
| Raw: []byte("{}"), | ||
| Object: handler.validator, | ||
| }, | ||
| }, | ||
| }) | ||
| Expect(response.Allowed).Should(BeFalse()) | ||
|
|
||
| apiStatus, ok := expectedError.(apierrs.APIStatus) | ||
| Expect(ok).Should(BeTrue()) | ||
| Expect(response.Result.Code).Should(Equal(apiStatus.Status().Code)) | ||
| Expect(*response.Result).Should(Equal(apiStatus.Status())) | ||
|
|
||
| }) | ||
|
|
||
| It("should return 403 response when ValidateDelete returns non-APIStatus error", func() { | ||
|
|
||
| handler, expectedError := createValidatingHandlerWhichReturnsRegularError() | ||
|
|
||
| response := handler.Handle(context.TODO(), Request{ | ||
| AdmissionRequest: v1beta1.AdmissionRequest{ | ||
| Operation: v1beta1.Delete, | ||
| OldObject: runtime.RawExtension{ | ||
| Raw: []byte("{}"), | ||
| Object: handler.validator, | ||
| }, | ||
| }, | ||
| }) | ||
| Expect(response.Allowed).Should(BeFalse()) | ||
| Expect(response.Result.Code).Should(Equal(int32(http.StatusForbidden))) | ||
| Expect(string(response.Result.Reason)).Should(Equal(expectedError.Error())) | ||
|
|
||
| }) | ||
| }) | ||
| }) | ||
|
|
||
| type fakeValidator struct { | ||
| ErrorToReturn error `json:"ErrorToReturn,omitempty"` | ||
| } | ||
|
|
||
| var _ Validator = &fakeValidator{} | ||
|
|
||
| var fakeValidatorVK = schema.GroupVersionKind{Group: "foo.test.org", Version: "v1", Kind: "fakeValidator"} | ||
|
|
||
| func (v *fakeValidator) ValidateCreate() error { | ||
| return v.ErrorToReturn | ||
| } | ||
|
|
||
| func (v *fakeValidator) ValidateUpdate(old runtime.Object) error { | ||
| return v.ErrorToReturn | ||
| } | ||
|
|
||
| func (v *fakeValidator) ValidateDelete() error { | ||
| return v.ErrorToReturn | ||
| } | ||
|
|
||
| func (v *fakeValidator) GetObjectKind() schema.ObjectKind { return v } | ||
|
|
||
| func (v *fakeValidator) DeepCopyObject() runtime.Object { | ||
| return &fakeValidator{ErrorToReturn: v.ErrorToReturn} | ||
| } | ||
|
|
||
| func (v *fakeValidator) GroupVersionKind() schema.GroupVersionKind { | ||
| return fakeValidatorVK | ||
| } | ||
|
|
||
| func (v *fakeValidator) SetGroupVersionKind(gvk schema.GroupVersionKind) {} | ||
|
|
||
| func createSucceedingValidatingHandler() *validatingHandler { | ||
| decoder, _ := NewDecoder(scheme.Scheme) | ||
ericabramov marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| f := &fakeValidator{ErrorToReturn: nil} | ||
| return &validatingHandler{f, decoder} | ||
ericabramov marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| func createValidatingHandlerWhichReturnsRegularError() (validatingHandler, error) { | ||
| decoder, _ := NewDecoder(scheme.Scheme) | ||
| errToReturn := goerrors.New("some error") | ||
| f := &fakeValidator{ErrorToReturn: errToReturn} | ||
| return validatingHandler{f, decoder}, errToReturn | ||
| } | ||
|
|
||
| func createValidatingHandlerWhichReturnsStatusError() (validatingHandler, error) { | ||
| decoder, _ := NewDecoder(scheme.Scheme) | ||
| errToReturn := &apierrs.StatusError{ | ||
| ErrStatus: v1.Status{ | ||
| Message: "some message", | ||
| Code: http.StatusUnprocessableEntity, | ||
| }, | ||
| } | ||
| f := &fakeValidator{ErrorToReturn: errToReturn} | ||
| return validatingHandler{f, decoder}, errToReturn | ||
| } | ||
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.