webserver

package module
v1.2.115 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 26, 2024 License: MIT Imports: 33 Imported by: 3

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GinLogFormatter added in v1.2.99

func GinLogFormatter(layout string) func(param gin.LogFormatterParams) string

GinLogFormatter is the log format function gin.Logger middleware uses.

func GinMuxer

func GinMuxer(muxer gin.IRouter) healthz.Muxer

Types

type CertKey

type CertKey struct {
	Cert string // public key, containing a PEM-encoded certificate, and possibly the complete certificate chain
	Key  string // private key, containing a PEM-encoded private key for the certificate specified by CertFile
}

CertKey a public/private key pair

type Factory

type Factory struct {
	// contains filtered or unexported fields
}

func NewFactory

func NewFactory(fc FactoryConfig, configs ...FactoryConfigFunc) (Factory, error)

func (*Factory) Config

func (f *Factory) Config() FactoryConfig

func (*Factory) GetBackendBindHostPort

func (f *Factory) GetBackendBindHostPort() string

GetBackendBindHostPort returns a address to listen.

func (*Factory) GetBackendExternalHostPort

func (f *Factory) GetBackendExternalHostPort() string

GetBackendExternalHostPort returns an address to expose with domain, if not set, use host instead.

func (*Factory) GetBackendServeHostPort

func (f *Factory) GetBackendServeHostPort(external bool) string

GetBackendServeHostPort returns an address to expose without domain, if not set, use resolver to resolve an ip

func (*Factory) HTTPScheme

func (f *Factory) HTTPScheme() string

func (*Factory) New

func (f *Factory) New() (*WebServer, error)

New creates a new server which logically combines the handling chain with the passed server. name is used to differentiate for logging. The handler chain in particular can be difficult as it starts delgating.

func (*Factory) ResolveBackendLocalUrl

func (f *Factory) ResolveBackendLocalUrl(relativePaths ...string) string

func (*Factory) ResolveLocalIp

func (f *Factory) ResolveLocalIp() string

type FactoryConfig

type FactoryConfig struct {
	// Name is the human-readable server name, optional
	Name string
	// BindAddress is the host port to bind to (local internet)
	// Will default to a value based on secure serving info and available ipv4 IPs.
	BindAddress string
	// ExternalAddress is the address advertised, even if BindAddress is a loopback. By default, this
	// is set to BindAddress if the later no loopback, or to the first host interface address.
	ExternalAddress string
	// ShutdownDelayDuration allows to block shutdown for some time, e.g. until endpoints pointing to this API server
	// have converged on all node. During this time, the API server keeps serving, /healthz will return 200,
	// but /readyz will return failure.
	ShutdownDelayDuration time.Duration

	TlsConfig                      *tls.Config
	Cors                           cors.Options     // for cors
	ForceDisableTls                bool             // disable tls
	LocalIpResolver                *LocalIpResolver // for resolve local ip to expose, used if advertise_addr is empty
	NoGrpcProxy                    bool             // disable http proxy for grpc client to connect grpc server
	PreferRegisterHTTPFromEndpoint bool             // prefer register http handler from endpoint

	// grpc middlewares
	MaxConcurrencyUnary          int           // for concurrent parallel requests of unary server, The default is 0 (no limit is given)
	MaxConcurrencyStream         int           // for concurrent parallel requests of stream server, The default is 0 (no limit is given)
	BurstLimitTimeoutUnary       time.Duration // for concurrent parallel requests of unary server, The default is 0 (no limit is given)
	BurstLimitTimeoutStream      time.Duration // for concurrent parallel requests of stream server, The default is 0 (no limit is given)
	HandledTimeoutUnary          time.Duration // for max handing time of unary server, The default is 0 (no limit is given)
	HandledTimeoutStream         time.Duration // for max handing time of unary server, The default is 0 (no limit is given)
	MaxReceiveMessageSizeInBytes int           // sets the maximum message size in bytes the grpc server can receive, The default is 0 (no limit is given).
	MaxSendMessageSizeInBytes    int           // sets the maximum message size in bytes the grpc server can send, The default is 0 (no limit is given).

	// Deprecated: takes no effect, use slog instead.
	EnableLogrusMiddleware bool // disable logrus middleware

	GatewayOptions []grpc_.GatewayOption
	GinMiddlewares []gin.HandlerFunc
}

FactoryConfig Config of Factory

func (*FactoryConfig) SetDefaults

func (fc *FactoryConfig) SetDefaults()

SetDefaults sets sensible values for unset fields in config. This is exported for testing: Configs passed to repository functions are copied and have default values set automatically.

func (*FactoryConfig) Validate

func (fc *FactoryConfig) Validate() error

Validate inspects the fields of the type to determine if they are valid.

type FactoryConfigFunc

type FactoryConfigFunc func(os *FactoryConfig) error

FactoryConfigFunc is an alias for a function that will take in a pointer to an FactoryConfig and modify it

type LocalIpResolver

type LocalIpResolver struct {
	Networks  []string
	Addresses []string
	Timeout   time.Duration
}

type Net

type Net struct {
	Host    string
	Domains []string // service name to register to consul for dns
	Port    int32
}

type PostStartHookFunc

type PostStartHookFunc func(ctx context.Context) error

PostStartHookFunc is a function that is called after the server has started. It must properly handle cases like:

  1. asynchronous start in multiple API server processes
  2. conflicts between the different processes all trying to perform the same action
  3. partially complete work (API server crashes while running your hook)
  4. API server access **BEFORE** your hook has completed

