github.com/argoproj/argo-cd/v2@v2.10.9/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/v2/pkg/client/clientset/versioned"
    11  	application "github.com/argoproj/argo-cd/v2/pkg/client/informers/externalversions/application"
    12  	internalinterfaces "github.com/argoproj/argo-cd/v2/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  	// wg tracks how many goroutines were started.
    35  	wg sync.WaitGroup
    36  	// shuttingDown is true when Shutdown has been called. It may still be running
    37  	// because it needs to wait for goroutines.
    38  	shuttingDown bool
    39  }
    40  
    41  // WithCustomResyncConfig sets a custom resync period for the specified informer types.
    42  func WithCustomResyncConfig(resyncConfig map[v1.Object]time.Duration) SharedInformerOption {
    43  	return func(factory *sharedInformerFactory) *sharedInformerFactory {
    44  		for k, v := range resyncConfig {
    45  			factory.customResync[reflect.TypeOf(k)] = v
    46  		}
    47  		return factory
    48  	}
    49  }
    50  
    51  // WithTweakListOptions sets a custom filter on all listers of the configured SharedInformerFactory.
    52  func WithTweakListOptions(tweakListOptions internalinterfaces.TweakListOptionsFunc) SharedInformerOption {
    53  	return func(factory *sharedInformerFactory) *sharedInformerFactory {
    54  		factory.tweakListOptions = tweakListOptions
    55  		return factory
    56  	}
    57  }
    58  
    59  // WithNamespace limits the SharedInformerFactory to the specified namespace.
    60  func WithNamespace(namespace string) SharedInformerOption {
    61  	return func(factory *sharedInformerFactory) *sharedInformerFactory {
    62  		factory.namespace = namespace
    63  		return factory
    64  	}
    65  }
    66  
    67  // NewSharedInformerFactory constructs a new instance of sharedInformerFactory for all namespaces.
    68  func NewSharedInformerFactory(client versioned.Interface, defaultResync time.Duration) SharedInformerFactory {
    69  	return NewSharedInformerFactoryWithOptions(client, defaultResync)
    70  }
    71  
    72  // NewFilteredSharedInformerFactory constructs a new instance of sharedInformerFactory.
    73  // Listers obtained via this SharedInformerFactory will be subject to the same filters
    74  // as specified here.
    75  // Deprecated: Please use NewSharedInformerFactoryWithOptions instead
    76  func NewFilteredSharedInformerFactory(client versioned.Interface, defaultResync time.Duration, namespace string, tweakListOptions internalinterfaces.TweakListOptionsFunc) SharedInformerFactory {
    77  	return NewSharedInformerFactoryWithOptions(client, defaultResync, WithNamespace(namespace), WithTweakListOptions(tweakListOptions))
    78  }
    79  
    80  // NewSharedInformerFactoryWithOptions constructs a new instance of a SharedInformerFactory with additional options.
    81  func NewSharedInformerFactoryWithOptions(client versioned.Interface, defaultResync time.Duration, options ...SharedInformerOption) SharedInformerFactory {
    82  	factory := &sharedInformerFactory{
    83  		client:           client,
    84  		namespace:        v1.NamespaceAll,
    85  		defaultResync:    defaultResync,
    86  		informers:        make(map[reflect.Type]cache.SharedIndexInformer),
    87  		startedInformers: make(map[reflect.Type]bool),
    88  		customResync:     make(map[reflect.Type]time.Duration),
    89  	}
    90  
    91  	// Apply all options
    92  	for _, opt := range options {
    93  		factory = opt(factory)
    94  	}
    95  
    96  	return factory
    97  }
    98  
    99  func (f *sharedInformerFactory) Start(stopCh <-chan struct{}) {
   100  	f.lock.Lock()
   101  	defer f.lock.Unlock()
   102  
   103  	if f.shuttingDown {
   104  		return
   105  	}
   106  
   107  	for informerType, informer := range f.informers {
   108  		if !f.startedInformers[informerType] {
   109  			f.wg.Add(1)
   110  			// We need a new variable in each loop iteration,
   111  			// otherwise the goroutine would use the loop variable
   112  			// and that keeps changing.
   113  			informer := informer
   114  			go func() {
   115  				defer f.wg.Done()
   116  				informer.Run(stopCh)
   117  			}()
   118  			f.startedInformers[informerType] = true
   119  		}
   120  	}
   121  }
   122  
   123  func (f *sharedInformerFactory) Shutdown() {
   124  	f.lock.Lock()
   125  	f.shuttingDown = true
   126  	f.lock.Unlock()
   127  
   128  	// Will return immediately if there is nothing to wait for.
   129  	f.wg.Wait()
   130  }
   131  
   132  func (f *sharedInformerFactory) WaitForCacheSync(stopCh <-chan struct{}) map[reflect.Type]bool {
   133  	informers := func() map[reflect.Type]cache.SharedIndexInformer {
   134  		f.lock.Lock()
   135  		defer f.lock.Unlock()
   136  
   137  		informers := map[reflect.Type]cache.SharedIndexInformer{}
   138  		for informerType, informer := range f.informers {
   139  			if f.startedInformers[informerType] {
   140  				informers[informerType] = informer
   141  			}
   142  		}
   143  		return informers
   144  	}()
   145  
   146  	res := map[reflect.Type]bool{}
   147  	for informType, informer := range informers {
   148  		res[informType] = cache.WaitForCacheSync(stopCh, informer.HasSynced)
   149  	}
   150  	return res
   151  }
   152  
   153  // InternalInformerFor returns the SharedIndexInformer for obj using an internal
   154  // client.
   155  func (f *sharedInformerFactory) InformerFor(obj runtime.Object, newFunc internalinterfaces.NewInformerFunc) cache.SharedIndexInformer {
   156  	f.lock.Lock()
   157  	defer f.lock.Unlock()
   158  
   159  	informerType := reflect.TypeOf(obj)
   160  	informer, exists := f.informers[informerType]
   161  	if exists {
   162  		return informer
   163  	}
   164  
   165  	resyncPeriod, exists := f.customResync[informerType]
   166  	if !exists {
   167  		resyncPeriod = f.defaultResync
   168  	}
   169  
   170  	informer = newFunc(f.client, resyncPeriod)
   171  	f.informers[informerType] = informer
   172  
   173  	return informer
   174  }
   175  
   176  // SharedInformerFactory provides shared informers for resources in all known
   177  // API group versions.
   178  //
   179  // It is typically used like this:
   180  //
   181  //	ctx, cancel := context.Background()
   182  //	defer cancel()
   183  //	factory := NewSharedInformerFactory(client, resyncPeriod)
   184  //	defer factory.WaitForStop()    // Returns immediately if nothing was started.
   185  //	genericInformer := factory.ForResource(resource)
   186  //	typedInformer := factory.SomeAPIGroup().V1().SomeType()
   187  //	factory.Start(ctx.Done())          // Start processing these informers.
   188  //	synced := factory.WaitForCacheSync(ctx.Done())
   189  //	for v, ok := range synced {
   190  //	    if !ok {
   191  //	        fmt.Fprintf(os.Stderr, "caches failed to sync: %v", v)
   192  //	        return
   193  //	    }
   194  //	}
   195  //
   196  //	// Creating informers can also be created after Start, but then
   197  //	// Start must be called again:
   198  //	anotherGenericInformer := factory.ForResource(resource)
   199  //	factory.Start(ctx.Done())
   200  type SharedInformerFactory interface {
   201  	internalinterfaces.SharedInformerFactory
   202  
   203  	// Start initializes all requested informers. They are handled in goroutines
   204  	// which run until the stop channel gets closed.
   205  	Start(stopCh <-chan struct{})
   206  
   207  	// Shutdown marks a factory as shutting down. At that point no new
   208  	// informers can be started anymore and Start will return without
   209  	// doing anything.
   210  	//
   211  	// In addition, Shutdown blocks until all goroutines have terminated. For that
   212  	// to happen, the close channel(s) that they were started with must be closed,
   213  	// either before Shutdown gets called or while it is waiting.
   214  	//
   215  	// Shutdown may be called multiple times, even concurrently. All such calls will
   216  	// block until all goroutines have terminated.
   217  	Shutdown()
   218  
   219  	// WaitForCacheSync blocks until all started informers' caches were synced
   220  	// or the stop channel gets closed.
   221  	WaitForCacheSync(stopCh <-chan struct{}) map[reflect.Type]bool
   222  
   223  	// ForResource gives generic access to a shared informer of the matching type.
   224  	ForResource(resource schema.GroupVersionResource) (GenericInformer, error)
   225  
   226  	// InternalInformerFor returns the SharedIndexInformer for obj using an internal
   227  	// client.
   228  	InformerFor(obj runtime.Object, newFunc internalinterfaces.NewInformerFunc) cache.SharedIndexInformer
   229  
   230  	Argoproj() application.Interface
   231  }
   232  
   233  func (f *sharedInformerFactory) Argoproj() application.Interface {
   234  	return application.New(f, f.namespace, f.tweakListOptions)
   235  }