Skip to content

Commit

Permalink
Set TypeMeta in AdmissionReview/v1 (#58)
Browse files Browse the repository at this point in the history
* return StatusBadRequest when body cannot be decoded

* set TypeMeta in AdmissionReview

* fixup! return StatusBadRequest when body cannot be decoded
  • Loading branch information
jizi authored Jun 1, 2023
1 parent 3690130 commit c79db1a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
7 changes: 5 additions & 2 deletions pkg/inject/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,12 +262,15 @@ func (wh *Webhook) serveInject(w http.ResponseWriter, r *http.Request) {
ar := v1.AdmissionReview{}
if _, _, err := deserializer.Decode(body, nil, &ar); err != nil {
log.Errorf("Could not decode body: %v", err)
reviewResponse = toAdmissionResponse(err)
http.Error(w, "could not decode body", http.StatusBadRequest)
return
} else {
reviewResponse = wh.inject(&ar)
}

response := v1.AdmissionReview{}
response := v1.AdmissionReview{
TypeMeta: ar.TypeMeta,
}
if reviewResponse != nil {
response.Response = reviewResponse
if ar.Request != nil {
Expand Down
12 changes: 11 additions & 1 deletion pkg/inject/hook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,11 @@ func TestInjectRequired(t *testing.T) {
}
}

var reviewTypeMeta = metav1.TypeMeta{
Kind: "AdmissionReview",
APIVersion: "admission.k8s.io/v1",
}

func makeTestData(t testing.TB, skip bool) []byte {
pod := corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Expand All @@ -213,6 +218,7 @@ func makeTestData(t testing.TB, skip bool) []byte {
}

review := v1.AdmissionReview{
TypeMeta: reviewTypeMeta,
Request: &v1.AdmissionRequest{
Kind: metav1.GroupVersionKind{},
Object: runtime.RawExtension{
Expand Down Expand Up @@ -402,7 +408,7 @@ func TestRunAndServe(t *testing.T) {
body: []byte{0, 1, 2, 3, 4, 5}, // random data
contentType: "application/json",
wantAllowed: false,
wantStatusCode: http.StatusOK,
wantStatusCode: http.StatusBadRequest,
},
{
name: "missing body",
Expand Down Expand Up @@ -437,6 +443,10 @@ func TestRunAndServe(t *testing.T) {
if err := json.Unmarshal(gotBody, &gotReview); err != nil {
t.Fatalf("could not decode response body: %v", err)
}
if gotReview.TypeMeta.String() != reviewTypeMeta.String() {
t.Fatalf("AdmissionReview.TypeMeta is wrong : got %s want %s",
gotReview.TypeMeta.String(), reviewTypeMeta.String())
}
if gotReview.Response.Allowed != c.wantAllowed {
t.Fatalf("AdmissionReview.Response.Allowed is wrong : got %v want %v",
gotReview.Response.Allowed, c.wantAllowed)
Expand Down

0 comments on commit c79db1a

Please sign in to comment.