...

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

Documentation: github.com/GoogleCloudPlatform/k8s-config-connector/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  	"context"
    19  	goflag "flag"
    20  	"fmt"
    21  	"io/ioutil"
    22  	"log"
    23  	"net/http"
    24  	_ "net/http/pprof" // Needed to allow pprof server to accept requests
    25  
    26  	"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/kccmanager"
    27  	controllermetrics "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/metrics"
    28  	"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/gcp/profiler"
    29  	"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/krmtotf"
    30  	"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/logging"
    31  	"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/metrics"
    32  	"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/ready"
    33  
    34  	flag "github.com/spf13/pflag"
    35  	_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
    36  	"k8s.io/client-go/rest"
    37  	"sigs.k8s.io/controller-runtime/pkg/client/config"
    38  	klog "sigs.k8s.io/controller-runtime/pkg/log"
    39  	"sigs.k8s.io/controller-runtime/pkg/manager"
    40  	"sigs.k8s.io/controller-runtime/pkg/manager/signals"
    41  )
    42  
    43  var logger = klog.Log.WithName("setup")
    44  
    45  func main() {
    46  	ctx := context.Background()
    47  
    48  	stop := signals.SetupSignalHandler()
    49  
    50  	var (
    51  		prometheusScrapeEndpoint string
    52  		scopedNamespace          string
    53  		userProjectOverride      bool
    54  		billingProject           string
    55  		enablePprof              bool
    56  		pprofPort                int
    57  	)
    58  	flag.StringVar(&prometheusScrapeEndpoint, "prometheus-scrape-endpoint", ":8888", "configure the Prometheus scrape endpoint; :8888 as default")
    59  	flag.BoolVar(&controllermetrics.ResourceNameLabel, "resource-name-label", false, "option to enable the resource name label on some Prometheus metrics; false by default")
    60  	flag.BoolVar(&userProjectOverride, "user-project-override", false, "option to use the resource project for preconditions, quota, and billing, instead of the project the credentials belong to; false by default")
    61  	flag.StringVar(&billingProject, "billing-project", "", "project to use for preconditions, quota, and billing if --user-project-override is enabled; empty by default; if this is left empty but --user-project-override is enabled, the resource's project will be used")
    62  	flag.StringVar(&scopedNamespace, "scoped-namespace", "", "scope controllers to only watch resources in the specified namespace; if unspecified, controllers will run in cluster scope")
    63  	flag.BoolVar(&enablePprof, "enable-pprof", false, "Enable the pprof server.")
    64  	flag.IntVar(&pprofPort, "pprof-port", 6060, "The port that the pprof server binds to if enabled.")
    65  	profiler.AddFlag(flag.CommandLine)
    66  	flag.CommandLine.AddGoFlagSet(goflag.CommandLine)
    67  	flag.Parse()
    68  
    69  	// Discard everything logged onto the Go standard logger. We do this since
    70  	// there are cases of Terraform logging sensitive data onto the Go standard
    71  	// logger.
    72  	log.SetOutput(ioutil.Discard)
    73  
    74  	logging.SetupLogger()
    75  
    76  	// Start pprof server if enabled
    77  	if enablePprof {
    78  		go func() {
    79  			if err := http.ListenAndServe(fmt.Sprintf(":%d", pprofPort), nil); err != nil {
    80  				logger.Error(err, "error while running pprof server")
    81  			}
    82  		}()
    83  	}
    84  
    85  	// Start Cloud Profiler agent if enabled
    86  	if err := profiler.StartIfEnabled(); err != nil {
    87  		logging.Fatal(err, "error starting Cloud Profiler agent")
    88  	}
    89  
    90  	// Get a config to talk to the apiserver
    91  	restCfg, err := config.GetConfig()
    92  	if err != nil {
    93  		logging.Fatal(err, "fatal getting configuration from APIServer.")
    94  	}
    95  
    96  	logger.Info("Creating the manager")
    97  	mgr, err := newManager(ctx, restCfg, scopedNamespace, userProjectOverride, billingProject)
    98  	if err != nil {
    99  		logging.Fatal(err, "error creating the manager")
   100  	}
   101  
   102  	// Register controller OpenCensus views
   103  	logger.Info("Registering controller OpenCensus views.")
   104  	if controllermetrics.ResourceNameLabel {
   105  		if err = metrics.RegisterControllerOpenCensusViewsWithResourceNameLabel(); err != nil {
   106  			logging.Fatal(err, "error registering controller OpenCensus views with resource name label.")
   107  		}
   108  	} else {
   109  		if err = metrics.RegisterControllerOpenCensusViews(); err != nil {
   110  			logging.Fatal(err, "error registering controller OpenCensus views.")
   111  		}
   112  	}
   113  
   114  	// Register the Prometheus exporter
   115  	logger.Info("Registering the Prometheus exporter")
   116  	if err = metrics.RegisterPrometheusExporter(prometheusScrapeEndpoint); err != nil {
   117  		logging.Fatal(err, "error registering the Prometheus exporter.")
   118  	}
   119  
   120  	// Record the process start time which will be used by prometheus-to-sd sidecar
   121  	if err = metrics.RecordProcessStartTime(); err != nil {
   122  		logging.Fatal(err, "error recording the process start time.")
   123  	}
   124  
   125  	// Set up the HTTP server for the readiness probe
   126  	logger.Info("Setting container as ready...")
   127  	ready.SetContainerAsReady()
   128  	logger.Info("Container is ready.")
   129  
   130  	logger.Info("Starting the Cmd.")
   131  
   132  	// Start the Cmd
   133  	logging.Fatal(mgr.Start(stop), "error during manager execution.")
   134  }
   135  
   136  func newManager(ctx context.Context, restCfg *rest.Config, scopedNamespace string, userProjectOverride bool, billingProject string) (manager.Manager, error) {
   137  	krmtotf.SetUserAgentForTerraformProvider()
   138  	controllersCfg := kccmanager.Config{
   139  		ManagerOptions: manager.Options{
   140  			Namespace: scopedNamespace,
   141  		},
   142  	}
   143  
   144  	controllersCfg.UserProjectOverride = userProjectOverride
   145  	controllersCfg.BillingProject = billingProject
   146  	mgr, err := kccmanager.New(ctx, restCfg, controllersCfg)
   147  	if err != nil {
   148  		return nil, fmt.Errorf("error creating manager: %w", err)
   149  	}
   150  	return mgr, nil
   151  }
   152  

View as plain text