Skip to content

Commit

Permalink
Rename ErrorResponse to Errored
Browse files Browse the repository at this point in the history
It's the same name used in alias.go, and it produces less-verbose code
without loss of readability.
  • Loading branch information
DirectXMan12 committed Feb 20, 2019
1 parent 89dfb30 commit e4c3610
Show file tree
Hide file tree
Showing 9 changed files with 21 additions and 21 deletions.
4 changes: 2 additions & 2 deletions example/mutatingwebhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func (a *podAnnotator) Handle(ctx context.Context, req admission.Request) admiss

err := a.decoder.Decode(req, pod)
if err != nil {
return admission.ErrorResponse(http.StatusBadRequest, err)
return admission.Errored(http.StatusBadRequest, err)
}

if pod.Annotations == nil {
Expand All @@ -48,7 +48,7 @@ func (a *podAnnotator) Handle(ctx context.Context, req admission.Request) admiss

marshaledPod, err := json.Marshal(pod)
if err != nil {
return admission.ErrorResponse(http.StatusInternalServerError, err)
return admission.Errored(http.StatusInternalServerError, err)
}

return admission.PatchResponseFromRaw(req.AdmissionRequest.Object.Raw, marshaledPod)
Expand Down
2 changes: 1 addition & 1 deletion example/validatingwebhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func (v *podValidator) Handle(ctx context.Context, req admission.Request) admiss

err := v.decoder.Decode(req, pod)
if err != nil {
return admission.ErrorResponse(http.StatusBadRequest, err)
return admission.Errored(http.StatusBadRequest, err)
}

key := "example-mutating-admission-webhook"
Expand Down
8 changes: 4 additions & 4 deletions pkg/webhook/admission/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ The following snippet is an example implementation of mutating handler.
pod := &corev1.Pod{}
err := m.decoder.Decode(req, pod)
if err != nil {
return admission.ErrorResponse(http.StatusBadRequest, err)
return admission.Errored(http.StatusBadRequest, err)
}
// Do deepcopy before actually mutate the object.
copy := pod.DeepCopy()
err = m.mutatePodsFn(ctx, copy)
if err != nil {
return admission.ErrorResponse(http.StatusInternalServerError, err)
return admission.Errored(http.StatusInternalServerError, err)
}
return admission.PatchResponse(pod, copy)
}
Expand Down Expand Up @@ -70,12 +70,12 @@ The following snippet is an example implementation of validating handler.
pod := &corev1.Pod{}
err := h.decoder.Decode(req, pod)
if err != nil {
return admission.ErrorResponse(http.StatusBadRequest, err)
return admission.Errored(http.StatusBadRequest, err)
}
allowed, reason, err := h.validatePodsFn(ctx, pod)
if err != nil {
return admission.ErrorResponse(http.StatusInternalServerError, err)
return admission.Errored(http.StatusInternalServerError, err)
}
return admission.ValidationResponse(allowed, reason)
}
Expand Down
10 changes: 5 additions & 5 deletions pkg/webhook/admission/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,14 @@ func (wh Webhook) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if r.Body != nil {
if body, err = ioutil.ReadAll(r.Body); err != nil {
log.Error(err, "unable to read the body from the incoming request")
reviewResponse = ErrorResponse(http.StatusBadRequest, err)
reviewResponse = Errored(http.StatusBadRequest, err)
wh.writeResponse(w, reviewResponse)
return
}
} else {
err = errors.New("request body is empty")
log.Error(err, "bad request")
reviewResponse = ErrorResponse(http.StatusBadRequest, err)
reviewResponse = Errored(http.StatusBadRequest, err)
wh.writeResponse(w, reviewResponse)
return
}
Expand All @@ -66,7 +66,7 @@ func (wh Webhook) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if contentType != "application/json" {
err = fmt.Errorf("contentType=%s, expected application/json", contentType)
log.Error(err, "unable to process a request with an unknown content type", "content type", contentType)
reviewResponse = ErrorResponse(http.StatusBadRequest, err)
reviewResponse = Errored(http.StatusBadRequest, err)
wh.writeResponse(w, reviewResponse)
return
}
Expand All @@ -78,7 +78,7 @@ func (wh Webhook) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}
if _, _, err := admissionCodecs.UniversalDeserializer().Decode(body, nil, &ar); err != nil {
log.Error(err, "unable to decode the request")
reviewResponse = ErrorResponse(http.StatusBadRequest, err)
reviewResponse = Errored(http.StatusBadRequest, err)
wh.writeResponse(w, reviewResponse)
return
}
Expand All @@ -96,6 +96,6 @@ func (wh *Webhook) writeResponse(w io.Writer, response Response) {
err := encoder.Encode(responseAdmissionReview)
if err != nil {
log.Error(err, "unable to encode the response")
wh.writeResponse(w, ErrorResponse(http.StatusInternalServerError, err))
wh.writeResponse(w, Errored(http.StatusInternalServerError, err))
}
}
4 changes: 2 additions & 2 deletions pkg/webhook/admission/multi.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func (hs multiMutating) Handle(ctx context.Context, req Request) Response {
return resp
}
if resp.PatchType != nil && *resp.PatchType != admissionv1beta1.PatchTypeJSONPatch {
return ErrorResponse(http.StatusInternalServerError,
return Errored(http.StatusInternalServerError,
fmt.Errorf("unexpected patch type returned by the handler: %v, only allow: %v",
resp.PatchType, admissionv1beta1.PatchTypeJSONPatch))
}
Expand All @@ -47,7 +47,7 @@ func (hs multiMutating) Handle(ctx context.Context, req Request) Response {
var err error
marshaledPatch, err := json.Marshal(patches)
if err != nil {
return ErrorResponse(http.StatusBadRequest, fmt.Errorf("error when marshaling the patch: %v", err))
return Errored(http.StatusBadRequest, fmt.Errorf("error when marshaling the patch: %v", err))
}
return Response{
AdmissionResponse: admissionv1beta1.AdmissionResponse{
Expand Down
6 changes: 3 additions & 3 deletions pkg/webhook/admission/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ func Patched(reason string, patches ...jsonpatch.JsonPatchOperation) Response {
return resp
}

// ErrorResponse creates a new Response for error-handling a request.
func ErrorResponse(code int32, err error) Response {
// Errored creates a new Response for error-handling a request.
func Errored(code int32, err error) Response {
return Response{
AdmissionResponse: admissionv1beta1.AdmissionResponse{
Allowed: false,
Expand Down Expand Up @@ -81,7 +81,7 @@ func ValidationResponse(allowed bool, reason string) Response {
func PatchResponseFromRaw(original, current []byte) Response {
patches, err := jsonpatch.CreatePatch(original, current)
if err != nil {
return ErrorResponse(http.StatusInternalServerError, err)
return Errored(http.StatusInternalServerError, err)
}
return Response{
Patches: patches,
Expand Down
4 changes: 2 additions & 2 deletions pkg/webhook/admission/response_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ var _ = Describe("Admission Webhook Response Helpers", func() {
})
})

Describe("ErrorResponse", func() {
Describe("Errored", func() {
It("should return a denied response with an error", func() {
err := errors.New("this is an error")
expected := Response{
Expand All @@ -128,7 +128,7 @@ var _ = Describe("Admission Webhook Response Helpers", func() {
},
},
}
resp := ErrorResponse(http.StatusBadRequest, err)
resp := Errored(http.StatusBadRequest, err)
Expect(resp).To(Equal(expected))
})
})
Expand Down
2 changes: 1 addition & 1 deletion pkg/webhook/admission/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func (w *Webhook) Handle(ctx context.Context, req Request) Response {
resp := w.Handler.Handle(ctx, req)
if err := resp.Complete(req); err != nil {
log.Error(err, "unable to encode response")
return ErrorResponse(http.StatusInternalServerError, errUnableToEncodeResponse)
return Errored(http.StatusInternalServerError, errUnableToEncodeResponse)
}

return resp
Expand Down
2 changes: 1 addition & 1 deletion pkg/webhook/alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,5 @@ var (
Patched = admission.Patched

// Errored indicates that an error occurred in the admission request.
Errored = admission.ErrorResponse
Errored = admission.Errored
)

0 comments on commit e4c3610

Please sign in to comment.