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