github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/client/informers/externalversions/factory.go (about)

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