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 ¶
- Constants
- func AddLiveness(ctx context.Context, handlerFunc http.HandlerFunc) context.Context
- func AddReadiness(ctx context.Context, handlerFunc http.HandlerFunc) context.Context
- func EnableInjectionOrDie(ctx context.Context, cfg *rest.Config) (context.Context, func())
- func GetConfig(ctx context.Context) *rest.Config
- func GetNamespaceScope(ctx context.Context) string
- func GetRESTConfig(serverURL, kubeconfig string) (*rest.Config, error)
- func GetResourceVersion(ctx context.Context) string
- func HasNamespaceScope(ctx context.Context) bool
- func ParseAndGetRESTConfigOrDie() *rest.Config
- func ServeHealthProbes(ctx context.Context, port int) error
- func WithConfig(ctx context.Context, cfg *rest.Config) context.Context
- func WithNamespaceScope(ctx context.Context, namespace string) context.Context
- func WithResourceVersion(ctx context.Context, resourceVersion string) context.Context
- type ClientFetcher
- type ClientInjector
- type ControllerConstructor
- type DuckFactoryInjector
- type FilteredInformersInjector
- type InformerFactoryInjector
- type InformerInjector
- type Interface
- type NamedControllerConstructor
Constants ¶
const HealthCheckDefaultPort = 8080
HealthCheckDefaultPort defines the default port number for health probes
Variables ¶
This section is empty.
Functions ¶
func AddLiveness ¶
AddLiveness signals to probe setup logic to add a user provided probe handler
func AddReadiness ¶
AddReadiness signals to probe setup logic to add a user provided probe handler
func EnableInjectionOrDie ¶
EnableInjectionOrDie enables Knative Client Injection, and provides a callback to start the informers. Both Context and Config are optional. Returns context with rest config set and a callback to start the informers after watches have been set.
Typical integration: ```go
ctx, startInformers := injection.EnableInjectionOrDie(signals.NewContext(), nil) ... start watches with informers, if required ... startInformers()
```
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 GetRESTConfig ¶
GetRESTConfig returns a rest.Config to be used for kubernetes client creation. Deprecated: use environment.ClientConfig package
func GetResourceVersion ¶
GetResourceVersion gets the resource version associated with the context.
func HasNamespaceScope ¶
HasNamespaceScope determines whether the provided context has been scoped to a particular namespace.
func ParseAndGetRESTConfigOrDie ¶
ParseAndGetRESTConfigOrDie parses the rest config flags and creates a client or dies by calling log.Fatalf.
func ServeHealthProbes ¶
ServeHealthProbes sets up liveness and readiness probes. If user sets no probes explicitly via the context then defaults are added.
func WithConfig ¶
WithConfig associates a given config with the context.
func WithNamespaceScope ¶
WithNamespaceScope associates a namespace scoping with the provided context, which will scope the informers produced by the downstream informer factories.
Types ¶
type ClientFetcher ¶
ClientFetcher holds the type of a callback that returns a particular client type from a context.
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 FilteredInformersInjector ¶
FilteredInformersInjector holds the type of a callback that attaches a set of particular filtered informers type 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 // RegisterClientFetcher registers a new callback that fetches a client from // a given context. RegisterClientFetcher(ClientFetcher) // FetchAllClients returns all known clients from the given context. FetchAllClients(context.Context) []interface{} // 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) // RegisterFilteredInformers registers a new filtered informer injector callback for associating // a new set of informers with a context. RegisterFilteredInformers(FilteredInformersInjector) // GetInformers fetches all of the registered informer injectors. GetInformers() []InformerInjector // GetFilteredInformers fetches all of the registered filtered informer injectors. GetFilteredInformers() []FilteredInformersInjector // 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{} )
type NamedControllerConstructor ¶
type NamedControllerConstructor struct { // Name is the name associated with the controller returned by ControllerConstructor. Name string // ControllerConstructor is a constructor for a controller. ControllerConstructor ControllerConstructor }
NamedControllerConstructor is a ControllerConstructor with an associated name.