factory.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. /*
  2. Copyright 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. // Code generated by informer-gen. DO NOT EDIT.
  14. package informers
  15. import (
  16. reflect "reflect"
  17. sync "sync"
  18. time "time"
  19. v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  20. runtime "k8s.io/apimachinery/pkg/runtime"
  21. schema "k8s.io/apimachinery/pkg/runtime/schema"
  22. admissionregistration "k8s.io/client-go/informers/admissionregistration"
  23. apiserverinternal "k8s.io/client-go/informers/apiserverinternal"
  24. apps "k8s.io/client-go/informers/apps"
  25. autoscaling "k8s.io/client-go/informers/autoscaling"
  26. batch "k8s.io/client-go/informers/batch"
  27. certificates "k8s.io/client-go/informers/certificates"
  28. coordination "k8s.io/client-go/informers/coordination"
  29. core "k8s.io/client-go/informers/core"
  30. discovery "k8s.io/client-go/informers/discovery"
  31. events "k8s.io/client-go/informers/events"
  32. extensions "k8s.io/client-go/informers/extensions"
  33. flowcontrol "k8s.io/client-go/informers/flowcontrol"
  34. internalinterfaces "k8s.io/client-go/informers/internalinterfaces"
  35. networking "k8s.io/client-go/informers/networking"
  36. node "k8s.io/client-go/informers/node"
  37. policy "k8s.io/client-go/informers/policy"
  38. rbac "k8s.io/client-go/informers/rbac"
  39. resource "k8s.io/client-go/informers/resource"
  40. scheduling "k8s.io/client-go/informers/scheduling"
  41. storage "k8s.io/client-go/informers/storage"
  42. kubernetes "k8s.io/client-go/kubernetes"
  43. cache "k8s.io/client-go/tools/cache"
  44. )
  45. // SharedInformerOption defines the functional option type for SharedInformerFactory.
  46. type SharedInformerOption func(*sharedInformerFactory) *sharedInformerFactory
  47. type sharedInformerFactory struct {
  48. client kubernetes.Interface
  49. namespace string
  50. tweakListOptions internalinterfaces.TweakListOptionsFunc
  51. lock sync.Mutex
  52. defaultResync time.Duration
  53. customResync map[reflect.Type]time.Duration
  54. informers map[reflect.Type]cache.SharedIndexInformer
  55. // startedInformers is used for tracking which informers have been started.
  56. // This allows Start() to be called multiple times safely.
  57. startedInformers map[reflect.Type]bool
  58. // wg tracks how many goroutines were started.
  59. wg sync.WaitGroup
  60. // shuttingDown is true when Shutdown has been called. It may still be running
  61. // because it needs to wait for goroutines.
  62. shuttingDown bool
  63. }
  64. // WithCustomResyncConfig sets a custom resync period for the specified informer types.
  65. func WithCustomResyncConfig(resyncConfig map[v1.Object]time.Duration) SharedInformerOption {
  66. return func(factory *sharedInformerFactory) *sharedInformerFactory {
  67. for k, v := range resyncConfig {
  68. factory.customResync[reflect.TypeOf(k)] = v
  69. }
  70. return factory
  71. }
  72. }
  73. // WithTweakListOptions sets a custom filter on all listers of the configured SharedInformerFactory.
  74. func WithTweakListOptions(tweakListOptions internalinterfaces.TweakListOptionsFunc) SharedInformerOption {
  75. return func(factory *sharedInformerFactory) *sharedInformerFactory {
  76. factory.tweakListOptions = tweakListOptions
  77. return factory
  78. }
  79. }
  80. // WithNamespace limits the SharedInformerFactory to the specified namespace.
  81. func WithNamespace(namespace string) SharedInformerOption {
  82. return func(factory *sharedInformerFactory) *sharedInformerFactory {
  83. factory.namespace = namespace
  84. return factory
  85. }
  86. }
  87. // NewSharedInformerFactory constructs a new instance of sharedInformerFactory for all namespaces.
  88. func NewSharedInformerFactory(client kubernetes.Interface, defaultResync time.Duration) SharedInformerFactory {
  89. return NewSharedInformerFactoryWithOptions(client, defaultResync)
  90. }
  91. // NewFilteredSharedInformerFactory constructs a new instance of sharedInformerFactory.
  92. // Listers obtained via this SharedInformerFactory will be subject to the same filters
  93. // as specified here.
  94. // Deprecated: Please use NewSharedInformerFactoryWithOptions instead
  95. func NewFilteredSharedInformerFactory(client kubernetes.Interface, defaultResync time.Duration, namespace string, tweakListOptions internalinterfaces.TweakListOptionsFunc) SharedInformerFactory {
  96. return NewSharedInformerFactoryWithOptions(client, defaultResync, WithNamespace(namespace), WithTweakListOptions(tweakListOptions))
  97. }
  98. // NewSharedInformerFactoryWithOptions constructs a new instance of a SharedInformerFactory with additional options.
  99. func NewSharedInformerFactoryWithOptions(client kubernetes.Interface, defaultResync time.Duration, options ...SharedInformerOption) SharedInformerFactory {
  100. factory := &sharedInformerFactory{
  101. client: client,
  102. namespace: v1.NamespaceAll,
  103. defaultResync: defaultResync,
  104. informers: make(map[reflect.Type]cache.SharedIndexInformer),
  105. startedInformers: make(map[reflect.Type]bool),
  106. customResync: make(map[reflect.Type]time.Duration),
  107. }
  108. // Apply all options
  109. for _, opt := range options {
  110. factory = opt(factory)
  111. }
  112. return factory
  113. }
  114. func (f *sharedInformerFactory) Start(stopCh <-chan struct{}) {
  115. f.lock.Lock()
  116. defer f.lock.Unlock()
  117. if f.shuttingDown {
  118. return
  119. }
  120. for informerType, informer := range f.informers {
  121. if !f.startedInformers[informerType] {
  122. f.wg.Add(1)
  123. // We need a new variable in each loop iteration,
  124. // otherwise the goroutine would use the loop variable
  125. // and that keeps changing.
  126. informer := informer
  127. go func() {
  128. defer f.wg.Done()
  129. informer.Run(stopCh)
  130. }()
  131. f.startedInformers[informerType] = true
  132. }
  133. }
  134. }
  135. func (f *sharedInformerFactory) Shutdown() {
  136. f.lock.Lock()
  137. f.shuttingDown = true
  138. f.lock.Unlock()
  139. // Will return immediately if there is nothing to wait for.
  140. f.wg.Wait()
  141. }
  142. func (f *sharedInformerFactory) WaitForCacheSync(stopCh <-chan struct{}) map[reflect.Type]bool {
  143. informers := func() map[reflect.Type]cache.SharedIndexInformer {
  144. f.lock.Lock()
  145. defer f.lock.Unlock()
  146. informers := map[reflect.Type]cache.SharedIndexInformer{}
  147. for informerType, informer := range f.informers {
  148. if f.startedInformers[informerType] {
  149. informers[informerType] = informer
  150. }
  151. }
  152. return informers
  153. }()
  154. res := map[reflect.Type]bool{}
  155. for informType, informer := range informers {
  156. res[informType] = cache.WaitForCacheSync(stopCh, informer.HasSynced)
  157. }
  158. return res
  159. }
  160. // InformerFor returns the SharedIndexInformer for obj using an internal
  161. // client.
  162. func (f *sharedInformerFactory) InformerFor(obj runtime.Object, newFunc internalinterfaces.NewInformerFunc) cache.SharedIndexInformer {
  163. f.lock.Lock()
  164. defer f.lock.Unlock()
  165. informerType := reflect.TypeOf(obj)
  166. informer, exists := f.informers[informerType]
  167. if exists {
  168. return informer
  169. }
  170. resyncPeriod, exists := f.customResync[informerType]
  171. if !exists {
  172. resyncPeriod = f.defaultResync
  173. }
  174. informer = newFunc(f.client, resyncPeriod)
  175. f.informers[informerType] = informer
  176. return informer
  177. }
  178. // SharedInformerFactory provides shared informers for resources in all known
  179. // API group versions.
  180. //
  181. // It is typically used like this:
  182. //
  183. // ctx, cancel := context.Background()
  184. // defer cancel()
  185. // factory := NewSharedInformerFactory(client, resyncPeriod)
  186. // defer factory.WaitForStop() // Returns immediately if nothing was started.
  187. // genericInformer := factory.ForResource(resource)
  188. // typedInformer := factory.SomeAPIGroup().V1().SomeType()
  189. // factory.Start(ctx.Done()) // Start processing these informers.
  190. // synced := factory.WaitForCacheSync(ctx.Done())
  191. // for v, ok := range synced {
  192. // if !ok {
  193. // fmt.Fprintf(os.Stderr, "caches failed to sync: %v", v)
  194. // return
  195. // }
  196. // }
  197. //
  198. // // Creating informers can also be created after Start, but then
  199. // // Start must be called again:
  200. // anotherGenericInformer := factory.ForResource(resource)
  201. // factory.Start(ctx.Done())
  202. type SharedInformerFactory interface {
  203. internalinterfaces.SharedInformerFactory
  204. // Start initializes all requested informers. They are handled in goroutines
  205. // which run until the stop channel gets closed.
  206. Start(stopCh <-chan struct{})
  207. // Shutdown marks a factory as shutting down. At that point no new
  208. // informers can be started anymore and Start will return without
  209. // doing anything.
  210. //
  211. // In addition, Shutdown blocks until all goroutines have terminated. For that
  212. // to happen, the close channel(s) that they were started with must be closed,
  213. // either before Shutdown gets called or while it is waiting.
  214. //
  215. // Shutdown may be called multiple times, even concurrently. All such calls will
  216. // block until all goroutines have terminated.
  217. Shutdown()
  218. // WaitForCacheSync blocks until all started informers' caches were synced
  219. // or the stop channel gets closed.
  220. WaitForCacheSync(stopCh <-chan struct{}) map[reflect.Type]bool
  221. // ForResource gives generic access to a shared informer of the matching type.
  222. ForResource(resource schema.GroupVersionResource) (GenericInformer, error)
  223. // InformerFor returns the SharedIndexInformer for obj using an internal
  224. // client.
  225. InformerFor(obj runtime.Object, newFunc internalinterfaces.NewInformerFunc) cache.SharedIndexInformer
  226. Admissionregistration() admissionregistration.Interface
  227. Internal() apiserverinternal.Interface
  228. Apps() apps.Interface
  229. Autoscaling() autoscaling.Interface
  230. Batch() batch.Interface
  231. Certificates() certificates.Interface
  232. Coordination() coordination.Interface
  233. Core() core.Interface
  234. Discovery() discovery.Interface
  235. Events() events.Interface
  236. Extensions() extensions.Interface
  237. Flowcontrol() flowcontrol.Interface
  238. Networking() networking.Interface
  239. Node() node.Interface
  240. Policy() policy.Interface
  241. Rbac() rbac.Interface
  242. Resource() resource.Interface
  243. Scheduling() scheduling.Interface
  244. Storage() storage.Interface
  245. }
  246. func (f *sharedInformerFactory) Admissionregistration() admissionregistration.Interface {
  247. return admissionregistration.New(f, f.namespace, f.tweakListOptions)
  248. }
  249. func (f *sharedInformerFactory) Internal() apiserverinternal.Interface {
  250. return apiserverinternal.New(f, f.namespace, f.tweakListOptions)
  251. }
  252. func (f *sharedInformerFactory) Apps() apps.Interface {
  253. return apps.New(f, f.namespace, f.tweakListOptions)
  254. }
  255. func (f *sharedInformerFactory) Autoscaling() autoscaling.Interface {
  256. return autoscaling.New(f, f.namespace, f.tweakListOptions)
  257. }
  258. func (f *sharedInformerFactory) Batch() batch.Interface {
  259. return batch.New(f, f.namespace, f.tweakListOptions)
  260. }
  261. func (f *sharedInformerFactory) Certificates() certificates.Interface {
  262. return certificates.New(f, f.namespace, f.tweakListOptions)
  263. }
  264. func (f *sharedInformerFactory) Coordination() coordination.Interface {
  265. return coordination.New(f, f.namespace, f.tweakListOptions)
  266. }
  267. func (f *sharedInformerFactory) Core() core.Interface {
  268. return core.New(f, f.namespace, f.tweakListOptions)
  269. }
  270. func (f *sharedInformerFactory) Discovery() discovery.Interface {
  271. return discovery.New(f, f.namespace, f.tweakListOptions)
  272. }
  273. func (f *sharedInformerFactory) Events() events.Interface {
  274. return events.New(f, f.namespace, f.tweakListOptions)
  275. }
  276. func (f *sharedInformerFactory) Extensions() extensions.Interface {
  277. return extensions.New(f, f.namespace, f.tweakListOptions)
  278. }
  279. func (f *sharedInformerFactory) Flowcontrol() flowcontrol.Interface {
  280. return flowcontrol.New(f, f.namespace, f.tweakListOptions)
  281. }
  282. func (f *sharedInformerFactory) Networking() networking.Interface {
  283. return networking.New(f, f.namespace, f.tweakListOptions)
  284. }
  285. func (f *sharedInformerFactory) Node() node.Interface {
  286. return node.New(f, f.namespace, f.tweakListOptions)
  287. }
  288. func (f *sharedInformerFactory) Policy() policy.Interface {
  289. return policy.New(f, f.namespace, f.tweakListOptions)
  290. }
  291. func (f *sharedInformerFactory) Rbac() rbac.Interface {
  292. return rbac.New(f, f.namespace, f.tweakListOptions)
  293. }
  294. func (f *sharedInformerFactory) Resource() resource.Interface {
  295. return resource.New(f, f.namespace, f.tweakListOptions)
  296. }
  297. func (f *sharedInformerFactory) Scheduling() scheduling.Interface {
  298. return scheduling.New(f, f.namespace, f.tweakListOptions)
  299. }
  300. func (f *sharedInformerFactory) Storage() storage.Interface {
  301. return storage.New(f, f.namespace, f.tweakListOptions)
  302. }