rkgrpc

package
v1.2.25 Latest Latest
Warning

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

Go to latest
Published: Jan 30, 2022 License: Apache-2.0 Imports: 68 Imported by: 6

Documentation

Overview

Package rkgrpc an implementation of rkentry.Entry which could be used start restful server with grpc framework

Experimental. This is used as grpc proxy server which forwarding grpc request to backend grpc server if not implemented. Currently, grpc-gateway and grpcurl is not supported. The grpc client called from code is supported.

Index

Constants

View Source
const (
	// GrpcEntryType default entry type
	GrpcEntryType = "gRPC"
	// GrpcEntryDescription default entry description
	GrpcEntryDescription = "Internal RK entry which helps to bootstrap with Grpc framework."
)
View Source
const (
	// ProxyEntryType default entry type
	ProxyEntryType = "ProxyEntry"
	// ProxyEntryNameDefault default entry name
	ProxyEntryNameDefault = "ProxyDefault"
	// ProxyEntryDescription default entry description
	ProxyEntryDescription = "Internal RK entry which implements proxy with Grpc framework."
	// HeaderBased header based proxy pattern
	HeaderBased = "headerBased"
	// PathBased grpc path(method) based proxy pattern
	PathBased = "pathBased"
	// IpBased remote IP based proxy pattern
	IpBased = "ipBased"
)

Variables

This section is empty.

Functions

func Codec added in v1.2.10

func Codec() encoding.Codec

Codec returns a proxying grpc.Codec with the default protobuf codec as parent.

See CodecWithParent.

func CodecWithFallback added in v1.2.10

func CodecWithFallback(fallback encoding.Codec) encoding.Codec

CodecWithParent returns a proxying grpc.Codec with a user provided codec as parent.

This codec is *crucial* to the functioning of the proxy. It allows the proxy server to be oblivious to the schema of the forwarded messages. It basically treats a gRPC message frame as raw bytes. However, if the server handler, or the client caller are not proxy-internal functions it will fall back to trying to decode the message using a fallback codec.

func HttpErrorHandler added in v1.2.0

func HttpErrorHandler(ctx context.Context, mux *runtime.ServeMux, marshaler runtime.Marshaler, w http.ResponseWriter, r *http.Request, err error)

HttpErrorHandler Mainly copies from runtime.DefaultHTTPErrorHandler. We reformat error response with rkerror.ErrorResp.

func IncomingHeaderMatcher added in v1.2.0

func IncomingHeaderMatcher(key string) (string, bool)

IncomingHeaderMatcher Pass out all metadata in http header to grpc metadata.

func NewRkGwServerMuxOptions added in v1.2.11

func NewRkGwServerMuxOptions(mOptIn *protojson.MarshalOptions, uOptIn *protojson.UnmarshalOptions) []runtime.ServeMuxOption

NewRkGwServerMuxOptions creates new gw server mux options.

func NewRule added in v1.2.10

func NewRule(opts ...ruleOption) *rule

NewRule create a new proxy rules with options.

func OutgoingHeaderMatcher added in v1.1.2

func OutgoingHeaderMatcher(key string) (string, bool)

OutgoingHeaderMatcher Pass out all metadata in grpc to http header.

func RegisterGrpcEntriesWithConfig added in v1.2.0

func RegisterGrpcEntriesWithConfig(configFilePath string) map[string]rkentry.Entry

RegisterGrpcEntriesWithConfig Register grpc entries with provided config file (Must YAML file).

Currently, support two ways to provide config file path. 1: With function parameters 2: With command line flag "--rkboot" described in rkcommon.BootConfigPathFlagKey (Will override function parameter if exists) Command line flag has high priority which would override function parameter

Error handling: Process will shutdown if any errors occur with rkcommon.ShutdownWithError function

Override elements in config file: We learned from HELM source code which would override elements in YAML file with "--set" flag followed with comma separated key/value pairs.

We are using "--rkset" described in rkcommon.BootConfigOverrideKey in order to distinguish with user flags Example of common usage: ./binary_file --rkset "key1=val1,key2=val2" Example of nested map: ./binary_file --rkset "outer.inner.key=val" Example of slice: ./binary_file --rkset "outer[0].key=val"

func TransparentHandler added in v1.2.10

