controller.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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. "sync"
  16. "time"
  17. "k8s.io/apimachinery/pkg/runtime"
  18. "k8s.io/apimachinery/pkg/util/clock"
  19. utilruntime "k8s.io/apimachinery/pkg/util/runtime"
  20. "k8s.io/apimachinery/pkg/util/wait"
  21. )
  22. // This file implements a low-level controller that is used in
  23. // sharedIndexInformer, which is an implementation of
  24. // SharedIndexInformer. Such informers, in turn, are key components
  25. // in the high level controllers that form the backbone of the
  26. // Kubernetes control plane. Look at those for examples, or the
  27. // example in
  28. // https://github.com/kubernetes/client-go/tree/master/examples/workqueue
  29. // .
  30. // Config contains all the settings for one of these low-level controllers.
  31. type Config struct {
  32. // The queue for your objects - has to be a DeltaFIFO due to
  33. // assumptions in the implementation. Your Process() function
  34. // should accept the output of this Queue's Pop() method.
  35. Queue
  36. // Something that can list and watch your objects.
  37. ListerWatcher
  38. // Something that can process a popped Deltas.
  39. Process ProcessFunc
  40. // ObjectType is an example object of the type this controller is
  41. // expected to handle. Only the type needs to be right, except
  42. // that when that is `unstructured.Unstructured` the object's
  43. // `"apiVersion"` and `"kind"` must also be right.
  44. ObjectType runtime.Object
  45. // FullResyncPeriod is the period at which ShouldResync is considered.
  46. FullResyncPeriod time.Duration
  47. // ShouldResync is periodically used by the reflector to determine
  48. // whether to Resync the Queue. If ShouldResync is `nil` or
  49. // returns true, it means the reflector should proceed with the
  50. // resync.
  51. ShouldResync ShouldResyncFunc
  52. // If true, when Process() returns an error, re-enqueue the object.
  53. // TODO: add interface to let you inject a delay/backoff or drop
  54. // the object completely if desired. Pass the object in
  55. // question to this interface as a parameter. This is probably moot
  56. // now that this functionality appears at a higher level.
  57. RetryOnError bool
  58. // Called whenever the ListAndWatch drops the connection with an error.
  59. WatchErrorHandler WatchErrorHandler
  60. // WatchListPageSize is the requested chunk size of initial and relist watch lists.
  61. WatchListPageSize int64
  62. }
  63. // ShouldResyncFunc is a type of function that indicates if a reflector should perform a
  64. // resync or not. It can be used by a shared informer to support multiple event handlers with custom
  65. // resync periods.
  66. type ShouldResyncFunc func() bool
  67. // ProcessFunc processes a single object.
  68. type ProcessFunc func(obj interface{}) error
  69. // `*controller` implements Controller
  70. type controller struct {
  71. config Config
  72. reflector *Reflector
  73. reflectorMutex sync.RWMutex
  74. clock clock.Clock
  75. }
  76. // Controller is a low-level controller that is parameterized by a
  77. // Config and used in sharedIndexInformer.
  78. type Controller interface {
  79. // Run does two things. One is to construct and run a Reflector
  80. // to pump objects/notifications from the Config's ListerWatcher
  81. // to the Config's Queue and possibly invoke the occasional Resync
  82. // on that Queue. The other is to repeatedly Pop from the Queue
  83. // and process with the Config's ProcessFunc. Both of these
  84. // continue until `stopCh` is closed.
  85. Run(stopCh <-chan struct{})
  86. // HasSynced delegates to the Config's Queue
  87. HasSynced() bool
  88. // LastSyncResourceVersion delegates to the Reflector when there
  89. // is one, otherwise returns the empty string
  90. LastSyncResourceVersion() string
  91. }
  92. // New makes a new Controller from the given Config.
  93. func New(c *Config) Controller {
  94. ctlr := &controller{
  95. config: *c,
  96. clock: &clock.RealClock{},
  97. }
  98. return ctlr
  99. }
  100. // Run begins processing items, and will continue until a value is sent down stopCh or it is closed.
  101. // It's an error to call Run more than once.
  102. // Run blocks; call via go.
  103. func (c *controller) Run(stopCh <-chan struct{}) {
  104. defer utilruntime.HandleCrash()
  105. go func() {
  106. <-stopCh
  107. c.config.Queue.Close()
  108. }()
  109. r := NewReflector(
  110. c.config.ListerWatcher,
  111. c.config.ObjectType,
  112. c.config.Queue,
  113. c.config.FullResyncPeriod,
  114. )
  115. r.ShouldResync = c.config.ShouldResync
  116. r.WatchListPageSize = c.config.WatchListPageSize
  117. r.clock = c.clock
  118. if c.config.WatchErrorHandler != nil {
  119. r.watchErrorHandler = c.config.WatchErrorHandler
  120. }
  121. c.reflectorMutex.Lock()
  122. c.reflector = r
  123. c.reflectorMutex.Unlock()
  124. var wg wait.Group
  125. wg.StartWithChannel(stopCh, r.Run)
  126. wait.Until(c.processLoop, time.Second, stopCh)
  127. wg.Wait()
  128. }
  129. // Returns true once this controller has completed an initial resource listing
  130. func (c *controller) HasSynced() bool {
  131. return c.config.Queue.HasSynced()
  132. }
  133. func (c *controller) LastSyncResourceVersion() string {
  134. c.reflectorMutex.RLock()
  135. defer c.reflectorMutex.RUnlock()
  136. if c.reflector == nil {
  137. return ""
  138. }
  139. return c.reflector.LastSyncResourceVersion()
  140. }
  141. // processLoop drains the work queue.
  142. // TODO: Consider doing the processing in parallel. This will require a little thought
  143. // to make sure that we don't end up processing the same object multiple times
  144. // concurrently.
  145. //
  146. // TODO: Plumb through the stopCh here (and down to the queue) so that this can
  147. // actually exit when the controller is stopped. Or just give up on this stuff
  148. // ever being stoppable. Converting this whole package to use Context would
  149. // also be helpful.
  150. func (c *controller) processLoop() {
  151. for {
  152. obj, err := c.config.Queue.Pop(PopProcessFunc(c.config.Process))
  153. if err != nil {
  154. if err == ErrFIFOClosed {
  155. return
  156. }
  157. if c.config.RetryOnError {
  158. // This is the safe way to re-enqueue.
  159. c.config.Queue.AddIfNotPresent(obj)
  160. }
  161. }
  162. }
  163. }
  164. // ResourceEventHandler can handle notifications for events that
  165. // happen to a resource. The events are informational only, so you
  166. // can't return an error. The handlers MUST NOT modify the objects
  167. // received; this concerns not only the top level of structure but all
  168. // the data structures reachable from it.
  169. // * OnAdd is called when an object is added.
  170. // * OnUpdate is called when an object is modified. Note that oldObj is the
  171. // last known state of the object-- it is possible that several changes
  172. // were combined together, so you can't use this to see every single
  173. // change. OnUpdate is also called when a re-list happens, and it will
  174. // get called even if nothing changed. This is useful for periodically
  175. // evaluating or syncing something.
  176. // * OnDelete will get the final state of the item if it is known, otherwise
  177. // it will get an object of type DeletedFinalStateUnknown. This can
  178. // happen if the watch is closed and misses the delete event and we don't
  179. // notice the deletion until the subsequent re-list.
  180. type ResourceEventHandler interface {
  181. OnAdd(obj interface{})
  182. OnUpdate(oldObj, newObj interface{})
  183. OnDelete(obj interface{})
  184. }
  185. // ResourceEventHandlerFuncs is an adaptor to let you easily specify as many or
  186. // as few of the notification functions as you want while still implementing
  187. // ResourceEventHandler. This adapter does not remove the prohibition against
  188. // modifying the objects.
  189. type ResourceEventHandlerFuncs struct {
  190. AddFunc func(obj interface{})
  191. UpdateFunc func(oldObj, newObj interface{})
  192. DeleteFunc func(obj interface{})
  193. }
  194. // OnAdd calls AddFunc if it's not nil.
  195. func (r ResourceEventHandlerFuncs) OnAdd(obj interface{}) {
  196. if r.AddFunc != nil {
  197. r.AddFunc(obj)
  198. }
  199. }
  200. // OnUpdate calls UpdateFunc if it's not nil.
  201. func (r ResourceEventHandlerFuncs) OnUpdate(oldObj, newObj interface{}) {
  202. if r.UpdateFunc != nil {
  203. r.UpdateFunc(oldObj, newObj)
  204. }
  205. }
  206. // OnDelete calls DeleteFunc if it's not nil.
  207. func (r ResourceEventHandlerFuncs) OnDelete(obj interface{}) {
  208. if r.DeleteFunc != nil {
  209. r.DeleteFunc(obj)
  210. }
  211. }
  212. // FilteringResourceEventHandler applies the provided filter to all events coming
  213. // in, ensuring the appropriate nested handler method is invoked. An object
  214. // that starts passing the filter after an update is considered an add, and an
  215. // object that stops passing the filter after an update is considered a delete.
  216. // Like the handlers, the filter MUST NOT modify the objects it is given.
  217. type FilteringResourceEventHandler struct {
  218. FilterFunc func(obj interface{}) bool
  219. Handler ResourceEventHandler
  220. }
  221. // OnAdd calls the nested handler only if the filter succeeds
  222. func (r FilteringResourceEventHandler) OnAdd(obj interface{}) {
  223. if !r.FilterFunc(obj) {
  224. return
  225. }
  226. r.Handler.OnAdd(obj)
  227. }
  228. // OnUpdate ensures the proper handler is called depending on whether the filter matches
  229. func (r FilteringResourceEventHandler) OnUpdate(oldObj, newObj interface{}) {
  230. newer := r.FilterFunc(newObj)
  231. older := r.FilterFunc(oldObj)
  232. switch {
  233. case newer && older:
  234. r.Handler.OnUpdate(oldObj, newObj)
  235. case newer && !older:
  236. r.Handler.OnAdd(newObj)
  237. case !newer && older:
  238. r.Handler.OnDelete(oldObj)
  239. default:
  240. // do nothing
  241. }
  242. }
  243. // OnDelete calls the nested handler only if the filter succeeds
  244. func (r FilteringResourceEventHandler) OnDelete(obj interface{}) {
  245. if !r.FilterFunc(obj) {
  246. return
  247. }
  248. r.Handler.OnDelete(obj)
  249. }
  250. // DeletionHandlingMetaNamespaceKeyFunc checks for
  251. // DeletedFinalStateUnknown objects before calling
  252. // MetaNamespaceKeyFunc.
  253. func DeletionHandlingMetaNamespaceKeyFunc(obj interface{}) (string, error) {
  254. if d, ok := obj.(DeletedFinalStateUnknown); ok {
  255. return d.Key, nil
  256. }
  257. return MetaNamespaceKeyFunc(obj)
  258. }
  259. // NewInformer returns a Store and a controller for populating the store
  260. // while also providing event notifications. You should only used the returned
  261. // Store for Get/List operations; Add/Modify/Deletes will cause the event
  262. // notifications to be faulty.
  263. //
  264. // Parameters:
  265. // * lw is list and watch functions for the source of the resource you want to
  266. // be informed of.
  267. // * objType is an object of the type that you expect to receive.
  268. // * resyncPeriod: if non-zero, will re-list this often (you will get OnUpdate
  269. // calls, even if nothing changed). Otherwise, re-list will be delayed as
  270. // long as possible (until the upstream source closes the watch or times out,
  271. // or you stop the controller).
  272. // * h is the object you want notifications sent to.
  273. //
  274. func NewInformer(
  275. lw ListerWatcher,
  276. objType runtime.Object,
  277. resyncPeriod time.Duration,
  278. h ResourceEventHandler,
  279. ) (Store, Controller) {
  280. // This will hold the client state, as we know it.
  281. clientState := NewStore(DeletionHandlingMetaNamespaceKeyFunc)
  282. return clientState, newInformer(lw, objType, resyncPeriod, h, clientState)
  283. }
  284. // NewIndexerInformer returns a Indexer and a controller for populating the index
  285. // while also providing event notifications. You should only used the returned
  286. // Index for Get/List operations; Add/Modify/Deletes will cause the event
  287. // notifications to be faulty.
  288. //
  289. // Parameters:
  290. // * lw is list and watch functions for the source of the resource you want to
  291. // be informed of.
  292. // * objType is an object of the type that you expect to receive.
  293. // * resyncPeriod: if non-zero, will re-list this often (you will get OnUpdate
  294. // calls, even if nothing changed). Otherwise, re-list will be delayed as
  295. // long as possible (until the upstream source closes the watch or times out,
  296. // or you stop the controller).
  297. // * h is the object you want notifications sent to.
  298. // * indexers is the indexer for the received object type.
  299. //
  300. func NewIndexerInformer(
  301. lw ListerWatcher,
  302. objType runtime.Object,
  303. resyncPeriod time.Duration,
  304. h ResourceEventHandler,
  305. indexers Indexers,
  306. ) (Indexer, Controller) {
  307. // This will hold the client state, as we know it.
  308. clientState := NewIndexer(DeletionHandlingMetaNamespaceKeyFunc, indexers)
  309. return clientState, newInformer(lw, objType, resyncPeriod, h, clientState)
  310. }
  311. // newInformer returns a controller for populating the store while also
  312. // providing event notifications.
  313. //
  314. // Parameters
  315. // * lw is list and watch functions for the source of the resource you want to
  316. // be informed of.
  317. // * objType is an object of the type that you expect to receive.
  318. // * resyncPeriod: if non-zero, will re-list this often (you will get OnUpdate
  319. // calls, even if nothing changed). Otherwise, re-list will be delayed as
  320. // long as possible (until the upstream source closes the watch or times out,
  321. // or you stop the controller).
  322. // * h is the object you want notifications sent to.
  323. // * clientState is the store you want to populate
  324. //
  325. func newInformer(
  326. lw ListerWatcher,
  327. objType runtime.Object,
  328. resyncPeriod time.Duration,
  329. h ResourceEventHandler,
  330. clientState Store,
  331. ) Controller {
  332. // This will hold incoming changes. Note how we pass clientState in as a
  333. // KeyLister, that way resync operations will result in the correct set
  334. // of update/delete deltas.
  335. fifo := NewDeltaFIFOWithOptions(DeltaFIFOOptions{
  336. KnownObjects: clientState,
  337. EmitDeltaTypeReplaced: true,
  338. })
  339. cfg := &Config{
  340. Queue: fifo,
  341. ListerWatcher: lw,
  342. ObjectType: objType,
  343. FullResyncPeriod: resyncPeriod,
  344. RetryOnError: false,
  345. Process: func(obj interface{}) error {
  346. // from oldest to newest
  347. for _, d := range obj.(Deltas) {
  348. switch d.Type {
  349. case Sync, Replaced, Added, Updated:
  350. if old, exists, err := clientState.Get(d.Object); err == nil && exists {
  351. if err := clientState.Update(d.Object); err != nil {
  352. return err
  353. }
  354. h.OnUpdate(old, d.Object)
  355. } else {
  356. if err := clientState.Add(d.Object); err != nil {
  357. return err
  358. }
  359. h.OnAdd(d.Object)
  360. }
  361. case Deleted:
  362. if err := clientState.Delete(d.Object); err != nil {
  363. return err
  364. }
  365. h.OnDelete(d.Object)
  366. }
  367. }
  368. return nil
  369. },
  370. }
  371. return New(cfg)
  372. }