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