github.com/mirantis/virtlet@v1.5.2-0.20191204181327-1659b8a48e9b/pkg/client/informers/externalversions/factory.go (about) 1 /* 2 Copyright 2019 Mirantis 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 versioned "github.com/Mirantis/virtlet/pkg/client/clientset/versioned" 27 internalinterfaces "github.com/Mirantis/virtlet/pkg/client/informers/externalversions/internalinterfaces" 28 virtlet_k8s "github.com/Mirantis/virtlet/pkg/client/informers/externalversions/virtlet.k8s" 29 v1 "k8s.io/apimachinery/pkg/apis/meta/v1" 30 runtime "k8s.io/apimachinery/pkg/runtime" 31 schema "k8s.io/apimachinery/pkg/runtime/schema" 32 cache "k8s.io/client-go/tools/cache" 33 ) 34 35 type sharedInformerFactory struct { 36 client versioned.Interface 37 namespace string 38 tweakListOptions internalinterfaces.TweakListOptionsFunc 39 lock sync.Mutex 40 defaultResync time.Duration 41 42 informers map[reflect.Type]cache.SharedIndexInformer 43 // startedInformers is used for tracking which informers have been started. 44 // This allows Start() to be called multiple times safely. 45 startedInformers map[reflect.Type]bool 46 } 47 48 // NewSharedInformerFactory constructs a new instance of sharedInformerFactory 49 func NewSharedInformerFactory(client versioned.Interface, defaultResync time.Duration) SharedInformerFactory { 50 return NewFilteredSharedInformerFactory(client, defaultResync, v1.NamespaceAll, nil) 51 } 52 53 // NewFilteredSharedInformerFactory constructs a new instance of sharedInformerFactory. 54 // Listers obtained via this SharedInformerFactory will be subject to the same filters 55 // as specified here. 56 func NewFilteredSharedInformerFactory(client versioned.Interface, defaultResync time.Duration, namespace string, tweakListOptions internalinterfaces.TweakListOptionsFunc) SharedInformerFactory { 57 return &sharedInformerFactory{ 58 client: client, 59 namespace: namespace, 60 tweakListOptions: tweakListOptions, 61 defaultResync: defaultResync, 62 informers: make(map[reflect.Type]cache.SharedIndexInformer), 63 startedInformers: make(map[reflect.Type]bool), 64 } 65 } 66 67 // Start initializes all requested informers. 68 func (f *sharedInformerFactory) Start(stopCh <-chan struct{}) { 69 f.lock.Lock() 70 defer f.lock.Unlock() 71 72 for informerType, informer := range f.informers { 73 if !f.startedInformers[informerType] { 74 go informer.Run(stopCh) 75 f.startedInformers[informerType] = true 76 } 77 } 78 } 79 80 // WaitForCacheSync waits for all started informers' cache were synced. 81 func (f *sharedInformerFactory) WaitForCacheSync(stopCh <-chan struct{}) map[reflect.Type]bool { 82 informers := func() map[reflect.Type]cache.SharedIndexInformer { 83 f.lock.Lock() 84 defer f.lock.Unlock() 85 86 informers := map[reflect.Type]cache.SharedIndexInformer{} 87 for informerType, informer := range f.informers { 88 if f.startedInformers[informerType] { 89 informers[informerType] = informer 90 } 91 } 92 return informers 93 }() 94 95 res := map[reflect.Type]bool{} 96 for informType, informer := range informers { 97 res[informType] = cache.WaitForCacheSync(stopCh, informer.HasSynced) 98 } 99 return res 100 } 101 102 // InternalInformerFor returns the SharedIndexInformer for obj using an internal 103 // client. 104 func (f *sharedInformerFactory) InformerFor(obj runtime.Object, newFunc internalinterfaces.NewInformerFunc) cache.SharedIndexInformer { 105 f.lock.Lock() 106 defer f.lock.Unlock() 107 108 informerType := reflect.TypeOf(obj) 109 informer, exists := f.informers[informerType] 110 if exists { 111 return informer 112 } 113 informer = newFunc(f.client, f.defaultResync) 114 f.informers[informerType] = informer 115 116 return informer 117 } 118 119 // SharedInformerFactory provides shared informers for resources in all known 120 // API group versions. 121 type SharedInformerFactory interface { 122 internalinterfaces.SharedInformerFactory 123 ForResource(resource schema.GroupVersionResource) (GenericInformer, error) 124 WaitForCacheSync(stopCh <-chan struct{}) map[reflect.Type]bool 125 126 Virtlet() virtlet_k8s.Interface 127 } 128 129 func (f *sharedInformerFactory) Virtlet() virtlet_k8s.Interface { 130 return virtlet_k8s.New(f, f.namespace, f.tweakListOptions) 131 }