From 735a955b18db3ab92414cb515c9ce907ac4db419 Mon Sep 17 00:00:00 2001 From: Dave Protasowski Date: Wed, 24 Jun 2020 10:52:19 -0400 Subject: [PATCH] Return TypeMeta as part of the admission response --- webhook/admission.go | 8 +++++++- webhook/admission_integration_test.go | 6 ++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/webhook/admission.go b/webhook/admission.go index 086f39dab1..46d4c08c4e 100644 --- a/webhook/admission.go +++ b/webhook/admission.go @@ -88,7 +88,13 @@ func admissionHandler(rootLogger *zap.SugaredLogger, stats StatsReporter, c Admi ctx := logging.WithLogger(r.Context(), logger) - var response admissionv1.AdmissionReview + response := admissionv1.AdmissionReview{ + // Use the same type meta as the request - this is required by the K8s API + // note: v1beta1 & v1 AdmissionReview shapes are identical so even though + // we're using v1 types we still support v1beta1 admission requests + TypeMeta: review.TypeMeta, + } + reviewResponse := c.Admit(ctx, review.Request) var patchType string if reviewResponse.PatchType != nil { diff --git a/webhook/admission_integration_test.go b/webhook/admission_integration_test.go index 82be9bb1f6..244124578d 100644 --- a/webhook/admission_integration_test.go +++ b/webhook/admission_integration_test.go @@ -28,6 +28,7 @@ import ( "testing" "time" + "github.com/google/go-cmp/cmp" "golang.org/x/sync/errgroup" jsonpatch "gomodules.xyz/jsonpatch/v2" admissionv1 "k8s.io/api/admission/v1" @@ -156,6 +157,11 @@ func TestAdmissionValidResponseForResource(t *testing.T) { t.Errorf("Failed to decode response: %v", err) return } + + if diff := cmp.Diff(rev.TypeMeta, reviewResponse.TypeMeta); diff != "" { + t.Errorf("expected the response typeMeta to be the same as the request (-want, +got)\n%s", diff) + return + } }() // Check that Admit calls block when they are initiated before informers sync.