...

Source file src/edge-infra.dev/pkg/k8s/runtime/controller/config.go

Documentation: edge-infra.dev/pkg/k8s/runtime/controller

     1  package controller
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"time"
     7  
     8  	"k8s.io/client-go/rest"
     9  	ctrl "sigs.k8s.io/controller-runtime"
    10  	metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
    11  	"sigs.k8s.io/controller-runtime/pkg/webhook"
    12  )
    13  
    14  var (
    15  	defaultMetricsBindAddress = ":8080"
    16  	defaultLeaderElection     = false
    17  )
    18  
    19  // private options struct, this struct is mutated via the public `Option`
    20  // functions
    21  type options struct {
    22  	config         *rest.Config
    23  	ctrlOptions    ctrl.Options
    24  	webHookOptions webhook.Options
    25  }
    26  
    27  // Option is used to configure a controller
    28  type Option func(*options)
    29  
    30  // WithCfg sets an explicit K8s client rest configuration, if one isn't provided,
    31  // the `ctrl.GetConfigOrDie()` is used.
    32  func WithCfg(c *rest.Config) Option {
    33  	return func(o *options) {
    34  		o.config = c
    35  	}
    36  }
    37  
    38  // WithLeaderElection enables leader election for the controller, defaults to
    39  // false
    40  func WithLeaderElection() Option {
    41  	return func(o *options) {
    42  		o.ctrlOptions.LeaderElection = true
    43  	}
    44  }
    45  
    46  // WithMetricsAddress sets the specific bind address for the controller, defaults
    47  // to ":8080"
    48  func WithMetricsAddress(a string) Option {
    49  	return func(o *options) {
    50  		o.ctrlOptions.Metrics.BindAddress = a
    51  	}
    52  }
    53  
    54  // WithCertDir is the directory that contains the server key and certificate.
    55  func WithCertDir(certDir string) Option {
    56  	return func(o *options) {
    57  		o.webHookOptions.CertDir = certDir
    58  	}
    59  }
    60  
    61  // WithPort is the port that the webhook server serves at.
    62  func WithPort(port int) Option {
    63  	return func(o *options) {
    64  		o.webHookOptions.Port = port
    65  	}
    66  }
    67  
    68  func WithPProf(route string, h http.Handler) Option {
    69  	return func(o *options) {
    70  		o.ctrlOptions.Metrics.ExtraHandlers[route] = h
    71  	}
    72  }
    73  
    74  func WithGracefulTimeout(timeout time.Duration) Option {
    75  	return func(o *options) {
    76  		o.ctrlOptions.GracefulShutdownTimeout = &timeout
    77  	}
    78  }
    79  
    80  // ProcessOptions applies the provided options to the default controller
    81  // configuration and produces a K8s client configuration and base set of options
    82  // for creating a controller with the controller-runtime package.
    83  func ProcessOptions(opts ...Option) (*rest.Config, ctrl.Options) {
    84  	o := &options{
    85  		ctrlOptions: ctrl.Options{
    86  			Metrics:        metricsserver.Options{BindAddress: defaultMetricsBindAddress},
    87  			LeaderElection: defaultLeaderElection},
    88  	}
    89  	for _, opt := range opts {
    90  		opt(o)
    91  	}
    92  
    93  	if o.config == nil {
    94  		// we don't use ctrl.GetConfigOrDie() here so that we can loudly panic,
    95  		// since consumers won't be able to do anythin with the error.
    96  		// if you call ctrl.GetConfigOrDie() and the logger has not been set up,
    97  		// you won't get any logs.
    98  		var err error
    99  		o.config, err = ctrl.GetConfig()
   100  		if err != nil {
   101  			panic(fmt.Sprintf("failed to get Kube client config: %s", err.Error()))
   102  		}
   103  	}
   104  
   105  	o.ctrlOptions.WebhookServer = webhook.NewServer(o.webHookOptions)
   106  
   107  	return o.config, o.ctrlOptions
   108  }
   109  

View as plain text