github.com/oam-dev/cluster-gateway@v1.9.0/cmd/addon-manager/main.go (about)

     1  package main
     2  
     3  import (
     4  	"context"
     5  	"flag"
     6  	"os"
     7  
     8  	"github.com/oam-dev/cluster-gateway/pkg/addon/agent"
     9  	"github.com/oam-dev/cluster-gateway/pkg/addon/controllers"
    10  	proxyv1alpha1 "github.com/oam-dev/cluster-gateway/pkg/apis/proxy/v1alpha1"
    11  	"github.com/oam-dev/cluster-gateway/pkg/util"
    12  	"github.com/oam-dev/cluster-gateway/pkg/util/cert"
    13  	"k8s.io/apimachinery/pkg/runtime"
    14  	"k8s.io/client-go/informers"
    15  	"k8s.io/client-go/kubernetes"
    16  	nativescheme "k8s.io/client-go/kubernetes/scheme"
    17  	"k8s.io/klog/v2"
    18  	"k8s.io/klog/v2/klogr"
    19  	apiregistrationv1 "k8s.io/kube-aggregator/pkg/apis/apiregistration/v1"
    20  	"open-cluster-management.io/addon-framework/pkg/addonmanager"
    21  	addonv1alpha1 "open-cluster-management.io/api/addon/v1alpha1"
    22  	ocmauthv1alpha1 "open-cluster-management.io/managed-serviceaccount/api/v1alpha1"
    23  	ctrl "sigs.k8s.io/controller-runtime"
    24  )
    25  
    26  var (
    27  	scheme   = runtime.NewScheme()
    28  	setupLog = ctrl.Log.WithName("setup")
    29  )
    30  
    31  func init() {
    32  	addonv1alpha1.AddToScheme(scheme)
    33  	proxyv1alpha1.AddToScheme(scheme)
    34  	nativescheme.AddToScheme(scheme)
    35  	apiregistrationv1.AddToScheme(scheme)
    36  	ocmauthv1alpha1.AddToScheme(scheme)
    37  }
    38  
    39  func main() {
    40  	var metricsAddr string
    41  	var enableLeaderElection bool
    42  	var probeAddr string
    43  	var signerSecretName string
    44  
    45  	logger := klogr.New()
    46  	klog.SetOutput(os.Stdout)
    47  	klog.InitFlags(flag.CommandLine)
    48  	flag.StringVar(&metricsAddr, "metrics-bind-address", ":48080", "The address the metric endpoint binds to.")
    49  	flag.StringVar(&probeAddr, "health-probe-bind-address", ":48081", "The address the probe endpoint binds to.")
    50  	flag.BoolVar(&enableLeaderElection, "leader-elect", false,
    51  		"Enable leader election for controller manager. "+
    52  			"Enabling this will ensure there is only one active controller manager.")
    53  	flag.StringVar(&signerSecretName, "signer-secret-name", "cluster-gateway-signer",
    54  		"The name of the secret to store the signer CA")
    55  
    56  	flag.Parse()
    57  	ctrl.SetLogger(logger)
    58  
    59  	mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
    60  		Scheme:                 scheme,
    61  		MetricsBindAddress:     metricsAddr,
    62  		Port:                   9443,
    63  		HealthProbeBindAddress: probeAddr,
    64  		LeaderElection:         enableLeaderElection,
    65  		LeaderElectionID:       "cluster-gateway-addon-manager",
    66  	})
    67  	if err != nil {
    68  		setupLog.Error(err, "unable to start manager")
    69  		os.Exit(1)
    70  	}
    71  
    72  	currentNamespace := os.Getenv("NAMESPACE")
    73  	if len(currentNamespace) == 0 {
    74  		inClusterNamespace, err := util.GetInClusterNamespace()
    75  		if err != nil {
    76  			klog.Fatal("the manager should be either running in a container or specify NAMESPACE environment")
    77  		}
    78  		currentNamespace = inClusterNamespace
    79  	}
    80  
    81  	caPair, err := cert.EnsureCAPair(mgr.GetConfig(), currentNamespace, signerSecretName)
    82  	if err != nil {
    83  		setupLog.Error(err, "unable to ensure ca signer")
    84  	}
    85  	nativeClient, err := kubernetes.NewForConfig(mgr.GetConfig())
    86  	if err != nil {
    87  		setupLog.Error(err, "unable to instantiate legacy client")
    88  		os.Exit(1)
    89  	}
    90  	informerFactory := informers.NewSharedInformerFactory(nativeClient, 0)
    91  	if err := controllers.SetupClusterGatewayInstallerWithManager(
    92  		mgr,
    93  		caPair,
    94  		nativeClient,
    95  		informerFactory.Core().V1().Secrets().Lister()); err != nil {
    96  		setupLog.Error(err, "unable to setup installer")
    97  		os.Exit(1)
    98  	}
    99  	if err := controllers.SetupClusterGatewayHealthProberWithManager(mgr); err != nil {
   100  		setupLog.Error(err, "unable to setup health prober")
   101  		os.Exit(1)
   102  	}
   103  
   104  	ctx := context.Background()
   105  	go informerFactory.Start(ctx.Done())
   106  
   107  	addonManager, err := addonmanager.New(mgr.GetConfig())
   108  	if err != nil {
   109  		setupLog.Error(err, "unable to set up ready check")
   110  		os.Exit(1)
   111  	}
   112  	if err := addonManager.AddAgent(agent.NewClusterGatewayAddonManager(
   113  		mgr.GetConfig(),
   114  		mgr.GetClient(),
   115  	)); err != nil {
   116  		setupLog.Error(err, "unable to register addon manager")
   117  		os.Exit(1)
   118  	}
   119  
   120  	ctx, cancel := context.WithCancel(ctrl.SetupSignalHandler())
   121  	defer cancel()
   122  	go addonManager.Start(ctx)
   123  
   124  	if err := mgr.Start(ctx); err != nil {
   125  		panic(err)
   126  	}
   127  
   128  }