knative.dev/pkg@v0.0.0-20260602142205-ac97e43f6622/injection/interface.go (about)

     1  /*
     2  Copyright 2019 The Knative 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  package injection
    18  
    19  import (
    20  	"context"
    21  	"sync"
    22  
    23  	"k8s.io/client-go/rest"
    24  
    25  	"knative.dev/pkg/configmap"
    26  	"knative.dev/pkg/controller"
    27  )
    28  
    29  // Interface is the interface for interacting with injection
    30  // implementations, such as our Default and Fake below.
    31  type Interface interface {
    32  	// RegisterClient registers a new injector callback for associating
    33  	// a new client with a context.
    34  	RegisterClient(ClientInjector)
    35  
    36  	// GetClients fetches all of the registered client injectors.
    37  	GetClients() []ClientInjector
    38  
    39  	// RegisterClientFetcher registers a new callback that fetches a client from
    40  	// a given context.
    41  	RegisterClientFetcher(ClientFetcher)
    42  
    43  	// FetchAllClients returns all known clients from the given context.
    44  	FetchAllClients(context.Context) []interface{}
    45  
    46  	// RegisterInformerFactory registers a new injector callback for associating
    47  	// a new informer factory with a context.
    48  	RegisterInformerFactory(InformerFactoryInjector)
    49  
    50  	// GetInformerFactories fetches all of the registered informer factory injectors.
    51  	GetInformerFactories() []InformerFactoryInjector
    52  
    53  	// RegisterDuck registers a new duck.InformerFactory for a particular type.
    54  	RegisterDuck(ii DuckFactoryInjector)
    55  
    56  	// GetDucks accesses the set of registered ducks.
    57  	GetDucks() []DuckFactoryInjector
    58  
    59  	// RegisterInformer registers a new injector callback for associating
    60  	// a new informer with a context.
    61  	RegisterInformer(InformerInjector)
    62  
    63  	// RegisterFilteredInformers registers a new filtered informer injector callback for associating
    64  	// a new set of informers with a context.
    65  	RegisterFilteredInformers(FilteredInformersInjector)
    66  
    67  	// GetInformers fetches all of the registered informer injectors.
    68  	GetInformers() []InformerInjector
    69  
    70  	// GetFilteredInformers fetches all of the registered filtered informer injectors.
    71  	GetFilteredInformers() []FilteredInformersInjector
    72  
    73  	// SetupInformers runs all of the injectors against a context, starting with
    74  	// the clients and the given rest.Config.  The resulting context is returned
    75  	// along with a list of the .Informer() for each of the injected informers,
    76  	// which is suitable for passing to controller.StartInformers().
    77  	// This does not setup or start any controllers.
    78  	SetupInformers(context.Context, *rest.Config) (context.Context, []controller.Informer)
    79  }
    80  
    81  type ControllerConstructor func(context.Context, configmap.Watcher) *controller.Impl
    82  
    83  // NamedControllerConstructor is a ControllerConstructor with an associated name.
    84  type NamedControllerConstructor struct {
    85  	// Name is the name associated with the controller returned by ControllerConstructor.
    86  	Name string
    87  	// ControllerConstructor is a constructor for a controller.
    88  	ControllerConstructor ControllerConstructor
    89  }
    90  
    91  var (
    92  	// Check that impl implements Interface
    93  	_ Interface = (*impl)(nil)
    94  
    95  	// Default is the injection interface with which informers should register
    96  	// to make themselves available to the controller process when reconcilers
    97  	// are being run for real.
    98  	Default Interface = &impl{}
    99  
   100  	// Fake is the injection interface with which informers should register
   101  	// to make themselves available to the controller process when it is being
   102  	// unit tested.
   103  	Fake Interface = &impl{}
   104  )
   105  
   106  type impl struct {
   107  	m sync.RWMutex
   108  
   109  	clients           []ClientInjector
   110  	clientFetchers    []ClientFetcher
   111  	factories         []InformerFactoryInjector
   112  	informers         []InformerInjector
   113  	filteredInformers []FilteredInformersInjector
   114  	ducks             []DuckFactoryInjector
   115  }