File tree 2 files changed +36
-0
lines changed
2 files changed +36
-0
lines changed Original file line number Diff line number Diff line change @@ -22,6 +22,7 @@ import (
22
22
23
23
authenticationv1 "k8s.io/api/authentication/v1"
24
24
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
25
+ admissionv1 "k8s.io/api/admission/v1"
25
26
)
26
27
27
28
// This is attached to contexts passed to webhook interfaces when
@@ -245,6 +246,7 @@ func IsDryRun(ctx context.Context) bool {
245
246
// This is attached to contexts passed to webhook interfaces with
246
247
// additional context from the HTTP request.
247
248
type httpReq struct {}
249
+ type AdmissionReq struct {}
248
250
249
251
// WithHTTPRequest associated the HTTP request object the webhook
250
252
// received with the context.
@@ -260,3 +262,18 @@ func GetHTTPRequest(ctx context.Context) *http.Request {
260
262
}
261
263
return v .(* http.Request )
262
264
}
265
+
266
+ // WithAdmissionRequest associated the admissionv1.AdmissionRequest object the webhook
267
+ // received with the context.
268
+ func WithAdmissionRequest (ctx context.Context , r * admissionv1.AdmissionRequest ) context.Context {
269
+ return context .WithValue (ctx , AdmissionReq {}, r )
270
+ }
271
+
272
+ // GetAdmissionRequest fetches the admissionv1.AdmissionRequest received by the webhook.
273
+ func GetAdmissionRequest (ctx context.Context ) * admissionv1.AdmissionRequest {
274
+ v := ctx .Value (AdmissionReq {})
275
+ if v == nil {
276
+ return nil
277
+ }
278
+ return v .(* admissionv1.AdmissionRequest )
279
+ }
Original file line number Diff line number Diff line change @@ -24,6 +24,7 @@ import (
24
24
"github.com/google/go-cmp/cmp"
25
25
authenticationv1 "k8s.io/api/authentication/v1"
26
26
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
27
+ admissionv1 "k8s.io/api/admission/v1"
27
28
)
28
29
29
30
func TestContexts (t * testing.T ) {
@@ -241,3 +242,21 @@ func TestGetHTTPRequest(t *testing.T) {
241
242
t .Errorf ("GetHTTPRequest() = %v, wanted %v" , got , want )
242
243
}
243
244
}
245
+
246
+ func TestGetAdmissionRequest (t * testing.T ) {
247
+ ctx := context .Background ()
248
+
249
+ if got := GetAdmissionRequest (ctx ); got != nil {
250
+ t .Errorf ("GetAdmissionRequest() = %v, wanted %v" , got , nil )
251
+ }
252
+
253
+ admReq := admissionv1.AdmissionRequest {
254
+ Name : "foo" ,
255
+ }
256
+ ctx = WithAdmissionRequest (ctx , & admReq )
257
+
258
+ if want , got := & admReq , GetAdmissionRequest (ctx ); got != want {
259
+ t .Errorf ("GetAdmissionRequest() = %v, wanted %v" , got , want )
260
+ }
261
+ }
262
+
You can’t perform that action at this time.
0 commit comments