github.com/k8snetworkplumbingwg/sriov-network-operator@v1.2.1-0.20240408194816-2d2e5a45d453/pkg/client/informers/externalversions/factory.go (about)

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