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