@@ -37,6 +37,7 @@ import (
37
37
v1 "k8s.io/api/core/v1"
38
38
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
39
39
"k8s.io/apimachinery/pkg/types"
40
+ "k8s.io/apimachinery/pkg/util/sets"
40
41
"k8s.io/apimachinery/pkg/util/wait"
41
42
clientset "k8s.io/client-go/kubernetes"
42
43
"k8s.io/client-go/rest"
@@ -146,7 +147,7 @@ func testWebhookTimeout(t *testing.T, watchCache bool) {
146
147
t .Fatalf ("Failed to build cert with error: %+v" , err )
147
148
}
148
149
149
- recorder := & timeoutRecorder {invocations : []invocation {}}
150
+ recorder := & timeoutRecorder {invocations : []invocation {}, markers : sets . NewString () }
150
151
webhookServer := httptest .NewUnstartedServer (newTimeoutWebhookHandler (recorder ))
151
152
webhookServer .TLS = & tls.Config {
152
153
@@ -182,7 +183,7 @@ func testWebhookTimeout(t *testing.T, watchCache bool) {
182
183
183
184
for i , tt := range testCases {
184
185
t .Run (tt .name , func (t * testing.T ) {
185
- upCh := recorder .Reset ()
186
+ recorder .Reset ()
186
187
ns := fmt .Sprintf ("reinvoke-%d" , i )
187
188
_ , err = client .CoreV1 ().Namespaces ().Create (context .TODO (), & v1.Namespace {ObjectMeta : metav1.ObjectMeta {Name : ns }}, metav1.CreateOptions {})
188
189
if err != nil {
@@ -260,13 +261,16 @@ func testWebhookTimeout(t *testing.T, watchCache bool) {
260
261
// wait until new webhook is called the first time
261
262
if err := wait .PollImmediate (time .Millisecond * 5 , wait .ForeverTestTimeout , func () (bool , error ) {
262
263
_ , err = client .CoreV1 ().Pods ("default" ).Patch (context .TODO (), timeoutMarkerFixture .Name , types .JSONPatchType , []byte ("[]" ), metav1.PatchOptions {})
263
- select {
264
- case <- upCh :
265
- return true , nil
266
- default :
267
- t .Logf ("Waiting for webhook to become effective, getting marker object: %v" , err )
264
+ received := recorder .MarkerReceived ()
265
+ if len (tt .mutatingWebhooks ) > 0 && ! received .Has ("mutating" ) {
266
+ t .Logf ("Waiting for mutating webhooks to become effective, getting marker object: %v" , err )
268
267
return false , nil
269
268
}
269
+ if len (tt .validatingWebhooks ) > 0 && ! received .Has ("validating" ) {
270
+ t .Logf ("Waiting for validating webhooks to become effective, getting marker object: %v" , err )
271
+ return false , nil
272
+ }
273
+ return true , nil
270
274
}); err != nil {
271
275
t .Fatal (err )
272
276
}
@@ -340,28 +344,24 @@ func testWebhookTimeout(t *testing.T, watchCache bool) {
340
344
341
345
type timeoutRecorder struct {
342
346
mu sync.Mutex
343
- upCh chan struct {}
344
- upOnce sync.Once
347
+ markers sets.String
345
348
invocations []invocation
346
349
}
347
350
348
- // Reset zeros out all counts and returns a channel that is closed when the first admission of the
349
- // marker object is received.
350
- func (i * timeoutRecorder ) Reset () chan struct {} {
351
+ // Reset zeros out all counts
352
+ func (i * timeoutRecorder ) Reset () {
351
353
i .mu .Lock ()
352
354
defer i .mu .Unlock ()
353
355
i .invocations = []invocation {}
354
- i .upCh = make (chan struct {})
355
- i .upOnce = sync.Once {}
356
- return i .upCh
356
+ i .markers = sets .NewString ()
357
357
}
358
358
359
- func (i * timeoutRecorder ) MarkerReceived () {
359
+ // MarkerReceived records the specified markers were received and returns the set of received markers
360
+ func (i * timeoutRecorder ) MarkerReceived (markers ... string ) sets.String {
360
361
i .mu .Lock ()
361
362
defer i .mu .Unlock ()
362
- i .upOnce .Do (func () {
363
- close (i .upCh )
364
- })
363
+ i .markers .Insert (markers ... )
364
+ return i .markers .Union (nil )
365
365
}
366
366
367
367
func (i * timeoutRecorder ) RecordInvocation (call invocation ) {
@@ -423,7 +423,12 @@ func newTimeoutWebhookHandler(recorder *timeoutRecorder) http.Handler {
423
423
// When resetting between tests, a marker object is patched until this webhook
424
424
// observes it, at which point it is considered ready.
425
425
if pod .Namespace == timeoutMarkerFixture .Namespace && pod .Name == timeoutMarkerFixture .Name {
426
- recorder .MarkerReceived ()
426
+ if strings .HasPrefix (r .URL .Path , "/mutating/" ) {
427
+ recorder .MarkerReceived ("mutating" )
428
+ }
429
+ if strings .HasPrefix (r .URL .Path , "/validating/" ) {
430
+ recorder .MarkerReceived ("validating" )
431
+ }
427
432
allow (w )
428
433
return
429
434
}
0 commit comments