From 413edf8776926420bf1dee0c9223855d1b803a5a Mon Sep 17 00:00:00 2001 From: rahulii Date: Fri, 7 Apr 2023 16:03:59 +0530 Subject: [PATCH 1/4] add admissionRequest in ctx --- apis/contexts.go | 17 +++++++++++++++++ apis/contexts_test.go | 19 +++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/apis/contexts.go b/apis/contexts.go index a3550dcebc..fa44e53f3a 100644 --- a/apis/contexts.go +++ b/apis/contexts.go @@ -22,6 +22,7 @@ import ( authenticationv1 "k8s.io/api/authentication/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + admissionv1 "k8s.io/api/admission/v1" ) // This is attached to contexts passed to webhook interfaces when @@ -245,6 +246,7 @@ func IsDryRun(ctx context.Context) bool { // This is attached to contexts passed to webhook interfaces with // additional context from the HTTP request. type httpReq struct{} +type AdmissionReq struct{} // WithHTTPRequest associated the HTTP request object the webhook // received with the context. @@ -260,3 +262,18 @@ func GetHTTPRequest(ctx context.Context) *http.Request { } return v.(*http.Request) } + +// WithAdmissionRequest associated the admissionv1.AdmissionRequest object the webhook +// received with the context. +func WithAdmissionRequest(ctx context.Context, r *admissionv1.AdmissionRequest) context.Context { + return context.WithValue(ctx, AdmissionReq{}, r) +} + +// GetAdmissionRequest fetches the admissionv1.AdmissionRequest received by the webhook. +func GetAdmissionRequest(ctx context.Context) *admissionv1.AdmissionRequest { + v := ctx.Value(AdmissionReq{}) + if v == nil { + return nil + } + return v.(*admissionv1.AdmissionRequest) +} diff --git a/apis/contexts_test.go b/apis/contexts_test.go index 469ada6149..8914f26298 100644 --- a/apis/contexts_test.go +++ b/apis/contexts_test.go @@ -24,6 +24,7 @@ import ( "github.com/google/go-cmp/cmp" authenticationv1 "k8s.io/api/authentication/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + admissionv1 "k8s.io/api/admission/v1" ) func TestContexts(t *testing.T) { @@ -241,3 +242,21 @@ func TestGetHTTPRequest(t *testing.T) { t.Errorf("GetHTTPRequest() = %v, wanted %v", got, want) } } + +func TestGetAdmissionRequest(t *testing.T) { + ctx := context.Background() + + if got := GetAdmissionRequest(ctx); got != nil { + t.Errorf("GetAdmissionRequest() = %v, wanted %v", got, nil) + } + + admReq := admissionv1.AdmissionRequest{ + Name: "foo", + } + ctx = WithAdmissionRequest(ctx, &admReq) + + if want, got := &admReq, GetAdmissionRequest(ctx); got != want { + t.Errorf("GetAdmissionRequest() = %v, wanted %v", got, want) + } +} + From 4872e40038913c860e53380b61c65ab6dc19f3f8 Mon Sep 17 00:00:00 2001 From: rahulii Date: Fri, 7 Apr 2023 16:22:51 +0530 Subject: [PATCH 2/4] run go fmt --- apis/contexts.go | 2 +- apis/contexts_test.go | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/apis/contexts.go b/apis/contexts.go index fa44e53f3a..e04bf554b9 100644 --- a/apis/contexts.go +++ b/apis/contexts.go @@ -20,9 +20,9 @@ import ( "context" "net/http" + admissionv1 "k8s.io/api/admission/v1" authenticationv1 "k8s.io/api/authentication/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - admissionv1 "k8s.io/api/admission/v1" ) // This is attached to contexts passed to webhook interfaces when diff --git a/apis/contexts_test.go b/apis/contexts_test.go index 8914f26298..ae39c97054 100644 --- a/apis/contexts_test.go +++ b/apis/contexts_test.go @@ -22,9 +22,9 @@ import ( "testing" "github.com/google/go-cmp/cmp" + admissionv1 "k8s.io/api/admission/v1" authenticationv1 "k8s.io/api/authentication/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - admissionv1 "k8s.io/api/admission/v1" ) func TestContexts(t *testing.T) { @@ -259,4 +259,3 @@ func TestGetAdmissionRequest(t *testing.T) { t.Errorf("GetAdmissionRequest() = %v, wanted %v", got, want) } } - From b9c79d36ccad86804678cdbed5b594796547e4b3 Mon Sep 17 00:00:00 2001 From: rahulii Date: Fri, 7 Apr 2023 16:24:23 +0530 Subject: [PATCH 3/4] change admission object --- apis/contexts_test.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/apis/contexts_test.go b/apis/contexts_test.go index ae39c97054..4b2a339a80 100644 --- a/apis/contexts_test.go +++ b/apis/contexts_test.go @@ -251,7 +251,12 @@ func TestGetAdmissionRequest(t *testing.T) { } admReq := admissionv1.AdmissionRequest{ - Name: "foo", + Operation: admissionv1.Create, + Kind: metav1.GroupVersionKind{ + Group: "pkg.knative.dev", + Version: "v1alpha1", + Kind: "Resource", + }, } ctx = WithAdmissionRequest(ctx, &admReq) From 4c8ae8b8ba12c7cf500d7345eecf1459ccc740f8 Mon Sep 17 00:00:00 2001 From: rahulii Date: Tue, 11 Apr 2023 14:30:15 +0530 Subject: [PATCH 4/4] address comments --- apis/contexts.go | 6 +++--- webhook/admission.go | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/apis/contexts.go b/apis/contexts.go index e04bf554b9..dea2802733 100644 --- a/apis/contexts.go +++ b/apis/contexts.go @@ -246,7 +246,7 @@ func IsDryRun(ctx context.Context) bool { // This is attached to contexts passed to webhook interfaces with // additional context from the HTTP request. type httpReq struct{} -type AdmissionReq struct{} +type admissionRequest struct{} // WithHTTPRequest associated the HTTP request object the webhook // received with the context. @@ -266,12 +266,12 @@ func GetHTTPRequest(ctx context.Context) *http.Request { // WithAdmissionRequest associated the admissionv1.AdmissionRequest object the webhook // received with the context. func WithAdmissionRequest(ctx context.Context, r *admissionv1.AdmissionRequest) context.Context { - return context.WithValue(ctx, AdmissionReq{}, r) + return context.WithValue(ctx, admissionRequest{}, r) } // GetAdmissionRequest fetches the admissionv1.AdmissionRequest received by the webhook. func GetAdmissionRequest(ctx context.Context) *admissionv1.AdmissionRequest { - v := ctx.Value(AdmissionReq{}) + v := ctx.Value(admissionRequest{}) if v == nil { return nil } diff --git a/webhook/admission.go b/webhook/admission.go index db131b902b..16451fb368 100644 --- a/webhook/admission.go +++ b/webhook/admission.go @@ -111,7 +111,7 @@ func admissionHandler(rootLogger *zap.SugaredLogger, stats StatsReporter, c Admi ) ctx := logging.WithLogger(r.Context(), logger) - ctx = apis.WithHTTPRequest(ctx, r) + ctx = apis.WithAdmissionRequest(ctx, review.Request) response := admissionv1.AdmissionReview{ // Use the same type meta as the request - this is required by the K8s API