Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run all controllers in the same process #313

Merged
merged 3 commits into from
Aug 7, 2018
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
1 change: 0 additions & 1 deletion cmd/controller-manager/kodata/LICENSE

This file was deleted.

1 change: 0 additions & 1 deletion cmd/controller-manager/kodata/VENDOR-LICENSE

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,16 @@ limitations under the License.
package main

import (
"flag"
"log"

channelsv1alpha1 "github.com/knative/eventing/pkg/apis/channels/v1alpha1"
feedsv1alpha1 "github.com/knative/eventing/pkg/apis/feeds/v1alpha1"
flowsv1alpha1 "github.com/knative/eventing/pkg/apis/flows/v1alpha1"
"github.com/knative/eventing/pkg/controller/feed"
"github.com/knative/eventing/pkg/controller/flow"
"github.com/knative/eventing/pkg/logconfig"

"github.com/knative/pkg/configmap"
"github.com/knative/pkg/logging"
"github.com/knative/pkg/logging/logkey"

istiov1alpha3 "github.com/knative/pkg/apis/istio/v1alpha3"
"go.uber.org/zap"

"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes"
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
"k8s.io/client-go/rest"

"github.com/knative/eventing/pkg/system"
"sigs.k8s.io/controller-runtime/pkg/client/config"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/manager"
Expand All @@ -52,51 +39,16 @@ type SchemeFunc func(*runtime.Scheme) error
// ProvideFunc adds a controller to a Manager.
type ProvideFunc func(manager.Manager) (controller.Controller, error)

func main() {
flag.Parse()

// Read the logging config and setup a logger.
cm, err := configmap.Load("/etc/config-logging")
if err != nil {
log.Fatalf("Error loading logging configuration: %v", err)
}
cfg, err := logging.NewConfigFromMap(cm, logconfig.ControllerManager)
if err != nil {
log.Fatalf("Error parsing logging configuration: %v", err)
}
logger, atomicLevel := logging.NewLoggerFromConfig(cfg, logconfig.ControllerManager)
defer logger.Sync()
logger = logger.With(zap.String(logkey.ControllerType, logconfig.ControllerManager))

logger.Info("Starting the Controller Manager")

// This tells controller-runtime to use zap to log internal messages.
// controllerRuntimeStart runs controllers written for controller-runtime. It's
// intended to be called from main(). Any controllers migrated to use
// controller-runtime should move their initialization to this function.
func controllerRuntimeStart() error {
logf.SetLogger(logf.ZapLogger(false))

// set up signals so we handle the first shutdown signal gracefully
stopCh := signals.SetupSignalHandler()

clusterConfig, err := rest.InClusterConfig()
if err != nil {
logger.Fatal("Failed to get in cluster config", err)
}

kubeClient, err := kubernetes.NewForConfig(clusterConfig)
if err != nil {
logger.Fatal("Failed to get the client set", err)
}

// Watch the logging config map and dynamically update logging levels.
configMapWatcher := configmap.NewDefaultWatcher(kubeClient, system.Namespace)
configMapWatcher.Watch(logconfig.ConfigName, logging.UpdateLevelFromConfigMap(logger, atomicLevel, logconfig.ControllerManager, logconfig.ControllerManager))
if err = configMapWatcher.Start(stopCh); err != nil {
logger.Fatalf("failed to start controller manager configmap watcher: %v", err)
}

// Setup a Manager
mrg, err := manager.New(config.GetConfigOrDie(), manager.Options{})
if err != nil {
log.Fatal(err)
return err
}

// Add custom types to this array to get them into the manager's scheme.
Expand All @@ -117,12 +69,11 @@ func main() {
flow.ProvideController,
}

// TODO(n3wscott): Send the logger to the controllers.
for _, provider := range providers {
if _, err := provider(mrg); err != nil {
log.Fatal(err)
return err
}
}

log.Fatal(mrg.Start(stopCh))
return mrg.Start(signals.SetupSignalHandler())
}
18 changes: 13 additions & 5 deletions cmd/controller/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package main
import (
"context"
"flag"
"log"
"net/http"
"time"

Expand All @@ -39,8 +40,6 @@ import (
sharedclientset "github.com/knative/pkg/client/clientset/versioned"
sharedinformers "github.com/knative/pkg/client/informers/externalversions"

"log"

"github.com/knative/eventing/pkg/logconfig"
"github.com/knative/eventing/pkg/system"
"github.com/knative/pkg/configmap"
Expand Down Expand Up @@ -122,7 +121,8 @@ func main() {
logger.Fatalf("failed to start controller config map watcher: %v", err)
}

// Add new controllers here.
// Add new controllers here, except controllers that use controller-runtime.
// Those should be added to controller-runtime-main.go.
ctors := []controller.Constructor{
bus.NewController,
clusterbus.NewController,
Expand Down Expand Up @@ -152,6 +152,13 @@ func main() {
}(ctrlr)
}

// Start the controller-runtime controllers.
go func() {
if err := controllerRuntimeStart(); err != nil {
logger.Fatalf("Error running controller-runtime controllers: %v", err)
}
}()

// Start the endpoint that Prometheus scraper talks to
srv := &http.Server{Addr: metricsScrapeAddr}
http.Handle(metricsScrapePath, promhttp.Handler())
Expand All @@ -171,6 +178,7 @@ func main() {
}

func init() {
flag.StringVar(&kubeconfig, "kubeconfig", "", "Path to a kubeconfig. Only required if out-of-cluster.")
flag.StringVar(&masterURL, "master", "", "The address of the Kubernetes API server. Overrides any value in kubeconfig. Only required if out-of-cluster.")
// These are commented because they're also defined by controller-runtime.
// flag.StringVar(&kubeconfig, "kubeconfig", "", "Path to a kubeconfig. Only required if out-of-cluster.")
// flag.StringVar(&masterURL, "master", "", "The address of the Kubernetes API server. Overrides any value in kubeconfig. Only required if out-of-cluster.")
}
41 changes: 0 additions & 41 deletions config/500-controller-manager.yaml

This file was deleted.