@@ -20,18 +20,23 @@ import (
2020 "context"
2121 "errors"
2222 "fmt"
23+ "io"
2324 "net/http"
2425 "net/http/httptest"
2526 "os"
2627 "strings"
2728
29+ "github.com/go-logr/logr"
2830 . "github.com/onsi/ginkgo/v2"
2931 . "github.com/onsi/gomega"
32+ "github.com/onsi/gomega/gbytes"
3033 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3134 "k8s.io/apimachinery/pkg/runtime"
3235 "k8s.io/apimachinery/pkg/runtime/schema"
3336
3437 "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"
3540 "sigs.k8s.io/controller-runtime/pkg/manager"
3641 "sigs.k8s.io/controller-runtime/pkg/scheme"
3742 "sigs.k8s.io/controller-runtime/pkg/webhook/admission"
@@ -49,11 +54,17 @@ var _ = Describe("webhook", func() {
4954})
5055
5156func runTests (admissionReviewVersion string ) {
52- var stop chan struct {}
57+ var (
58+ stop chan struct {}
59+ logBuffer * gbytes.Buffer
60+ testingLogger logr.Logger
61+ )
5362
5463 BeforeEach (func () {
5564 stop = make (chan struct {})
5665 newController = controller .New
66+ logBuffer = gbytes .NewBuffer ()
67+ testingLogger = zap .New (zap .JSONEncoder (), zap .WriteTo (io .MultiWriter (logBuffer , GinkgoWriter )))
5768 })
5869
5970 AfterEach (func () {
@@ -214,6 +225,9 @@ func runTests(admissionReviewVersion string) {
214225 err = WebhookManagedBy (m ).
215226 WithDefaulter (& TestCustomDefaulter {}).
216227 For (& TestDefaulter {}).
228+ WithLogConstructor (func (base logr.Logger , req * admission.Request ) logr.Logger {
229+ return admission .DefaultLogConstructor (testingLogger , req )
230+ }).
217231 Complete ()
218232 ExpectWithOffset (1 , err ).NotTo (HaveOccurred ())
219233 svr := m .GetWebhookServer ()
@@ -225,16 +239,17 @@ func runTests(admissionReviewVersion string) {
225239 "request":{
226240 "uid":"07e52e8d-4513-11e9-a716-42010a800270",
227241 "kind":{
228- "group":"",
242+ "group":"foo.test.org ",
229243 "version":"v1",
230244 "kind":"TestDefaulter"
231245 },
232246 "resource":{
233- "group":"",
247+ "group":"foo.test.org ",
234248 "version":"v1",
235249 "resource":"testdefaulter"
236250 },
237251 "namespace":"default",
252+ "name":"foo",
238253 "operation":"CREATE",
239254 "object":{
240255 "replica":1
@@ -263,6 +278,7 @@ func runTests(admissionReviewVersion string) {
263278 ExpectWithOffset (1 , w .Body ).To (ContainSubstring (`"allowed":true` ))
264279 ExpectWithOffset (1 , w .Body ).To (ContainSubstring (`"patch":` ))
265280 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"` ))
266282
267283 By ("sending a request to a validating webhook path that doesn't exist" )
268284 path = generateValidatePath (testDefaulterGVK )
@@ -431,6 +447,9 @@ func runTests(admissionReviewVersion string) {
431447 err = WebhookManagedBy (m ).
432448 WithValidator (& TestCustomValidator {}).
433449 For (& TestValidator {}).
450+ WithLogConstructor (func (base logr.Logger , req * admission.Request ) logr.Logger {
451+ return admission .DefaultLogConstructor (testingLogger , req )
452+ }).
434453 Complete ()
435454 ExpectWithOffset (1 , err ).NotTo (HaveOccurred ())
436455 svr := m .GetWebhookServer ()
@@ -442,16 +461,17 @@ func runTests(admissionReviewVersion string) {
442461 "request":{
443462 "uid":"07e52e8d-4513-11e9-a716-42010a800270",
444463 "kind":{
445- "group":"",
464+ "group":"foo.test.org ",
446465 "version":"v1",
447- "kind":"TestValidator "
466+ "kind":"TestDefaulter "
448467 },
449468 "resource":{
450- "group":"",
469+ "group":"foo.test.org ",
451470 "version":"v1",
452- "resource":"testvalidator "
471+ "resource":"testdefaulter "
453472 },
454473 "namespace":"default",
474+ "name":"foo",
455475 "operation":"UPDATE",
456476 "object":{
457477 "replica":1
@@ -491,6 +511,7 @@ func runTests(admissionReviewVersion string) {
491511 By ("sanity checking the response contains reasonable field" )
492512 ExpectWithOffset (1 , w .Body ).To (ContainSubstring (`"allowed":false` ))
493513 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"` ))
494515 })
495516
496517 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 {
845866type TestCustomDefaulter struct {}
846867
847868func (* TestCustomDefaulter ) Default (ctx context.Context , obj runtime.Object ) error {
869+ logf .FromContext (ctx ).Info ("Defaulting object" )
848870 req , err := admission .RequestFromContext (ctx )
849871 if err != nil {
850872 return fmt .Errorf ("expected admission.Request in ctx: %w" , err )
@@ -867,6 +889,7 @@ var _ admission.CustomDefaulter = &TestCustomDefaulter{}
867889type TestCustomValidator struct {}
868890
869891func (* TestCustomValidator ) ValidateCreate (ctx context.Context , obj runtime.Object ) error {
892+ logf .FromContext (ctx ).Info ("Validating object" )
870893 req , err := admission .RequestFromContext (ctx )
871894 if err != nil {
872895 return fmt .Errorf ("expected admission.Request in ctx: %w" , err )
@@ -883,6 +906,7 @@ func (*TestCustomValidator) ValidateCreate(ctx context.Context, obj runtime.Obje
883906}
884907
885908func (* TestCustomValidator ) ValidateUpdate (ctx context.Context , oldObj , newObj runtime.Object ) error {
909+ logf .FromContext (ctx ).Info ("Validating object" )
886910 req , err := admission .RequestFromContext (ctx )
887911 if err != nil {
888912 return fmt .Errorf ("expected admission.Request in ctx: %w" , err )
@@ -903,6 +927,7 @@ func (*TestCustomValidator) ValidateUpdate(ctx context.Context, oldObj, newObj r
903927}
904928
905929func (* TestCustomValidator ) ValidateDelete (ctx context.Context , obj runtime.Object ) error {
930+ logf .FromContext (ctx ).Info ("Validating object" )
906931 req , err := admission .RequestFromContext (ctx )
907932 if err != nil {
908933 return fmt .Errorf ("expected admission.Request in ctx: %w" , err )
0 commit comments