Skip to content

Commit 07bcf93

Browse files
authored
Merge pull request kubernetes#131321 from karlkfi/karl-very-fast-watch
chore: Add VeryShortWatchError typed error
2 parents 74e84db + 6eff9db commit 07bcf93

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

staging/src/k8s.io/client-go/tools/cache/reflector.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -960,7 +960,7 @@ loop:
960960

961961
watchDuration := clock.Since(start)
962962
if watchDuration < 1*time.Second && eventCount == 0 {
963-
return watchListBookmarkReceived, fmt.Errorf("very short watch: %s: Unexpected watch close - watch lasted less than a second and no items received", name)
963+
return watchListBookmarkReceived, &VeryShortWatchError{Name: name}
964964
}
965965
klog.FromContext(ctx).V(4).Info("Watch close", "reflector", name, "type", expectedTypeName, "totalItems", eventCount)
966966
return watchListBookmarkReceived, nil
@@ -1162,3 +1162,16 @@ type noopTicker struct{}
11621162
func (t *noopTicker) C() <-chan time.Time { return nil }
11631163

11641164
func (t *noopTicker) Stop() {}
1165+
1166+
// VeryShortWatchError is returned when the watch result channel is closed
1167+
// within one second, without having sent any events.
1168+
type VeryShortWatchError struct {
1169+
// Name of the Reflector
1170+
Name string
1171+
}
1172+
1173+
// Error implements the error interface
1174+
func (e *VeryShortWatchError) Error() string {
1175+
return fmt.Sprintf("very short watch: %s: Unexpected watch close - "+
1176+
"watch lasted less than a second and no items received", e.Name)
1177+
}

staging/src/k8s.io/client-go/tools/cache/reflector_test.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -291,8 +291,7 @@ func TestReflectorHandleWatchResultChanClosedBefore(t *testing.T) {
291291
// Simulate the result channel being closed by the producer before handleWatch is called.
292292
close(resultCh)
293293
err := handleWatch(ctx, time.Now(), fw, s, g.expectedType, g.expectedGVK, g.name, g.typeDescription, g.setLastSyncResourceVersion, g.clock, nevererrc)
294-
// TODO(karlkfi): Add exact error type for "very short watch"
295-
require.Error(t, err)
294+
require.Equal(t, &VeryShortWatchError{Name: g.name}, err)
296295
// Ensure handleWatch calls ResultChan and Stop
297296
assert.Equal(t, []string{"ResultChan", "Stop"}, calls)
298297
}
@@ -323,8 +322,7 @@ func TestReflectorHandleWatchResultChanClosedAfter(t *testing.T) {
323322
},
324323
}
325324
err := handleWatch(ctx, time.Now(), fw, s, g.expectedType, g.expectedGVK, g.name, g.typeDescription, g.setLastSyncResourceVersion, g.clock, nevererrc)
326-
// TODO(karlkfi): Add exact error type for "very short watch"
327-
require.Error(t, err)
325+
require.Equal(t, &VeryShortWatchError{Name: g.name}, err)
328326
// Ensure handleWatch calls ResultChan and Stop
329327
assert.Equal(t, []string{"ResultChan", "Stop"}, calls)
330328
}

0 commit comments

Comments
 (0)