...

Source file src/k8s.io/apiextensions-apiserver/pkg/client/informers/externalversions/factory.go

Documentation: k8s.io/apiextensions-apiserver/pkg/client/informers/externalversions

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

View as plain text