listwatch.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /*
  2. Copyright 2015 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package cache
  14. import (
  15. "context"
  16. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  17. "k8s.io/apimachinery/pkg/fields"
  18. "k8s.io/apimachinery/pkg/runtime"
  19. "k8s.io/apimachinery/pkg/watch"
  20. restclient "k8s.io/client-go/rest"
  21. "k8s.io/client-go/util/watchlist"
  22. )
  23. // Lister is any object that knows how to perform an initial list.
  24. //
  25. // Ideally, all implementations of Lister should also implement ListerWithContext.
  26. type Lister interface {
  27. // List should return a list type object; the Items field will be extracted, and the
  28. // ResourceVersion field will be used to start the watch in the right place.
  29. //
  30. // Deprecated: use ListerWithContext.ListWithContext instead.
  31. List(options metav1.ListOptions) (runtime.Object, error)
  32. }
  33. // ListerWithContext is any object that knows how to perform an initial list.
  34. type ListerWithContext interface {
  35. // ListWithContext should return a list type object; the Items field will be extracted, and the
  36. // ResourceVersion field will be used to start the watch in the right place.
  37. ListWithContext(ctx context.Context, options metav1.ListOptions) (runtime.Object, error)
  38. }
  39. func ToListerWithContext(l Lister) ListerWithContext {
  40. if l, ok := l.(ListerWithContext); ok {
  41. return l
  42. }
  43. return listerWrapper{
  44. parent: l,
  45. }
  46. }
  47. type listerWrapper struct {
  48. parent Lister
  49. }
  50. func (l listerWrapper) ListWithContext(ctx context.Context, options metav1.ListOptions) (runtime.Object, error) {
  51. return l.parent.List(options)
  52. }
  53. // Watcher is any object that knows how to start a watch on a resource.
  54. //
  55. // Ideally, all implementations of Watcher should also implement WatcherWithContext.
  56. type Watcher interface {
  57. // Watch should begin a watch at the specified version.
  58. //
  59. // If Watch returns an error, it should handle its own cleanup, including
  60. // but not limited to calling Stop() on the watch, if one was constructed.
  61. // This allows the caller to ignore the watch, if the error is non-nil.
  62. //
  63. // Deprecated: use WatcherWithContext.WatchWithContext instead.
  64. Watch(options metav1.ListOptions) (watch.Interface, error)
  65. }
  66. // WatcherWithContext is any object that knows how to start a watch on a resource.
  67. type WatcherWithContext interface {
  68. // WatchWithContext should begin a watch at the specified version.
  69. //
  70. // If Watch returns an error, it should handle its own cleanup, including
  71. // but not limited to calling Stop() on the watch, if one was constructed.
  72. // This allows the caller to ignore the watch, if the error is non-nil.
  73. WatchWithContext(ctx context.Context, options metav1.ListOptions) (watch.Interface, error)
  74. }
  75. func ToWatcherWithContext(w Watcher) WatcherWithContext {
  76. if w, ok := w.(WatcherWithContext); ok {
  77. return w
  78. }
  79. return watcherWrapper{
  80. parent: w,
  81. }
  82. }
  83. type watcherWrapper struct {
  84. parent Watcher
  85. }
  86. func (l watcherWrapper) WatchWithContext(ctx context.Context, options metav1.ListOptions) (watch.Interface, error) {
  87. return l.parent.Watch(options)
  88. }
  89. // ListerWatcher is any object that knows how to perform an initial list and start a watch on a resource.
  90. //
  91. // Ideally, all implementations of ListerWatcher should also implement ListerWatcherWithContext.
  92. type ListerWatcher interface {
  93. Lister
  94. Watcher
  95. }
  96. // ListerWatcherWithContext is any object that knows how to perform an initial list and start a watch on a resource.
  97. type ListerWatcherWithContext interface {
  98. ListerWithContext
  99. WatcherWithContext
  100. }
  101. func ToListerWatcherWithContext(lw ListerWatcher) ListerWatcherWithContext {
  102. if lw, ok := lw.(ListerWatcherWithContext); ok {
  103. return lw
  104. }
  105. return listerWatcherWrapper{
  106. ListerWithContext: ToListerWithContext(lw),
  107. WatcherWithContext: ToWatcherWithContext(lw),
  108. }
  109. }
  110. type listerWatcherWrapper struct {
  111. ListerWithContext
  112. WatcherWithContext
  113. }
  114. type listWatcherWithWatchListSemanticsWrapper struct {
  115. *ListWatch
  116. // unsupportedWatchListSemantics indicates whether a client explicitly does NOT support
  117. // WatchList semantics.
  118. //
  119. // Over the years, unit tests in kube have been written in many different ways.
  120. // After enabling the WatchListClient feature by default, existing tests started failing.
  121. // To avoid breaking lots of existing client-go users after upgrade,
  122. // we introduced this field as an opt-in.
  123. //
  124. // When true, the reflector disables WatchList even if the feature gate is enabled.
  125. unsupportedWatchListSemantics bool
  126. }
  127. func (lw *listWatcherWithWatchListSemanticsWrapper) IsWatchListSemanticsUnSupported() bool {
  128. return lw.unsupportedWatchListSemantics
  129. }
  130. // ToListWatcherWithWatchListSemantics returns a ListerWatcher
  131. // that knows whether the provided client explicitly
  132. // does NOT support the WatchList semantics. This allows Reflectors
  133. // to adapt their behavior based on client capabilities.
  134. func ToListWatcherWithWatchListSemantics(lw *ListWatch, client any) ListerWatcher {
  135. return &listWatcherWithWatchListSemanticsWrapper{
  136. lw,
  137. watchlist.DoesClientNotSupportWatchListSemantics(client),
  138. }
  139. }
  140. // ListFunc knows how to list resources
  141. //
  142. // Deprecated: use ListWithContextFunc instead.
  143. type ListFunc func(options metav1.ListOptions) (runtime.Object, error)
  144. // ListWithContextFunc knows how to list resources
  145. type ListWithContextFunc func(ctx context.Context, options metav1.ListOptions) (runtime.Object, error)
  146. // WatchFunc knows how to watch resources
  147. //
  148. // Deprecated: use WatchFuncWithContext instead.
  149. type WatchFunc func(options metav1.ListOptions) (watch.Interface, error)
  150. // WatchFuncWithContext knows how to watch resources
  151. type WatchFuncWithContext func(ctx context.Context, options metav1.ListOptions) (watch.Interface, error)
  152. // ListWatch knows how to list and watch a set of apiserver resources.
  153. // It satisfies the ListerWatcher and ListerWatcherWithContext interfaces.
  154. // It is a convenience function for users of NewReflector, etc.
  155. // ListFunc or ListWithContextFunc must be set. Same for WatchFunc and WatchFuncWithContext.
  156. // ListWithContextFunc and WatchFuncWithContext are preferred if
  157. // a context is available, otherwise ListFunc and WatchFunc.
  158. //
  159. // Beware of the inconsistent naming of the two WithContext methods.
  160. // This was unintentional, but fixing it now would force the ecosystem
  161. // to go through a breaking Go API change and was deemed not worth it.
  162. //
  163. // NewFilteredListWatchFromClient sets all of the functions to ensure that callers
  164. // which only know about ListFunc and WatchFunc continue to work.
  165. type ListWatch struct {
  166. // Deprecated: use ListWithContext instead.
  167. ListFunc ListFunc
  168. // Deprecated: use WatchWithContext instead.
  169. WatchFunc WatchFunc
  170. ListWithContextFunc ListWithContextFunc
  171. WatchFuncWithContext WatchFuncWithContext
  172. // DisableChunking requests no chunking for this list watcher.
  173. DisableChunking bool
  174. }
  175. var (
  176. _ ListerWatcher = &ListWatch{}
  177. _ ListerWatcherWithContext = &ListWatch{}
  178. )
  179. // Getter interface knows how to access Get method from RESTClient.
  180. type Getter interface {
  181. Get() *restclient.Request
  182. }
  183. // NewListWatchFromClient creates a new ListWatch from the specified client, resource, namespace and field selector.
  184. // For backward compatibility, all function fields are populated.
  185. func NewListWatchFromClient(c Getter, resource string, namespace string, fieldSelector fields.Selector) *ListWatch {
  186. optionsModifier := func(options *metav1.ListOptions) {
  187. options.FieldSelector = fieldSelector.String()
  188. }
  189. return NewFilteredListWatchFromClient(c, resource, namespace, optionsModifier)
  190. }
  191. // NewFilteredListWatchFromClient creates a new ListWatch from the specified client, resource, namespace, and option modifier.
  192. // Option modifier is a function takes a ListOptions and modifies the consumed ListOptions. Provide customized modifier function
  193. // to apply modification to ListOptions with a field selector, a label selector, or any other desired options.
  194. // For backward compatibility, all function fields are populated.
  195. func NewFilteredListWatchFromClient(c Getter, resource string, namespace string, optionsModifier func(options *metav1.ListOptions)) *ListWatch {
  196. listFunc := func(options metav1.ListOptions) (runtime.Object, error) {
  197. optionsModifier(&options)
  198. return c.Get().
  199. Namespace(namespace).
  200. Resource(resource).
  201. VersionedParams(&options, metav1.ParameterCodec).
  202. Do(context.Background()).
  203. Get()
  204. }
  205. watchFunc := func(options metav1.ListOptions) (watch.Interface, error) {
  206. options.Watch = true
  207. optionsModifier(&options)
  208. return c.Get().
  209. Namespace(namespace).
  210. Resource(resource).
  211. VersionedParams(&options, metav1.ParameterCodec).
  212. Watch(context.Background())
  213. }
  214. listFuncWithContext := func(ctx context.Context, options metav1.ListOptions) (runtime.Object, error) {
  215. optionsModifier(&options)
  216. return c.Get().
  217. Namespace(namespace).
  218. Resource(resource).
  219. VersionedParams(&options, metav1.ParameterCodec).
  220. Do(ctx).
  221. Get()
  222. }
  223. watchFuncWithContext := func(ctx context.Context, options metav1.ListOptions) (watch.Interface, error) {
  224. options.Watch = true
  225. optionsModifier(&options)
  226. return c.Get().
  227. Namespace(namespace).
  228. Resource(resource).
  229. VersionedParams(&options, metav1.ParameterCodec).
  230. Watch(ctx)
  231. }
  232. return &ListWatch{
  233. ListFunc: listFunc,
  234. WatchFunc: watchFunc,
  235. ListWithContextFunc: listFuncWithContext,
  236. WatchFuncWithContext: watchFuncWithContext,
  237. }
  238. }
  239. // List a set of apiserver resources
  240. //
  241. // Deprecated: use ListWatchWithContext.ListWithContext instead.
  242. func (lw *ListWatch) List(options metav1.ListOptions) (runtime.Object, error) {
  243. // ListWatch is used in Reflector, which already supports pagination.
  244. // Don't paginate here to avoid duplication.
  245. if lw.ListFunc != nil {
  246. return lw.ListFunc(options)
  247. }
  248. return lw.ListWithContextFunc(context.Background(), options)
  249. }
  250. // List a set of apiserver resources
  251. func (lw *ListWatch) ListWithContext(ctx context.Context, options metav1.ListOptions) (runtime.Object, error) {
  252. // ListWatch is used in Reflector, which already supports pagination.
  253. // Don't paginate here to avoid duplication.
  254. if lw.ListWithContextFunc != nil {
  255. return lw.ListWithContextFunc(ctx, options)
  256. }
  257. return lw.ListFunc(options)
  258. }
  259. // Watch a set of apiserver resources
  260. //
  261. // Deprecated: use ListWatchWithContext.WatchWithContext instead.
  262. func (lw *ListWatch) Watch(options metav1.ListOptions) (watch.Interface, error) {
  263. if lw.WatchFunc != nil {
  264. return lw.WatchFunc(options)
  265. }
  266. return lw.WatchFuncWithContext(context.Background(), options)
  267. }
  268. // Watch a set of apiserver resources
  269. func (lw *ListWatch) WatchWithContext(ctx context.Context, options metav1.ListOptions) (watch.Interface, error) {
  270. if lw.WatchFuncWithContext != nil {
  271. return lw.WatchFuncWithContext(ctx, options)
  272. }
  273. return lw.WatchFunc(options)
  274. }