factory.go 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. // Copyright 2026 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/squat/kilo/pkg/k8s/clientset/versioned"
  21. internalinterfaces "github.com/squat/kilo/pkg/k8s/informers/internalinterfaces"
  22. kilo "github.com/squat/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. transform cache.TransformFunc
  38. informers map[reflect.Type]cache.SharedIndexInformer
  39. // startedInformers is used for tracking which informers have been started.
  40. // This allows Start() to be called multiple times safely.
  41. startedInformers map[reflect.Type]bool
  42. // wg tracks how many goroutines were started.
  43. wg sync.WaitGroup
  44. // shuttingDown is true when Shutdown has been called. It may still be running
  45. // because it needs to wait for goroutines.
  46. shuttingDown bool
  47. }
  48. // WithCustomResyncConfig sets a custom resync period for the specified informer types.
  49. func WithCustomResyncConfig(resyncConfig map[v1.Object]time.Duration) SharedInformerOption {
  50. return func(factory *sharedInformerFactory) *sharedInformerFactory {
  51. for k, v := range resyncConfig {
  52. factory.customResync[reflect.TypeOf(k)] = v
  53. }
  54. return factory
  55. }
  56. }
  57. // WithTweakListOptions sets a custom filter on all listers of the configured SharedInformerFactory.
  58. func WithTweakListOptions(tweakListOptions internalinterfaces.TweakListOptionsFunc) SharedInformerOption {
  59. return func(factory *sharedInformerFactory) *sharedInformerFactory {
  60. factory.tweakListOptions = tweakListOptions
  61. return factory
  62. }
  63. }
  64. // WithNamespace limits the SharedInformerFactory to the specified namespace.
  65. func WithNamespace(namespace string) SharedInformerOption {
  66. return func(factory *sharedInformerFactory) *sharedInformerFactory {
  67. factory.namespace = namespace
  68. return factory
  69. }
  70. }
  71. // WithTransform sets a transform on all informers.
  72. func WithTransform(transform cache.TransformFunc) SharedInformerOption {
  73. return func(factory *sharedInformerFactory) *sharedInformerFactory {
  74. factory.transform = transform
  75. return factory
  76. }
  77. }
  78. // NewSharedInformerFactory constructs a new instance of sharedInformerFactory for all namespaces.
  79. func NewSharedInformerFactory(client versioned.Interface, defaultResync time.Duration) SharedInformerFactory {
  80. return NewSharedInformerFactoryWithOptions(client, defaultResync)
  81. }
  82. // NewFilteredSharedInformerFactory constructs a new instance of sharedInformerFactory.
  83. // Listers obtained via this SharedInformerFactory will be subject to the same filters
  84. // as specified here.
  85. // Deprecated: Please use NewSharedInformerFactoryWithOptions instead
  86. func NewFilteredSharedInformerFactory(client versioned.Interface, defaultResync time.Duration, namespace string, tweakListOptions internalinterfaces.TweakListOptionsFunc) SharedInformerFactory {
  87. return NewSharedInformerFactoryWithOptions(client, defaultResync, WithNamespace(namespace), WithTweakListOptions(tweakListOptions))
  88. }
  89. // NewSharedInformerFactoryWithOptions constructs a new instance of a SharedInformerFactory with additional options.
  90. func NewSharedInformerFactoryWithOptions(client versioned.Interface, defaultResync time.Duration, options ...SharedInformerOption) SharedInformerFactory {
  91. factory := &sharedInformerFactory{
  92. client: client,
  93. namespace: v1.NamespaceAll,
  94. defaultResync: defaultResync,
  95. informers: make(map[reflect.Type]cache.SharedIndexInformer),
  96. startedInformers: make(map[reflect.Type]bool),
  97. customResync: make(map[reflect.Type]time.Duration),
  98. }
  99. // Apply all options
  100. for _, opt := range options {
  101. factory = opt(factory)
  102. }
  103. return factory
  104. }
  105. func (f *sharedInformerFactory) Start(stopCh <-chan struct{}) {
  106. f.lock.Lock()
  107. defer f.lock.Unlock()
  108. if f.shuttingDown {
  109. return
  110. }
  111. for informerType, informer := range f.informers {
  112. if !f.startedInformers[informerType] {
  113. f.wg.Add(1)
  114. // We need a new variable in each loop iteration,
  115. // otherwise the goroutine would use the loop variable
  116. // and that keeps changing.
  117. informer := informer
  118. go func() {
  119. defer f.wg.Done()
  120. informer.Run(stopCh)
  121. }()
  122. f.startedInformers[informerType] = true
  123. }
  124. }
  125. }
  126. func (f *sharedInformerFactory) Shutdown() {
  127. f.lock.Lock()
  128. f.shuttingDown = true
  129. f.lock.Unlock()
  130. // Will return immediately if there is nothing to wait for.
  131. f.wg.Wait()
  132. }
  133. func (f *sharedInformerFactory) WaitForCacheSync(stopCh <-chan struct{}) map[reflect.Type]bool {
  134. informers := func() map[reflect.Type]cache.SharedIndexInformer {
  135. f.lock.Lock()
  136. defer f.lock.Unlock()
  137. informers := map[reflect.Type]cache.SharedIndexInformer{}
  138. for informerType, informer := range f.informers {
  139. if f.startedInformers[informerType] {
  140. informers[informerType] = informer
  141. }
  142. }
  143. return informers
  144. }()
  145. res := map[reflect.Type]bool{}
  146. for informType, informer := range informers {
  147. res[informType] = cache.WaitForCacheSync(stopCh, informer.HasSynced)
  148. }
  149. return res
  150. }
  151. // InformerFor returns the SharedIndexInformer for obj using an internal
  152. // client.
  153. func (f *sharedInformerFactory) InformerFor(obj runtime.Object, newFunc internalinterfaces.NewInformerFunc) cache.SharedIndexInformer {
  154. f.lock.Lock()
  155. defer f.lock.Unlock()
  156. informerType := reflect.TypeOf(obj)
  157. informer, exists := f.informers[informerType]
  158. if exists {
  159. return informer
  160. }
  161. resyncPeriod, exists := f.customResync[informerType]
  162. if !exists {
  163. resyncPeriod = f.defaultResync
  164. }
  165. informer = newFunc(f.client, resyncPeriod)
  166. informer.SetTransform(f.transform)
  167. f.informers[informerType] = informer
  168. return informer
  169. }
  170. // SharedInformerFactory provides shared informers for resources in all known
  171. // API group versions.
  172. //
  173. // It is typically used like this:
  174. //
  175. // ctx, cancel := context.Background()
  176. // defer cancel()
  177. // factory := NewSharedInformerFactory(client, resyncPeriod)
  178. // defer factory.WaitForStop() // Returns immediately if nothing was started.
  179. // genericInformer := factory.ForResource(resource)
  180. // typedInformer := factory.SomeAPIGroup().V1().SomeType()
  181. // factory.Start(ctx.Done()) // Start processing these informers.
  182. // synced := factory.WaitForCacheSync(ctx.Done())
  183. // for v, ok := range synced {
  184. // if !ok {
  185. // fmt.Fprintf(os.Stderr, "caches failed to sync: %v", v)
  186. // return
  187. // }
  188. // }
  189. //
  190. // // Creating informers can also be created after Start, but then
  191. // // Start must be called again:
  192. // anotherGenericInformer := factory.ForResource(resource)
  193. // factory.Start(ctx.Done())
  194. type SharedInformerFactory interface {
  195. internalinterfaces.SharedInformerFactory
  196. // Start initializes all requested informers. They are handled in goroutines
  197. // which run until the stop channel gets closed.
  198. Start(stopCh <-chan struct{})
  199. // Shutdown marks a factory as shutting down. At that point no new
  200. // informers can be started anymore and Start will return without
  201. // doing anything.
  202. //
  203. // In addition, Shutdown blocks until all goroutines have terminated. For that
  204. // to happen, the close channel(s) that they were started with must be closed,
  205. // either before Shutdown gets called or while it is waiting.
  206. //
  207. // Shutdown may be called multiple times, even concurrently. All such calls will
  208. // block until all goroutines have terminated.
  209. Shutdown()
  210. // WaitForCacheSync blocks until all started informers' caches were synced
  211. // or the stop channel gets closed.
  212. WaitForCacheSync(stopCh <-chan struct{}) map[reflect.Type]bool
  213. // ForResource gives generic access to a shared informer of the matching type.
  214. ForResource(resource schema.GroupVersionResource) (GenericInformer, error)
  215. // InformerFor returns the SharedIndexInformer for obj using an internal
  216. // client.
  217. InformerFor(obj runtime.Object, newFunc internalinterfaces.NewInformerFunc) cache.SharedIndexInformer
  218. Kilo() kilo.Interface
  219. }
  220. func (f *sharedInformerFactory) Kilo() kilo.Interface {
  221. return kilo.New(f, f.namespace, f.tweakListOptions)
  222. }