github.com/verrazzano/verrazzano@v1.7.0/platform-operator/main.go (about)

     1  // Copyright (c) 2020, 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 main
     5  
     6  import (
     7  	"flag"
     8  	"github.com/fluent/fluent-operator/v2/apis/fluentbit/v1alpha2"
     9  	moduleapi "github.com/verrazzano/verrazzano-modules/module-operator/apis/platform/v1alpha1"
    10  	"os"
    11  
    12  	acmev1 "github.com/cert-manager/cert-manager/pkg/apis/acme/v1"
    13  	cmapiv1 "github.com/cert-manager/cert-manager/pkg/apis/certmanager/v1"
    14  	oam "github.com/crossplane/oam-kubernetes-runtime/apis/core"
    15  	promoperapi "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
    16  	vmov1 "github.com/verrazzano/verrazzano-monitoring-operator/pkg/apis/vmcontroller/v1"
    17  	vzappclusters "github.com/verrazzano/verrazzano/application-operator/apis/clusters/v1alpha1"
    18  	vzapp "github.com/verrazzano/verrazzano/application-operator/apis/oam/v1alpha1"
    19  	clustersv1alpha1 "github.com/verrazzano/verrazzano/cluster-operator/apis/clusters/v1alpha1"
    20  	"github.com/verrazzano/verrazzano/pkg/helm"
    21  	vzlog "github.com/verrazzano/verrazzano/pkg/log"
    22  	installv1alpha1 "github.com/verrazzano/verrazzano/platform-operator/apis/verrazzano/v1alpha1"
    23  	installv1beta1 "github.com/verrazzano/verrazzano/platform-operator/apis/verrazzano/v1beta1"
    24  	"github.com/verrazzano/verrazzano/platform-operator/apis/verrazzano/validators"
    25  	internalconfig "github.com/verrazzano/verrazzano/platform-operator/internal/config"
    26  	"github.com/verrazzano/verrazzano/platform-operator/internal/operatorinit"
    27  	"go.uber.org/zap"
    28  	istioclinet "istio.io/client-go/pkg/apis/networking/v1alpha3"
    29  	istioclisec "istio.io/client-go/pkg/apis/security/v1beta1"
    30  	v1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
    31  	"k8s.io/apimachinery/pkg/runtime"
    32  	utilruntime "k8s.io/apimachinery/pkg/util/runtime"
    33  	clientgoscheme "k8s.io/client-go/kubernetes/scheme"
    34  	_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
    35  	kzap "sigs.k8s.io/controller-runtime/pkg/log/zap"
    36  	// +kubebuilder:scaffold:imports
    37  )
    38  
    39  var scheme = runtime.NewScheme()
    40  
    41  func init() {
    42  	_ = clientgoscheme.AddToScheme(scheme)
    43  	_ = vmov1.AddToScheme(scheme)
    44  	_ = installv1alpha1.AddToScheme(scheme)
    45  	_ = installv1beta1.AddToScheme(scheme)
    46  	_ = clustersv1alpha1.AddToScheme(scheme)
    47  
    48  	_ = istioclinet.AddToScheme(scheme)
    49  	_ = istioclisec.AddToScheme(scheme)
    50  
    51  	_ = oam.AddToScheme(scheme)
    52  
    53  	_ = vzapp.AddToScheme(scheme)
    54  	_ = vzappclusters.AddToScheme(scheme)
    55  
    56  	// Add cert-manager components to the scheme
    57  	_ = cmapiv1.AddToScheme(scheme)
    58  	_ = acmev1.AddToScheme(scheme)
    59  
    60  	_ = v1alpha2.AddToScheme(scheme)
    61  
    62  	// Add the Prometheus Operator resources to the scheme
    63  	_ = promoperapi.AddToScheme(scheme)
    64  
    65  	// Add K8S api-extensions so that we can list CustomResourceDefinitions during uninstall of VZ
    66  	_ = v1.AddToScheme(scheme)
    67  	utilruntime.Must(installv1beta1.AddToScheme(scheme))
    68  
    69  	// Add module
    70  	_ = moduleapi.AddToScheme(scheme)
    71  
    72  	// +kubebuilder:scaffold:scheme
    73  }
    74  
    75  func main() {
    76  
    77  	// config will hold the entire operator config
    78  	config := internalconfig.Get()
    79  	var bomOverride string
    80  
    81  	flag.StringVar(&config.MetricsAddr, "metrics-addr", config.MetricsAddr, "The address the metric endpoint binds to.")
    82  	flag.BoolVar(&config.LeaderElectionEnabled, "enable-leader-election", config.LeaderElectionEnabled,
    83  		"Enable leader election for controller manager. "+
    84  			"Enabling this will ensure there is only one active controller manager.")
    85  	flag.StringVar(&config.CertDir, "cert-dir", config.CertDir, "The directory containing tls.crt and tls.key.")
    86  	flag.BoolVar(&config.DryRun, "dry-run", config.DryRun, "Run operator in dry run mode.")
    87  	flag.BoolVar(&config.WebhookValidationEnabled, "enable-webhook-validation", config.WebhookValidationEnabled,
    88  		"Enable webhooks validation for the operator")
    89  	flag.BoolVar(&config.CloudCredentialWatchEnabled, "enable-cloud-credential-watch", config.CloudCredentialWatchEnabled,
    90  		"Enable watch for cloud credential updates")
    91  	flag.BoolVar(&config.ResourceRequirementsValidation, "resource-validation",
    92  		config.ResourceRequirementsValidation, "Enables of resource validation webhooks.")
    93  	flag.BoolVar(&config.RunWebhooks, "run-webhooks", config.RunWebhooks,
    94  		"Runs in webhook mode; if false, runs the main operator reconcile loop")
    95  	flag.BoolVar(&config.RunWebhookInit, "run-webhook-init", config.RunWebhookInit,
    96  		"Runs the webhook initialization code")
    97  	flag.StringVar(&config.VerrazzanoRootDir, "vz-root-dir", config.VerrazzanoRootDir,
    98  		"Specify the root directory of Verrazzano (used for development)")
    99  	flag.StringVar(&bomOverride, "bom-path", "", "BOM file location")
   100  	flag.BoolVar(&helm.Debug, "helm-debug", helm.Debug, "Add the --debug flag to helm commands")
   101  	flag.Int64Var(&config.HealthCheckPeriodSeconds, "health-check-period", config.HealthCheckPeriodSeconds,
   102  		"Health check period seconds; set to 0 to disable health checks")
   103  	flag.Int64Var(&config.MySQLCheckPeriodSeconds, "mysql-check-period", config.MySQLCheckPeriodSeconds,
   104  		"MySQL check period seconds; set to 0 to disable MySQL checks")
   105  	flag.Int64Var(&config.MySQLRepairTimeoutSeconds, "mysql-repair-timeout", config.MySQLRepairTimeoutSeconds,
   106  		"MySQL repair timeout seconds")
   107  
   108  	// Add the zap logger flag set to the CLI.
   109  	opts := kzap.Options{}
   110  	opts.BindFlags(flag.CommandLine)
   111  
   112  	flag.Parse()
   113  	kzap.UseFlagOptions(&opts)
   114  	vzlog.InitLogs(opts)
   115  
   116  	// Save the config as immutable from this point on.
   117  	internalconfig.Set(config)
   118  	log := zap.S()
   119  
   120  	log.Info("Starting Verrazzano Platform Operator")
   121  	// Set the BOM file path for the operator
   122  	if len(bomOverride) > 0 {
   123  		log.Infof("Using BOM override file %s", bomOverride)
   124  		internalconfig.SetDefaultBomFilePath(bomOverride)
   125  	}
   126  
   127  	// Log the Verrazzano version
   128  	version, err := validators.GetCurrentBomVersion()
   129  	if err == nil {
   130  		log.Infof("Verrazzano version: %s", version.ToString())
   131  	} else {
   132  		log.Errorf("Failed to get the Verrazzano version from the BOM: %v", err)
   133  	}
   134  
   135  	// This allows separation of webhooks and operator
   136  	var exitErr error
   137  	if config.RunWebhookInit {
   138  		exitErr = operatorinit.WebhookInit(config, log)
   139  	} else if config.RunWebhooks {
   140  		exitErr = operatorinit.StartWebhookServers(config, log, scheme)
   141  	} else {
   142  		exitErr = operatorinit.StartPlatformOperator(config, log, scheme)
   143  	}
   144  	if exitErr != nil {
   145  		log.Errorf("Error occurred during execution: %v", exitErr)
   146  		os.Exit(1)
   147  	}
   148  }