Skip to content
This repository has been archived by the owner on Jun 24, 2020. It is now read-only.

Pass config from main to the controller. #407

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions cmd/manager/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,17 @@ import (
"knative.dev/serving-operator/pkg/reconciler/knativeserving"
)

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(*knativeserving.MasterURL, *knativeserving.Kubeconfig)
cfg, err := sharedmain.GetConfig(*masterURL, *kubeconfig)
if err != nil {
log.Fatal("Error building kubeconfig", err)
}
sharedmain.MainWithConfig(signals.NewContext(), "serving-operator", cfg, knativeserving.NewController)
sharedmain.MainWithConfig(signals.NewContext(), "serving-operator", cfg, knativeserving.NewControllerWithConfig(cfg))
}
10 changes: 7 additions & 3 deletions minikube-operator-example/cmd/manager/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,17 @@ import (
"knative.dev/serving-operator/pkg/reconciler/knativeserving"
)

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(*knativeserving.MasterURL, *knativeserving.Kubeconfig)
cfg, err := sharedmain.GetConfig(*masterURL, *kubeconfig)
if err != nil {
log.Fatal("Error building kubeconfig", err)
}
ctx := signals.NewContext()
sharedmain.MainWithConfig(ctx, "serving_operator", cfg, knativeserving.NewController)
sharedmain.MainWithConfig(signals.NewContext(), "serving-operator", cfg, knativeserving.NewControllerWithConfig(cfg))
}
102 changes: 45 additions & 57 deletions pkg/reconciler/knativeserving/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,17 @@ package knativeserving

import (
"context"
"flag"
"os"
"path/filepath"

kubeclient "knative.dev/pkg/client/injection/kube/client"
"knative.dev/pkg/injection/sharedmain"
"knative.dev/pkg/logging"
servingclient "knative.dev/serving-operator/pkg/client/injection/client"

"github.com/go-logr/zapr"
mfc "github.com/manifestival/client-go-client"
mf "github.com/manifestival/manifestival"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/cache"
deploymentinformer "knative.dev/pkg/client/injection/kube/informers/apps/v1/deployment"
"knative.dev/pkg/configmap"
Expand All @@ -42,61 +41,50 @@ const (
controllerAgentName = "knativeserving-controller"
)

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.")
)

// 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 {
knativeServingInformer := knativeServinginformer.Get(ctx)
deploymentInformer := deploymentinformer.Get(ctx)
logger := logging.FromContext(ctx)

statsReporter, err := reconciler.NewStatsReporter(controllerAgentName)
if err != nil {
logger.Fatal(err)
}

c := &Reconciler{
kubeClientSet: kubeclient.Get(ctx),
knativeServingClientSet: servingclient.Get(ctx),
statsReporter: statsReporter,
knativeServingLister: knativeServingInformer.Lister(),
servings: map[string]int64{},
platform: common.GetPlatforms(ctx),
}

koDataDir := os.Getenv("KO_DATA_PATH")

cfg, err := sharedmain.GetConfig(*MasterURL, *Kubeconfig)
if err != nil {
logger.Error(err, "Error building kubeconfig")
func NewControllerWithConfig(cfg *rest.Config) func(context.Context, configmap.Watcher) *controller.Impl {
return func(ctx context.Context, cmw configmap.Watcher) *controller.Impl {
knativeServingInformer := knativeServinginformer.Get(ctx)
deploymentInformer := deploymentinformer.Get(ctx)
logger := logging.FromContext(ctx)

statsReporter, err := reconciler.NewStatsReporter(controllerAgentName)
if err != nil {
logger.Fatal(err)
}

c := &Reconciler{
kubeClientSet: kubeclient.Get(ctx),
knativeServingClientSet: servingclient.Get(ctx),
statsReporter: statsReporter,
knativeServingLister: knativeServingInformer.Lister(),
servings: map[string]int64{},
platform: common.GetPlatforms(ctx),
}

koDataDir := os.Getenv("KO_DATA_PATH")

config, err := mfc.NewManifest(filepath.Join(koDataDir, "knative-serving/"),
cfg,
mf.UseLogger(zapr.NewLogger(logger.Desugar()).WithName("manifestival")))
if err != nil {
logger.Error(err, "Error creating the Manifest for knative-serving")
os.Exit(1)
}

c.config = config
impl := knsreconciler.NewImpl(ctx, c)

logger.Info("Setting up event handlers")

knativeServingInformer.Informer().AddEventHandler(controller.HandleAll(impl.Enqueue))

deploymentInformer.Informer().AddEventHandler(cache.FilteringResourceEventHandler{
FilterFunc: controller.Filter(v1alpha1.SchemeGroupVersion.WithKind("KnativeServing")),
Handler: controller.HandleAll(impl.EnqueueControllerOf),
})

return impl
}

config, err := mfc.NewManifest(filepath.Join(koDataDir, "knative-serving/"),
cfg,
mf.UseLogger(zapr.NewLogger(logger.Desugar()).WithName("manifestival")))
if err != nil {
logger.Error(err, "Error creating the Manifest for knative-serving")
os.Exit(1)
}

c.config = config
impl := knsreconciler.NewImpl(ctx, c)

logger.Info("Setting up event handlers")

knativeServingInformer.Informer().AddEventHandler(controller.HandleAll(impl.Enqueue))

deploymentInformer.Informer().AddEventHandler(cache.FilteringResourceEventHandler{
FilterFunc: controller.Filter(v1alpha1.SchemeGroupVersion.WithKind("KnativeServing")),
Handler: controller.HandleAll(impl.EnqueueControllerOf),
})

return impl
}