github.com/verrazzano/verrazzano@v1.7.0/application-operator/internal/operatorinit/run_operator.go (about)

     1  // Copyright (c) 2022, 2023, Oracle and/or its affiliates.
     2  // Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
     3  
     4  package operatorinit
     5  
     6  import (
     7  	"github.com/verrazzano/verrazzano/application-operator/controllers/appconfig"
     8  	"github.com/verrazzano/verrazzano/application-operator/controllers/cohworkload"
     9  	"github.com/verrazzano/verrazzano/application-operator/controllers/containerizedworkload"
    10  	"github.com/verrazzano/verrazzano/application-operator/controllers/helidonworkload"
    11  	"github.com/verrazzano/verrazzano/application-operator/controllers/ingresstrait"
    12  	"github.com/verrazzano/verrazzano/application-operator/controllers/loggingtrait"
    13  	"github.com/verrazzano/verrazzano/application-operator/controllers/metricsbinding"
    14  	"github.com/verrazzano/verrazzano/application-operator/controllers/metricstrait"
    15  	"github.com/verrazzano/verrazzano/application-operator/controllers/namespace"
    16  	"github.com/verrazzano/verrazzano/application-operator/controllers/wlsworkload"
    17  	"github.com/verrazzano/verrazzano/application-operator/metricsexporter"
    18  	"github.com/verrazzano/verrazzano/pkg/k8sutil"
    19  	vzlog "github.com/verrazzano/verrazzano/pkg/log"
    20  	"sigs.k8s.io/controller-runtime/pkg/manager"
    21  
    22  	vzlog2 "github.com/verrazzano/verrazzano/pkg/log/vzlog"
    23  	"github.com/verrazzano/verrazzano/pkg/nginxutil"
    24  	"go.uber.org/zap"
    25  	"k8s.io/apimachinery/pkg/runtime"
    26  	ctrl "sigs.k8s.io/controller-runtime"
    27  )
    28  
    29  func StartApplicationOperator(metricsAddr string, enableLeaderElection bool, defaultMetricsScraper string, log *zap.SugaredLogger, scheme *runtime.Scheme) error {
    30  	ingressNGINXNamespace, err := nginxutil.DetermineNamespaceForIngressNGINX(vzlog2.DefaultLogger())
    31  	if err != nil {
    32  		return err
    33  	}
    34  	nginxutil.SetIngressNGINXNamespace(ingressNGINXNamespace)
    35  
    36  	mgr, err := ctrl.NewManager(k8sutil.GetConfigOrDieFromController(), ctrl.Options{
    37  		Scheme:             scheme,
    38  		MetricsBindAddress: metricsAddr,
    39  		Port:               9443,
    40  		LeaderElection:     enableLeaderElection,
    41  		LeaderElectionID:   "5df248b3.verrazzano.io",
    42  	})
    43  	if err != nil {
    44  		log.Errorf("Failed to start manager: %v", err)
    45  		return err
    46  	}
    47  
    48  	log.Info("Starting application reconcilers")
    49  	if err := setupAppReconcilers(mgr, defaultMetricsScraper, log); err != nil {
    50  		return err
    51  	}
    52  
    53  	// Initialize the metricsExporter
    54  	if err := metricsexporter.StartMetricsServer(); err != nil {
    55  		log.Errorf("Failed to create metrics exporter: %v", err)
    56  		return err
    57  	}
    58  
    59  	// +kubebuilder:scaffold:builder
    60  
    61  	log.Info("Starting manager")
    62  	if err = mgr.Start(ctrl.SetupSignalHandler()); err != nil {
    63  		log.Errorf("Failed to run manager: %v", err)
    64  		return err
    65  	}
    66  	return err
    67  }
    68  
    69  func setupAppReconcilers(mgr manager.Manager, defaultMetricsScraper string, log *zap.SugaredLogger) error {
    70  	logger, err := vzlog.BuildZapInfoLogger(0)
    71  	if err != nil {
    72  		return err
    73  	}
    74  	if err := (&ingresstrait.Reconciler{
    75  		Client: mgr.GetClient(),
    76  		Log:    log,
    77  		Scheme: mgr.GetScheme(),
    78  	}).SetupWithManager(mgr); err != nil {
    79  		log.Errorf("Failed to create IngressTrait controller: %v", err)
    80  		return err
    81  	}
    82  	metricsReconciler := &metricstrait.Reconciler{
    83  		Client:  mgr.GetClient(),
    84  		Log:     log,
    85  		Scheme:  mgr.GetScheme(),
    86  		Scraper: defaultMetricsScraper,
    87  	}
    88  
    89  	if err = metricsReconciler.SetupWithManager(mgr); err != nil {
    90  		log.Errorf("Failed to create MetricsTrait controller: %v", err)
    91  		return err
    92  	}
    93  
    94  	if err != nil {
    95  		log.Errorf("Failed to create ApplicationConfiguration logger: %v", err)
    96  		return err
    97  	}
    98  	if err = (&cohworkload.Reconciler{
    99  		Client:  mgr.GetClient(),
   100  		Log:     logger,
   101  		Scheme:  mgr.GetScheme(),
   102  		Metrics: metricsReconciler,
   103  	}).SetupWithManager(mgr); err != nil {
   104  		log.Errorf("Failed to create VerrazzanoCoherenceWorkload controller: %v", err)
   105  		return err
   106  	}
   107  	wlsWorkloadReconciler := &wlsworkload.Reconciler{
   108  		Client:  mgr.GetClient(),
   109  		Log:     log,
   110  		Scheme:  mgr.GetScheme(),
   111  		Metrics: metricsReconciler,
   112  	}
   113  	if err = wlsWorkloadReconciler.SetupWithManager(mgr); err != nil {
   114  		log.Errorf("Failed to create VerrazzanoWeblogicWorkload controller %v", err)
   115  		return err
   116  	}
   117  	if err = (&helidonworkload.Reconciler{
   118  		Client:  mgr.GetClient(),
   119  		Log:     log,
   120  		Scheme:  mgr.GetScheme(),
   121  		Metrics: metricsReconciler,
   122  	}).SetupWithManager(mgr); err != nil {
   123  		log.Errorf("Failed to create VerrazzanoHelidonWorkload controller: %v", err)
   124  		return err
   125  	}
   126  	// Setup the namespace reconciler
   127  	if _, err := namespace.NewNamespaceController(mgr, log.With("controller", "VerrazzanoNamespaceController")); err != nil {
   128  		log.Errorf("Failed to create VerrazzanoNamespaceController controller: %v", err)
   129  		return err
   130  	}
   131  
   132  	if err = (&loggingtrait.LoggingTraitReconciler{
   133  		Client: mgr.GetClient(),
   134  		Log:    log,
   135  		Scheme: mgr.GetScheme(),
   136  	}).SetupWithManager(mgr); err != nil {
   137  		log.Errorf("Failed to create LoggingTrait controller: %v", err)
   138  		return err
   139  	}
   140  
   141  	if err = (&appconfig.Reconciler{
   142  		Client: mgr.GetClient(),
   143  		Log:    logger,
   144  		Scheme: mgr.GetScheme(),
   145  	}).SetupWithManager(mgr); err != nil {
   146  		log.Errorf("Failed to create ApplicationConfiguration controller: %v", err)
   147  		return err
   148  	}
   149  	if err = (&containerizedworkload.Reconciler{
   150  		Client: mgr.GetClient(),
   151  		Log:    logger,
   152  		Scheme: mgr.GetScheme(),
   153  	}).SetupWithManager(mgr); err != nil {
   154  		log.Errorf("Failed to create ContainerizedWorkload controller: %v", err)
   155  		return err
   156  	}
   157  	// Register the metrics workload controller
   158  	if err = (&metricsbinding.Reconciler{
   159  		Client: mgr.GetClient(),
   160  		Log:    logger,
   161  		Scheme: mgr.GetScheme(),
   162  	}).SetupWithManager(mgr); err != nil {
   163  		log.Errorf("Failed to create MetricsBinding controller: %v", err)
   164  		return err
   165  	}
   166  	return nil
   167  }