github.com/zppinho/prow@v0.0.0-20240510014325-1738badeb017/pkg/client/informers/externalversions/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 externalversions
    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  	cache "k8s.io/client-go/tools/cache"
    30  	versioned "sigs.k8s.io/prow/pkg/client/clientset/versioned"
    31  	internalinterfaces "sigs.k8s.io/prow/pkg/client/informers/externalversions/internalinterfaces"
    32  	prowjobs "sigs.k8s.io/prow/pkg/client/informers/externalversions/prowjobs"
    33  )
    34  
    35  // SharedInformerOption defines the functional option type for SharedInformerFactory.
    36  type SharedInformerOption func(*sharedInformerFactory) *sharedInformerFactory
    37  
    38  type sharedInformerFactory struct {
    39  	client           versioned.Interface
    40  	namespace        string
    41  	tweakListOptions internalinterfaces.TweakListOptionsFunc
    42  	lock             sync.Mutex
    43  	defaultResync    time.Duration
    44  	customResync     map[reflect.Type]time.Duration
    45  
    46  	informers map[reflect.Type]cache.SharedIndexInformer
    47  	// startedInformers is used for tracking which informers have been started.
    48  	// This allows Start() to be called multiple times safely.
    49  	startedInformers map[reflect.Type]bool
    50  }
    51  
    52  // WithCustomResyncConfig sets a custom resync period for the specified informer types.
    53  func WithCustomResyncConfig(resyncConfig map[v1.Object]time.Duration) SharedInformerOption {
    54  	return func(factory *sharedInformerFactory) *sharedInformerFactory {
    55  		for k, v := range resyncConfig {
    56  			factory.customResync[reflect.TypeOf(k)] = v
    57  		}
    58  		return factory
    59  	}
    60  }
    61  
    62  // WithTweakListOptions sets a custom filter on all listers of the configured SharedInformerFactory.
    63  func WithTweakListOptions(tweakListOptions internalinterfaces.TweakListOptionsFunc) SharedInformerOption {
    64  	return func(factory *sharedInformerFactory) *sharedInformerFactory {
    65  		factory.tweakListOptions = tweakListOptions
    66  		return factory
    67  	}
    68  }
    69  
    70  // WithNamespace limits the SharedInformerFactory to the specified namespace.
    71  func WithNamespace(namespace string) SharedInformerOption {
    72  	return func(factory *sharedInformerFactory) *sharedInformerFactory {
    73  		factory.namespace = namespace
    74  		return factory
    75  	}
    76  }
    77  
    78  // NewSharedInformerFactory constructs a new instance of sharedInformerFactory for all namespaces.
    79  func NewSharedInformerFactory(client versioned.Interface, defaultResync time.Duration) SharedInformerFactory {
    80  	return NewSharedInformerFactoryWithOptions(client, defaultResync)
    81  }
    82  
    83  // NewFilteredSharedInformerFactory constructs a new instance of sharedInformerFactory.
    84  // Listers obtained via this SharedInformerFactory will be subject to the same filters
    85  // as specified here.
    86  // Deprecated: Please use NewSharedInformerFactoryWithOptions instead
    87  func NewFilteredSharedInformerFactory(client versioned.Interface, defaultResync time.Duration, namespace string, tweakListOptions internalinterfaces.TweakListOptionsFunc) SharedInformerFactory {
    88  	return NewSharedInformerFactoryWithOptions(client, defaultResync, WithNamespace(namespace), WithTweakListOptions(tweakListOptions))
    89  }
    90  
    91  // NewSharedInformerFactoryWithOptions constructs a new instance of a SharedInformerFactory with additional options.
    92  func NewSharedInformerFactoryWithOptions(client versioned.Interface, defaultResync time.Duration, options ...SharedInformerOption) SharedInformerFactory {
    93  	factory := &sharedInformerFactory{
    94  		client:           client,
    95  		namespace:        v1.NamespaceAll,
    96  		defaultResync:    defaultResync,
    97  		informers:        make(map[reflect.Type]cache.SharedIndexInformer),
    98  		startedInformers: make(map[reflect.Type]bool),
    99  		customResync:     make(map[reflect.Type]time.Duration),
   100  	}
   101  
   102  	// Apply all options
   103  	for _, opt := range options {
   104  		factory = opt(factory)
   105  	}
   106  
   107  	return factory
   108  }
   109  
   110  // Start initializes all requested informers.
   111  func (f *sharedInformerFactory) Start(stopCh <-chan struct{}) {
   112  	f.lock.Lock()
   113  	defer f.lock.Unlock()
   114  
   115  	for informerType, informer := range f.informers {
   116  		if !f.startedInformers[informerType] {
   117  			go informer.Run(stopCh)
   118  			f.startedInformers[informerType] = true
   119  		}
   120  	}
   121  }
   122  
   123  // WaitForCacheSync waits for all started informers' cache were synced.
   124  func (f *sharedInformerFactory) WaitForCacheSync(stopCh <-chan struct{}) map[reflect.Type]bool {
   125  	informers := func() map[reflect.Type]cache.SharedIndexInformer {
   126  		f.lock.Lock()
   127  		defer f.lock.Unlock()
   128  
   129  		informers := map[reflect.Type]cache.SharedIndexInformer{}
   130  		for informerType, informer := range f.informers {
   131  			if f.startedInformers[informerType] {
   132  				informers[informerType] = informer
   133  			}
   134  		}
   135  		return informers
   136  	}()
   137  
   138  	res := map[reflect.Type]bool{}
   139  	for informType, informer := range informers {
   140  		res[informType] = cache.WaitForCacheSync(stopCh, informer.HasSynced)
   141  	}
   142  	return res
   143  }
   144  
   145  // InternalInformerFor returns the SharedIndexInformer for obj using an internal
   146  // client.
   147  func (f *sharedInformerFactory) InformerFor(obj runtime.Object, newFunc internalinterfaces.NewInformerFunc) cache.SharedIndexInformer {
   148  	f.lock.Lock()
   149  	defer f.lock.Unlock()
   150  
   151  	informerType := reflect.TypeOf(obj)
   152  	informer, exists := f.informers[informerType]
   153  	if exists {
   154  		return informer
   155  	}
   156  
   157  	resyncPeriod, exists := f.customResync[informerType]
   158  	if !exists {
   159  		resyncPeriod = f.defaultResync
   160  	}
   161  
   162  	informer = newFunc(f.client, resyncPeriod)
   163  	f.informers[informerType] = informer
   164  
   165  	return informer
   166  }
   167  
   168  // SharedInformerFactory provides shared informers for resources in all known
   169  // API group versions.
   170  type SharedInformerFactory interface {
   171  	internalinterfaces.SharedInformerFactory
   172  	ForResource(resource schema.GroupVersionResource) (GenericInformer, error)
   173  	WaitForCacheSync(stopCh <-chan struct{}) map[reflect.Type]bool
   174  
   175  	Prow() prowjobs.Interface
   176  }
   177  
   178  func (f *sharedInformerFactory) Prow() prowjobs.Interface {
   179  	return prowjobs.New(f, f.namespace, f.tweakListOptions)
   180  }