func TransparentHandler(director Director) grpc.StreamHandler

TransparentHandler returns a handler that attempts to proxy all requests that are not registered in the server. The indented use here is as a transparent proxy, where the server doesn't know about the services implemented by the backends. It should be used as a `grpc.UnknownServiceHandler`.

This can *only* be used if the `server` also uses grpcproxy.CodecForServer() ServerOption.

func WithHeaderPatterns added in v1.2.10

func WithHeaderPatterns(pattern ...*HeaderPattern) ruleOption

WithHeaderPatterns provide header based patterns.

func WithIpPatterns added in v1.2.10

func WithIpPatterns(pattern ...*IpPattern) ruleOption

WithIpPatterns provide IP based patterns.

func WithPathPatterns added in v1.2.10

func WithPathPatterns(pattern ...*PathPattern) ruleOption

WithPathPatterns provide path based patterns.

Types

type BootConfig added in v1.2.19

type BootConfig struct {
	Grpc []struct {
		Name               string                          `yaml:"name" json:"name"`
		Description        string                          `yaml:"description" json:"description"`
		Port               uint64                          `yaml:"port" json:"port"`
		Enabled            bool                            `yaml:"enabled" json:"enabled"`
		EnableReflection   bool                            `yaml:"enableReflection" json:"enableReflection"`
		NoRecvMsgSizeLimit bool                            `yaml:"noRecvMsgSizeLimit" json:"noRecvMsgSizeLimit"`
		CertEntry          string                          `yaml:"certEntry" json:"certEntry"`
		CommonService      rkentry.BootConfigCommonService `yaml:"commonService" json:"commonService"`
		Sw                 rkentry.BootConfigSw            `yaml:"sw" json:"sw"`
		Tv                 rkentry.BootConfigTv            `yaml:"tv" json:"tv"`
		Prom               rkentry.BootConfigProm          `yaml:"prom" json:"prom"`
		Static             rkentry.BootConfigStaticHandler `yaml:"static" json:"static"`
		Proxy              BootConfigProxy                 `yaml:"proxy" json:"proxy"`
		EnableRkGwOption   bool                            `yaml:"enableRkGwOption" json:"enableRkGwOption"`
		GwOption           *gwOption                       `yaml:"gwOption" json:"gwOption"`
		GwMappingFilePaths []string                        `yaml:"gwMappingFilePaths" json:"gwMappingFilePaths"`
		Interceptors       struct {
			LoggingZap       rkmidlog.BootConfig     `yaml:"loggingZap" json:"loggingZap"`
			MetricsProm      rkmidmetrics.BootConfig `yaml:"metricsProm" json:"metricsProm"`
			Auth             rkmidauth.BootConfig    `yaml:"auth" json:"auth"`
			Cors             rkmidcors.BootConfig    `yaml:"cors" json:"cors"`
			Secure           rkmidsec.BootConfig     `yaml:"secure" json:"secure"`
			Meta             rkmidmeta.BootConfig    `yaml:"meta" json:"meta"`
			Jwt              rkmidjwt.BootConfig     `yaml:"jwt" json:"jwt"`
			Csrf             rkmidcsrf.BootConfig    `yaml:"csrf" yaml:"csrf"`
			RateLimit        rkmidlimit.BootConfig   `yaml:"rateLimit" json:"rateLimit"`
			Timeout          rkmidtimeout.BootConfig `yaml:"timeout" json:"timeout"`
			TracingTelemetry rkmidtrace.BootConfig   `yaml:"tracingTelemetry" json:"tracingTelemetry"`
		} `yaml:"interceptors" json:"interceptors"`
		Logger struct {
			ZapLogger   string `yaml:"zapLogger" json:"zapLogger"`
			EventLogger string `yaml:"eventLogger" json:"eventLogger"`
		} `yaml:"logger" json:"logger"`
	} `yaml:"grpc" json:"grpc"`
}

BootConfig Boot config which is for grpc entry.

type BootConfigProxy added in v1.2.10

type BootConfigProxy struct {
	Enabled bool `yaml:"enabled" json:"enabled"`
	Rules   []struct {
		Type        string   `yaml:"type" json:"type"`
		HeaderPairs []string `yaml:"headerPairs" json:"headerPairs"`
		Dest        []string `yaml:"dest" json:"dest"`
		Paths       []string `yaml:"paths" json:"paths"`
		Ips         []string `yaml:"ips" json:"ips"`
	} `yaml:"rules" json:"rules"`
}