Think of it like a mini-controller that is super privileged and gets to run in-process If you use this feature, tag @deads2k on github who has promised to review code for anyone's PostStartHook until it becomes easier to use. ctx will be cancelled when WebServer is Closed or any other PostStartHookFunc failed.

type PostStartHookProvider

type PostStartHookProvider interface {
	PostStartHook() (string, PostStartHookFunc, error)
}

PostStartHookProvider is an interface in addition to provide a post start hook for the api server

type PreShutdownHookFunc

type PreShutdownHookFunc func() error

PreShutdownHookFunc is a function that can be added to the shutdown logic.

type TLS

type TLS struct {
	Enable        bool
	KeyPairBase64 *CertKey // key pair in base64 format encoded from pem
	KeyPairPath   *CertKey // key pair stored in file from pem
	// service_name is used to verify the hostname on the returned
	// certificates unless InsecureSkipVerify is given. It is also included
	// in the client's handshake to support virtual hosting unless it is
	// an IP address.
	ServiceName      string
	AllowedTlsCidrs  []string //"127.0.0.1/24"
	WhitelistedPaths []string
}

type WebHandler

type WebHandler interface {
	SetRoutes(ginRouter gin.IRouter, grpcRouter *grpc.Gateway)
}

type WebServer

type WebServer struct {
	Name string
	// BindAddress is the host name to use for bind (local internet) facing URLs (e.g. Loopback)
	// Will default to a value based on secure serving info and available ipv4 IPs.
	BindAddress string
	// ExternalAddress is the host name to use for external (public internet) facing URLs (e.g. Swagger)
	// Will default to a value based on secure serving info and available ipv4 IPs.
	ExternalAddress string

	PreferRegisterHTTPFromEndpoint bool // prefer register http handler from endpoint

	// ShutdownDelayDuration allows to block shutdown for some time, e.g. until endpoints pointing to this API server
	// have converged on all node. During this time, the API server keeps serving, /healthz will return 200,
	// but /readyz will return failure.
	ShutdownDelayDuration time.Duration

	// ShutdownTimeout is the timeout used for server shutdown. This specifies the timeout before server
	// gracefully shutdown returns.
	ShutdownTimeout time.Duration
	// contains filtered or unexported fields
}

func NewWebServer

func NewWebServer(fc FactoryConfig, configs ...FactoryConfigFunc) (*WebServer, error)

func (*WebServer) AddBootSequenceHealthChecks

func (s *WebServer) AddBootSequenceHealthChecks(checks ...healthz.HealthChecker) error

AddBootSequenceHealthChecks adds health checks to the old healthz endpoint (for backwards compatibility reasons) as well as livez and readyz. The livez grace period is defined by the value of the command-line flag --livez-grace-period; before the grace period elapses, the livez health checks will default to healthy. One may want to set a grace period in order to prevent the kubelet from restarting the kube-apiserver due to long-ish boot sequences. Readyz health checks, on the other hand, have no grace period, since readyz should fail until boot fully completes.

func (*WebServer) AddBootSequencePostStartHook added in v1.2.111

func (s *WebServer) AddBootSequencePostStartHook(name string, hook PostStartHookFunc) error

AddBootSequencePostStartHook allows you to add a PostStartHook in order.

func (*WebServer) AddBootSequencePreShutdownHook added in v1.2.111

func (s *WebServer) AddBootSequencePreShutdownHook(name string, hook PreShutdownHookFunc) error

AddBootSequencePreShutdownHook allows you to add a PreShutdownHook in reverse order.

func (*WebServer) AddHealthChecks

func (s *WebServer) AddHealthChecks(checks ...healthz.HealthChecker) error

AddHealthChecks adds HealthCheck(s) to health endpoints (healthz, livez, readyz) but configures the liveness grace period to be zero, which means we expect this health check to immediately indicate that the apiserver is unhealthy.

func (*WebServer) AddPostStartHook

func (s *WebServer) AddPostStartHook(name string, hook PostStartHookFunc) error

AddPostStartHook allows you to add a PostStartHook.

func (*WebServer) AddPostStartHookOrDie

func (s *WebServer) AddPostStartHookOrDie(name string, hook PostStartHookFunc)

AddPostStartHookOrDie allows you to add a PostStartHook, but dies on failure

func (*WebServer) AddPreShutdownHook

func (s *WebServer) AddPreShutdownHook(name string, hook PreShutdownHookFunc) error

AddPreShutdownHook allows you to add a PreShutdownHook.

func (*WebServer) AddPreShutdownHookOrDie

func (s *WebServer) AddPreShutdownHookOrDie(name string, hook PreShutdownHookFunc)

AddPreShutdownHookOrDie allows you to add a PostStartHook, but dies on failure

func (*WebServer) InstallWebHandlers

func (s *WebServer) InstallWebHandlers(handlers ...WebHandler)

func (*WebServer) PrepareRun

func (s *WebServer) PrepareRun() (preparedWebServer, error)

PrepareRun does post API installation setup steps. It calls recursively the same function of the delegates.

func (*WebServer) RunPostStartHooks

func (s *WebServer) RunPostStartHooks(ctx context.Context) error

RunPostStartHooks runs the PostStartHooks for the server

func (*WebServer) RunPreShutdownHooks

func (s *WebServer) RunPreShutdownHooks() error

RunPreShutdownHooks runs the PreShutdownHooks for the server

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL