Skip to content

Commit

Permalink
Use admReview.Response in error cases too
Browse files Browse the repository at this point in the history
  • Loading branch information
rajivnathan committed Nov 1, 2023
1 parent 80df784 commit c1f6dff
Showing 1 changed file with 5 additions and 7 deletions.
12 changes: 5 additions & 7 deletions pkg/webhook/mutatingwebhook/mutate.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,16 @@ func handleMutate(logger logr.Logger, w http.ResponseWriter, r *http.Request, mu
admReview := admissionv1.AdmissionReview{}
if _, _, err := deserializer.Decode(admReviewBody, nil, &admReview); err != nil {
logger.Error(err, "unable to deserialize the admission review object", "body", string(admReviewBody))
writeResponse(logger, http.StatusBadRequest, w, []byte("unable to read the body of the request"))
return
admReview.Response = responseWithError("", err)

Check warning on line 45 in pkg/webhook/mutatingwebhook/mutate.go

View check run for this annotation

Codecov / codecov/patch

pkg/webhook/mutatingwebhook/mutate.go#L44-L45

Added lines #L44 - L45 were not covered by tests
} else if admReview.Request == nil {
err = fmt.Errorf("admission review request is nil")
logger.Error(err, "invalid admission review request", "AdmissionReview", admReview)
writeResponse(logger, http.StatusBadRequest, w, []byte("unable to read the body of the request"))
return
admReview.Response = responseWithError("", err)

Check warning on line 49 in pkg/webhook/mutatingwebhook/mutate.go

View check run for this annotation

Codecov / codecov/patch

pkg/webhook/mutatingwebhook/mutate.go#L47-L49

Added lines #L47 - L49 were not covered by tests
} else {
// mutate the request
admReview.Response = mutator(admReview)
}

// mutate the request
admReview.Response = mutator(admReview)

respBody, err := json.Marshal(admReview)
if err != nil {
logger.Error(err, "unable to marshal the admission review with response", "admissionReview", admReview)
Expand Down

0 comments on commit c1f6dff

Please sign in to comment.