...

Source file src/github.com/GoogleCloudPlatform/k8s-config-connector/operator/cmd/manager/main.go

Documentation: github.com/GoogleCloudPlatform/k8s-config-connector/operator/cmd/manager

     1  // Copyright 2022 Google LLC
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package main
    16  
    17  import (
    18  	goflag "flag"
    19  	"fmt"
    20  	"net/http"
    21  	_ "net/http/pprof" // Needed to allow pprof server to accept requests
    22  	"os"
    23  
    24  	customizev1alpha1 "github.com/GoogleCloudPlatform/k8s-config-connector/operator/pkg/apis/core/customize/v1alpha1"
    25  	corev1v1beta1 "github.com/GoogleCloudPlatform/k8s-config-connector/operator/pkg/apis/core/v1beta1"
    26  	"github.com/GoogleCloudPlatform/k8s-config-connector/operator/pkg/controllers/configconnector"
    27  	"github.com/GoogleCloudPlatform/k8s-config-connector/operator/pkg/controllers/configconnectorcontext"
    28  	"github.com/GoogleCloudPlatform/k8s-config-connector/operator/pkg/logging"
    29  	"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/kccmanager/nocache"
    30  	"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/gcp/profiler"
    31  
    32  	flag "github.com/spf13/pflag"
    33  	apiextensions "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
    34  	"k8s.io/apimachinery/pkg/runtime"
    35  	clientgoscheme "k8s.io/client-go/kubernetes/scheme"
    36  	_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
    37  	ctrl "sigs.k8s.io/controller-runtime"
    38  	"sigs.k8s.io/kubebuilder-declarative-pattern/pkg/patterns/addon"
    39  )
    40  
    41  var (
    42  	scheme   = runtime.NewScheme()
    43  	setupLog = ctrl.Log.WithName("setup")
    44  )
    45  
    46  func init() {
    47  	_ = clientgoscheme.AddToScheme(scheme)
    48  	_ = apiextensions.SchemeBuilder.AddToScheme(scheme)
    49  
    50  	_ = corev1v1beta1.AddToScheme(scheme)
    51  	// +kubebuilder:scaffold:scheme
    52  	_ = customizev1alpha1.AddToScheme(scheme)
    53  	// +kubebuilder:scaffold:scheme
    54  }
    55  
    56  func main() {
    57  	var metricsAddr string
    58  	var enableLeaderElection bool
    59  	var repoPath string
    60  	var enablePprof bool
    61  	var pprofPort int
    62  
    63  	flag.CommandLine.AddGoFlagSet(goflag.CommandLine)
    64  	profiler.AddFlag(flag.CommandLine)
    65  	flag.StringVar(&repoPath, "local-repo", "./channels", "location of local repository to use")
    66  	flag.StringVar(&metricsAddr, "metrics-addr", ":8080", "The address the metric endpoint binds to.")
    67  	flag.BoolVar(&enableLeaderElection, "enable-leader-election", false,
    68  		"Enable leader election for controller manager. Enabling this will ensure there is only one active controller manager.")
    69  	flag.BoolVar(&enablePprof, "enable-pprof", false, "Enable the pprof server.")
    70  	flag.IntVar(&pprofPort, "pprof-port", 6060, "The port that the pprof server binds to if enabled.")
    71  	flag.Parse()
    72  
    73  	ctrl.SetLogger(logging.BuildLogger(os.Stderr))
    74  
    75  	// Start pprof server if enabled
    76  	if enablePprof {
    77  		go func() {
    78  			if err := http.ListenAndServe(fmt.Sprintf(":%d", pprofPort), nil); err != nil {
    79  				setupLog.Error(err, "error while running pprof server")
    80  			}
    81  		}()
    82  	}
    83  
    84  	// Start Cloud Profiler agent if enabled
    85  	if err := profiler.StartIfEnabled(); err != nil {
    86  		setupLog.Error(err, "error starting Cloud Profiler agent")
    87  		os.Exit(1)
    88  	}
    89  
    90  	addon.Init()
    91  
    92  	mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
    93  		Scheme:             scheme,
    94  		MetricsBindAddress: metricsAddr,
    95  		LeaderElection:     enableLeaderElection,
    96  		Port:               9443,
    97  		// Disable the caching for the client. The cached reader will lazily list structured resources cross namespaces.
    98  		// The operator mostly only cares about resources in cnrm-system namespace.
    99  		NewClient: nocache.NoCacheClientFunc,
   100  	})
   101  	if err != nil {
   102  		setupLog.Error(err, "unable to start manager")
   103  		os.Exit(1)
   104  	}
   105  
   106  	if err = configconnector.Add(mgr, repoPath); err != nil {
   107  		setupLog.Error(err, "unable to create controller", "controller", "ConfigConnector")
   108  		os.Exit(1)
   109  	}
   110  
   111  	if err = configconnectorcontext.Add(mgr, repoPath); err != nil {
   112  		setupLog.Error(err, "unable to create controller", "controller", "ConfigConnectorContext")
   113  		os.Exit(1)
   114  	}
   115  
   116  	setupLog.Info("starting manager")
   117  	if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil {
   118  		setupLog.Error(err, "problem running manager")
   119  		os.Exit(1)
   120  	}
   121  }
   122  

View as plain text