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