diff --git a/example/mutatingwebhook.go b/example/mutatingwebhook.go index 24d2d4dd6c..ec361bf811 100644 --- a/example/mutatingwebhook.go +++ b/example/mutatingwebhook.go @@ -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 { @@ -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) diff --git a/example/validatingwebhook.go b/example/validatingwebhook.go index 09983b33f2..6c02616b28 100644 --- a/example/validatingwebhook.go +++ b/example/validatingwebhook.go @@ -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" diff --git a/pkg/webhook/admission/doc.go b/pkg/webhook/admission/doc.go index 0126aad03a..f33bd11163 100644 --- a/pkg/webhook/admission/doc.go +++ b/pkg/webhook/admission/doc.go @@ -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) } @@ -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) } diff --git a/pkg/webhook/admission/http.go b/pkg/webhook/admission/http.go index d678e24778..2943e445dd 100644 --- a/pkg/webhook/admission/http.go +++ b/pkg/webhook/admission/http.go @@ -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 } @@ -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 } @@ -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 } @@ -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)) } } diff --git a/pkg/webhook/admission/multi.go b/pkg/webhook/admission/multi.go index 43d98dd4c9..a65be69f68 100644 --- a/pkg/webhook/admission/multi.go +++ b/pkg/webhook/admission/multi.go @@ -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)) } @@ -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{ diff --git a/pkg/webhook/admission/response.go b/pkg/webhook/admission/response.go index 0701abc70d..7f9b544712 100644 --- a/pkg/webhook/admission/response.go +++ b/pkg/webhook/admission/response.go @@ -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, @@ -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, diff --git a/pkg/webhook/admission/response_test.go b/pkg/webhook/admission/response_test.go index f9fe987567..8e2dfde48e 100644 --- a/pkg/webhook/admission/response_test.go +++ b/pkg/webhook/admission/response_test.go @@ -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{ @@ -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)) }) }) diff --git a/pkg/webhook/admission/webhook.go b/pkg/webhook/admission/webhook.go index 5a2907c086..ff8a41fefc 100644 --- a/pkg/webhook/admission/webhook.go +++ b/pkg/webhook/admission/webhook.go @@ -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 diff --git a/pkg/webhook/alias.go b/pkg/webhook/alias.go index 6639e6bc73..d721878b7f 100644 --- a/pkg/webhook/alias.go +++ b/pkg/webhook/alias.go @@ -63,5 +63,5 @@ var ( Patched = admission.Patched // Errored indicates that an error occurred in the admission request. - Errored = admission.ErrorResponse + Errored = admission.Errored )