BootConfigProxy Boot config which is for proxy entry.

1: Enabled: Enable prom entry. 2: Rules: Provide rules for proxying.

type Director added in v1.2.10

type Director func(context.Context) (context.Context, *grpc.ClientConn, error)

Director creates context and connection based on proxy rules.

type GrpcEntry added in v1.2.0

type GrpcEntry struct {
	EntryName         string                    `json:"entryName" yaml:"entryName"`
	EntryType         string                    `json:"entryType" yaml:"entryType"`
	EntryDescription  string                    `json:"-" yaml:"-"`
	ZapLoggerEntry    *rkentry.ZapLoggerEntry   `json:"-" yaml:"-"`
	EventLoggerEntry  *rkentry.EventLoggerEntry `json:"-" yaml:"-"`
	Port              uint64                    `json:"port" yaml:"port"`
	TlsConfig         *tls.Config               `json:"-" yaml:"-"`
	TlsConfigInsecure *tls.Config               `json:"-" yaml:"-"`
	// GRPC related
	Server             *grpc.Server                   `json:"-" yaml:"-"`
	ServerOpts         []grpc.ServerOption            `json:"-" yaml:"-"`
	UnaryInterceptors  []grpc.UnaryServerInterceptor  `json:"-" yaml:"-"`
	StreamInterceptors []grpc.StreamServerInterceptor `json:"-" yaml:"-"`
	GrpcRegF           []GrpcRegFunc                  `json:"-" yaml:"-"`
	EnableReflection   bool                           `json:"enableReflection" yaml:"enableReflection"`
	// Gateway related
	HttpMux             *http.ServeMux             `json:"-" yaml:"-"`
	HttpServer          *http.Server               `json:"-" yaml:"-"`
	GwMux               *gwruntime.ServeMux        `json:"-" yaml:"-"`
	GwMuxOptions        []gwruntime.ServeMuxOption `json:"-" yaml:"-"`
	GwRegF              []GwRegFunc                `json:"-" yaml:"-"`
	GwMappingFilePaths  []string                   `json:"gwMappingFilePaths" yaml:"gwMappingFilePaths"`
	GwDialOptions       []grpc.DialOption          `json:"-" yaml:"-"`
	GwHttpToGrpcMapping map[string]*gwRule         `json:"gwMapping" yaml:"gwMapping"`

	// Utility related
	SwEntry            *rkentry.SwEntry                `json:"-" yaml:"-"`
	TvEntry            *rkentry.TvEntry                `json:"-" yaml:"-"`
	ProxyEntry         *ProxyEntry                     `json:"-" yaml:"-"`
	PromEntry          *rkentry.PromEntry              `json:"-" yaml:"-"`
	StaticFileEntry    *rkentry.StaticFileHandlerEntry `json:"-" yaml:"-"`
	CommonServiceEntry *rkentry.CommonServiceEntry     `json:"-" yaml:"-"`
	CertEntry          *rkentry.CertEntry              `json:"-" yaml:"-"`
	// contains filtered or unexported fields
}

GrpcEntry implements rkentry.Entry interface.

func GetGrpcEntry added in v1.2.0

func GetGrpcEntry(name string) *GrpcEntry

GetGrpcEntry Get GrpcEntry from rkentry.GlobalAppCtx.

func RegisterGrpcEntry added in v1.2.0

func RegisterGrpcEntry(opts ...GrpcEntryOption) *GrpcEntry

RegisterGrpcEntry Register GrpcEntry with options.

func (*GrpcEntry) AddGwCorsOptions added in v1.2.12

func (entry *GrpcEntry) AddGwCorsOptions(opts ...rkmidcors.Option)

AddGwCorsOptions Enable CORS at gateway side with options.

func (*GrpcEntry) AddGwCsrfOptions added in v1.2.13

func (entry *GrpcEntry) AddGwCsrfOptions(opts ...rkmidcsrf.Option)

AddGwCsrfOptions Enable CORS at gateway side with options.

func (*GrpcEntry) AddGwDialOptions added in v1.2.3

