factory.go 6.2 KB

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