github.phpd.cn/cilium/cilium@v1.6.12/pkg/k8s/client/informers/externalversions/factory.go (about) 1 // Copyright 2017-2019 Authors of Cilium 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/cilium/cilium/pkg/k8s/client/clientset/versioned" 25 ciliumio "github.com/cilium/cilium/pkg/k8s/client/informers/externalversions/cilium.io" 26 internalinterfaces "github.com/cilium/cilium/pkg/k8s/client/informers/externalversions/internalinterfaces" 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 } 49 50 // WithCustomResyncConfig sets a custom resync period for the specified informer types. 51 func WithCustomResyncConfig(resyncConfig map[v1.Object]time.Duration) SharedInformerOption { 52 return func(factory *sharedInformerFactory) *sharedInformerFactory { 53 for k, v := range resyncConfig { 54 factory.customResync[reflect.TypeOf(k)] = v 55 } 56 return factory 57 } 58 } 59 60 // WithTweakListOptions sets a custom filter on all listers of the configured SharedInformerFactory. 61 func WithTweakListOptions(tweakListOptions internalinterfaces.TweakListOptionsFunc) SharedInformerOption { 62 return func(factory *sharedInformerFactory) *sharedInformerFactory { 63 factory.tweakListOptions = tweakListOptions 64 return factory 65 } 66 } 67 68 // WithNamespace limits the SharedInformerFactory to the specified namespace. 69 func WithNamespace(namespace string) SharedInformerOption { 70 return func(factory *sharedInformerFactory) *sharedInformerFactory { 71 factory.namespace = namespace 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 // Start initializes all requested informers. 109 func (f *sharedInformerFactory) Start(stopCh <-chan struct{}) { 110 f.lock.Lock() 111 defer f.lock.Unlock() 112 113 for informerType, informer := range f.informers { 114 if !f.startedInformers[informerType] { 115 go informer.Run(stopCh) 116 f.startedInformers[informerType] = true 117 } 118 } 119 } 120 121 // WaitForCacheSync waits for all started informers' cache were synced. 122 func (f *sharedInformerFactory) WaitForCacheSync(stopCh <-chan struct{}) map[reflect.Type]bool { 123 informers := func() map[reflect.Type]cache.SharedIndexInformer { 124 f.lock.Lock() 125 defer f.lock.Unlock() 126 127 informers := map[reflect.Type]cache.SharedIndexInformer{} 128 for informerType, informer := range f.informers { 129 if f.startedInformers[informerType] { 130 informers[informerType] = informer 131 } 132 } 133 return informers 134 }() 135 136 res := map[reflect.Type]bool{} 137 for informType, informer := range informers { 138 res[informType] = cache.WaitForCacheSync(stopCh, informer.HasSynced) 139 } 140 return res 141 } 142 143 // InternalInformerFor returns the SharedIndexInformer for obj using an internal 144 // client. 145 func (f *sharedInformerFactory) InformerFor(obj runtime.Object, newFunc internalinterfaces.NewInformerFunc) cache.SharedIndexInformer { 146 f.lock.Lock() 147 defer f.lock.Unlock() 148 149 informerType := reflect.TypeOf(obj) 150 informer, exists := f.informers[informerType] 151 if exists { 152 return informer 153 } 154 155 resyncPeriod, exists := f.customResync[informerType] 156 if !exists { 157 resyncPeriod = f.defaultResync 158 } 159 160 informer = newFunc(f.client, resyncPeriod) 161 f.informers[informerType] = informer 162 163 return informer 164 } 165 166 // SharedInformerFactory provides shared informers for resources in all known 167 // API group versions. 168 type SharedInformerFactory interface { 169 internalinterfaces.SharedInformerFactory 170 ForResource(resource schema.GroupVersionResource) (GenericInformer, error) 171 WaitForCacheSync(stopCh <-chan struct{}) map[reflect.Type]bool 172 173 Cilium() ciliumio.Interface 174 } 175 176 func (f *sharedInformerFactory) Cilium() ciliumio.Interface { 177 return ciliumio.New(f, f.namespace, f.tweakListOptions) 178 }