github.com/verrazzano/verrazzano@v1.7.0/cluster-operator/main.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 main 5 6 import ( 7 "flag" 8 v1 "k8s.io/api/rbac/v1" 9 "os" 10 11 clustersv1alpha1 "github.com/verrazzano/verrazzano/cluster-operator/apis/clusters/v1alpha1" 12 "github.com/verrazzano/verrazzano/cluster-operator/internal/operatorinit" 13 vzlog "github.com/verrazzano/verrazzano/pkg/log" 14 "github.com/verrazzano/verrazzano/platform-operator/apis/verrazzano/v1beta1" 15 "go.uber.org/zap" 16 istioclinet "istio.io/client-go/pkg/apis/networking/v1beta1" 17 k8sapiext "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" 18 "k8s.io/apimachinery/pkg/runtime" 19 utilruntime "k8s.io/apimachinery/pkg/util/runtime" 20 clientgoscheme "k8s.io/client-go/kubernetes/scheme" 21 _ "k8s.io/client-go/plugin/pkg/client/auth/gcp" 22 ctrl "sigs.k8s.io/controller-runtime" 23 kzap "sigs.k8s.io/controller-runtime/pkg/log/zap" 24 // +kubebuilder:scaffold:imports 25 ) 26 27 var ( 28 scheme = runtime.NewScheme() 29 30 metricsAddr string 31 enableLeaderElection bool 32 probeAddr string 33 runWebhooks bool 34 runWebhookInit bool 35 certDir string 36 ingressHost string 37 enableQuickCreate bool 38 disableCAPIRancherRegistration bool 39 ) 40 41 func init() { 42 utilruntime.Must(clientgoscheme.AddToScheme(scheme)) 43 utilruntime.Must(k8sapiext.AddToScheme(scheme)) 44 utilruntime.Must(istioclinet.AddToScheme(scheme)) 45 46 utilruntime.Must(clustersv1alpha1.AddToScheme(scheme)) 47 utilruntime.Must(v1beta1.AddToScheme(scheme)) 48 utilruntime.Must(v1.AddToScheme(scheme)) 49 // +kubebuilder:scaffold:scheme 50 } 51 52 func main() { 53 props := handleFlags() 54 log := zap.S() 55 56 if runWebhookInit { 57 err := operatorinit.WebhookInit(log, props) 58 if err != nil { 59 os.Exit(1) 60 } 61 } else if runWebhooks { 62 err := operatorinit.StartWebhookServer(log, props) 63 if err != nil { 64 os.Exit(1) 65 } 66 } else { 67 err := operatorinit.StartClusterOperator(log, props) 68 if err != nil { 69 os.Exit(1) 70 } 71 } 72 } 73 74 // handleFlags sets up the CLI flags, parses them, and initializes loggers 75 func handleFlags() operatorinit.Properties { 76 flag.StringVar(&metricsAddr, "metrics-bind-address", ":8080", "The address the metric endpoint binds to.") 77 flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.") 78 flag.BoolVar(&enableLeaderElection, "leader-elect", false, 79 "Enable leader election for controller manager. "+ 80 "Enabling this will ensure there is only one active controller manager.") 81 flag.BoolVar(&runWebhooks, "run-webhooks", false, 82 "Runs in webhook mode; if false, runs the main operator reconcile loop") 83 flag.BoolVar(&runWebhookInit, "run-webhook-init", false, 84 "Runs the webhook initialization code") 85 flag.BoolVar(&enableQuickCreate, "quick-create", true, "If true, enables Quick Create Clusters") 86 flag.StringVar(&certDir, "cert-dir", "/etc/certs/", "The directory containing tls.crt and tls.key.") 87 flag.StringVar(&ingressHost, "ingress-host", "", "The host used for Rancher API requests.") 88 flag.BoolVar(&disableCAPIRancherRegistration, "disable-capi-rancher-registration", false, 89 "Disables the registration of CAPI-based clusters with Rancher") 90 91 opts := kzap.Options{} 92 opts.BindFlags(flag.CommandLine) 93 flag.Parse() 94 95 kzap.UseFlagOptions(&opts) 96 vzlog.InitLogs(opts) 97 ctrl.SetLogger(kzap.New(kzap.UseFlagOptions(&opts))) 98 return operatorinit.Properties{ 99 Scheme: scheme, 100 CertificateDir: certDir, 101 MetricsAddress: metricsAddr, 102 ProbeAddress: probeAddr, 103 IngressHost: ingressHost, 104 EnableLeaderElection: enableLeaderElection, 105 EnableQuickCreate: enableQuickCreate, 106 DisableCAPIRancherRegistration: disableCAPIRancherRegistration, 107 } 108 }