volcano.sh/apis@v1.8.2/pkg/client/informers/externalversions/factory.go (about) 1 /* 2 Copyright 2021 The Volcano 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 // Code generated by informer-gen. DO NOT EDIT. 17 18 package externalversions 19 20 import ( 21 reflect "reflect" 22 sync "sync" 23 time "time" 24 25 v1 "k8s.io/apimachinery/pkg/apis/meta/v1" 26 runtime "k8s.io/apimachinery/pkg/runtime" 27 schema "k8s.io/apimachinery/pkg/runtime/schema" 28 cache "k8s.io/client-go/tools/cache" 29 versioned "volcano.sh/apis/pkg/client/clientset/versioned" 30 batch "volcano.sh/apis/pkg/client/informers/externalversions/batch" 31 bus "volcano.sh/apis/pkg/client/informers/externalversions/bus" 32 flow "volcano.sh/apis/pkg/client/informers/externalversions/flow" 33 internalinterfaces "volcano.sh/apis/pkg/client/informers/externalversions/internalinterfaces" 34 nodeinfo "volcano.sh/apis/pkg/client/informers/externalversions/nodeinfo" 35 scheduling "volcano.sh/apis/pkg/client/informers/externalversions/scheduling" 36 ) 37 38 // SharedInformerOption defines the functional option type for SharedInformerFactory. 39 type SharedInformerOption func(*sharedInformerFactory) *sharedInformerFactory 40 41 type sharedInformerFactory struct { 42 client versioned.Interface 43 namespace string 44 tweakListOptions internalinterfaces.TweakListOptionsFunc 45 lock sync.Mutex 46 defaultResync time.Duration 47 customResync map[reflect.Type]time.Duration 48 49 informers map[reflect.Type]cache.SharedIndexInformer 50 // startedInformers is used for tracking which informers have been started. 51 // This allows Start() to be called multiple times safely. 52 startedInformers map[reflect.Type]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 // Start initializes all requested informers. 114 func (f *sharedInformerFactory) Start(stopCh <-chan struct{}) { 115 f.lock.Lock() 116 defer f.lock.Unlock() 117 118 for informerType, informer := range f.informers { 119 if !f.startedInformers[informerType] { 120 go informer.Run(stopCh) 121 f.startedInformers[informerType] = true 122 } 123 } 124 } 125 126 // WaitForCacheSync waits for all started informers' cache were synced. 127 func (f *sharedInformerFactory) WaitForCacheSync(stopCh <-chan struct{}) map[reflect.Type]bool { 128 informers := func() map[reflect.Type]cache.SharedIndexInformer { 129 f.lock.Lock() 130 defer f.lock.Unlock() 131 132 informers := map[reflect.Type]cache.SharedIndexInformer{} 133 for informerType, informer := range f.informers { 134 if f.startedInformers[informerType] { 135 informers[informerType] = informer 136 } 137 } 138 return informers 139 }() 140 141 res := map[reflect.Type]bool{} 142 for informType, informer := range informers { 143 res[informType] = cache.WaitForCacheSync(stopCh, informer.HasSynced) 144 } 145 return res 146 } 147 148 // InternalInformerFor returns the SharedIndexInformer for obj using an internal 149 // client. 150 func (f *sharedInformerFactory) InformerFor(obj runtime.Object, newFunc internalinterfaces.NewInformerFunc) cache.SharedIndexInformer { 151 f.lock.Lock() 152 defer f.lock.Unlock() 153 154 informerType := reflect.TypeOf(obj) 155 informer, exists := f.informers[informerType] 156 if exists { 157 return informer 158 } 159 160 resyncPeriod, exists := f.customResync[informerType] 161 if !exists { 162 resyncPeriod = f.defaultResync 163 } 164 165 informer = newFunc(f.client, resyncPeriod) 166 f.informers[informerType] = informer 167 168 return informer 169 } 170 171 // SharedInformerFactory provides shared informers for resources in all known 172 // API group versions. 173 type SharedInformerFactory interface { 174 internalinterfaces.SharedInformerFactory 175 ForResource(resource schema.GroupVersionResource) (GenericInformer, error) 176 WaitForCacheSync(stopCh <-chan struct{}) map[reflect.Type]bool 177 178 Batch() batch.Interface 179 Bus() bus.Interface 180 Flow() flow.Interface 181 Nodeinfo() nodeinfo.Interface 182 Scheduling() scheduling.Interface 183 } 184 185 func (f *sharedInformerFactory) Batch() batch.Interface { 186 return batch.New(f, f.namespace, f.tweakListOptions) 187 } 188 189 func (f *sharedInformerFactory) Bus() bus.Interface { 190 return bus.New(f, f.namespace, f.tweakListOptions) 191 } 192 193 func (f *sharedInformerFactory) Flow() flow.Interface { 194 return flow.New(f, f.namespace, f.tweakListOptions) 195 } 196 197 func (f *sharedInformerFactory) Nodeinfo() nodeinfo.Interface { 198 return nodeinfo.New(f, f.namespace, f.tweakListOptions) 199 } 200 201 func (f *sharedInformerFactory) Scheduling() scheduling.Interface { 202 return scheduling.New(f, f.namespace, f.tweakListOptions) 203 }