vitess.io/vitess@v0.16.2/go/vt/topo/k8stopo/client/informers/externalversions/factory.go (about)

     1  /*
     2  Copyright 2020 The Vitess 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  
    31  	versioned "vitess.io/vitess/go/vt/topo/k8stopo/client/clientset/versioned"
    32  	internalinterfaces "vitess.io/vitess/go/vt/topo/k8stopo/client/informers/externalversions/internalinterfaces"
    33  	topo "vitess.io/vitess/go/vt/topo/k8stopo/client/informers/externalversions/topo"
    34  )
    35  
    36  // SharedInformerOption defines the functional option type for SharedInformerFactory.
    37  type SharedInformerOption func(*sharedInformerFactory) *sharedInformerFactory
    38  
    39  type sharedInformerFactory struct {
    40  	client           versioned.Interface
    41  	namespace        string
    42  	tweakListOptions internalinterfaces.TweakListOptionsFunc
    43  	lock             sync.Mutex
    44  	defaultResync    time.Duration
    45  	customResync     map[reflect.Type]time.Duration
    46  
    47  	informers map[reflect.Type]cache.SharedIndexInformer
    48  	// startedInformers is used for tracking which informers have been started.
    49  	// This allows Start() to be called multiple times safely.
    50  	startedInformers map[reflect.Type]bool
    51  }
    52  
    53  // WithCustomResyncConfig sets a custom resync period for the specified informer types.
    54  func WithCustomResyncConfig(resyncConfig map[v1.Object]time.Duration) SharedInformerOption {
    55  	return func(factory *sharedInformerFactory) *sharedInformerFactory {
    56  		for k, v := range resyncConfig {
    57  			factory.customResync[reflect.TypeOf(k)] = v
    58  		}
    59  		return factory
    60  	}
    61  }
    62  
    63  // WithTweakListOptions sets a custom filter on all listers of the configured SharedInformerFactory.
    64  func WithTweakListOptions(tweakListOptions internalinterfaces.TweakListOptionsFunc) SharedInformerOption {
    65  	return func(factory *sharedInformerFactory) *sharedInformerFactory {
    66  		factory.tweakListOptions = tweakListOptions
    67  		return factory
    68  	}
    69  }
    70  
    71  // WithNamespace limits the SharedInformerFactory to the specified namespace.
    72  func WithNamespace(namespace string) SharedInformerOption {
    73  	return func(factory *sharedInformerFactory) *sharedInformerFactory {
    74  		factory.namespace = namespace
    75  		return factory
    76  	}
    77  }
    78  
    79  // NewSharedInformerFactory constructs a new instance of sharedInformerFactory for all namespaces.
    80  func NewSharedInformerFactory(client versioned.Interface, defaultResync time.Duration) SharedInformerFactory {
    81  	return NewSharedInformerFactoryWithOptions(client, defaultResync)
    82  }
    83  
    84  // NewFilteredSharedInformerFactory constructs a new instance of sharedInformerFactory.
    85  // Listers obtained via this SharedInformerFactory will be subject to the same filters
    86  // as specified here.
    87  // Deprecated: Please use NewSharedInformerFactoryWithOptions instead
    88  func NewFilteredSharedInformerFactory(client versioned.Interface, defaultResync time.Duration, namespace string, tweakListOptions internalinterfaces.TweakListOptionsFunc) SharedInformerFactory {
    89  	return NewSharedInformerFactoryWithOptions(client, defaultResync, WithNamespace(namespace), WithTweakListOptions(tweakListOptions))
    90  }
    91  
    92  // NewSharedInformerFactoryWithOptions constructs a new instance of a SharedInformerFactory with additional options.
    93  func NewSharedInformerFactoryWithOptions(client versioned.Interface, defaultResync time.Duration, options ...SharedInformerOption) SharedInformerFactory {
    94  	factory := &sharedInformerFactory{
    95  		client:           client,
    96  		namespace:        v1.NamespaceAll,
    97  		defaultResync:    defaultResync,
    98  		informers:        make(map[reflect.Type]cache.SharedIndexInformer),
    99  		startedInformers: make(map[reflect.Type]bool),
   100  		customResync:     make(map[reflect.Type]time.Duration),
   101  	}
   102  
   103  	// Apply all options
   104  	for _, opt := range options {
   105  		factory = opt(factory)
   106  	}
   107  
   108  	return factory
   109  }
   110  
   111  // Start initializes all requested informers.
   112  func (f *sharedInformerFactory) Start(stopCh <-chan struct{}) {
   113  	f.lock.Lock()
   114  	defer f.lock.Unlock()
   115  
   116  	for informerType, informer := range f.informers {
   117  		if !f.startedInformers[informerType] {
   118  			go informer.Run(stopCh)
   119  			f.startedInformers[informerType] = true
   120  		}
   121  	}
   122  }
   123  
   124  // WaitForCacheSync waits for all started informers' cache were synced.
   125  func (f *sharedInformerFactory) WaitForCacheSync(stopCh <-chan struct{}) map[reflect.Type]bool {
   126  	informers := func() map[reflect.Type]cache.SharedIndexInformer {
   127  		f.lock.Lock()
   128  		defer f.lock.Unlock()
   129  
   130  		informers := map[reflect.Type]cache.SharedIndexInformer{}
   131  		for informerType, informer := range f.informers {
   132  			if f.startedInformers[informerType] {
   133  				informers[informerType] = informer
   134  			}
   135  		}
   136  		return informers
   137  	}()
   138  
   139  	res := map[reflect.Type]bool{}
   140  	for informType, informer := range informers {
   141  		res[informType] = cache.WaitForCacheSync(stopCh, informer.HasSynced)
   142  	}
   143  	return res
   144  }
   145  
   146  // InternalInformerFor returns the SharedIndexInformer for obj using an internal
   147  // client.
   148  func (f *sharedInformerFactory) InformerFor(obj runtime.Object, newFunc internalinterfaces.NewInformerFunc) cache.SharedIndexInformer {
   149  	f.lock.Lock()
   150  	defer f.lock.Unlock()
   151  
   152  	informerType := reflect.TypeOf(obj)
   153  	informer, exists := f.informers[informerType]
   154  	if exists {
   155  		return informer
   156  	}
   157  
   158  	resyncPeriod, exists := f.customResync[informerType]
   159  	if !exists {
   160  		resyncPeriod = f.defaultResync
   161  	}
   162  
   163  	informer = newFunc(f.client, resyncPeriod)
   164  	f.informers[informerType] = informer
   165  
   166  	return informer
   167  }
   168  
   169  // SharedInformerFactory provides shared informers for resources in all known
   170  // API group versions.
   171  type SharedInformerFactory interface {
   172  	internalinterfaces.SharedInformerFactory
   173  	ForResource(resource schema.GroupVersionResource) (GenericInformer, error)
   174  	WaitForCacheSync(stopCh <-chan struct{}) map[reflect.Type]bool
   175  
   176  	Topo() topo.Interface
   177  }
   178  
   179  func (f *sharedInformerFactory) Topo() topo.Interface {
   180  	return topo.New(f, f.namespace, f.tweakListOptions)
   181  }