diff --git a/pkg/webhook/admission/http.go b/pkg/webhook/admission/http.go index 57e465abb3..4735800195 100644 --- a/pkg/webhook/admission/http.go +++ b/pkg/webhook/admission/http.go @@ -42,27 +42,23 @@ func init() { var _ http.Handler = &Webhook{} func (wh *Webhook) ServeHTTP(w http.ResponseWriter, r *http.Request) { - var body []byte - var err error ctx := r.Context() if wh.WithContextFunc != nil { ctx = wh.WithContextFunc(ctx, r) } - var reviewResponse Response if r.Body == nil { - err = errors.New("request body is empty") + err := errors.New("request body is empty") wh.getLogger(nil).Error(err, "bad request") - reviewResponse = Errored(http.StatusBadRequest, err) - wh.writeResponse(w, reviewResponse) + wh.writeResponse(w, Errored(http.StatusBadRequest, err)) return } defer r.Body.Close() - if body, err = io.ReadAll(r.Body); err != nil { + body, err := io.ReadAll(r.Body) + if err != nil { wh.getLogger(nil).Error(err, "unable to read the body from the incoming request") - reviewResponse = Errored(http.StatusBadRequest, err) - wh.writeResponse(w, reviewResponse) + wh.writeResponse(w, Errored(http.StatusBadRequest, err)) return } @@ -70,8 +66,7 @@ func (wh *Webhook) ServeHTTP(w http.ResponseWriter, r *http.Request) { if contentType := r.Header.Get("Content-Type"); contentType != "application/json" { err = fmt.Errorf("contentType=%s, expected application/json", contentType) wh.getLogger(nil).Error(err, "unable to process a request with unknown content type") - reviewResponse = Errored(http.StatusBadRequest, err) - wh.writeResponse(w, reviewResponse) + wh.writeResponse(w, Errored(http.StatusBadRequest, err)) return } @@ -89,14 +84,12 @@ func (wh *Webhook) ServeHTTP(w http.ResponseWriter, r *http.Request) { _, actualAdmRevGVK, err := admissionCodecs.UniversalDeserializer().Decode(body, nil, &ar) if err != nil { wh.getLogger(nil).Error(err, "unable to decode the request") - reviewResponse = Errored(http.StatusBadRequest, err) - wh.writeResponse(w, reviewResponse) + wh.writeResponse(w, Errored(http.StatusBadRequest, err)) return } wh.getLogger(&req).V(5).Info("received request") - reviewResponse = wh.Handle(ctx, req) - wh.writeResponseTyped(w, reviewResponse, actualAdmRevGVK) + wh.writeResponseTyped(w, wh.Handle(ctx, req), actualAdmRevGVK) } // writeResponse writes response to w generically, i.e. without encoding GVK information. diff --git a/pkg/webhook/authentication/http.go b/pkg/webhook/authentication/http.go index c0fd9aba55..3b0d5c4959 100644 --- a/pkg/webhook/authentication/http.go +++ b/pkg/webhook/authentication/http.go @@ -42,36 +42,31 @@ func init() { var _ http.Handler = &Webhook{} func (wh *Webhook) ServeHTTP(w http.ResponseWriter, r *http.Request) { - var body []byte - var err error ctx := r.Context() if wh.WithContextFunc != nil { ctx = wh.WithContextFunc(ctx, r) } - var reviewResponse Response if r.Body == nil { - err = errors.New("request body is empty") + err := errors.New("request body is empty") wh.getLogger(nil).Error(err, "bad request") - reviewResponse = Errored(err) - wh.writeResponse(w, reviewResponse) + wh.writeResponse(w, Errored(err)) return } defer r.Body.Close() - if body, err = io.ReadAll(r.Body); err != nil { + body, err := io.ReadAll(r.Body) + if err != nil { wh.getLogger(nil).Error(err, "unable to read the body from the incoming request") - reviewResponse = Errored(err) - wh.writeResponse(w, reviewResponse) + wh.writeResponse(w, Errored(err)) return } // verify the content type is accurate if contentType := r.Header.Get("Content-Type"); contentType != "application/json" { - err = fmt.Errorf("contentType=%s, expected application/json", contentType) + err := fmt.Errorf("contentType=%s, expected application/json", contentType) wh.getLogger(nil).Error(err, "unable to process a request with unknown content type") - reviewResponse = Errored(err) - wh.writeResponse(w, reviewResponse) + wh.writeResponse(w, Errored(err)) return } @@ -90,22 +85,19 @@ func (wh *Webhook) ServeHTTP(w http.ResponseWriter, r *http.Request) { _, actualTokRevGVK, err := authenticationCodecs.UniversalDeserializer().Decode(body, nil, &ar) if err != nil { wh.getLogger(nil).Error(err, "unable to decode the request") - reviewResponse = Errored(err) - wh.writeResponse(w, reviewResponse) + wh.writeResponse(w, Errored(err)) return } wh.getLogger(&req).V(5).Info("received request") if req.Spec.Token == "" { - err = errors.New("token is empty") + err := errors.New("token is empty") wh.getLogger(&req).Error(err, "bad request") - reviewResponse = Errored(err) - wh.writeResponse(w, reviewResponse) + wh.writeResponse(w, Errored(err)) return } - reviewResponse = wh.Handle(ctx, req) - wh.writeResponseTyped(w, reviewResponse, actualTokRevGVK) + wh.writeResponseTyped(w, wh.Handle(ctx, req), actualTokRevGVK) } // writeResponse writes response to w generically, i.e. without encoding GVK information.