Skip to content

Commit

Permalink
✨ use more reasonable status code for ValidationResponse
Browse files Browse the repository at this point in the history
  • Loading branch information
Mengqi Yu committed May 6, 2019
1 parent 8c0b805 commit 8a06d9b
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 6 deletions.
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
}
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

0 comments on commit 8a06d9b

Please sign in to comment.