Commit Graph

347 Commits

Author SHA1 Message Date
Patrick Ohly
4e28a08da3 client-go: call out WithContext inconsistency
This raises awareness so that developers hopefully get it right without having to
learn about it from compile errors. It also explains that creating a PR to
fix the naming is not desired.

Kubernetes-commit: f40892f4813f29fc305d59073d2464a1fd7d6fe5
2025-05-08 14:29:21 +02:00
Huan Yan
115024100c cleanup: remove error comment of AddIndexers func
Kubernetes-commit: a271d3e5339fa60b661b316a0e2f378f3afc51af
2025-03-16 23:39:33 +08:00
Karl Isenberg
921628ecd6 chore: Add VeryShortWatchError typed error
- Add a new VeryShortWatchError struct for error matching, returned
  by `handleAnyWatch`, up through `Reflector.ListAndWatch`.
- Update test expectations to match exact errors.

Kubernetes-commit: 6eff9db0f10db72f2c64390e106a80621d136439
2025-04-15 11:48:57 -07:00
Karl Isenberg
e9ce1dd558 fix: Watcher deadlock from Stop not being called
Change:
- refactor Reflector.ListAndWatch and Reflector.watch to always call
  watcher.Stop.
- refactor Reflector.handleAnyWatch to always call watcher.Stop,
  EXCEPT when exitOnWatchListBookmarkReceived &&
  watchListBookmarkReceived.
- Update unit tests with these new expectations.

Effect:
- ensures watcher.Stop is always called at least once
- avoids deadlocks in watcher implementations when watcher.Watch is
  called, but watcher.Stop is never called.

Note: It's impossible to guarantee that Stop will only be called once.
So watch.Interface implementations must tollerate Stop being called
multiple times.

Kubernetes-commit: 3e609ecf6e945bf4562bddfc563fde9a4c3d0d90
2025-04-11 14:30:29 -07:00
Jordan Liggitt
a3cad0dc91 Clean up leaked goroutines in cache unit tests
Kubernetes-commit: 6747bf7a9cb3009aae4cba1e1d249d56a66a981b
2025-04-01 13:59:32 -04:00
Antonio Ojea
21dc3b4441 benchmark to show inefficient linear search lookup
goos: linux
goarch: amd64
pkg: k8s.io/client-go/tools/cache
cpu: Intel(R) Xeon(R) CPU @ 2.60GHz
BenchmarkLister_Match_1k_100
BenchmarkLister_Match_1k_100-48      	   41910	     28255 ns/op	   16384 B/op	       1 allocs/op
BenchmarkLister_Match_10k_100
BenchmarkLister_Match_10k_100-48     	    3487	    337728 ns/op	  163848 B/op	       1 allocs/op
BenchmarkLister_Match_100k_100
BenchmarkLister_Match_100k_100-48    	     222	   7040793 ns/op	 1605659 B/op	       1 allocs/op
BenchmarkLister_Match_1M_100
BenchmarkLister_Match_1M_100-48      	      12	  97962328 ns/op	16007172 B/op	       1 allocs/op
PASS
ok  	k8s.io/client-go/tools/cache	10.480s

Kubernetes-commit: 33fbce73ae203ffeb1b9ea63ac43d567d1bdb1ad
2025-03-21 06:09:23 +00:00
Huan Yan
4ba25ca711 fix: shared informer typos
Kubernetes-commit: b38bfaa2ac99deb28eb8c70fcd9466492b292a01
2025-03-11 20:00:16 +08:00
Joe Betz
bcd36428c5 Revert PR 125102: Add unit tests to client-go/tools/cache/listers.go
Kubernetes-commit: 37ddc637f55af547862ac41452ade42ed15b778b
2025-03-12 11:43:15 -04:00
0xMALVEE
8410e24e31 testes written for cache/listers.go
Kubernetes-commit: 8e8770961616369ac47149c69e7dc29562578d51
2024-05-23 23:40:18 +06:00
Tim Hockin
4a6072d89f Use randfill, do API renames
Kubernetes-commit: e54719bb6674fac228671e0786d19c2cf27b08a3
2025-02-20 09:45:22 -08:00
Patrick Ohly
bad1caabde client-go + apimachinery watch: context support
The Lister and Watcher interfaces only supported methods without context, but
were typically implemented with client-go API calls which need a context. New
interfaces get added using the same approach as in
https://github.com/kubernetes/kubernetes/pull/129109.

