k8s.io/client-go@v0.22.2/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  	scheduling "k8s.io/client-go/informers/scheduling"
    47  	storage "k8s.io/client-go/informers/storage"
    48  	kubernetes "k8s.io/client-go/kubernetes"
    49  	cache "k8s.io/client-go/tools/cache"
    50  )
    51  
    52  // SharedInformerOption defines the functional option type for SharedInformerFactory.
    53  type SharedInformerOption func(*sharedInformerFactory) *sharedInformerFactory
    54  
    55  type sharedInformerFactory struct {
    56  	client           kubernetes.Interface
    57  	namespace        string
    58  	tweakListOptions internalinterfaces.TweakListOptionsFunc
    59  	lock             sync.Mutex
    60  	defaultResync    time.Duration
    61  	customResync     map[reflect.Type]time.Duration
    62  
    63  	informers map[reflect.Type]cache.SharedIndexInformer
    64  	// startedInformers is used for tracking which informers have been started.
    65  	// This allows Start() to be called multiple times safely.
    66  	startedInformers map[reflect.Type]bool
    67  }
    68  
    69  // WithCustomResyncConfig sets a custom resync period for the specified informer types.
    70  func WithCustomResyncConfig(resyncConfig map[v1.Object]time.Duration) SharedInformerOption {
    71  	return func(factory *sharedInformerFactory) *sharedInformerFactory {
    72  		for k, v := range resyncConfig {
    73  			factory.customResync[reflect.TypeOf(k)] = v
    74  		}
    75  		return factory
    76  	}
    77  }
    78  
    79  // WithTweakListOptions sets a custom filter on all listers of the configured SharedInformerFactory.
    80  func WithTweakListOptions(tweakListOptions internalinterfaces.TweakListOptionsFunc) SharedInformerOption {
    81  	return func(factory *sharedInformerFactory) *sharedInformerFactory {
    82  		factory.tweakListOptions = tweakListOptions
    83  		return factory
    84  	}
    85  }
    86  
    87  // WithNamespace limits the SharedInformerFactory to the specified namespace.
    88  func WithNamespace(namespace string) SharedInformerOption {
    89  	return func(factory *sharedInformerFactory) *sharedInformerFactory {
    90  		factory.namespace = namespace
    91  		return factory
    92  	}
    93  }
    94  
    95  // NewSharedInformerFactory constructs a new instance of sharedInformerFactory for all namespaces.
    96  func NewSharedInformerFactory(client kubernetes.Interface, defaultResync time.Duration) SharedInformerFactory {
    97  	return NewSharedInformerFactoryWithOptions(client, defaultResync)
    98  }
    99  
   100  // NewFilteredSharedInformerFactory constructs a new instance of sharedInformerFactory.
   101  // Listers obtained via this SharedInformerFactory will be subject to the same filters
   102  // as specified here.
   103  // Deprecated: Please use NewSharedInformerFactoryWithOptions instead
   104  func NewFilteredSharedInformerFactory(client kubernetes.Interface, defaultResync time.Duration, namespace string, tweakListOptions internalinterfaces.TweakListOptionsFunc) SharedInformerFactory {
   105  	return NewSharedInformerFactoryWithOptions(client, defaultResync, WithNamespace(namespace), WithTweakListOptions(tweakListOptions))
   106  }
   107  
   108  // NewSharedInformerFactoryWithOptions constructs a new instance of a SharedInformerFactory with additional options.
   109  func NewSharedInformerFactoryWithOptions(client kubernetes.Interface, defaultResync time.Duration, options ...SharedInformerOption) SharedInformerFactory {
   110  	factory := &sharedInformerFactory{
   111  		client:           client,
   112  		namespace:        v1.NamespaceAll,
   113  		defaultResync:    defaultResync,
   114  		informers:        make(map[reflect.Type]cache.SharedIndexInformer),
   115  		startedInformers: make(map[reflect.Type]bool),
   116  		customResync:     make(map[reflect.Type]time.Duration),
   117  	}
   118  
   119  	// Apply all options
   120  	for _, opt := range options {
   121  		factory = opt(factory)
   122  	}
   123  
   124  	return factory
   125  }
   126  
   127  // Start initializes all requested informers.
   128  func (f *sharedInformerFactory) Start(stopCh <-chan struct{}) {
   129  	f.lock.Lock()
   130  	defer f.lock.Unlock()
   131  
   132  	for informerType, informer := range f.informers {
   133  		if !f.startedInformers[informerType] {
   134  			go informer.Run(stopCh)
   135  			f.startedInformers[informerType] = true
   136  		}
   137  	}
   138  }
   139  
   140  // WaitForCacheSync waits for all started informers' cache were synced.
   141  func (f *sharedInformerFactory) WaitForCacheSync(stopCh <-chan struct{}) map[reflect.Type]bool {
   142  	informers := func() map[reflect.Type]cache.SharedIndexInformer {
   143  		f.lock.Lock()
   144  		defer f.lock.Unlock()
   145  
   146  		informers := map[reflect.Type]cache.SharedIndexInformer{}
   147  		for informerType, informer := range f.informers {
   148  			if f.startedInformers[informerType] {
   149  				informers[informerType] = informer
   150  			}
   151  		}
   152  		return informers
   153  	}()
   154  
   155  	res := map[reflect.Type]bool{}
   156  	for informType, informer := range informers {
   157  		res[informType] = cache.WaitForCacheSync(stopCh, informer.HasSynced)
   158  	}
   159  	return res
   160  }
   161  
   162  // InternalInformerFor returns the SharedIndexInformer for obj using an internal
   163  // client.
   164  func (f *sharedInformerFactory) InformerFor(obj runtime.Object, newFunc internalinterfaces.NewInformerFunc) cache.SharedIndexInformer {
   165  	f.lock.Lock()
   166  	defer f.lock.Unlock()
   167  
   168  	informerType := reflect.TypeOf(obj)
   169  	informer, exists := f.informers[informerType]
   170  	if exists {
   171  		return informer
   172  	}
   173  
   174  	resyncPeriod, exists := f.customResync[informerType]
   175  	if !exists {
   176  		resyncPeriod = f.defaultResync
   177  	}
   178  
   179  	informer = newFunc(f.client, resyncPeriod)
   180  	f.informers[informerType] = informer
   181  
   182  	return informer
   183  }
   184  
   185  // SharedInformerFactory provides shared informers for resources in all known
   186  // API group versions.
   187  type SharedInformerFactory interface {
   188  	internalinterfaces.SharedInformerFactory
   189  	ForResource(resource schema.GroupVersionResource) (GenericInformer, error)
   190  	WaitForCacheSync(stopCh <-chan struct{}) map[reflect.Type]bool
   191  
   192  	Admissionregistration() admissionregistration.Interface
   193  	Internal() apiserverinternal.Interface
   194  	Apps() apps.Interface
   195  	Autoscaling() autoscaling.Interface
   196  	Batch() batch.Interface
   197  	Certificates() certificates.Interface
   198  	Coordination() coordination.Interface
   199  	Core() core.Interface
   200  	Discovery() discovery.Interface
   201  	Events() events.Interface
   202  	Extensions() extensions.Interface
   203  	Flowcontrol() flowcontrol.Interface
   204  	Networking() networking.Interface
   205  	Node() node.Interface
   206  	Policy() policy.Interface
   207  	Rbac() rbac.Interface
   208  	Scheduling() scheduling.Interface
   209  	Storage() storage.Interface
   210  }
   211  
   212  func (f *sharedInformerFactory) Admissionregistration() admissionregistration.Interface {
   213  	return admissionregistration.New(f, f.namespace, f.tweakListOptions)
   214  }
   215  
   216  func (f *sharedInformerFactory) Internal() apiserverinternal.Interface {
   217  	return apiserverinternal.New(f, f.namespace, f.tweakListOptions)
   218  }
   219  
   220  func (f *sharedInformerFactory) Apps() apps.Interface {
   221  	return apps.New(f, f.namespace, f.tweakListOptions)
   222  }
   223  
   224  func (f *sharedInformerFactory) Autoscaling() autoscaling.Interface {
   225  	return autoscaling.New(f, f.namespace, f.tweakListOptions)
   226  }
   227  
   228  func (f *sharedInformerFactory) Batch() batch.Interface {
   229  	return batch.New(f, f.namespace, f.tweakListOptions)
   230  }
   231  
   232  func (f *sharedInformerFactory) Certificates() certificates.Interface {
   233  	return certificates.New(f, f.namespace, f.tweakListOptions)
   234  }
   235  
   236  func (f *sharedInformerFactory) Coordination() coordination.Interface {
   237  	return coordination.New(f, f.namespace, f.tweakListOptions)
   238  }
   239  
   240  func (f *sharedInformerFactory) Core() core.Interface {
   241  	return core.New(f, f.namespace, f.tweakListOptions)
   242  }
   243  
   244  func (f *sharedInformerFactory) Discovery() discovery.Interface {
   245  	return discovery.New(f, f.namespace, f.tweakListOptions)
   246  }
   247  
   248  func (f *sharedInformerFactory) Events() events.Interface {
   249  	return events.New(f, f.namespace, f.tweakListOptions)
   250  }
   251  
   252  func (f *sharedInformerFactory) Extensions() extensions.Interface {
   253  	return extensions.New(f, f.namespace, f.tweakListOptions)
   254  }
   255  
   256  func (f *sharedInformerFactory) Flowcontrol() flowcontrol.Interface {
   257  	return flowcontrol.New(f, f.namespace, f.tweakListOptions)
   258  }
   259  
   260  func (f *sharedInformerFactory) Networking() networking.Interface {
   261  	return networking.New(f, f.namespace, f.tweakListOptions)
   262  }
   263  
   264  func (f *sharedInformerFactory) Node() node.Interface {
   265  	return node.New(f, f.namespace, f.tweakListOptions)
   266  }
   267  
   268  func (f *sharedInformerFactory) Policy() policy.Interface {
   269  	return policy.New(f, f.namespace, f.tweakListOptions)
   270  }
   271  
   272  func (f *sharedInformerFactory) Rbac() rbac.Interface {
   273  	return rbac.New(f, f.namespace, f.tweakListOptions)
   274  }
   275  
   276  func (f *sharedInformerFactory) Scheduling() scheduling.Interface {
   277  	return scheduling.New(f, f.namespace, f.tweakListOptions)
   278  }
   279  
   280  func (f *sharedInformerFactory) Storage() storage.Interface {
   281  	return storage.New(f, f.namespace, f.tweakListOptions)
   282  }