server

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 23, 2019 License: Apache-2.0 Imports: 28 Imported by: 0

README

Report

Services running

Service Healthz Healthz (5824)
--healthz-port 5824
Secure Insecure
--insecure
HTTP (5823) (*)
--no-grpc
na na na(3) na(1)
GRPC (5823)
--no-http
ok(2) ok(2) no(4) ok(2)
HTTP (5823) & GRPC (15823) (**)
--port-grpc 15823
ok(5) ok(5) no(6) ok(5)
HTTP/GRPC (5823) ok(8) ok(8) ok(8) ok(7)
TODO/FIX
  • (4) : Send the CA certificate and use it at the client
  • (6) : Create a certificate for gRPC using its port. GRPC does not work bc it's using port 15823 and cert is for port 5823. But, do we really need HTTP & GRPC running on different ports?
Comments

(*) : This option do not expose GRPC, but GRPC is required internally so the HTTP Gateway can use it. If this is what the user wants, then use a HTTP framework. (**) : Why would be needed HTTP & GRPC needed in 2 different ports?

Available Options
  • start server: start server without options expose HTTP and gRPC API securely (TLS) on same port. HealthCheck also runs on same port.
  • --insecure: expose HTTP or GRPC API insecurely, no TLS configured.
  • --no-http: HTTP/REST API is not required, only GRPC. This option disable HealCheck on HTTP port, it is only listening on gRPC.
  • --healthz-port: run health check in a different HTTP port. With --no-http this option does not apply, health check runs only on gRPC. Default to same port as HTTP/REST

Create TLS Keys

openssl req -newkey rsa:2048 -nodes -keyout kubekit.key -x509 -days 365 -out kubekit.crt -subj "/C=US/ST=California/L=San Diego/O=LifeRaft/OU=KubeKit/CN=localhost:5823"
openssl req -newkey rsa:2048 -nodes -keyout kubekit-grpc.key -x509 -days 365 -out kubekit-grpc.crt -subj "/C=US/ST=California/L=San Diego/O=LifeRaft/OU=KubeKit/CN=localhost:15823"

rm -f kubekit{,-grpc}.{key,crt}

Commands

  1. HTTP (5823) Insecure:
kubekit start server --debug --no-grpc --insecure [--healthz-port 5824]
kubekitctl --no-grpc --insecure [--healthz-port 5824]
curl -X GET http://localhost:5823/api/v1/version
  1. GRPC (5823) Insecure:
kubekit start server --debug --no-http --insecure [--healthz-port 5824]
kubekitctl --no-http --insecure [--healthz-port 5824]
  1. HTTP (5823) Secure:
kubekit start server --debug --no-grpc [--healthz-port 5824]
kubekitctl --no-grpc [--healthz-port 5824]
curl -X GET -k https://localhost:5823/api/v1/version
  1. GRPC (5823) Secure:
kubekit start server --debug --no-http [--healthz-port 5824]
kubekitctl --no-http [--healthz-port 5824]
  1. HTTP (5823) & GRPC (15823) Insecure:
kubekit start server --debug --grpc-port 15823 --insecure [--healthz-port 5824]
kubekitctl --grpc-port 15823 --insecure [--healthz-port 5824]
curl -X GET http://localhost:5823/api/v1/version
  1. HTTP (5823) & GRPC (15823) Secure:
kubekit start server --debug --port-grpc 15823 [--healthz-port 5824]
kubekitctl [--healthz-port 5824]
curl -X GET -k https://localhost:5823/api/v1/version
  1. HTTP/GRPC (5823) Insecure:
kubekit start server --debug --insecure [--healthz-port 5824]
kubekitctl --insecure [--healthz-port 5824]
curl -X GET http://localhost:5823/api/v1/version
  1. HTTP/GRPC (5823) Secure:
kubekit start server --debug [--healthz-port 5824]
kubekitctl [--healthz-port 5824]
curl -X GET -k https://localhost:5823/api/v1/version

Debugging

Use the following env variables to identify gRPC errors at the client and server terminal.

export GRPC_GO_LOG_VERBOSITY_LEVEL=99
export GRPC_GO_LOG_SEVERITY_LEVEL=info

Documentation

Index

Constants

View Source
const EnvTLSServerPassword = "KUBEKIT_SERVER_TLS_PASS"

EnvTLSServerPassword is the name of the environment variable to store/pass the password of the CA key

Variables

This section is empty.

Functions

func GetFreePort

func GetFreePort() (string, error)

GetFreePort returns an available port in the system

func IsPortFree

func IsPortFree(port string) bool

IsPortFree checks if the given port is available to use

func StartAndWait

func StartAndWait(address string, parentUI *ui.UI, services ...*Service) error