func (entry *GrpcEntry) AddGwDialOptions(opts ...grpc.DialOption)

AddGwDialOptions Add grpc dial options called from grpc gateway

func (*GrpcEntry) AddGwSecureOptions added in v1.2.13

func (entry *GrpcEntry) AddGwSecureOptions(opts ...rkmidsec.Option)

AddGwSecureOptions Enable secure at gateway side with options.

func (*GrpcEntry) AddRegFuncGrpc added in v1.2.3

func (entry *GrpcEntry) AddRegFuncGrpc(f ...GrpcRegFunc)

AddRegFuncGrpc Add grpc registration func.

func (*GrpcEntry) AddRegFuncGw added in v1.2.3

func (entry *GrpcEntry) AddRegFuncGw(f ...GwRegFunc)

AddRegFuncGw Add gateway registration func.

func (*GrpcEntry) AddServerOptions added in v1.2.0

func (entry *GrpcEntry) AddServerOptions(opts ...grpc.ServerOption)

AddServerOptions Add grpc server options.

func (*GrpcEntry) AddStreamInterceptors added in v1.2.0

func (entry *GrpcEntry) AddStreamInterceptors(inter ...grpc.StreamServerInterceptor)

AddStreamInterceptors Add stream interceptor.

func (*GrpcEntry) AddUnaryInterceptors added in v1.2.0

func (entry *GrpcEntry) AddUnaryInterceptors(inter ...grpc.UnaryServerInterceptor)

AddUnaryInterceptors Add unary interceptor.

func (*GrpcEntry) Apis added in v1.2.19

func (entry *GrpcEntry) Apis(w http.ResponseWriter, req *http.Request)

Apis Stub

func (*GrpcEntry) Bootstrap added in v1.2.0

func (entry *GrpcEntry) Bootstrap(ctx context.Context)

Bootstrap GrpcEntry.

func (*GrpcEntry) GetDescription added in v1.2.0

func (entry *GrpcEntry) GetDescription() string

GetDescription Get description of entry.

func (*GrpcEntry) GetName added in v1.2.0

func (entry *GrpcEntry) GetName() string

GetName Get entry name.

func (*GrpcEntry) GetType added in v1.2.0

func (entry *GrpcEntry) GetType() string

GetType Get entry type.

func (*GrpcEntry) GwErrorMapping added in v1.2.19

func (entry *GrpcEntry) GwErrorMapping(w http.ResponseWriter, req *http.Request)

GwErrorMapping Get error mapping file contents.

func (*GrpcEntry) Interrupt added in v1.2.0

func (entry *GrpcEntry) Interrupt(ctx context.Context)

Interrupt GrpcEntry.

func (*GrpcEntry) IsCommonServiceEnabled added in v1.2.0

func (entry *GrpcEntry) IsCommonServiceEnabled() bool

IsCommonServiceEnabled Is common service enabled?

func (*GrpcEntry) IsPromEnabled added in v1.2.3

func (entry *GrpcEntry) IsPromEnabled() bool

IsPromEnabled Is prometheus client enabled?

func (*GrpcEntry) IsProxyEnabled added in v1.2.10

func (entry *GrpcEntry) IsProxyEnabled() bool

IsProxyEnabled Is proxy enabled?

func (*GrpcEntry) IsStaticFileHandlerEnabled added in v1.2.13

func (entry *GrpcEntry) IsStaticFileHandlerEnabled() bool

IsStaticFileHandlerEnabled Is static file handler entry enabled?

func (*GrpcEntry) IsSwEnabled added in v1.2.3

func (entry *GrpcEntry) IsSwEnabled() bool

IsSwEnabled Is swagger enabled?

func (*GrpcEntry) IsTlsEnabled added in v1.2.0

func (entry *GrpcEntry) IsTlsEnabled() bool

IsTlsEnabled Is TLS enabled?

func (*GrpcEntry) IsTvEnabled added in v1.2.3

func (entry *GrpcEntry) IsTvEnabled() bool

IsTvEnabled Is tv enabled?

func (*GrpcEntry) MarshalJSON added in v1.2.0

func (entry *GrpcEntry) MarshalJSON() ([]byte, error)

MarshalJSON Marshal entry.

func (*GrpcEntry) Req added in v1.2.19

