factory.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 externalversions
  15. import (
  16. reflect "reflect"
  17. sync "sync"
  18. time "time"
  19. versioned "github.com/rook/rook/pkg/client/clientset/versioned"
  20. cephrookio "github.com/rook/rook/pkg/client/informers/externalversions/ceph.rook.io"
  21. internalinterfaces "github.com/rook/rook/pkg/client/informers/externalversions/internalinterfaces"
  22. v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  23. runtime "k8s.io/apimachinery/pkg/runtime"
  24. schema "k8s.io/apimachinery/pkg/runtime/schema"
  25. cache "k8s.io/client-go/tools/cache"
  26. )
  27. // SharedInformerOption defines the functional option type for SharedInformerFactory.
  28. type SharedInformerOption func(*sharedInformerFactory) *sharedInformerFactory
  29. type sharedInformerFactory struct {
  30. client versioned.Interface
  31. namespace string
  32. tweakListOptions internalinterfaces.TweakListOptionsFunc
  33. lock sync.Mutex
  34. defaultResync time.Duration
  35. customResync map[reflect.Type]time.Duration
  36. informers map[reflect.Type]cache.SharedIndexInformer
  37. // startedInformers is used for tracking which informers have been started.
  38. // This allows Start() to be called multiple times safely.
  39. startedInformers map[reflect.Type]bool
  40. }
  41. // WithCustomResyncConfig sets a custom resync period for the specified informer types.
  42. func WithCustomResyncConfig(resyncConfig map[v1.Object]time.Duration) SharedInformerOption {
  43. return func(factory *sharedInformerFactory) *sharedInformerFactory {
  44. for k, v := range resyncConfig {
  45. factory.customResync[reflect.TypeOf(k)] = v
  46. }
  47. return factory
  48. }
  49. }
  50. // WithTweakListOptions sets a custom filter on all listers of the configured SharedInformerFactory.
  51. func WithTweakListOptions(tweakListOptions internalinterfaces.TweakListOptionsFunc) SharedInformerOption {
  52. return func(factory *sharedInformerFactory) *sharedInformerFactory {
  53. factory.tweakListOptions = tweakListOptions
  54. return factory
  55. }
  56. }
  57. // WithNamespace limits the SharedInformerFactory to the specified namespace.
  58. func WithNamespace(namespace string) SharedInformerOption {
  59. return func(factory *sharedInformerFactory) *sharedInformerFactory {
  60. factory.namespace = namespace
  61. return factory
  62. }
  63. }
  64. // NewSharedInformerFactory constructs a new instance of sharedInformerFactory for all namespaces.
  65. func NewSharedInformerFactory(client versioned.Interface, defaultResync time.Duration) SharedInformerFactory {
  66. return NewSharedInformerFactoryWithOptions(client, defaultResync)
  67. }
  68. // NewFilteredSharedInformerFactory constructs a new instance of sharedInformerFactory.
  69. // Listers obtained via this SharedInformerFactory will be subject to the same filters
  70. // as specified here.
  71. // Deprecated: Please use NewSharedInformerFactoryWithOptions instead
  72. func NewFilteredSharedInformerFactory(client versioned.Interface, defaultResync time.Duration, namespace string, tweakListOptions internalinterfaces.TweakListOptionsFunc) SharedInformerFactory {
  73. return NewSharedInformerFactoryWithOptions(client, defaultResync, WithNamespace(namespace), WithTweakListOptions(tweakListOptions))
  74. }
  75. // NewSharedInformerFactoryWithOptions constructs a new instance of a SharedInformerFactory with additional options.
  76. func NewSharedInformerFactoryWithOptions(client versioned.Interface, defaultResync time.Duration, options ...SharedInformerOption) SharedInformerFactory {
  77. factory := &sharedInformerFactory{
  78. client: client,
  79. namespace: v1.NamespaceAll,
  80. defaultResync: defaultResync,
  81. informers: make(map[reflect.Type]cache.SharedIndexInformer),
  82. startedInformers: make(map[reflect.Type]bool),
  83. customResync: make(map[reflect.Type]time.Duration),
  84. }
  85. // Apply all options
  86. for _, opt := range options {
  87. factory = opt(factory)
  88. }
  89. return factory
  90. }
  91. // Start initializes all requested informers.
  92. func (f *sharedInformerFactory) Start(stopCh <-chan struct{}) {
  93. f.lock.Lock()
  94. defer f.lock.Unlock()
  95. for informerType, informer := range f.informers {
  96. if !f.startedInformers[informerType] {
  97. go informer.Run(stopCh)
  98. f.startedInformers[informerType] = true
  99. }
  100. }
  101. }
  102. // WaitForCacheSync waits for all started informers' cache were synced.
  103. func (f *sharedInformerFactory) WaitForCacheSync(stopCh <-chan struct{}) map[reflect.Type]bool {
  104. informers := func() map[reflect.Type]cache.SharedIndexInformer {
  105. f.lock.Lock()
  106. defer f.lock.Unlock()
  107. informers := map[reflect.Type]cache.SharedIndexInformer{}
  108. for informerType, informer := range f.informers {
  109. if f.startedInformers[informerType] {
  110. informers[informerType] = informer
  111. }
  112. }
  113. return informers
  114. }()
  115. res := map[reflect.Type]bool{}
  116. for informType, informer := range informers {
  117. res[informType] = cache.WaitForCacheSync(stopCh, informer.HasSynced)
  118. }
  119. return res
  120. }
  121. // InternalInformerFor returns the SharedIndexInformer for obj using an internal
  122. // client.
  123. func (f *sharedInformerFactory) InformerFor(obj runtime.Object, newFunc internalinterfaces.NewInformerFunc) cache.SharedIndexInformer {
  124. f.lock.Lock()
  125. defer f.lock.Unlock()
  126. informerType := reflect.TypeOf(obj)
  127. informer, exists := f.informers[informerType]
  128. if exists {
  129. return informer
  130. }
  131. resyncPeriod, exists := f.customResync[informerType]
  132. if !exists {
  133. resyncPeriod = f.defaultResync
  134. }
  135. informer = newFunc(f.client, resyncPeriod)
  136. f.informers[informerType] = informer
  137. return informer
  138. }
  139. // SharedInformerFactory provides shared informers for resources in all known
  140. // API group versions.
  141. type SharedInformerFactory interface {
  142. internalinterfaces.SharedInformerFactory
  143. ForResource(resource schema.GroupVersionResource) (GenericInformer, error)
  144. WaitForCacheSync(stopCh <-chan struct{}) map[reflect.Type]bool
  145. Ceph() cephrookio.Interface
  146. }
  147. func (f *sharedInformerFactory) Ceph() cephrookio.Interface {
  148. return cephrookio.New(f, f.namespace, f.tweakListOptions)
  149. }