github.com/verrazzano/verrazzano@v1.7.0/authproxy/main.go (about)

     1  // Copyright (c) 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  	"os"
     9  
    10  	"github.com/verrazzano/verrazzano/authproxy/src/config"
    11  	"github.com/verrazzano/verrazzano/authproxy/src/proxy"
    12  	"github.com/verrazzano/verrazzano/pkg/k8sutil"
    13  	vzlog "github.com/verrazzano/verrazzano/pkg/log"
    14  	"go.uber.org/zap"
    15  	corev1 "k8s.io/api/core/v1"
    16  	"k8s.io/apimachinery/pkg/runtime"
    17  	utilruntime "k8s.io/apimachinery/pkg/util/runtime"
    18  	ctrl "sigs.k8s.io/controller-runtime"
    19  	kzap "sigs.k8s.io/controller-runtime/pkg/log/zap"
    20  )
    21  
    22  var proxyPort int
    23  
    24  func main() {
    25  	handleFlags()
    26  	log := zap.S()
    27  
    28  	err := config.InitConfiguration(log)
    29  	if err != nil {
    30  		os.Exit(1)
    31  	}
    32  
    33  	log.Info("Initializing the proxy server")
    34  	authproxy := proxy.InitializeProxy(proxyPort)
    35  
    36  	scheme := runtime.NewScheme()
    37  	utilruntime.Must(corev1.AddToScheme(scheme))
    38  	opts := ctrl.Options{
    39  		Scheme: scheme,
    40  	}
    41  
    42  	// create a controller manager in order to create a K8S in-cluster client
    43  	mgr, err := ctrl.NewManager(k8sutil.GetConfigOrDieFromController(), opts)
    44  	if err != nil {
    45  		log.Errorf("Failed to initialize the controller manager")
    46  		os.Exit(1)
    47  	}
    48  
    49  	log.Info("Starting manager")
    50  	go func() {
    51  		if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil {
    52  			log.Errorf("Failed starting controller-runtime manager: %v", err)
    53  			os.Exit(1)
    54  		}
    55  	}()
    56  
    57  	log.Info("Configuring the proxy Kubernetes API client")
    58  	err = proxy.ConfigureKubernetesAPIProxy(authproxy, mgr.GetClient(), log)
    59  	if err != nil {
    60  		os.Exit(1)
    61  	}
    62  
    63  	log.Info("Starting up proxy server to listen for requests")
    64  	err = authproxy.ListenAndServe()
    65  	if err != nil {
    66  		os.Exit(1)
    67  	}
    68  }
    69  
    70  // handleFlags sets up the CLI flags, parses them, and initializes loggers
    71  func handleFlags() {
    72  	flag.IntVar(&proxyPort, "port", 8777, "Port the Auth Proxy listens on.")
    73  
    74  	opts := kzap.Options{}
    75  	opts.BindFlags(flag.CommandLine)
    76  	flag.Parse()
    77  
    78  	kzap.UseFlagOptions(&opts)
    79  	vzlog.InitLogs(opts)
    80  	ctrl.SetLogger(kzap.New(kzap.UseFlagOptions(&opts)))
    81  }