Documentation ¶
Overview ¶
Package injection defines the mechanisms through which clients, informers and shared informer factories are injected into a shared controller binary implementation.
There are two primary contexts where the usage of the injection package is interesting. The first is in the context of implementations of `controller.Reconciler` being wrapped in a `*controller.Impl`:
import ( // Simply linking this triggers the injection of the informer, which links // the factory triggering its injection, and which links the client, // triggering its injection. All you need to know is that it works :) deployinformer "knative.dev/pkg/injection/informers/kubeinformers/appsv1/deployment" "knative.dev/pkg/injection" ) func NewController(ctx context.Context) *controller.Impl { deploymentInformer := deployinformer.Get(ctx) // Pass deploymentInformer.Lister() to Reconciler ... // Set up events on deploymentInformer.Informer() ... }
Then in `package main` the entire controller process can be set up via:
package main import ( // The set of controllers this controller process runs. // Linking these will register their transitive dependencies, after // which the shared main can set up the rest. "github.com/knative/foo/pkg/reconciler/matt" "github.com/knative/foo/pkg/reconciler/scott" "github.com/knative/foo/pkg/reconciler/ville" "github.com/knative/foo/pkg/reconciler/dave" // This defines the shared main for injected controllers. "knative.dev/pkg/injection/sharedmain" ) func main() { sharedmain.Main("mycomponent", // We pass in the list of controllers to construct, and that's it! // If we forget to add this, go will complain about the unused import. matt.NewController, scott.NewController, ville.NewController, dave.NewController, ) }
If you want to adapt the above to run the controller within a single namespace, you can instead do something like:
package main import ( // The set of controllers this controller process runs. // Linking these will register their transitive dependencies, after // which the shared main can set up the rest. "github.com/knative/foo/pkg/reconciler/matt" "github.com/knative/foo/pkg/reconciler/scott" "github.com/knative/foo/pkg/reconciler/ville" "github.com/knative/foo/pkg/reconciler/dave" // This defines the shared main for injected controllers. "knative.dev/pkg/injection/sharedmain" // These are used to set up the context. "knative.dev/pkg/injection" "knative.dev/pkg/signals" ) func main() { // Scope the shared informer factories to the provided namespace. ctx := injection.WithNamespace(signals.NewContext(), "the-namespace") // Use our initial context when setting up the controllers. sharedmain.MainWithContext(ctx, "mycomponent", // We pass in the list of controllers to construct, and that's it! // If we forget to add this, go will complain about the unused import. matt.NewController, scott.NewController, ville.NewController, dave.NewController, ) }
Index ¶
- func GetNamespaceScope(ctx context.Context) string
- func HasNamespaceScope(ctx context.Context) bool
- func WithNamespaceScope(ctx context.Context, namespace string) context.Context
- type ClientInjector
- type ControllerConstructor
- type DuckFactoryInjector
- type InformerFactoryInjector
- type InformerInjector
- type Interface
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GetNamespaceScope ¶
GetNamespaceScope accesses the namespace associated with the provided context. This should be called when the injection logic is setting up shared informer factories.
func HasNamespaceScope ¶
HasNamespaceScope determines whether the provided context has been scoped to a particular namespace.
Types ¶
type ClientInjector ¶
ClientInjector holds the type of a callback that attaches a particular client type to a context.
type ControllerConstructor ¶
type DuckFactoryInjector ¶
DuckFactoryInjector holds the type of a callback that attaches a particular duck-type informer factory to a context.
type InformerFactoryInjector ¶
InformerFactoryInjector holds the type of a callback that attaches a particular factory type to a context.
type InformerInjector ¶
InformerInjector holds the type of a callback that attaches a particular informer type to a context.
type Interface ¶
type Interface interface { // RegisterClient registers a new injector callback for associating // a new client with a context. RegisterClient(ClientInjector) // GetClients fetches all of the registered client injectors. GetClients() []ClientInjector // RegisterInformerFactory registers a new injector callback for associating // a new informer factory with a context. RegisterInformerFactory(InformerFactoryInjector) // GetInformerFactories fetches all of the registered informer factory injectors. GetInformerFactories() []InformerFactoryInjector // RegisterDuck registers a new duck.InformerFactory for a particular type. RegisterDuck(ii DuckFactoryInjector) // GetDucks accesses the set of registered ducks. GetDucks() []DuckFactoryInjector // RegisterInformer registers a new injector callback for associating // a new informer with a context. RegisterInformer(InformerInjector) // GetInformers fetches all of the registered informer injectors. GetInformers() []InformerInjector // SetupInformers runs all of the injectors against a context, starting with // the clients and the given rest.Config. The resulting context is returned // along with a list of the .Informer() for each of the injected informers, // which is suitable for passing to controller.StartInformers(). // This does not setup or start any controllers. SetupInformers(context.Context, *rest.Config) (context.Context, []controller.Informer) }
Interface is the interface for interacting with injection implementations, such as our Default and Fake below.
var ( // Default is the injection interface with which informers should register // to make themselves available to the controller process when reconcilers // are being run for real. Default Interface = &impl{} // Fake is the injection interface with which informers should register // to make themselves available to the controller process when it is being // unit tested. Fake Interface = &impl{} )