From 2863d9d7059fd08c6eef247c6101b63deb584f69 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Markus=20Th=C3=B6mmes?= Date: Tue, 21 Apr 2020 11:09:55 +0200 Subject: [PATCH] Pass config from main to the controller. --- cmd/manager/main.go | 9 ++- pkg/reconciler/knativeeventing/controller.go | 69 ++++++++------------ 2 files changed, 35 insertions(+), 43 deletions(-) diff --git a/cmd/manager/main.go b/cmd/manager/main.go index 7716b268..616ab658 100644 --- a/cmd/manager/main.go +++ b/cmd/manager/main.go @@ -24,12 +24,17 @@ import ( "knative.dev/pkg/signals" ) +var ( + masterURL = flag.String("master", "", "The address of the Kubernetes API server. Overrides any value in kubeconfig. Only required if out-of-cluster.") + kubeconfig = flag.String("kubeconfig", "", "Path to a kubeconfig. Only required if out-of-cluster.") +) + func main() { flag.Parse() - cfg, err := sharedmain.GetConfig(*knativeeventing.MasterURL, *knativeeventing.Kubeconfig) + cfg, err := sharedmain.GetConfig(*masterURL, *kubeconfig) if err != nil { log.Fatal("Error building kubeconfig", err) } - sharedmain.MainWithConfig(signals.NewContext(), "eventing-operator", cfg, knativeeventing.NewController) + sharedmain.MainWithConfig(signals.NewContext(), "eventing-operator", cfg, knativeeventing.NewControllerWithConfig(cfg)) } diff --git a/pkg/reconciler/knativeeventing/controller.go b/pkg/reconciler/knativeeventing/controller.go index dc76c71b..37f1287b 100644 --- a/pkg/reconciler/knativeeventing/controller.go +++ b/pkg/reconciler/knativeeventing/controller.go @@ -15,7 +15,6 @@ package knativeeventing import ( "context" - "flag" "os" "path/filepath" @@ -25,13 +24,13 @@ import ( "k8s.io/apimachinery/pkg/util/sets" "k8s.io/client-go/tools/cache" + "k8s.io/client-go/rest" "knative.dev/eventing-operator/pkg/apis/eventing/v1alpha1" knativeEventinginformer "knative.dev/eventing-operator/pkg/client/injection/informers/eventing/v1alpha1/knativeeventing" rbase "knative.dev/eventing-operator/pkg/reconciler" deploymentinformer "knative.dev/pkg/client/injection/kube/informers/apps/v1/deployment" "knative.dev/pkg/configmap" "knative.dev/pkg/controller" - "knative.dev/pkg/injection/sharedmain" ) const ( @@ -39,52 +38,40 @@ const ( reconcilerName = "KnativeEventing" ) -var ( - recursive = flag.Bool("recursive", false, "If filename is a directory, process all manifests recursively") - MasterURL = flag.String("master", "", "The address of the Kubernetes API server. Overrides any value in kubeconfig. Only required if out-of-cluster.") - Kubeconfig = flag.String("kubeconfig", "", "Path to a kubeconfig. Only required if out-of-cluster.") -) - -// NewController initializes the controller and is called by the generated code +// NewControllerWithConfig initializes the controller and is called by the generated code // Registers eventhandlers to enqueue events -func NewController( - ctx context.Context, - cmw configmap.Watcher, -) *controller.Impl { - knativeEventingInformer := knativeEventinginformer.Get(ctx) - deploymentInformer := deploymentinformer.Get(ctx) +func NewControllerWithConfig(cfg *rest.Config) func(context.Context, configmap.Watcher) *controller.Impl { + return func(ctx context.Context, cmw configmap.Watcher) *controller.Impl { + knativeEventingInformer := knativeEventinginformer.Get(ctx) + deploymentInformer := deploymentinformer.Get(ctx) - c := &Reconciler{ - Base: rbase.NewBase(ctx, controllerAgentName, cmw), - knativeEventingLister: knativeEventingInformer.Lister(), - eventings: sets.String{}, - } + c := &Reconciler{ + Base: rbase.NewBase(ctx, controllerAgentName, cmw), + knativeEventingLister: knativeEventingInformer.Lister(), + eventings: sets.String{}, + } - koDataDir := os.Getenv("KO_DATA_PATH") + koDataDir := os.Getenv("KO_DATA_PATH") - cfg, err := sharedmain.GetConfig(*MasterURL, *Kubeconfig) - if err != nil { - c.Logger.Error(err, "Error building kubeconfig") - } + config, err := mfc.NewManifest(filepath.Join(koDataDir, "knative-eventing/"), cfg, + mf.UseLogger(zapr.NewLogger(c.Logger.Desugar()).WithName("manifestival"))) + if err != nil { + c.Logger.Error(err, "Error creating the Manifest for knative-eventing") + os.Exit(1) + } - config, err := mfc.NewManifest(filepath.Join(koDataDir, "knative-eventing/"), cfg, - mf.UseLogger(zapr.NewLogger(c.Logger.Desugar()).WithName("manifestival"))) - if err != nil { - c.Logger.Error(err, "Error creating the Manifest for knative-eventing") - os.Exit(1) - } - - c.config = config - impl := controller.NewImpl(c, c.Logger, reconcilerName) + c.config = config + impl := controller.NewImpl(c, c.Logger, reconcilerName) - c.Logger.Info("Setting up event handlers for %s", reconcilerName) + c.Logger.Info("Setting up event handlers for %s", reconcilerName) - knativeEventingInformer.Informer().AddEventHandler(controller.HandleAll(impl.Enqueue)) + knativeEventingInformer.Informer().AddEventHandler(controller.HandleAll(impl.Enqueue)) - deploymentInformer.Informer().AddEventHandler(cache.FilteringResourceEventHandler{ - FilterFunc: controller.Filter(v1alpha1.SchemeGroupVersion.WithKind("KnativeEventing")), - Handler: controller.HandleAll(impl.EnqueueControllerOf), - }) + deploymentInformer.Informer().AddEventHandler(cache.FilteringResourceEventHandler{ + FilterFunc: controller.Filter(v1alpha1.SchemeGroupVersion.WithKind("KnativeEventing")), + Handler: controller.HandleAll(impl.EnqueueControllerOf), + }) - return impl + return impl + } }