Controller Implementation and Design

Controller Implementation

cmd/controller

Pass the new controller implementation to the shared main

  1. import (
  2. // The set of controllers this controller process runs.
  3. "knative.dev/sample-source/pkg/reconciler/sample"
  4. // This defines the shared main for injected controllers.
  5. "knative.dev/pkg/injection/sharedmain"
  6. )
  7. func main() {
  8. sharedmain.Main("sample-source-controller", sample.NewController)
  9. }

Define the NewController implementation, it will be passed a configmap.Watcher, as well as a context which the injected listers will use for the reconciler struct arguments

  1. func NewController(
  2. ctx context.Context,
  3. cmw configmap.Watcher,
  4. ) *controller.Impl {
  5. // ...
  6. deploymentInformer := deploymentinformer.Get(ctx)
  7. sinkBindingInformer := sinkbindinginformer.Get(ctx)
  8. sampleSourceInformer := samplesourceinformer.Get(ctx)
  9. r := &Reconciler{
  10. dr: &reconciler.DeploymentReconciler{KubeClientSet: kubeclient.Get(ctx)},
  11. sbr: &reconciler.SinkBindingReconciler{EventingClientSet: eventingclient.Get(ctx)},
  12. // Config accessor takes care of tracing/config/logging config propagation to the receive adapter
  13. configAccessor: reconcilersource.WatchConfigurations(ctx, "sample-source", cmw),
  14. }

The base reconciler is imported from the knative.dev/pkg dependency:

  1. import (
  2. // ...
  3. reconcilersource "knative.dev/eventing/pkg/reconciler/source"
  4. // ...
  5. )

Ensure the correct informers have EventHandlers filtered to them

  1. sampleSourceInformer.Informer().AddEventHandler(controller.HandleAll(impl.Enqueue))

Controller for the 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

  1. deploymentInformer.Informer().AddEventHandler(cache.FilteringResourceEventHandler{
  2. FilterFunc: controller.FilterGroupKind(v1alpha1.Kind("SampleSource")),
  3. Handler: controller.HandleAll(impl.EnqueueControllerOf),
  4. })
  5. sinkBindingInformer.Informer().AddEventHandler(cache.FilteringResourceEventHandler{
  6. FilterFunc: controller.FilterGroupKind(v1alpha1.Kind("SampleSource")),
  7. Handler: controller.HandleAll(impl.EnqueueControllerOf),
  8. })