StartAndWait starts the HTTP/REST and gRPC server on the same port. It returns when the server is manually stopped or due to an error, returning such error

func StartSecureAndWait

func StartSecureAndWait(address string, certDir, pubKeyFile, privKeyFile, clientCAFile string, parentUI *ui.UI, services ...*Service) error

StartSecureAndWait starts the HTTP/REST and gRPC server on the same port using TLS. It returns when the server is manually stopped or due to an error, returning such error

Types

type APIError

type APIError struct {
	StatusCode int    `json:"code" yaml:"code" toml:"code"`
	Status     string `json:"status" yaml:"status" toml:"status"`
	Message    string `json:"message" yaml:"message" toml:"message"`
	Target     string `json:"target" yaml:"target" toml:"target"`
}

APIError represents an error to return to the HTTP/REST API, usually in JSON format

func Error

func Error(code int, target, message string) *APIError

Error creates an API Error

func Errorf

func Errorf(code int, target, message string, a ...interface{}) *APIError

Errorf creates an error with a formatted error message

func (*APIError) JSON

func (e *APIError) JSON(pp bool) ([]byte, error)

JSON returns the API Error in JSON format

func (*APIError) String

func (e *APIError) String() string

func (*APIError) Stringf

func (e *APIError) Stringf(format string) string

Stringf returns the API Error in the given format: json, yaml or toml

func (*APIError) TOML

func (e *APIError) TOML() ([]byte, error)

TOML returns the API Error in TOML format

func (*APIError) Write

func (e *APIError) Write(w http.ResponseWriter)

Write writes the error to the given http response writer

func (*APIError) YAML

func (e *APIError) YAML() ([]byte, error)

YAML returns the API Error in YAML format

type HealthCheckResponse

type HealthCheckResponse struct {
	StatusCode int `json:"code" yaml:"code" toml:"code"`

	Status  string `json:"status" yaml:"status" toml:"status"`
	Message string `json:"message" yaml:"message" toml:"message"`
	Service string `json:"service" yaml:"service" toml:"service"`
	// contains filtered or unexported fields
}

HealthCheckResponse encapsulate the server response of the Health Check

func HealthzResponse

func HealthzResponse(code int, service string, message ...string) *HealthCheckResponse

HealthzResponse creates a HealthCheck Response. Uses a default message if the message is not provided

func HealthzResponsef

func HealthzResponsef(code int, service, message string, a ...interface{}) *HealthCheckResponse

HealthzResponsef creates an error with a formatted error message

func (*HealthCheckResponse) JSON

func (r *HealthCheckResponse) JSON(pp bool) ([]byte, error)

JSON returns the API Error in JSON format

func (*HealthCheckResponse) String

func (r *HealthCheckResponse) String() string

func (*HealthCheckResponse) Stringf

func (r *HealthCheckResponse) Stringf(format string) string

Stringf returns the API Error in the given format: json, yaml or toml

func (*HealthCheckResponse) TOML

func (r *HealthCheckResponse) TOML() ([]byte, error)

TOML returns the API Error in TOML format

func (*HealthCheckResponse) Write

Write writes the error to the given http response writer

func (*HealthCheckResponse) YAML

func (r *HealthCheckResponse) YAML() ([]byte, error)

YAML returns the API Error in YAML format

type RegisterHandlerFromEndpoint

type RegisterHandlerFromEndpoint func(context.Context, *runtime.ServeMux, string, []grpc.DialOption) error

RegisterHandlerFromEndpoint is a function from the gRPC GateWay API to dialing to "endpoint" and closing the connection when "ctx" gets done

type Server

type Server struct {
	Name string
	// contains filtered or unexported fields
}

Server encapsulate the server configuration

func New

func New(ctx context.Context, name, host string, parentUI *ui.UI) *Server

New creates a new Server to expose the HTTP/REST and gRPC API as well as the Swagger configuration

func NewTLS

func NewTLS(ctx context.Context, name, host string, certDir, pubKeyFile, privKeyFile, clientCAFile string, parentUI *ui.UI) *Server

NewTLS creates a new server using TLS

func (*Server) AddService

func (s *Server) AddService(name string, service *Service) *Server

AddService appends a new service to serve

func (*Server) AddServices

func (s *Server) AddServices(services Services) *Server

AddServices appends all the given services to serve

func (*Server) AllowCORS

func (s *Server) AllowCORS() *Server

AllowCORS makes the server to allow CORS (Cross-Origin Resource Sharing)

func (*Server) Err

func (s *Server) Err() error

Err returns the errors ocurred

func (*Server) GenerateCertificates

func (s *Server) GenerateCertificates(certDir, pubKeyFile, privKeyFile, caFile string) (err error)

GenerateCertificates assign the server certificates either by loading the given files, from the certificates directory or generating them from a self-signed generated CA certificate