Kubernetes-commit: 6688adae142e37114d9dfa8d94cd1d8a91fbcc13
2024-12-20 13:55:47 +01:00
David Eads
f2d9cfb8c8 switch to using the real FIFO
Kubernetes-commit: a9aab298b4738f4ea9111131cdf193a3b1ba14e5
2025-01-10 16:23:23 -05:00
David Eads
43bf1a1b0a make a real FIFO implementation
Kubernetes-commit: 24b43ea96ec60ca2c574ccc6b51c989488ca3210
2025-01-10 16:23:07 -05:00
David Eads
52af3bdc0f Remove cache.ErrRequeue
cache.ErrRequeue advertised itself as a way to requeue failures on a
FIFO, but it suffers the same problems as AddIfNotPresent.  If we do
requeue an item at the end, we'll move the informer back in time.  If we
requeue at the beginning we'll simply wedge FIFO.

We didn't find examples in the wild, but by removing the error type
those impacted will get a compile error and get to decide what action is
most appropriate for their failure.  Most of the time, proceeding to the
next item is best.

Kubernetes-commit: 238c32a1d9b2c72d648193fa8642a53a2884975f
2025-01-20 13:59:43 -05:00
David Eads
d853ccf18c Remove Queue.AddIfNotPresent
Logically a cache.Queue.AddIfNotPresent means that the informer can move
back in time since an older item is placed after newer items.  The
alternative of placing errors at the head of the queue leads to
indefinite memory growth and repeated failures on retry.

Luckily this behavior was behind RetryOnError, which was always set to
false and impossible for normal users to set to true.  By removing the
function and setting, impacted users (none found in a github search)
will get a compile failure.

Kubernetes-commit: 8e77ac000131019d5aa49c19aa1f477f6dac4d59
2025-01-20 13:54:32 -05:00
David Eads
f29637f7f2 shrink the cache.Queue interface to what is actually used
Kubernetes-commit: 0ddab1694579d523e3865c75ca44d6cdf1b0ef93
2025-01-08 15:31:47 -05:00
Patrick Ohly
40cace856c client-go/tools/cache: fix TestAddWhileActive
4638ba971661497b147906b8977ae206c9dd6e44 added tracking of the goroutine which
executes informer.Run. In the TestAddWhileActive the original `go
informer.Run()` was left in place, causing a data race between the two
`informer.Run` instances:

==================
WARNING: DATA RACE
Read at 0x00c000262398 by goroutine 5302:
  k8s.io/client-go/tools/cache.(*controller).RunWithContext()
      /home/prow/go/src/k8s.io/kubernetes/staging/src/k8s.io/client-go/tools/cache/controller.go:162 +0x1ad
  k8s.io/client-go/tools/cache.(*sharedIndexInformer).RunWithContext()
      /home/prow/go/src/k8s.io/kubernetes/staging/src/k8s.io/client-go/tools/cache/shared_informer.go:584 +0x6c5
  k8s.io/client-go/tools/cache.(*sharedIndexInformer).Run()
      /home/prow/go/src/k8s.io/kubernetes/staging/src/k8s.io/client-go/tools/cache/shared_informer.go:527 +0x48
  k8s.io/client-go/tools/cache.TestAddWhileActive.gowrap1()
      /home/prow/go/src/k8s.io/kubernetes/staging/src/k8s.io/client-go/tools/cache/shared_informer_test.go:1080 +0x17

