k8s.io/client-go@v0.31.1/informers/factory.go (about)

     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  	// Warning: Start does not block. When run in a go-routine, it will race with a later WaitForCacheSync.
   251  	Start(stopCh <-chan struct{})
   252  
   253  	// Shutdown marks a factory as shutting down. At that point no new
   254  	// informers can be started anymore and Start will return without
   255  	// doing anything.
   256  	//
   257  	// In addition, Shutdown blocks until all goroutines have terminated. For that
   258  	// to happen, the close channel(s) that they were started with must be closed,
   259  	// either before Shutdown gets called or while it is waiting.
   260  	//
   261  	// Shutdown may be called multiple times, even concurrently. All such calls will
   262  	// block until all goroutines have terminated.
   263  	Shutdown()
   264  
   265  	// WaitForCacheSync blocks until all started informers' caches were synced
   266  	// or the stop channel gets closed.
   267  	WaitForCacheSync(stopCh <-chan struct{}) map[reflect.Type]bool
   268  
   269  	// ForResource gives generic access to a shared informer of the matching type.
   270  	ForResource(resource schema.GroupVersionResource) (GenericInformer, error)
   271  
   272  	// InformerFor returns the SharedIndexInformer for obj using an internal
   273  	// client.
   274  	InformerFor(obj runtime.Object, newFunc internalinterfaces.NewInformerFunc) cache.SharedIndexInformer
   275  
   276  	Admissionregistration() admissionregistration.Interface
   277  	Internal() apiserverinternal.Interface
   278  	Apps() apps.Interface
   279  	Autoscaling() autoscaling.Interface
   280  	Batch() batch.Interface
   281  	Certificates() certificates.Interface
   282  	Coordination() coordination.Interface
   283  	Core() core.Interface
   284  	Discovery() discovery.Interface
   285  	Events() events.Interface
   286  	Extensions() extensions.Interface
   287  	Flowcontrol() flowcontrol.Interface
   288  	Networking() networking.Interface
   289  	Node() node.Interface
   290  	Policy() policy.Interface
   291  	Rbac() rbac.Interface
   292  	Resource() resource.Interface
   293  	Scheduling() scheduling.Interface
   294  	Storage() storage.Interface
   295  	Storagemigration() storagemigration.Interface
   296  }
   297  
   298  func (f *sharedInformerFactory) Admissionregistration() admissionregistration.Interface {
   299  	return admissionregistration.New(f, f.namespace, f.tweakListOptions)
   300  }
   301  
   302  func (f *sharedInformerFactory) Internal() apiserverinternal.Interface {
   303  	return apiserverinternal.New(f, f.namespace, f.tweakListOptions)
   304  }
   305  
   306  func (f *sharedInformerFactory) Apps() apps.Interface {
   307  	return apps.New(f, f.namespace, f.tweakListOptions)
   308  }
   309  
   310  func (f *sharedInformerFactory) Autoscaling() autoscaling.Interface {
   311  	return autoscaling.New(f, f.namespace, f.tweakListOptions)
   312  }
   313  
   314  func (f *sharedInformerFactory) Batch() batch.Interface {
   315  	return batch.New(f, f.namespace, f.tweakListOptions)
   316  }
   317  
   318  func (f *sharedInformerFactory) Certificates() certificates.Interface {
   319  	return certificates.New(f, f.namespace, f.tweakListOptions)
   320  }
   321  
   322  func (f *sharedInformerFactory) Coordination() coordination.Interface {
   323  	return coordination.New(f, f.namespace, f.tweakListOptions)
   324  }
   325  
   326  func (f *sharedInformerFactory) Core() core.Interface {
   327  	return core.New(f, f.namespace, f.tweakListOptions)
   328  }
   329  
   330  func (f *sharedInformerFactory) Discovery() discovery.Interface {
   331  	return discovery.New(f, f.namespace, f.tweakListOptions)
   332  }
   333  
   334  func (f *sharedInformerFactory) Events() events.Interface {
   335  	return events.New(f, f.namespace, f.tweakListOptions)
   336  }
   337  
   338  func (f *sharedInformerFactory) Extensions() extensions.Interface {
   339  	return extensions.New(f, f.namespace, f.tweakListOptions)
   340  }
   341  
   342  func (f *sharedInformerFactory) Flowcontrol() flowcontrol.Interface {
   343  	return flowcontrol.New(f, f.namespace, f.tweakListOptions)
   344  }
   345  
   346  func (f *sharedInformerFactory) Networking() networking.Interface {
   347  	return networking.New(f, f.namespace, f.tweakListOptions)
   348  }
   349  
   350  func (f *sharedInformerFactory) Node() node.Interface {
   351  	return node.New(f, f.namespace, f.tweakListOptions)
   352  }
   353  
   354  func (f *sharedInformerFactory) Policy() policy.Interface {
   355  	return policy.New(f, f.namespace, f.tweakListOptions)
   356  }
   357  
   358  func (f *sharedInformerFactory) Rbac() rbac.Interface {
   359  	return rbac.New(f, f.namespace, f.tweakListOptions)
   360  }
   361  
   362  func (f *sharedInformerFactory) Resource() resource.Interface {
   363  	return resource.New(f, f.namespace, f.tweakListOptions)
   364  }
   365  
   366  func (f *sharedInformerFactory) Scheduling() scheduling.Interface {
   367  	return scheduling.New(f, f.namespace, f.tweakListOptions)
   368  }
   369  
   370  func (f *sharedInformerFactory) Storage() storage.Interface {
   371  	return storage.New(f, f.namespace, f.tweakListOptions)
   372  }
   373  
   374  func (f *sharedInformerFactory) Storagemigration() storagemigration.Interface {
   375  	return storagemigration.New(f, f.namespace, f.tweakListOptions)
   376  }