...

Source file src/github.com/linkerd/linkerd2/controller/gen/client/informers/externalversions/factory.go

Documentation: github.com/linkerd/linkerd2/controller/gen/client/informers/externalversions

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

View as plain text