Skip to content

Commit dcc5360

Browse files
committed
Deflake timeout admission test
1 parent 205d5c5 commit dcc5360

File tree

1 file changed

+25
-20
lines changed

1 file changed

+25
-20
lines changed

test/integration/apiserver/admissionwebhook/timeout_test.go

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import (
3737
v1 "k8s.io/api/core/v1"
3838
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3939
"k8s.io/apimachinery/pkg/types"
40+
"k8s.io/apimachinery/pkg/util/sets"
4041
"k8s.io/apimachinery/pkg/util/wait"
4142
clientset "k8s.io/client-go/kubernetes"
4243
"k8s.io/client-go/rest"
@@ -146,7 +147,7 @@ func testWebhookTimeout(t *testing.T, watchCache bool) {
146147
t.Fatalf("Failed to build cert with error: %+v", err)
147148
}
148149

149-
recorder := &timeoutRecorder{invocations: []invocation{}}
150+
recorder := &timeoutRecorder{invocations: []invocation{}, markers: sets.NewString()}
150151
webhookServer := httptest.NewUnstartedServer(newTimeoutWebhookHandler(recorder))
151152
webhookServer.TLS = &tls.Config{
152153

@@ -182,7 +183,7 @@ func testWebhookTimeout(t *testing.T, watchCache bool) {
182183

183184
for i, tt := range testCases {
184185
t.Run(tt.name, func(t *testing.T) {
185-
upCh := recorder.Reset()
186+
recorder.Reset()
186187
ns := fmt.Sprintf("reinvoke-%d", i)
187188
_, err = client.CoreV1().Namespaces().Create(context.TODO(), &v1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: ns}}, metav1.CreateOptions{})
188189
if err != nil {
@@ -260,13 +261,16 @@ func testWebhookTimeout(t *testing.T, watchCache bool) {
260261
// wait until new webhook is called the first time
261262
if err := wait.PollImmediate(time.Millisecond*5, wait.ForeverTestTimeout, func() (bool, error) {
262263
_, 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)
268267
return false, nil
269268
}
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
270274
}); err != nil {
271275
t.Fatal(err)
272276
}
@@ -340,28 +344,24 @@ func testWebhookTimeout(t *testing.T, watchCache bool) {
340344

341345
type timeoutRecorder struct {
342346
mu sync.Mutex
343-
upCh chan struct{}
344-
upOnce sync.Once
347+
markers sets.String
345348
invocations []invocation
346349
}
347350

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() {
351353
i.mu.Lock()
352354
defer i.mu.Unlock()
353355
i.invocations = []invocation{}
354-
i.upCh = make(chan struct{})
355-
i.upOnce = sync.Once{}
356-
return i.upCh
356+
i.markers = sets.NewString()
357357
}
358358

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 {
360361
i.mu.Lock()
361362
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)
365365
}
366366

367367
func (i *timeoutRecorder) RecordInvocation(call invocation) {
@@ -423,7 +423,12 @@ func newTimeoutWebhookHandler(recorder *timeoutRecorder) http.Handler {
423423
// When resetting between tests, a marker object is patched until this webhook
424424
// observes it, at which point it is considered ready.
425425
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+
}
427432
allow(w)
428433
return
429434
}

0 commit comments

Comments
 (0)