kubectl/pkg/polymorphichelpers
Patrick Ohly e9f6aad68f client-go testing: support List+Watch with ResourceVersion
Quite a lot of unit tests set up informers with a fake client, do
informerFactory.WaitForCacheSync, then create or modify objects. Such tests
suffered from a race: because the fake client only delivered objects to the
watch after the watch has been created, creating an object too early caused
that object to not get delivered to the informer.

Usually the timing worked out okay because WaitForCacheSync typically slept a
bit while polling, giving the Watch call time to complete, but this race has
also gone wrong occasionally. Now with WaitForCacheSync returning more promptly
without polling (work in progress), the race goes wrong more often.

Instead of working around this in unit tests it's better to improve the fake
client such that List+Watch works reliably, regardless of the timing. The fake
client has traditionally not touched ResourceVersion in stored objects and
doing so now might break unit tests, so the added support for ResourceVersion
is intentionally limited to List+Watch.

The test simulates "real" usage of informers. It runs in a synctest bubble and
completes quickly:

    go  test -v .
    === RUN   TestListAndWatch
        listandwatch_test.go:67: I0101 01:00:00.000000] Listed configMaps="&ConfigMapList{ListMeta:{ 1  <nil>},Items:[]ConfigMap{ConfigMap{ObjectMeta:{cm1  default    0 0001-01-01 00:00:00 +0000 UTC <nil> <nil> map[] map[] [] [] []},Data:map[string]string{},BinaryData:map[string][]byte{},Immutable:nil,},},}" err=null
        listandwatch_test.go:79: I0101 01:00:00.000000] Delaying Watch...
        listandwatch_test.go:90: I0101 01:00:00.100000] Caches synced
        listandwatch_test.go:107: I0101 01:00:00.100000] Created second ConfigMap
        listandwatch_test.go:81: I0101 01:00:00.100000] Continuing Watch...
    --- PASS: TestListAndWatch (0.00s)
    PASS
    ok  	k8s.io/client-go/testing/internal	0.009s

Some users of the fake client need to be updated to avoid test failures:
- ListMeta comparisons have to be updated.
- Optional: pass ListOptions into tracker.Watch. It's optional because
  the implementation behaves as before when options are missing,
  but the List+Watch race fix only works when options are passed.

Kubernetes-commit: 56448506075c3db1d16b5bbf0c581b833a4646f1
2025-12-27 21:57:54 +01:00
..
attachablepodforobject.go Move pkg/kubectl/polymorphichelpers staging 2019-07-27 19:48:15 -07:00
canbeexposed.go Move pkg/kubectl/polymorphichelpers staging 2019-07-27 19:48:15 -07:00
canbeexposed_test.go Move pkg/kubectl/polymorphichelpers staging 2019-07-27 19:48:15 -07:00
helpers.go feat: add all-pods log flag to kubectl 2024-05-07 19:43:46 -04:00
helpers_test.go client-go testing: support List+Watch with ResourceVersion 2025-12-27 21:57:54 +01:00
history.go kubectl: use bytes.Buffer.String 2023-06-04 15:45:24 +02:00
history_test.go describer: improve job and pod template output 2024-01-05 14:29:01 -08:00
historyviewer.go Move pkg/kubectl/polymorphichelpers staging 2019-07-27 19:48:15 -07:00
interface.go feat: add all-pods log flag to kubectl 2024-05-07 19:43:46 -04:00
logsforobject.go feat: add all-pods log flag to kubectl 2024-05-07 19:43:46 -04:00
logsforobject_test.go feat: add all-pods log flag to kubectl 2024-05-07 19:43:46 -04:00
mapbasedselectorforobject.go Move pkg/kubectl/polymorphichelpers staging 2019-07-27 19:48:15 -07:00
mapbasedselectorforobject_test.go Move pkg/kubectl/polymorphichelpers staging 2019-07-27 19:48:15 -07:00
multiprotocolsforobject.go fix: kubectl expose fails for apps with same-port, different-protocol 2023-06-19 09:58:54 +08:00
multiprotocolsforobject_test.go fix: kubectl expose fails for apps with same-port, different-protocol 2023-06-19 09:58:54 +08:00
objectpauser.go Move pkg/kubectl/polymorphichelpers staging 2019-07-27 19:48:15 -07:00
objectpauser_test.go Move pkg/kubectl/polymorphichelpers staging 2019-07-27 19:48:15 -07:00
objectrestarter.go Move pkg/kubectl/polymorphichelpers staging 2019-07-27 19:48:15 -07:00
objectresumer.go Move pkg/kubectl/polymorphichelpers staging 2019-07-27 19:48:15 -07:00
objectresumer_test.go Move pkg/kubectl/polymorphichelpers staging 2019-07-27 19:48:15 -07:00
portsforobject.go fix: kubectl expose fails for apps with same-port, different-protocol 2023-06-19 09:58:54 +08:00
portsforobject_test.go fix: kubectl expose fails for apps with same-port, different-protocol 2023-06-19 09:58:54 +08:00
protocolsforobject.go Move pkg/kubectl/polymorphichelpers staging 2019-07-27 19:48:15 -07:00
protocolsforobject_test.go Move pkg/kubectl/polymorphichelpers staging 2019-07-27 19:48:15 -07:00
rollback.go kubectl:fix namespace missing in kubectl rollout undo 2022-08-11 10:59:09 +08:00
rollback_test.go Drop null creationTimestamp from test fixtures 2025-03-24 09:37:26 -04:00
rollbacker.go Move pkg/kubectl/polymorphichelpers staging 2019-07-27 19:48:15 -07:00
rollout_status.go Fix kubectl conversions 2019-11-17 18:35:55 +01:00
rollout_status_test.go Fix kubectl conversions 2019-11-17 18:35:55 +01:00
statusviewer.go Move pkg/kubectl/polymorphichelpers staging 2019-07-27 19:48:15 -07:00
updatepodspec.go Fix batch/v1 CronJob support in create, describe and polymorphichelpers 2021-03-09 09:44:02 +01:00
updatepodspec_test.go Fix batch/v1 CronJob support in create, describe and polymorphichelpers 2021-03-09 09:44:02 +01:00