Skip to content
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

✨ use more reasonable status code for ValidationResponse #400

Merged
merged 1 commit into from
May 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/builder/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ var _ = Describe("application", func() {
Expect(w.Code).To(Equal(http.StatusOK))
By("sanity checking the response contains reasonable field")
Expect(w.Body).To(ContainSubstring(`"allowed":false`))
Expect(w.Body).To(ContainSubstring(`"code":200`))
Expect(w.Body).To(ContainSubstring(`"code":403`))
})

It("should scaffold defaulting and validating webhooks if the type implements both Defaulter and Validator interfaces", func() {
Expand Down
11 changes: 8 additions & 3 deletions pkg/webhook/admission/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,20 @@ func Errored(code int32, err error) Response {

// ValidationResponse returns a response for admitting a request.
func ValidationResponse(allowed bool, reason string) Response {
code := http.StatusForbidden
if allowed {
code = http.StatusOK
}
mengqiy marked this conversation as resolved.
Show resolved Hide resolved
resp := Response{
AdmissionResponse: admissionv1beta1.AdmissionResponse{
Allowed: allowed,
Result: &metav1.Status{
Code: int32(code),
},
},
}
if len(reason) > 0 {
resp.Result = &metav1.Status{
Reason: metav1.StatusReason(reason),
}
resp.Result.Reason = metav1.StatusReason(reason)
}
return resp
}
Expand Down
24 changes: 22 additions & 2 deletions pkg/webhook/admission/response_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ var _ = Describe("Admission Webhook Response Helpers", func() {
Response{
AdmissionResponse: admissionv1beta1.AdmissionResponse{
Allowed: true,
Result: &metav1.Status{
Code: http.StatusOK,
},
},
},
))
Expand All @@ -46,6 +49,7 @@ var _ = Describe("Admission Webhook Response Helpers", func() {
AdmissionResponse: admissionv1beta1.AdmissionResponse{
Allowed: true,
Result: &metav1.Status{
Code: http.StatusOK,
Reason: "acceptable",
},
},
Expand All @@ -60,6 +64,9 @@ var _ = Describe("Admission Webhook Response Helpers", func() {
Response{
AdmissionResponse: admissionv1beta1.AdmissionResponse{
Allowed: false,
Result: &metav1.Status{
Code: http.StatusForbidden,
},
},
},
))
Expand All @@ -71,6 +78,7 @@ var _ = Describe("Admission Webhook Response Helpers", func() {
AdmissionResponse: admissionv1beta1.AdmissionResponse{
Allowed: false,
Result: &metav1.Status{
Code: http.StatusForbidden,
Reason: "UNACCEPTABLE!",
},
},
Expand All @@ -96,6 +104,9 @@ var _ = Describe("Admission Webhook Response Helpers", func() {
Response{
AdmissionResponse: admissionv1beta1.AdmissionResponse{
Allowed: true,
Result: &metav1.Status{
Code: http.StatusOK,
},
},
Patches: ops,
},
Expand All @@ -107,6 +118,7 @@ var _ = Describe("Admission Webhook Response Helpers", func() {
AdmissionResponse: admissionv1beta1.AdmissionResponse{
Allowed: true,
Result: &metav1.Status{
Code: http.StatusOK,
Reason: "some changes",
},
},
Expand Down Expand Up @@ -141,6 +153,7 @@ var _ = Describe("Admission Webhook Response Helpers", func() {
AdmissionResponse: admissionv1beta1.AdmissionResponse{
Allowed: true,
Result: &metav1.Status{
Code: http.StatusOK,
Reason: "acceptable",
},
},
Expand All @@ -153,6 +166,7 @@ var _ = Describe("Admission Webhook Response Helpers", func() {
AdmissionResponse: admissionv1beta1.AdmissionResponse{
Allowed: false,
Result: &metav1.Status{
Code: http.StatusForbidden,
Reason: "UNACCEPTABLE!",
},
},
Expand All @@ -161,20 +175,26 @@ var _ = Describe("Admission Webhook Response Helpers", func() {
})

It("should return an admission decision", func() {
By("checking that it returns a 'denied' response when allowed is false")
By("checking that it returns an 'allowed' response when allowed is true")
Expect(ValidationResponse(true, "")).To(Equal(
Response{
AdmissionResponse: admissionv1beta1.AdmissionResponse{
Allowed: true,
Result: &metav1.Status{
Code: http.StatusOK,
},
},
},
))

By("checking that it returns an 'allowed' response when allowed is true")
By("checking that it returns an 'denied' response when allowed is false")
Expect(ValidationResponse(false, "")).To(Equal(
Response{
AdmissionResponse: admissionv1beta1.AdmissionResponse{
Allowed: false,
Result: &metav1.Status{
Code: http.StatusForbidden,
},
},
},
))
Expand Down