agones.dev/agones@v1.53.0/pkg/client/informers/externalversions/factory.go (about)

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