func (*Server) IsHealthy

func (s *Server) IsHealthy() bool

IsHealthy return true if all the services are serving. If one service is not serving, the server is not healthy

func (*Server) IsServiceNotServing

func (s *Server) IsServiceNotServing(name string) bool

IsServiceNotServing return true if the given service name is serving

func (*Server) IsServiceServing

func (s *Server) IsServiceServing(name string) bool

IsServiceServing return true if the given service name is serving

func (*Server) IsServing

func (s *Server) IsServing(service string) bool

IsServing returns true if given service ("grpc", "http", "mux") is running

func (*Server) LoadCertificates

func (s *Server) LoadCertificates(certDir, pubKeyFile, privKeyFile, caFile string) (err error)

LoadCertificates assign the server certificates loading the given files or from the certificates directory. Th certificates files (server key, server cert & CA cert) are created by the user.

func (*Server) RegisterHealthCheck

func (s *Server) RegisterHealthCheck() *Server

RegisterHealthCheck enable the Health Check for the gRPC server. Health is checked with gRPC using the same gRRPC server using the proto from 'google.golang.org/grpc/health/grpc_health_v1'

func (*Server) ResumeService

func (s *Server) ResumeService(name string) *Server

ResumeService sets all serving status to SERVING, and configures the server to accept all future status changes.

func (*Server) ResumeServices

func (s *Server) ResumeServices() *Server

ResumeServices sets all serving status to SERVING, and configures the server to accept all future status changes.

func (*Server) SetCORS

func (s *Server) SetCORS(enabled bool) *Server

SetCORS enable CORS if and only if the first parmeter (enabled) is `true`

func (*Server) SetServiceStatus

func (s *Server) SetServiceStatus(name string, status grpc_health_v1.HealthCheckResponse_ServingStatus) *Server

SetServiceStatus change the status of the given service

func (*Server) SetTLS

func (s *Server) SetTLS(enabled bool, certDir, pubKeyFile, privKeyFile, caFile string) *Server

SetTLS enable TLS with the given certificates if and only if the first parmeter (enabled) is `true`

func (*Server) ShutdownService

func (s *Server) ShutdownService(name string) *Server

ShutdownService sets all serving status to NOT_SERVING, and configures the server to ignore all future status changes.

func (*Server) ShutdownServices

func (s *Server) ShutdownServices() *Server

ShutdownServices sets all serving status to NOT_SERVING, and configures the server to ignore all future status changes.

func (*Server) Start

func (s *Server) Start(port string, ports ...string) *Server

Start starts a server to expose the provisioner API using HTTP/REST and gRPC

func (*Server) StartGRPC

func (s *Server) StartGRPC(port string) *Server

StartGRPC starts the gRPC server

func (*Server) StartHTTP

func (s *Server) StartHTTP(port string) *Server

StartHTTP starts the HTTP/REST server

func (*Server) StartHealthz

func (s *Server) StartHealthz() *Server

StartHealthz starts the Health Check Server on HTTP/REST on the given port

func (*Server) StartMux

func (s *Server) StartMux(port string) *Server

StartMux starts a Multiplex server to redirect the traffic to the HTTP/REST and GRPC services, allowing to have both services in one port

func (*Server) Stop

func (s *Server) Stop() *Server

Stop stops both servers GRPC and HTTP/REST

func (*Server) StopGRPC

func (s *Server) StopGRPC() *Server

StopGRPC stops the GRPC server if it's running

func (*Server) StopHTTP

func (s *Server) StopHTTP() *Server

StopHTTP stops the HTTP/REST server if it's running

func (*Server) StopMux

func (s *Server) StopMux() *Server

StopMux stops the Multiplex server if it's running

func (*Server) Wait

func (s *Server) Wait() *Server

Wait waits for any server to stop either by the user or an error

func (*Server) WithHealthCheck

func (s *Server) WithHealthCheck(port string) *Server

WithHealthCheck enable the server to have health check

func (*Server) WithSwagger

func (s *Server) WithSwagger() *Server

WithSwagger enable the HTTP server to present the swagger JSON

func (*Server) WithTLS

func (s *Server) WithTLS(certDir, pubKeyFile, privKeyFile, caFile string) *Server

WithTLS enable and configure TLS to the server

type Service

type Service struct {
	Version                     string
	Name                        string
	ServiceRegister             ServiceRegisterable
	RegisterHandlerFromEndpoint RegisterHandlerFromEndpoint
	SwaggerBytes                []byte
}

Service contain the information a server needs from a service to operate

type ServiceRegisterable

type ServiceRegisterable interface {
	Register(*grpc.Server)
}

ServiceRegisterable is an interface for a service to implement a function to register itself to a gRPC server

type Services

type Services map[string]*Service

Services is a collection of services

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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