...

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

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

     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  
    23  	"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/apis"
    24  	"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/kccmanager/nocache"
    25  	"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/registration"
    26  	"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/gcp/profiler"
    27  	"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/logging"
    28  	"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/ready"
    29  
    30  	flag "github.com/spf13/pflag"
    31  	corev1 "k8s.io/api/core/v1"
    32  	apiextensions "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
    33  	"sigs.k8s.io/controller-runtime/pkg/client/config"
    34  	klog "sigs.k8s.io/controller-runtime/pkg/log"
    35  	"sigs.k8s.io/controller-runtime/pkg/manager"
    36  	"sigs.k8s.io/controller-runtime/pkg/manager/signals"
    37  )
    38  
    39  var logger = klog.Log.WithName("setup")
    40  
    41  func main() {
    42  	stop := signals.SetupSignalHandler()
    43  
    44  	var enablePprof bool
    45  	var pprofPort int
    46  
    47  	profiler.AddFlag(flag.CommandLine)
    48  	flag.CommandLine.AddGoFlagSet(goflag.CommandLine)
    49  	flag.BoolVar(&enablePprof, "enable-pprof", false, "Enable the pprof server.")
    50  	flag.IntVar(&pprofPort, "pprof-port", 6060, "The port that the pprof server binds to if enabled.")
    51  	flag.Parse()
    52  
    53  	// Enable packages using the Kubernetes controller-runtime logging package to log
    54  	logging.SetupLogger()
    55  
    56  	// Start pprof server if enabled
    57  	if enablePprof {
    58  		go func() {
    59  			if err := http.ListenAndServe(fmt.Sprintf(":%d", pprofPort), nil); err != nil {
    60  				logger.Error(err, "error while running pprof server")
    61  			}
    62  		}()
    63  	}
    64  
    65  	// Start Cloud Profiler agent if enabled
    66  	if err := profiler.StartIfEnabled(); err != nil {
    67  		logging.Fatal(err, "error starting Cloud Profiler agent")
    68  	}
    69  
    70  	// Get a config to talk to the apiserver
    71  	cfg, err := config.GetConfig()
    72  	if err != nil {
    73  		logging.Fatal(err, "error getting config to talk to API server")
    74  	}
    75  
    76  	// Create a new Manager to provide shared dependencies and start components
    77  	mgr, err := manager.New(cfg, manager.Options{
    78  		// Disable cache to avoid stale reads (e.g. of pods, which we need do
    79  		// to determine if a controller pod exists for a namespace).
    80  		// TODO(jcanseco): Determine if disabling the cache for this controller
    81  		// is really necessary. Disable it for now to play it safe.
    82  		NewClient: nocache.NoCacheClientFunc,
    83  	})
    84  	if err != nil {
    85  		logging.Fatal(err, "error creating the manager")
    86  	}
    87  
    88  	// Set up schemes
    89  	apis.AddToSchemes = append(apis.AddToSchemes,
    90  		corev1.AddToScheme,
    91  		apiextensions.AddToScheme,
    92  	)
    93  	if err := apis.AddToScheme(mgr.GetScheme()); err != nil {
    94  		logging.Fatal(err, "error setting up schemes")
    95  	}
    96  
    97  	// Register the registration controller, which will dynamically create
    98  	// controllers for all our resources.
    99  	if err := registration.Add(mgr, nil, nil, nil, nil, registration.RegisterUnmanagedDetectorController); err != nil {
   100  		logging.Fatal(err, "error adding registration controller")
   101  	}
   102  
   103  	// Set up the HTTP server for the readiness probe
   104  	logger.Info("Setting container as ready...")
   105  	ready.SetContainerAsReady()
   106  	logger.Info("Container is ready.")
   107  
   108  	logger.Info("Starting the Cmd.")
   109  
   110  	// Start the Cmd
   111  	logging.Fatal(mgr.Start(stop), "error during manager execution")
   112  }
   113  

View as plain text