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