github.com/projectcalico/api@v0.0.0-20231218190037-9183ab93f33e/pkg/client/informers_generated/externalversions/factory.go (about) 1 // Copyright (c) 2023 Tigera, Inc. All rights reserved. 2 3 // Code generated by informer-gen. DO NOT EDIT. 4 5 package externalversions 6 7 import ( 8 reflect "reflect" 9 sync "sync" 10 time "time" 11 12 clientset "github.com/projectcalico/api/pkg/client/clientset_generated/clientset" 13 internalinterfaces "github.com/projectcalico/api/pkg/client/informers_generated/externalversions/internalinterfaces" 14 projectcalico "github.com/projectcalico/api/pkg/client/informers_generated/externalversions/projectcalico" 15 v1 "k8s.io/apimachinery/pkg/apis/meta/v1" 16 runtime "k8s.io/apimachinery/pkg/runtime" 17 schema "k8s.io/apimachinery/pkg/runtime/schema" 18 cache "k8s.io/client-go/tools/cache" 19 ) 20 21 // SharedInformerOption defines the functional option type for SharedInformerFactory. 22 type SharedInformerOption func(*sharedInformerFactory) *sharedInformerFactory 23 24 type sharedInformerFactory struct { 25 client clientset.Interface 26 namespace string 27 tweakListOptions internalinterfaces.TweakListOptionsFunc 28 lock sync.Mutex 29 defaultResync time.Duration 30 customResync map[reflect.Type]time.Duration 31 32 informers map[reflect.Type]cache.SharedIndexInformer 33 // startedInformers is used for tracking which informers have been started. 34 // This allows Start() to be called multiple times safely. 35 startedInformers map[reflect.Type]bool 36 // wg tracks how many goroutines were started. 37 wg sync.WaitGroup 38 // shuttingDown is true when Shutdown has been called. It may still be running 39 // because it needs to wait for goroutines. 40 shuttingDown bool 41 } 42 43 // WithCustomResyncConfig sets a custom resync period for the specified informer types. 44 func WithCustomResyncConfig(resyncConfig map[v1.Object]time.Duration) SharedInformerOption { 45 return func(factory *sharedInformerFactory) *sharedInformerFactory { 46 for k, v := range resyncConfig { 47 factory.customResync[reflect.TypeOf(k)] = v 48 } 49 return factory 50 } 51 } 52 53 // WithTweakListOptions sets a custom filter on all listers of the configured SharedInformerFactory. 54 func WithTweakListOptions(tweakListOptions internalinterfaces.TweakListOptionsFunc) SharedInformerOption { 55 return func(factory *sharedInformerFactory) *sharedInformerFactory { 56 factory.tweakListOptions = tweakListOptions 57 return factory 58 } 59 } 60 61 // WithNamespace limits the SharedInformerFactory to the specified namespace. 62 func WithNamespace(namespace string) SharedInformerOption { 63 return func(factory *sharedInformerFactory) *sharedInformerFactory { 64 factory.namespace = namespace 65 return factory 66 } 67 } 68 69 // NewSharedInformerFactory constructs a new instance of sharedInformerFactory for all namespaces. 70 func NewSharedInformerFactory(client clientset.Interface, defaultResync time.Duration) SharedInformerFactory { 71 return NewSharedInformerFactoryWithOptions(client, defaultResync) 72 } 73 74 // NewFilteredSharedInformerFactory constructs a new instance of sharedInformerFactory. 75 // Listers obtained via this SharedInformerFactory will be subject to the same filters 76 // as specified here. 77 // Deprecated: Please use NewSharedInformerFactoryWithOptions instead 78 func NewFilteredSharedInformerFactory(client clientset.Interface, defaultResync time.Duration, namespace string, tweakListOptions internalinterfaces.TweakListOptionsFunc) SharedInformerFactory { 79 return NewSharedInformerFactoryWithOptions(client, defaultResync, WithNamespace(namespace), WithTweakListOptions(tweakListOptions)) 80 } 81 82 // NewSharedInformerFactoryWithOptions constructs a new instance of a SharedInformerFactory with additional options. 83 func NewSharedInformerFactoryWithOptions(client clientset.Interface, defaultResync time.Duration, options ...SharedInformerOption) SharedInformerFactory { 84 factory := &sharedInformerFactory{ 85 client: client, 86 namespace: v1.NamespaceAll, 87 defaultResync: defaultResync, 88 informers: make(map[reflect.Type]cache.SharedIndexInformer), 89 startedInformers: make(map[reflect.Type]bool), 90 customResync: make(map[reflect.Type]time.Duration), 91 } 92 93 // Apply all options 94 for _, opt := range options { 95 factory = opt(factory) 96 } 97 98 return factory 99 } 100 101 func (f *sharedInformerFactory) Start(stopCh <-chan struct{}) { 102 f.lock.Lock() 103 defer f.lock.Unlock() 104 105 if f.shuttingDown { 106 return 107 } 108 109 for informerType, informer := range f.informers { 110 if !f.startedInformers[informerType] { 111 f.wg.Add(1) 112 // We need a new variable in each loop iteration, 113 // otherwise the goroutine would use the loop variable 114 // and that keeps changing. 115 informer := informer 116 go func() { 117 defer f.wg.Done() 118 informer.Run(stopCh) 119 }() 120 f.startedInformers[informerType] = true 121 } 122 } 123 } 124 125 func (f *sharedInformerFactory) Shutdown() { 126 f.lock.Lock() 127 f.shuttingDown = true 128 f.lock.Unlock() 129 130 // Will return immediately if there is nothing to wait for. 131 f.wg.Wait() 132 } 133 134 func (f *sharedInformerFactory) WaitForCacheSync(stopCh <-chan struct{}) map[reflect.Type]bool { 135 informers := func() map[reflect.Type]cache.SharedIndexInformer { 136 f.lock.Lock() 137 defer f.lock.Unlock() 138 139 informers := map[reflect.Type]cache.SharedIndexInformer{} 140 for informerType, informer := range f.informers { 141 if f.startedInformers[informerType] { 142 informers[informerType] = informer 143 } 144 } 145 return informers 146 }() 147 148 res := map[reflect.Type]bool{} 149 for informType, informer := range informers { 150 res[informType] = cache.WaitForCacheSync(stopCh, informer.HasSynced) 151 } 152 return res 153 } 154 155 // InternalInformerFor returns the SharedIndexInformer for obj using an internal 156 // client. 157 func (f *sharedInformerFactory) InformerFor(obj runtime.Object, newFunc internalinterfaces.NewInformerFunc) cache.SharedIndexInformer { 158 f.lock.Lock() 159 defer f.lock.Unlock() 160 161 informerType := reflect.TypeOf(obj) 162 informer, exists := f.informers[informerType] 163 if exists { 164 return informer 165 } 166 167 resyncPeriod, exists := f.customResync[informerType] 168 if !exists { 169 resyncPeriod = f.defaultResync 170 } 171 172 informer = newFunc(f.client, resyncPeriod) 173 f.informers[informerType] = informer 174 175 return informer 176 } 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 205 // Start initializes all requested informers. They are handled in goroutines 206 // which run until the stop channel gets closed. 207 Start(stopCh <-chan struct{}) 208 209 // Shutdown marks a factory as shutting down. At that point no new 210 // informers can be started anymore and Start will return without 211 // doing anything. 212 // 213 // In addition, Shutdown blocks until all goroutines have terminated. For that 214 // to happen, the close channel(s) that they were started with must be closed, 215 // either before Shutdown gets called or while it is waiting. 216 // 217 // Shutdown may be called multiple times, even concurrently. All such calls will 218 // block until all goroutines have terminated. 219 Shutdown() 220 221 // WaitForCacheSync blocks until all started informers' caches were synced 222 // or the stop channel gets closed. 223 WaitForCacheSync(stopCh <-chan struct{}) map[reflect.Type]bool 224 225 // ForResource gives generic access to a shared informer of the matching type. 226 ForResource(resource schema.GroupVersionResource) (GenericInformer, error) 227 228 // InternalInformerFor returns the SharedIndexInformer for obj using an internal 229 // client. 230 InformerFor(obj runtime.Object, newFunc internalinterfaces.NewInformerFunc) cache.SharedIndexInformer 231 232 Projectcalico() projectcalico.Interface 233 } 234 235 func (f *sharedInformerFactory) Projectcalico() projectcalico.Interface { 236 return projectcalico.New(f, f.namespace, f.tweakListOptions) 237 }