@@ -20,18 +20,23 @@ import (
20
20
"context"
21
21
"errors"
22
22
"fmt"
23
+ "io"
23
24
"net/http"
24
25
"net/http/httptest"
25
26
"os"
26
27
"strings"
27
28
29
+ "github.com/go-logr/logr"
28
30
. "github.com/onsi/ginkgo/v2"
29
31
. "github.com/onsi/gomega"
32
+ "github.com/onsi/gomega/gbytes"
30
33
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
31
34
"k8s.io/apimachinery/pkg/runtime"
32
35
"k8s.io/apimachinery/pkg/runtime/schema"
33
36
34
37
"sigs.k8s.io/controller-runtime/pkg/controller"
38
+ logf "sigs.k8s.io/controller-runtime/pkg/log"
39
+ "sigs.k8s.io/controller-runtime/pkg/log/zap"
35
40
"sigs.k8s.io/controller-runtime/pkg/manager"
36
41
"sigs.k8s.io/controller-runtime/pkg/scheme"
37
42
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
@@ -49,11 +54,17 @@ var _ = Describe("webhook", func() {
49
54
})
50
55
51
56
func runTests (admissionReviewVersion string ) {
52
- var stop chan struct {}
57
+ var (
58
+ stop chan struct {}
59
+ logBuffer * gbytes.Buffer
60
+ testingLogger logr.Logger
61
+ )
53
62
54
63
BeforeEach (func () {
55
64
stop = make (chan struct {})
56
65
newController = controller .New
66
+ logBuffer = gbytes .NewBuffer ()
67
+ testingLogger = zap .New (zap .JSONEncoder (), zap .WriteTo (io .MultiWriter (logBuffer , GinkgoWriter )))
57
68
})
58
69
59
70
AfterEach (func () {
@@ -214,6 +225,9 @@ func runTests(admissionReviewVersion string) {
214
225
err = WebhookManagedBy (m ).
215
226
WithDefaulter (& TestCustomDefaulter {}).
216
227
For (& TestDefaulter {}).
228
+ WithLogConstructor (func (base logr.Logger , req * admission.Request ) logr.Logger {
229
+ return admission .DefaultLogConstructor (testingLogger , req )
230
+ }).
217
231
Complete ()
218
232
ExpectWithOffset (1 , err ).NotTo (HaveOccurred ())
219
233
svr := m .GetWebhookServer ()
@@ -225,16 +239,17 @@ func runTests(admissionReviewVersion string) {
225
239
"request":{
226
240
"uid":"07e52e8d-4513-11e9-a716-42010a800270",
227
241
"kind":{
228
- "group":"",
242
+ "group":"foo.test.org ",
229
243
"version":"v1",
230
244
"kind":"TestDefaulter"
231
245
},
232
246
"resource":{
233
- "group":"",
247
+ "group":"foo.test.org ",
234
248
"version":"v1",
235
249
"resource":"testdefaulter"
236
250
},
237
251
"namespace":"default",
252
+ "name":"foo",
238
253
"operation":"CREATE",
239
254
"object":{
240
255
"replica":1
@@ -263,6 +278,7 @@ func runTests(admissionReviewVersion string) {
263
278
ExpectWithOffset (1 , w .Body ).To (ContainSubstring (`"allowed":true` ))
264
279
ExpectWithOffset (1 , w .Body ).To (ContainSubstring (`"patch":` ))
265
280
ExpectWithOffset (1 , w .Body ).To (ContainSubstring (`"code":200` ))
281
+ EventuallyWithOffset (1 , logBuffer ).Should (gbytes .Say (`"msg":"Defaulting object","object":{"name":"foo","namespace":"default"},"namespace":"default","name":"foo","resource":{"group":"foo.test.org","version":"v1","resource":"testdefaulter"},"user":"","requestID":"07e52e8d-4513-11e9-a716-42010a800270"` ))
266
282
267
283
By ("sending a request to a validating webhook path that doesn't exist" )
268
284
path = generateValidatePath (testDefaulterGVK )
@@ -431,6 +447,9 @@ func runTests(admissionReviewVersion string) {
431
447
err = WebhookManagedBy (m ).
432
448
WithValidator (& TestCustomValidator {}).
433
449
For (& TestValidator {}).
450
+ WithLogConstructor (func (base logr.Logger , req * admission.Request ) logr.Logger {
451
+ return admission .DefaultLogConstructor (testingLogger , req )
452
+ }).
434
453
Complete ()
435
454
ExpectWithOffset (1 , err ).NotTo (HaveOccurred ())
436
455
svr := m .GetWebhookServer ()
@@ -442,16 +461,17 @@ func runTests(admissionReviewVersion string) {
442
461
"request":{
443
462
"uid":"07e52e8d-4513-11e9-a716-42010a800270",
444
463
"kind":{
445
- "group":"",
464
+ "group":"foo.test.org ",
446
465
"version":"v1",
447
- "kind":"TestValidator "
466
+ "kind":"TestDefaulter "
448
467
},
449
468
"resource":{
450
- "group":"",
469
+ "group":"foo.test.org ",
451
470
"version":"v1",
452
- "resource":"testvalidator "
471
+ "resource":"testdefaulter "
453
472
},
454
473
"namespace":"default",
474
+ "name":"foo",
455
475
"operation":"UPDATE",
456
476
"object":{
457
477
"replica":1
@@ -491,6 +511,7 @@ func runTests(admissionReviewVersion string) {
491
511
By ("sanity checking the response contains reasonable field" )
492
512
ExpectWithOffset (1 , w .Body ).To (ContainSubstring (`"allowed":false` ))
493
513
ExpectWithOffset (1 , w .Body ).To (ContainSubstring (`"code":403` ))
514
+ EventuallyWithOffset (1 , logBuffer ).Should (gbytes .Say (`"msg":"Validating object","object":{"name":"foo","namespace":"default"},"namespace":"default","name":"foo","resource":{"group":"foo.test.org","version":"v1","resource":"testdefaulter"},"user":"","requestID":"07e52e8d-4513-11e9-a716-42010a800270"` ))
494
515
})
495
516
496
517
It ("should scaffold defaulting and validating webhooks if the type implements both Defaulter and Validator interfaces" , func () {
@@ -845,6 +866,7 @@ func (dv *TestDefaultValidator) ValidateDelete() error {
845
866
type TestCustomDefaulter struct {}
846
867
847
868
func (* TestCustomDefaulter ) Default (ctx context.Context , obj runtime.Object ) error {
869
+ logf .FromContext (ctx ).Info ("Defaulting object" )
848
870
req , err := admission .RequestFromContext (ctx )
849
871
if err != nil {
850
872
return fmt .Errorf ("expected admission.Request in ctx: %w" , err )
@@ -867,6 +889,7 @@ var _ admission.CustomDefaulter = &TestCustomDefaulter{}
867
889
type TestCustomValidator struct {}
868
890
869
891
func (* TestCustomValidator ) ValidateCreate (ctx context.Context , obj runtime.Object ) error {
892
+ logf .FromContext (ctx ).Info ("Validating object" )
870
893
req , err := admission .RequestFromContext (ctx )
871
894
if err != nil {
872
895
return fmt .Errorf ("expected admission.Request in ctx: %w" , err )
@@ -883,6 +906,7 @@ func (*TestCustomValidator) ValidateCreate(ctx context.Context, obj runtime.Obje
883
906
}
884
907
885
908
func (* TestCustomValidator ) ValidateUpdate (ctx context.Context , oldObj , newObj runtime.Object ) error {
909
+ logf .FromContext (ctx ).Info ("Validating object" )
886
910
req , err := admission .RequestFromContext (ctx )
887
911
if err != nil {
888
912
return fmt .Errorf ("expected admission.Request in ctx: %w" , err )
@@ -903,6 +927,7 @@ func (*TestCustomValidator) ValidateUpdate(ctx context.Context, oldObj, newObj r
903
927
}
904
928
905
929
func (* TestCustomValidator ) ValidateDelete (ctx context.Context , obj runtime.Object ) error {
930
+ logf .FromContext (ctx ).Info ("Validating object" )
906
931
req , err := admission .RequestFromContext (ctx )
907
932
if err != nil {
908
933
return fmt .Errorf ("expected admission.Request in ctx: %w" , err )
0 commit comments