Previous write at 0x00c000262398 by goroutine 5301:
  k8s.io/client-go/tools/cache.New()
      /home/prow/go/src/k8s.io/kubernetes/staging/src/k8s.io/client-go/tools/cache/controller.go:142 +0x9de
  k8s.io/client-go/tools/cache.(*sharedIndexInformer).RunWithContext.func1()
      /home/prow/go/src/k8s.io/kubernetes/staging/src/k8s.io/client-go/tools/cache/shared_informer.go:562 +0xa78
  k8s.io/client-go/tools/cache.(*sharedIndexInformer).RunWithContext()
      /home/prow/go/src/k8s.io/kubernetes/staging/src/k8s.io/client-go/tools/cache/shared_informer.go:565 +0x119
  k8s.io/client-go/tools/cache.(*sharedIndexInformer).Run()
      /home/prow/go/src/k8s.io/kubernetes/staging/src/k8s.io/client-go/tools/cache/shared_informer.go:527 +0x44
  k8s.io/client-go/tools/cache.(*sharedIndexInformer).Run-fm()
      <autogenerated>:1 +0x17
  k8s.io/client-go/tools/cache.TestAddWhileActive.(*Group).StartWithChannel.func2()
      /home/prow/go/src/k8s.io/kubernetes/staging/src/k8s.io/apimachinery/pkg/util/wait/wait.go:55 +0x38
  k8s.io/apimachinery/pkg/util/wait.(*Group).Start.func1()
      /home/prow/go/src/k8s.io/kubernetes/staging/src/k8s.io/apimachinery/pkg/util/wait/wait.go:72 +0x86

Goroutine 5302 (running) created at:
  k8s.io/client-go/tools/cache.TestAddWhileActive()
      /home/prow/go/src/k8s.io/kubernetes/staging/src/k8s.io/client-go/tools/cache/shared_informer_test.go:1080 +0x93e
  testing.tRunner()
      /usr/local/go/src/testing/testing.go:1690 +0x226
  testing.(*T).Run.gowrap1()
      /usr/local/go/src/testing/testing.go:1743 +0x44

Goroutine 5301 (running) created at:
  k8s.io/apimachinery/pkg/util/wait.(*Group).Start()
      /home/prow/go/src/k8s.io/kubernetes/staging/src/k8s.io/apimachinery/pkg/util/wait/wait.go:70 +0xe4
  k8s.io/apimachinery/pkg/util/wait.(*Group).StartWithChannel()
      /home/prow/go/src/k8s.io/kubernetes/staging/src/k8s.io/apimachinery/pkg/util/wait/wait.go:54 +0x7e6
  k8s.io/client-go/tools/cache.TestAddWhileActive()
      /home/prow/go/src/k8s.io/kubernetes/staging/src/k8s.io/client-go/tools/cache/shared_informer_test.go:1074 +0x6a1
  testing.tRunner()
      /usr/local/go/src/testing/testing.go:1690 +0x226
  testing.(*T).Run.gowrap1()
      /usr/local/go/src/testing/testing.go:1743 +0x44
==================

Kubernetes-commit: d66ced5730fa60c04b0a39df58a156b7045585f6
2025-01-14 14:06:31 +01:00
Patrick Ohly
498e3efe74 client-go cache: fix TestHammerController
The test relied on a 100ms sleep to ensure that controller was done.  If that
race was lost, one goroutine was intentionally prevented from completing by
locking a mutex permanently. A TODO was left about detecting that.