func (entry *GrpcEntry) Req(w http.ResponseWriter, req *http.Request)

Req Stub

func (*GrpcEntry) String added in v1.2.0

func (entry *GrpcEntry) String() string

String Stringfy entry.

func (*GrpcEntry) TV added in v1.2.19

func (entry *GrpcEntry) TV(w http.ResponseWriter, req *http.Request)

TV Http handler of /rk/v1/tv/*.

func (*GrpcEntry) UnmarshalJSON added in v1.2.0

func (entry *GrpcEntry) UnmarshalJSON([]byte) error

UnmarshalJSON Not supported.

type GrpcEntryOption added in v1.2.0

type GrpcEntryOption func(*GrpcEntry)

GrpcEntryOption GrpcEntry option.

func WithCertEntry added in v1.2.19

func WithCertEntry(certEntry *rkentry.CertEntry) GrpcEntryOption

WithCertEntry Provide rkentry.CertEntry.

func WithCommonServiceEntry added in v1.2.19

func WithCommonServiceEntry(commonService *rkentry.CommonServiceEntry) GrpcEntryOption

WithCommonServiceEntry Provide CommonServiceEntry.

func WithDescription added in v1.2.19

func WithDescription(description string) GrpcEntryOption

WithDescription Provide description.

func WithEnableReflection added in v1.2.19

func WithEnableReflection(enabled bool) GrpcEntryOption

WithEnableReflection Provide EnableReflection.

func WithEventLoggerEntry added in v1.2.19

func WithEventLoggerEntry(logger *rkentry.EventLoggerEntry) GrpcEntryOption

WithEventLoggerEntry Provide rkentry.EventLoggerEntry

func WithGrpcDialOptions added in v1.2.19

func WithGrpcDialOptions(opts ...grpc.DialOption) GrpcEntryOption

WithGrpcDialOptions Provide grpc dial options.

func WithGrpcRegF added in v1.2.3

func WithGrpcRegF(f ...GrpcRegFunc) GrpcEntryOption

WithGrpcRegF Provide GrpcRegFunc.

func WithGwMappingFilePaths added in v1.2.19

func WithGwMappingFilePaths(paths ...string) GrpcEntryOption

WithGwMappingFilePaths Provide gateway mapping configuration file paths.

func WithGwMuxOptions added in v1.2.19

func WithGwMuxOptions(opts ...gwruntime.ServeMuxOption) GrpcEntryOption

WithGwMuxOptions Provide gateway server mux options.

func WithGwRegF added in v1.2.19

func WithGwRegF(f ...GwRegFunc) GrpcEntryOption

WithGwRegF Provide registration function.

func WithName

func WithName(name string) GrpcEntryOption

WithName Provide name.

func WithPort

func WithPort(port uint64) GrpcEntryOption

WithPort Provide port.

func WithPromEntry added in v1.2.19

func WithPromEntry(prom *rkentry.PromEntry) GrpcEntryOption

WithPromEntry Provide PromEntry.

func WithProxyEntry added in v1.2.19

func WithProxyEntry(proxy *ProxyEntry) GrpcEntryOption

WithProxyEntry Provide ProxyEntry.

func WithServerOptions

func WithServerOptions(opts ...grpc.ServerOption) GrpcEntryOption

WithServerOptions Provide grpc.ServerOption.

func WithStaticFileHandlerEntry added in v1.2.19

func WithStaticFileHandlerEntry(staticEntry *rkentry.StaticFileHandlerEntry) GrpcEntryOption

WithStaticFileHandlerEntry provide StaticFileHandlerEntry.

func WithStreamInterceptors

func WithStreamInterceptors(opts ...grpc.StreamServerInterceptor) GrpcEntryOption

WithStreamInterceptors Provide grpc.StreamServerInterceptor.

func WithSwEntry added in v1.2.19

func WithSwEntry(sw *rkentry.SwEntry) GrpcEntryOption

WithSwEntry Provide SwEntry.

func WithTvEntry added in v1.2.19

func WithTvEntry(tv *rkentry.TvEntry) GrpcEntryOption

WithTvEntry Provide TvEntry.

func WithUnaryInterceptors

func WithUnaryInterceptors(opts ...grpc.UnaryServerInterceptor) GrpcEntryOption

WithUnaryInterceptors Provide grpc.UnaryServerInterceptor.

func WithZapLoggerEntry added in v1.2.19

func WithZapLoggerEntry(logger *rkentry.ZapLoggerEntry) GrpcEntryOption

WithZapLoggerEntry Provide rkentry.ZapLoggerEntry

type GrpcRegFunc added in v1.2.0

type GrpcRegFunc func(server *grpc.Server)

GrpcRegFunc Grpc registration func.

type GwRegFunc added in v1.2.0

GwRegFunc Registration function grpc gateway.

type HeaderPattern added in v1.2.10

type HeaderPattern struct {
	Headers map[string]string
	Dest    []string
}

HeaderPattern defines proxy rules based on header.

Proxy will validate headers in metadata with provided rules.

type IpPattern added in v1.2.10

type IpPattern struct {
	Cidrs []string
	Dest  []string
}

IpPattern defines proxy rules based on remote IPs.

Ip rule support CIDR.

type PathPattern added in v1.2.10

type PathPattern struct {
	Paths []string
	Dest  []string
}

PathPattern defines proxy rules based on path.

The incoming path should match with rules. Path rule support regex.

type ProxyEntry added in v1.2.10

type ProxyEntry struct {
	EntryName        string                    `json:"entryName" yaml:"entryName"`
	EntryType        string                    `json:"entryType" yaml:"entryType"`
	EntryDescription string                    `json:"-" yaml:"-"`
	ZapLoggerEntry   *rkentry.ZapLoggerEntry   `json:"-" yaml:"-"`
	EventLoggerEntry *rkentry.EventLoggerEntry `json:"-" yaml:"-"`
	// contains filtered or unexported fields
}

func NewProxyEntry added in v1.2.10

func NewProxyEntry(opts ...ProxyEntryOption) *ProxyEntry

NewProxyEntry Create a proxy entry with options

func (*ProxyEntry) Bootstrap added in v1.2.10

func (entry *ProxyEntry) Bootstrap(ctx context.Context)

Bootstrap Start prometheus client

func (*ProxyEntry) GetDescription added in v1.2.10

func (entry *ProxyEntry) GetDescription() string

GetDescription Get description of entry

func (*ProxyEntry) GetName added in v1.2.10

func (entry *ProxyEntry) GetName() string

GetName Return name of proxy entry

func (*ProxyEntry) GetType added in v1.2.10

func (entry *ProxyEntry) GetType() string

GetType Return type of prom entry

func (*ProxyEntry) Interrupt added in v1.2.10

func (entry *ProxyEntry) Interrupt(ctx context.Context)

Interrupt Shutdown prometheus client

func (*ProxyEntry) MarshalJSON added in v1.2.10

func (entry *ProxyEntry) MarshalJSON() ([]byte, error)

MarshalJSON Marshal entry

func (*ProxyEntry) String added in v1.2.10

func (entry *ProxyEntry) String() string

String Stringfy prom entry

func (*ProxyEntry) UnmarshalJSON added in v1.2.10

func (entry *ProxyEntry) UnmarshalJSON(b []byte) error

UnmarshalJSON Unmarshal entry

type ProxyEntryOption added in v1.2.10

type ProxyEntryOption func(*ProxyEntry)

ProxyEntryOption Proxy entry option used while initializing proxy entry via code

func WithEventLoggerEntryProxy added in v1.2.10

func WithEventLoggerEntryProxy(eventLoggerEntry *rkentry.EventLoggerEntry) ProxyEntryOption

WithEventLoggerEntryProxy rkentry.EventLoggerEntry of proxy entry

func WithNameProxy added in v1.2.10

func WithNameProxy(name string) ProxyEntryOption

WithNameProm Name of proxy entry

func WithRuleProxy added in v1.2.10

func WithRuleProxy(r *rule) ProxyEntryOption

WithRuleProxy Provide rule

func WithZapLoggerEntryProxy added in v1.2.10

func WithZapLoggerEntryProxy(zapLoggerEntry *rkentry.ZapLoggerEntry) ProxyEntryOption

WithZapLoggerEntryProxy rkentry.ZapLoggerEntry of proxy entry

Directories

Path Synopsis
api
gen

Jump to

Keyboard shortcuts

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