...

Source file src/k8s.io/client-go/informers/factory.go

Documentation: k8s.io/client-go/informers

     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 informers
    20  
    21  import (
    22  	reflect "reflect"
    23  	sync "sync"
    24  	time "time"
    25  
    26  	v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    27  	runtime "k8s.io/apimachinery/pkg/runtime"
    28  	schema "k8s.io/apimachinery/pkg/runtime/schema"
    29  	admissionregistration "k8s.io/client-go/informers/admissionregistration"
    30  	apiserverinternal "k8s.io/client-go/informers/apiserverinternal"
    31  	apps "k8s.io/client-go/informers/apps"
    32  	autoscaling "k8s.io/client-go/informers/autoscaling"
    33  	batch "k8s.io/client-go/informers/batch"
    34  	certificates "k8s.io/client-go/informers/certificates"
    35  	coordination "k8s.io/client-go/informers/coordination"
    36  	core "k8s.io/client-go/informers/core"
    37  	discovery "k8s.io/client-go/informers/discovery"
    38  	events "k8s.io/client-go/informers/events"
    39  	extensions "k8s.io/client-go/informers/extensions"
    40  	flowcontrol "k8s.io/client-go/informers/flowcontrol"
    41  	internalinterfaces "k8s.io/client-go/informers/internalinterfaces"
    42  	networking "k8s.io/client-go/informers/networking"
    43  	node "k8s.io/client-go/informers/node"
    44  	policy "k8s.io/client-go/informers/policy"
    45  	rbac "k8s.io/client-go/informers/rbac"
    46  	resource "k8s.io/client-go/informers/resource"
    47  	scheduling "k8s.io/client-go/informers/scheduling"
    48  	storage "k8s.io/client-go/informers/storage"
    49  	storagemigration "k8s.io/client-go/informers/storagemigration"
    50  	kubernetes "k8s.io/client-go/kubernetes"
    51  	cache "k8s.io/client-go/tools/cache"
    52  )
    53  
    54  // SharedInformerOption defines the functional option type for SharedInformerFactory.
    55  type SharedInformerOption func(*sharedInformerFactory) *sharedInformerFactory
    56  
    57  type sharedInformerFactory struct {
    58  	client           kubernetes.Interface
    59  	namespace        string
    60  	tweakListOptions internalinterfaces.TweakListOptionsFunc
    61  	lock             sync.Mutex
    62  	defaultResync    time.Duration
    63  	customResync     map[reflect.Type]time.Duration
    64  	transform        cache.TransformFunc
    65  
    66  	informers map[reflect.Type]cache.SharedIndexInformer
    67  	// startedInformers is used for tracking which informers have been started.
    68  	// This allows Start() to be called multiple times safely.
    69  	startedInformers map[reflect.Type]bool
    70  	// wg tracks how many goroutines were started.
    71  	wg sync.WaitGroup
    72  	// shuttingDown is true when Shutdown has been called. It may still be running
    73  	// because it needs to wait for goroutines.
    74  	shuttingDown bool
    75  }
    76  
    77  // WithCustomResyncConfig sets a custom resync period for the specified informer types.
    78  func WithCustomResyncConfig(resyncConfig map[v1.Object]time.Duration) SharedInformerOption {
    79  	return func(factory *sharedInformerFactory) *sharedInformerFactory {
    80  		for k, v := range resyncConfig {
    81  			factory.customResync[reflect.TypeOf(k)] = v
    82  		}
    83  		return factory
    84  	}
    85  }
    86  
    87  // WithTweakListOptions sets a custom filter on all listers of the configured SharedInformerFactory.
    88  func WithTweakListOptions(tweakListOptions internalinterfaces.TweakListOptionsFunc) SharedInformerOption {
    89  	return func(factory *sharedInformerFactory) *sharedInformerFactory {
    90  		factory.tweakListOptions = tweakListOptions
    91  		return factory
    92  	}
    93  }
    94  
    95  // WithNamespace limits the SharedInformerFactory to the specified namespace.
    96  func WithNamespace(namespace string) SharedInformerOption {
    97  	return func(factory *sharedInformerFactory) *sharedInformerFactory {
    98  		factory.namespace = namespace
    99  		return factory
   100  	}
   101  }
   102  
   103  // WithTransform sets a transform on all informers.
   104  func WithTransform(transform cache.TransformFunc) SharedInformerOption {
   105  	return func(factory *sharedInformerFactory) *sharedInformerFactory {
   106  		factory.transform = transform
   107  		return factory
   108  	}
   109  }
   110  
   111  // NewSharedInformerFactory constructs a new instance of sharedInformerFactory for all namespaces.
   112  func NewSharedInformerFactory(client kubernetes.Interface, defaultResync time.Duration) SharedInformerFactory {
   113  	return NewSharedInformerFactoryWithOptions(client, defaultResync)
   114  }
   115  
   116  // NewFilteredSharedInformerFactory constructs a new instance of sharedInformerFactory.
   117  // Listers obtained via this SharedInformerFactory will be subject to the same filters
   118  // as specified here.
   119  // Deprecated: Please use NewSharedInformerFactoryWithOptions instead
   120  func NewFilteredSharedInformerFactory(client kubernetes.Interface, defaultResync time.Duration, namespace string, tweakListOptions internalinterfaces.TweakListOptionsFunc) SharedInformerFactory {
   121  	return NewSharedInformerFactoryWithOptions(client, defaultResync, WithNamespace(namespace), WithTweakListOptions(tweakListOptions))
   122  }
   123  
   124  // NewSharedInformerFactoryWithOptions constructs a new instance of a SharedInformerFactory with additional options.
   125  func NewSharedInformerFactoryWithOptions(client kubernetes.Interface, defaultResync time.Duration, options ...SharedInformerOption) SharedInformerFactory {
   126  	factory := &sharedInformerFactory{
   127  		client:           client,
   128  		namespace:        v1.NamespaceAll,
   129  		defaultResync:    defaultResync,
   130  		informers:        make(map[reflect.Type]cache.SharedIndexInformer),
   131  		startedInformers: make(map[reflect.Type]bool),
   132  		customResync:     make(map[reflect.Type]time.Duration),
   133  	}
   134  
   135  	// Apply all options
   136  	for _, opt := range options {
   137  		factory = opt(factory)
   138  	}
   139  
   140  	return factory
   141  }
   142  
   143  func (f *sharedInformerFactory) Start(stopCh <-chan struct{}) {
   144  	f.lock.Lock()
   145  	defer f.lock.Unlock()
   146  
   147  	if f.shuttingDown {
   148  		return
   149  	}
   150  
   151  	for informerType, informer := range f.informers {
   152  		if !f.startedInformers[informerType] {
   153  			f.wg.Add(1)
   154  			// We need a new variable in each loop iteration,
   155  			// otherwise the goroutine would use the loop variable
   156  			// and that keeps changing.
   157  			informer := informer
   158  			go func() {
   159  				defer f.wg.Done()
   160  				informer.Run(stopCh)
   161  			}()
   162  			f.startedInformers[informerType] = true
   163  		}
   164  	}
   165  }
   166  
   167  func (f *sharedInformerFactory) Shutdown() {
   168  	f.lock.Lock()
   169  	f.shuttingDown = true
   170  	f.lock.Unlock()
   171  
   172  	// Will return immediately if there is nothing to wait for.
   173  	f.wg.Wait()
   174  }
   175  
   176  func (f *sharedInformerFactory) WaitForCacheSync(stopCh <-chan struct{}) map[reflect.Type]bool {
   177  	informers := func() map[reflect.Type]cache.SharedIndexInformer {
   178  		f.lock.Lock()
   179  		defer f.lock.Unlock()
   180  
   181  		informers := map[reflect.Type]cache.SharedIndexInformer{}
   182  		for informerType, informer := range f.informers {
   183  			if f.startedInformers[informerType] {
   184  				informers[informerType] = informer
   185  			}
   186  		}
   187  		return informers
   188  	}()
   189  
   190  	res := map[reflect.Type]bool{}
   191  	for informType, informer := range informers {
   192  		res[informType] = cache.WaitForCacheSync(stopCh, informer.HasSynced)
   193  	}
   194  	return res
   195  }
   196  
   197  // InformerFor returns the SharedIndexInformer for obj using an internal
   198  // client.
   199  func (f *sharedInformerFactory) InformerFor(obj runtime.Object, newFunc internalinterfaces.NewInformerFunc) cache.SharedIndexInformer {
   200  	f.lock.Lock()
   201  	defer f.lock.Unlock()
   202  
   203  	informerType := reflect.TypeOf(obj)
   204  	informer, exists := f.informers[informerType]
   205  	if exists {
   206  		return informer
   207  	}
   208  
   209  	resyncPeriod, exists := f.customResync[informerType]
   210  	if !exists {
   211  		resyncPeriod = f.defaultResync
   212  	}
   213  
   214  	informer = newFunc(f.client, resyncPeriod)
   215  	informer.SetTransform(f.transform)
   216  	f.informers[informerType] = informer
   217  
   218  	return informer
   219  }
   220  
   221  // SharedInformerFactory provides shared informers for resources in all known
   222  // API group versions.
   223  //
   224  // It is typically used like this:
   225  //
   226  //	ctx, cancel := context.Background()
   227  //	defer cancel()
   228  //	factory := NewSharedInformerFactory(client, resyncPeriod)
   229  //	defer factory.WaitForStop()    // Returns immediately if nothing was started.
   230  //	genericInformer := factory.ForResource(resource)
   231  //	typedInformer := factory.SomeAPIGroup().V1().SomeType()
   232  //	factory.Start(ctx.Done())          // Start processing these informers.
   233  //	synced := factory.WaitForCacheSync(ctx.Done())
   234  //	for v, ok := range synced {
   235  //	    if !ok {
   236  //	        fmt.Fprintf(os.Stderr, "caches failed to sync: %v", v)
   237  //	        return
   238  //	    }
   239  //	}
   240  //
   241  //	// Creating informers can also be created after Start, but then
   242  //	// Start must be called again:
   243  //	anotherGenericInformer := factory.ForResource(resource)
   244  //	factory.Start(ctx.Done())
   245  type SharedInformerFactory interface {
   246  	internalinterfaces.SharedInformerFactory
   247  
   248  	// Start initializes all requested informers. They are handled in goroutines
   249  	// which run until the stop channel gets closed.
   250  	Start(stopCh <-chan struct{})
   251  
   252  	// Shutdown marks a factory as shutting down. At that point no new
   253  	// informers can be started anymore and Start will return without
   254  	// doing anything.
   255  	//
   256  	// In addition, Shutdown blocks until all goroutines have terminated. For that
   257  	// to happen, the close channel(s) that they were started with must be closed,
   258  	// either before Shutdown gets called or while it is waiting.
   259  	//
   260  	// Shutdown may be called multiple times, even concurrently. All such calls will
   261  	// block until all goroutines have terminated.
   262  	Shutdown()
   263  
   264  	// WaitForCacheSync blocks until all started informers' caches were synced
   265  	// or the stop channel gets closed.
   266  	WaitForCacheSync(stopCh <-chan struct{}) map[reflect.Type]bool
   267  
   268  	// ForResource gives generic access to a shared informer of the matching type.
   269  	ForResource(resource schema.GroupVersionResource) (GenericInformer, error)
   270  
   271  	// InformerFor returns the SharedIndexInformer for obj using an internal
   272  	// client.
   273  	InformerFor(obj runtime.Object, newFunc internalinterfaces.NewInformerFunc) cache.SharedIndexInformer
   274  
   275  	Admissionregistration() admissionregistration.Interface
   276  	Internal() apiserverinternal.Interface
   277  	Apps() apps.Interface
   278  	Autoscaling() autoscaling.Interface
   279  	Batch() batch.Interface
   280  	Certificates() certificates.Interface
   281  	Coordination() coordination.Interface
   282  	Core() core.Interface
   283  	Discovery() discovery.Interface
   284  	Events() events.Interface
   285  	Extensions() extensions.Interface
   286  	Flowcontrol() flowcontrol.Interface
   287  	Networking() networking.Interface
   288  	Node() node.Interface
   289  	Policy() policy.Interface
   290  	Rbac() rbac.Interface
   291  	Resource() resource.Interface
   292  	Scheduling() scheduling.Interface
   293  	Storage() storage.Interface
   294  	Storagemigration() storagemigration.Interface
   295  }
   296  
   297  func (f *sharedInformerFactory) Admissionregistration() admissionregistration.Interface {
   298  	return admissionregistration.New(f, f.namespace, f.tweakListOptions)
   299  }
   300  
   301  func (f *sharedInformerFactory) Internal() apiserverinternal.Interface {
   302  	return apiserverinternal.New(f, f.namespace, f.tweakListOptions)
   303  }
   304  
   305  func (f *sharedInformerFactory) Apps() apps.Interface {
   306  	return apps.New(f, f.namespace, f.tweakListOptions)
   307  }
   308  
   309  func (f *sharedInformerFactory) Autoscaling() autoscaling.Interface {
   310  	return autoscaling.New(f, f.namespace, f.tweakListOptions)
   311  }
   312  
   313  func (f *sharedInformerFactory) Batch() batch.Interface {
   314  	return batch.New(f, f.namespace, f.tweakListOptions)
   315  }
   316  
   317  func (f *sharedInformerFactory) Certificates() certificates.Interface {
   318  	return certificates.New(f, f.namespace, f.tweakListOptions)
   319  }
   320  
   321  func (f *sharedInformerFactory) Coordination() coordination.Interface {
   322  	return coordination.New(f, f.namespace, f.tweakListOptions)
   323  }
   324  
   325  func (f *sharedInformerFactory) Core() core.Interface {
   326  	return core.New(f, f.namespace, f.tweakListOptions)
   327  }
   328  
   329  func (f *sharedInformerFactory) Discovery() discovery.Interface {
   330  	return discovery.New(f, f.namespace, f.tweakListOptions)
   331  }
   332  
   333  func (f *sharedInformerFactory) Events() events.Interface {
   334  	return events.New(f, f.namespace, f.tweakListOptions)
   335  }
   336  
   337  func (f *sharedInformerFactory) Extensions() extensions.Interface {
   338  	return extensions.New(f, f.namespace, f.tweakListOptions)
   339  }
   340  
   341  func (f *sharedInformerFactory) Flowcontrol() flowcontrol.Interface {
   342  	return flowcontrol.New(f, f.namespace, f.tweakListOptions)
   343  }
   344  
   345  func (f *sharedInformerFactory) Networking() networking.Interface {
   346  	return networking.New(f, f.namespace, f.tweakListOptions)
   347  }
   348  
   349  func (f *sharedInformerFactory) Node() node.Interface {
   350  	return node.New(f, f.namespace, f.tweakListOptions)
   351  }
   352  
   353  func (f *sharedInformerFactory) Policy() policy.Interface {
   354  	return policy.New(f, f.namespace, f.tweakListOptions)
   355  }
   356  
   357  func (f *sharedInformerFactory) Rbac() rbac.Interface {
   358  	return rbac.New(f, f.namespace, f.tweakListOptions)
   359  }
   360  
   361  func (f *sharedInformerFactory) Resource() resource.Interface {
   362  	return resource.New(f, f.namespace, f.tweakListOptions)
   363  }
   364  
   365  func (f *sharedInformerFactory) Scheduling() scheduling.Interface {
   366  	return scheduling.New(f, f.namespace, f.tweakListOptions)
   367  }
   368  
   369  func (f *sharedInformerFactory) Storage() storage.Interface {
   370  	return storage.New(f, f.namespace, f.tweakListOptions)
   371  }
   372  
   373  func (f *sharedInformerFactory) Storagemigration() storagemigration.Interface {
   374  	return storagemigration.New(f, f.namespace, f.tweakListOptions)
   375  }
   376  

View as plain text