Adding goroutine leak checking in
https://github.com/kubernetes/kubernetes/pull/126387 revealed that this race
indeed sometimes is lost because the goroutine
leaked (https://github.com/kubernetes/kubernetes/issues/129400).

Waiting for controller shutdown instead of relying on timing should fix this.

Kubernetes-commit: 8e1403563a60f3b7a258e3bbb64b5c3a7f6548fb
2024-12-29 12:18:50 +01:00
Patrick Ohly
5d289bc44c client-go/tools/cache: add APIs with context parameter
The context is used for cancellation and to support contextual logging.

In most cases, alternative *WithContext APIs get added, except for
NewIntegerResourceVersionMutationCache where code searches indicate that the
API is not used downstream.

An API break around SharedInformer couldn't be avoided because the
alternative (keeping the interface unchanged and adding a second one with
the new method) would have been worse. controller-runtime needs to be updated
because it implements that interface in a test package. Downstream consumers of
controller-runtime will work unless they use those test package.

Converting Kubernetes to use the other new alternatives will follow. In the
meantime, usage of the new alternatives cannot be enforced via logcheck
yet (see https://github.com/kubernetes/kubernetes/issues/126379 for the
process).

Passing context through and checking it for cancellation is tricky for event
handlers. A better approach is to map the context cancellation to the normal
removal of an event handler via a helper goroutine. Thanks to the new
HandleErrorWithLogr and HandleCrashWithLogr, remembering the logger is
sufficient for handling problems at runtime.

Kubernetes-commit: 4638ba971661497b147906b8977ae206c9dd6e44
2024-07-26 15:26:00 +02:00
Patrick Ohly
b836a27b07 client-go/tools/cache: goroutine leak checking
Several tests leaked goroutines. All of those get fixed where possible
without API changes. Goleak is used to prevent regressions.

One new test specifically covers shutdown of an informer and its event
handlers.

Kubernetes-commit: 0ba43734b4c8998b4aaeb1fa2bec8dee609fa50a
2024-11-28 17:59:36 +01:00
Patrick Ohly
70a4950f35 remove import doc comments
The "// import <path>" comment has been superseded by Go modules.
We don't have to remove them, but doing so has some advantages:

- They are used inconsistently, which is confusing.
- We can then also remove the (currently broken) hack/update-vanity-imports.sh.
- Last but not least, it would be a first step towards avoiding the k8s.io domain.

This commit was generated with
   sed -i -e 's;^package \(.*\) // import.*;package \1;' $(git grep -l '^package.*// import' | grep -v 'vendor/')

Everything was included, except for
   package labels // import k8s.io/kubernetes/pkg/util/labels
because that package is marked as "read-only".

Kubernetes-commit: 8a908e0c0bd96a3455edf7e3b5f5af90564e65b0
2024-12-02 14:43:58 +01:00
Abu Kashem
e132ac21c9 handle watch for unsafe delete
Kubernetes-commit: 25efc8f2d136a9574166be02789ac727c5b4a3fd
2024-11-05 20:36:56 -05:00
Alexander Stephan
ce42c297f4 Make getter names for reflector fields idiomatic
Kubernetes-commit: fdabcb5141094cb99afde24ec1df15026b4ba349
2024-10-13 20:53:25 +00:00
Alexander Stephan
146d03f559 Export name and typeDescription members of reflector struct
Kubernetes-commit: 154b8aa8806d743c41d1acf0b498bdecf1767872
2024-09-26 15:09:59 +00:00
Andy Goldstein
da3ab4e935 Move ncdc to emeritus
I am moving myself to emeritus as I am now firmly on the end-user side
of things.

Signed-off-by: Andy Goldstein <andy.goldstein@gmail.com>

Kubernetes-commit: 3ab816dcabf37acda33c665ab5aa85f1f6163bc1
2024-08-17 13:00:07 -04:00
Matthieu MOREL
acc5917341 fix: enable empty and len rules from testifylint on pkg package
Signed-off-by: Matthieu MOREL <matthieu.morel35@gmail.com>

Co-authored-by: Patrick Ohly <patrick.ohly@intel.com>

Kubernetes-commit: f014b754fb5925dfbca6e27a44d0c3968b157e14
2024-06-28 21:20:13 +02:00
Karl Isenberg
48d8fc7e2e Add details to watch interface method comments
The watch.Interface design is hard to change, because it would break
most client-go users that perform watches. So instead of changing the
interface to be more user friendly, this change updates the method
comments to explain the different responsibilities of the consumer
(client user) and the producer (interface implementer).

Kubernetes-commit: 1f35231a1d4f7b8586a7ec589c799729eeb4f7c4
2024-06-12 13:06:22 -07:00
Karl Isenberg
f29a36dfab Refactor Reflector ListAndWatch
- Extract watchWithResync to simplify ListAndWatch
- Wrap watchHandler with two variants, one for WatchList and one for
  just Watch.
- Replace a bool pointer arg with a bool arg and bool return, to
  improve readability.
- Use errors.Is to satisfy the linter
- Use %w to wrap the store.Replace error, to allow unwrapping.

Kubernetes-commit: 65fc1bb463c85a4c85e619bf7acac9503e23a253
2024-06-12 13:14:55 -07:00
Lukasz Szaszkiewicz
5347b09f1d client-go/reflector: remove reflector_data_consistency_detector_test.go
Kubernetes-commit: 0c96a00217d6e2a4c9cb1cf6c0bc719d570678fc
2024-06-11 10:42:07 +02:00
Lukasz Szaszkiewicz
b681e77bec client-go/reflector: use consistencydetector.IsDataConsistencyDetectionForWatchListEnabled
Kubernetes-commit: f6c68908ba37dbb7af602b8f88b5f395025d2384
2024-06-10 23:01:04 +02:00
Wojciech Tyczyński
b444e6c32e Implement ResilientWatchCacheInitialization
Kubernetes-commit: a8ef6e9f0104a44023162bb8229fb677ec80beb1
2024-04-29 14:19:46 +02:00
Karl Isenberg
8c9cb8838f Improve Reflector unit tests
- Add tests to confirm that Stop is always called.
- Add TODOs to show were Stop is not currently being called
  (to fix in a future PR)

Kubernetes-commit: ab5aa4762fd5206d0dbd8412d7c6f3b76533a122
2024-06-03 12:15:38 -07:00
Lukasz Szaszkiewicz
538b7809aa move checkWatchListDataConsistencyIfRequested back to client-go/tools/cache
Kubernetes-commit: cb44f83b3d500bb2ed29f7634095bc64c9b21729
2024-05-27 11:22:28 +02:00
Lukasz Szaszkiewicz
e428fc295c move client-go/tools/cache/reflector_data_consistency_detector to client-go/util/consistencydetector
Kubernetes-commit: 272dfc9d7e61481dfcdc4d6a021385d9cd85ba5f
2024-05-27 11:10:43 +02:00
Lukasz Szaszkiewicz
00036b79c4 client-go/tools/cache/reflector_data_consistency_detector: refactor unit tests
Kubernetes-commit: 18837d60ae12ac70380ce5cf21cfd8e0d221263a
2024-05-27 11:10:00 +02:00
Lukasz Szaszkiewicz
6bdde7723e client-go/consistency-detector: change the signature of checkWatchListConsistencyIfRequested
the signature of the method was tightly connected to the reflector,
making it difficult to use for anything other than a reflector.

this simple refactor makes the method more generic.

Kubernetes-commit: 83c7542abc8c542c01ecb67376f134b2071c5304
2024-04-22 14:01:22 +02:00
Lukasz Szaszkiewicz
4a34196022 replace ENABLE_CLIENT_GO_WATCH_LIST_ALPHA with WatchListClient gate
Kubernetes-commit: 9248cccc27fdd52a9a99fd9ad6e47ada9ee8b568
2024-04-30 08:24:53 +02:00
Lukasz Szaszkiewicz
0280901a4d client-go/reflector: warns when the bookmark event for initial events hasn't been received
Kubernetes-commit: 93960f489069a744afda1be42f82349e25d7e4d7
2024-04-29 15:18:54 +02:00
Wojciech Tyczyński
2fe05741c1 Fix race in informer transformers
Kubernetes-commit: e9f74597a8e7104b26640614e952d4453654453b
2024-04-17 11:19:08 +02:00
Stephen Kitt
841e997e33 Improve the lister function documentation
In particular, document that ListAllByNamespace delegates to ListAll
if no namespace is specified.

Signed-off-by: Stephen Kitt <skitt@redhat.com>

Kubernetes-commit: 54e899317ef46e3b70827cacee244717022db0ad
2024-04-19 17:58:02 +02:00
Wojciech Tyczyński
b9e952f4d7 Allow for configuring MinWatchTimeout in Reflector and Informer.
Kubernetes-commit: 29e38c19b853b6cc3950541b1727395acf5eb4d3
2024-04-09 09:12:54 +02:00
Wojciech Tyczyński
7da319745b Refactor informer constructors
Kubernetes-commit: 4da185a601e1f657a873dfd7e51efcc8a3b94c37
2024-04-09 09:06:38 +02:00
Lan Liang
fa185a21db cleanup: delete rand.Seed(time.Now().UnixNano()) and using global number generator.
see https://tip.golang.org/doc/go1.20

Signed-off-by: Lan Liang <gcslyp@gmail.com>

Kubernetes-commit: dc992adad385ab631e4a528ee6a342ea71e7a379
2024-03-18 08:10:12 +00:00
Lukasz Szaszkiewicz
45e17fede0 client-go/cache/reflector: use metav1.InitialEventsAnnotationKey
Kubernetes-commit: a953539fb57b2ee18337f323ef15425a4d6207ed
2024-03-11 12:56:40 +01:00
Nilekh Chaudhari
02f21344ac feat: implements svm controller
Signed-off-by: Nilekh Chaudhari <1626598+nilekhc@users.noreply.github.com>

Kubernetes-commit: 9161302e7fd3a5fb055b2f2572c6e1228240bb51
2024-01-04 19:34:05 +00:00
Mike Spreitzer
61be9f118e Add DeletionHandlingObjectToName
Signed-off-by: Mike Spreitzer <mspreitz@us.ibm.com>

Kubernetes-commit: d60a25b2db52d7e180b0962e23cc06e71f36fb29
2024-01-26 13:28:06 -05:00
Lukasz Szaszkiewicz
202c415847 client-go/reflector: make UseWatchList a pointer
until #115478(use streaming against the etcd storage)
is resolved the cacher need a way to disable the streaming.

Kubernetes-commit: 41e706600aea7468f486150d951d3b8948ce89d5
2024-01-19 13:48:29 +01:00
John Howard
785e19661f client-go: allow adding indexes after informer starts
Kubernetes-commit: d96a9858d396d7f418d24ea47bdc92ef8429f707
2023-03-31 15:57:18 -07:00
tao.yang
0447e1f9ce cleanup: omit comparison with bool constants
Signed-off-by: tao.yang <tao.yang@daocloud.io>

Kubernetes-commit: b35357b6c08f21ba0fd312536051394c2567ec79
2023-09-04 16:59:23 +08:00
Lukasz Szaszkiewicz
0f984dc7fc client-go/reflector: introduce a data consistency mechanism for the watch-list feature.
checkWatchListConsistencyIfRequested performs a data consistency check only when
the KUBE_WATCHLIST_INCONSISTENCY_DETECTOR environment variable was set during a binary startup.

The consistency check is meant to be enforced only in the CI, not in production.
The check ensures that data retrieved by the watch-list api call
is exactly the same as data received by the standard list api call.

Note that this function will panic when data inconsistency is detected.
This is intentional because we want to catch it in the CI.

Kubernetes-commit: b31e7793d0d873a71c90caf8455556aa905cf88d
2023-10-06 14:26:47 +02:00