Controller Implementation¶
cmd/controller¶
Pass the new controller implementation to the shared main
import (
// The set of controllers this controller process runs.
"knative.dev/sample-source/pkg/reconciler/sample"
// This defines the shared main for injected controllers.
"knative.dev/pkg/injection/sharedmain"
)
func main() {
sharedmain.Main("sample-source-controller", sample.NewController)
}
configmap.Watcher
, as well as a context which the injected listers will use for the reconciler struct arguments
func NewController(
ctx context.Context,
cmw configmap.Watcher,
) *controller.Impl {
// ...
deploymentInformer := deploymentinformer.Get(ctx)
sinkBindingInformer := sinkbindinginformer.Get(ctx)
sampleSourceInformer := samplesourceinformer.Get(ctx)
r := &Reconciler{
dr: &reconciler.DeploymentReconciler{KubeClientSet: kubeclient.Get(ctx)},
sbr: &reconciler.SinkBindingReconciler{EventingClientSet: eventingclient.Get(ctx)},
// Config accessor takes care of tracing/config/logging config propagation to the receive adapter
configAccessor: reconcilersource.WatchConfigurations(ctx, "sample-source", cmw),
}
import (
// ...
reconcilersource "knative.dev/eventing/pkg/reconciler/source"
// ...
)
sampleSourceInformer.Informer().AddEventHandler(controller.HandleAll(impl.Enqueue))
SampleSource
uses Deployment
and SinkBinding
resources to deploy and also bind the event source and the receive adapter. Also ensure the informers are set up correctly for these secondary resources
deploymentInformer.Informer().AddEventHandler(cache.FilteringResourceEventHandler{
FilterFunc: controller.FilterGroupKind(v1alpha1.Kind("SampleSource")),
Handler: controller.HandleAll(impl.EnqueueControllerOf),
})
sinkBindingInformer.Informer().AddEventHandler(cache.FilteringResourceEventHandler{
FilterFunc: controller.FilterGroupKind(v1alpha1.Kind("SampleSource")),
Handler: controller.HandleAll(impl.EnqueueControllerOf),
})