github.com/verrazzano/verrazzano-monitoring-operator@v0.0.30/pkg/client/informers/externalversions/factory.go (about)

     1  // Copyright (c) 2020, 2022, Oracle and/or its affiliates.
     2  // Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
     3  
     4  // Code generated by informer-gen. DO NOT EDIT.
     5  
     6  package externalversions
     7  
     8  import (
     9  	reflect "reflect"
    10  	sync "sync"
    11  	time "time"
    12  
    13  	versioned "github.com/verrazzano/verrazzano-monitoring-operator/pkg/client/clientset/versioned"
    14  	internalinterfaces "github.com/verrazzano/verrazzano-monitoring-operator/pkg/client/informers/externalversions/internalinterfaces"
    15  	vmcontroller "github.com/verrazzano/verrazzano-monitoring-operator/pkg/client/informers/externalversions/vmcontroller"
    16  	v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    17  	runtime "k8s.io/apimachinery/pkg/runtime"
    18  	schema "k8s.io/apimachinery/pkg/runtime/schema"
    19  	cache "k8s.io/client-go/tools/cache"
    20  )
    21  
    22  // SharedInformerOption defines the functional option type for SharedInformerFactory.
    23  type SharedInformerOption func(*sharedInformerFactory) *sharedInformerFactory
    24  
    25  type sharedInformerFactory struct {
    26  	client           versioned.Interface
    27  	namespace        string
    28  	tweakListOptions internalinterfaces.TweakListOptionsFunc
    29  	lock             sync.Mutex
    30  	defaultResync    time.Duration
    31  	customResync     map[reflect.Type]time.Duration
    32  
    33  	informers map[reflect.Type]cache.SharedIndexInformer
    34  	// startedInformers is used for tracking which informers have been started.
    35  	// This allows Start() to be called multiple times safely.
    36  	startedInformers map[reflect.Type]bool
    37  }
    38  
    39  // WithCustomResyncConfig sets a custom resync period for the specified informer types.
    40  func WithCustomResyncConfig(resyncConfig map[v1.Object]time.Duration) SharedInformerOption {
    41  	return func(factory *sharedInformerFactory) *sharedInformerFactory {
    42  		for k, v := range resyncConfig {
    43  			factory.customResync[reflect.TypeOf(k)] = v
    44  		}
    45  		return factory
    46  	}
    47  }
    48  
    49  // WithTweakListOptions sets a custom filter on all listers of the configured SharedInformerFactory.
    50  func WithTweakListOptions(tweakListOptions internalinterfaces.TweakListOptionsFunc) SharedInformerOption {
    51  	return func(factory *sharedInformerFactory) *sharedInformerFactory {
    52  		factory.tweakListOptions = tweakListOptions
    53  		return factory
    54  	}
    55  }
    56  
    57  // WithNamespace limits the SharedInformerFactory to the specified namespace.
    58  func WithNamespace(namespace string) SharedInformerOption {
    59  	return func(factory *sharedInformerFactory) *sharedInformerFactory {
    60  		factory.namespace = namespace
    61  		return factory
    62  	}
    63  }
    64  
    65  // NewSharedInformerFactory constructs a new instance of sharedInformerFactory for all namespaces.
    66  func NewSharedInformerFactory(client versioned.Interface, defaultResync time.Duration) SharedInformerFactory {
    67  	return NewSharedInformerFactoryWithOptions(client, defaultResync)
    68  }
    69  
    70  // NewFilteredSharedInformerFactory constructs a new instance of sharedInformerFactory.
    71  // Listers obtained via this SharedInformerFactory will be subject to the same filters
    72  // as specified here.
    73  // Deprecated: Please use NewSharedInformerFactoryWithOptions instead
    74  func NewFilteredSharedInformerFactory(client versioned.Interface, defaultResync time.Duration, namespace string, tweakListOptions internalinterfaces.TweakListOptionsFunc) SharedInformerFactory {
    75  	return NewSharedInformerFactoryWithOptions(client, defaultResync, WithNamespace(namespace), WithTweakListOptions(tweakListOptions))
    76  }
    77  
    78  // NewSharedInformerFactoryWithOptions constructs a new instance of a SharedInformerFactory with additional options.
    79  func NewSharedInformerFactoryWithOptions(client versioned.Interface, defaultResync time.Duration, options ...SharedInformerOption) SharedInformerFactory {
    80  	factory := &sharedInformerFactory{
    81  		client:           client,
    82  		namespace:        v1.NamespaceAll,
    83  		defaultResync:    defaultResync,
    84  		informers:        make(map[reflect.Type]cache.SharedIndexInformer),
    85  		startedInformers: make(map[reflect.Type]bool),
    86  		customResync:     make(map[reflect.Type]time.Duration),
    87  	}
    88  
    89  	// Apply all options
    90  	for _, opt := range options {
    91  		factory = opt(factory)
    92  	}
    93  
    94  	return factory
    95  }
    96  
    97  // Start initializes all requested informers.
    98  func (f *sharedInformerFactory) Start(stopCh <-chan struct{}) {
    99  	f.lock.Lock()
   100  	defer f.lock.Unlock()
   101  
   102  	for informerType, informer := range f.informers {
   103  		if !f.startedInformers[informerType] {
   104  			go informer.Run(stopCh)
   105  			f.startedInformers[informerType] = true
   106  		}
   107  	}
   108  }
   109  
   110  // WaitForCacheSync waits for all started informers' cache were synced.
   111  func (f *sharedInformerFactory) WaitForCacheSync(stopCh <-chan struct{}) map[reflect.Type]bool {
   112  	informers := func() map[reflect.Type]cache.SharedIndexInformer {
   113  		f.lock.Lock()
   114  		defer f.lock.Unlock()
   115  
   116  		informers := map[reflect.Type]cache.SharedIndexInformer{}
   117  		for informerType, informer := range f.informers {
   118  			if f.startedInformers[informerType] {
   119  				informers[informerType] = informer
   120  			}
   121  		}
   122  		return informers
   123  	}()
   124  
   125  	res := map[reflect.Type]bool{}
   126  	for informType, informer := range informers {
   127  		res[informType] = cache.WaitForCacheSync(stopCh, informer.HasSynced)
   128  	}
   129  	return res
   130  }
   131  
   132  // InternalInformerFor returns the SharedIndexInformer for obj using an internal
   133  // client.
   134  func (f *sharedInformerFactory) InformerFor(obj runtime.Object, newFunc internalinterfaces.NewInformerFunc) cache.SharedIndexInformer {
   135  	f.lock.Lock()
   136  	defer f.lock.Unlock()
   137  
   138  	informerType := reflect.TypeOf(obj)
   139  	informer, exists := f.informers[informerType]
   140  	if exists {
   141  		return informer
   142  	}
   143  
   144  	resyncPeriod, exists := f.customResync[informerType]
   145  	if !exists {
   146  		resyncPeriod = f.defaultResync
   147  	}
   148  
   149  	informer = newFunc(f.client, resyncPeriod)
   150  	f.informers[informerType] = informer
   151  
   152  	return informer
   153  }
   154  
   155  // SharedInformerFactory provides shared informers for resources in all known
   156  // API group versions.
   157  type SharedInformerFactory interface {
   158  	internalinterfaces.SharedInformerFactory
   159  	ForResource(resource schema.GroupVersionResource) (GenericInformer, error)
   160  	WaitForCacheSync(stopCh <-chan struct{}) map[reflect.Type]bool
   161  
   162  	Verrazzano() vmcontroller.Interface
   163  }
   164  
   165  func (f *sharedInformerFactory) Verrazzano() vmcontroller.Interface {
   166  	return vmcontroller.New(f, f.namespace, f.tweakListOptions)
   167  }