service

package
v7.0.1+incompatible Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2019 License: Apache-2.0 Imports: 22 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// HEARTBEAT_DURATION time in second between two registration. The host will
	// be deleted if etcd didn't received any new registration in those 5 seocnds
	HEARTBEAT_DURATION = 5
)

Variables

This section is empty.

Functions

func Client

func Client() etcd.Client

Client will generate a valid etcd client from the following environment variables:

  • ETCD_HOSTS: a list of etcd hosts comma separated
  • ETCD_HOST: a single etcd host
  • ETCD_CACERT: The ca certificate
  • ETCD_TLS_CERT: The client tls cert
  • ETCD_TLS_KEY: The client tls key
  • ETCD_TLS_INMEMORY: Is the tls configuration filename or raw certificates

func KAPI

func KAPI() etcd.KeysAPI

KAPI provide a etcd KeysAPI for a client provided by the Client() method

func Subscribe

func Subscribe(service string) etcd.Watcher

Subscribe to every event that happen to a service

func SubscribeDown

func SubscribeDown(service string) (<-chan string, <-chan *etcd.Error)

SubscribeDown return a channel that will notice you everytime a host loose his etcd registration

func SubscribeNew

func SubscribeNew(service string) (<-chan *Host, <-chan *etcd.Error)

SubscribeNew return a channel that will notice you everytime a new host is registred.

Types

type Credentials

type Credentials struct {
	User     string
	Password string
}

Credentials store service credentials

type GetHostResponse

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

GetHostResponse is the HostResponse Implementation of the HostResponse interface used by the the Service methods. This only provide the error wrapping logic, all the actaul logic for these method are done by the Host struct.

func (*GetHostResponse) Err

func (q *GetHostResponse) Err() error

Err will return an error if an error happened on any previous steps.

func (*GetHostResponse) Host

func (q *GetHostResponse) Host() (*Host, error)

Host will return the host represented by this error.

func (*GetHostResponse) PrivateURL

func (q *GetHostResponse) PrivateURL(scheme, path string) (string, error)

PrivateURL will return the private URL of this host

func (*GetHostResponse) URL

func (q *GetHostResponse) URL(scheme, path string) (string, error)

URL will return the public URL of this host

type GetServiceResponse

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

GetServiceResponse is the implementation of the ServiceResponse interface used by the Get method This only provide the error wrapping logic, all the actual logic for thsese mêthod are done by the Service struct.

func (*GetServiceResponse) All

func (q *GetServiceResponse) All() (Hosts, error)

All will return a slice of all the hosts registred to the service

func (*GetServiceResponse) Err

func (q *GetServiceResponse) Err() error

Err wil return an error if an error happend when you've called tre Get method, or nil if no error were detected.

func (*GetServiceResponse) First

func (q *GetServiceResponse) First() HostResponse

First will return the first host registred to the service If the ServiceResponse is errored, the errors will be passed to the HostResponse

func (*GetServiceResponse) One

One will return a host choosen randomly in all the hosts of the service If the ServiceResponse is errored, the errors will be passed to the HostResponse

func (*GetServiceResponse) Service

func (q *GetServiceResponse) Service() (*Service, error)

Service will return the service returned by the Get method. If the service was not found, no error will be return but the service will only contains a Name field.

func (*GetServiceResponse) URL

func (q *GetServiceResponse) URL(scheme, path string) (string, error)

URL build url for the specified service. If the service is not public, a random host will be choosen and an url will be generated.

type Host

type Host struct {
	// Hostname  must be a publicly available IP/FQDN if the service is public or a private IP/FQDN id the service is private.
	Hostname string `json:"name"`
	// Name of the service which this host store. This will be overwritten by the Register Function
	Name string `json:"service_name"`
	// Ports is the ports accessible on a public network if the service is Public or the ports accessible on a private network if the service is private
	Ports Ports `json:"ports"`
	// User name used to authenticate to this service. The Register function can override this at any time if the service is public
	User string `json:"user,omitempty"`
	// Password used to authenticate to this service. The Register function can override this at any time if the service is public
	Password string `json:"password,omitempty"`
	// Public is set to true if the service is public
	Public bool `json:"public,omitempty"`
	// PrivateHostname is the private IP/FQDN used to communicate with this service on the private network
	// This will defaults to Hostname if empty
	PrivateHostname string `json:"private_hostname,omitempty"`
	// PrivatePorts is the private ports used to communicate with this service on the private network.
	// This will defaults to Ports if empty
	PrivatePorts Ports `json:"private_ports,omitempty"`
	// Critical will be set to true is the service is critical
	Critical bool `json:"critical,omitempty"`
	// UUID is the service UUID, this must have the following pattern: uuid-PrivateHostname
	UUID string `json:"uuid,omitempty"`
}

Host store all the host informations. This is also used to store the host in the etcd services directory.

func (*Host) PrivateURL

func (h *Host) PrivateURL(scheme, path string) (string, error)

PrivateURL will provide a valid url to contact this service on the Private network this method will fallback to the URL method if the host does not provide any PrivateURL

func (*Host) URL

func (h *Host) URL(scheme, path string) (string, error)

URL will return a valid url to contact this service on the specific protocol provided by the scheme parameter

type HostResponse

type HostResponse interface {
	Err() error
	Host() (*Host, error)
	URL(scheme, path string) (string, error)
	PrivateURL(scheme, path string) (string, error)
}

HostResponse is the interface used to provide a single host response. This interface provide a standard API used fot method chaining like:

Get("my-service").First().Url()

To provide such API errores need to be stored and sent at the last moment. To do so each "final" methods (like URL or Host) will check if the Response is errored before continuing to their own logic.

type Hosts

type Hosts []*Host

Hosts will represent a slice of hosts

func (Hosts) String

func (hs Hosts) String() string

type Ports

type Ports map[string]string

Ports is a representation of the ports exposed by a host or a service. The key is the protocol name and the value is the port used for this protocol. Typical usage is:

	Ports{
		"http":"80",
		"https": "443",
 }

type Registration

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

Registration is the RegistrationWrapper implementation used by the Register method

func NewRegistration

func NewRegistration(ctx context.Context, uuid string, cred chan Credentials) *Registration

NewRegistration initialize the Registration struct

func Register

func Register(ctx context.Context, service string, host Host) *Registration

Register a host with a service name and a host description. The last chan is a stop method. If something is written on this channel, any goroutines launch by this method will stop.

This service will launch two go routines. The first one will maintain the registration every 5 seconds and the second one will check if the service credentials don't change and notify otherwise

func (*Registration) Credentials

func (w *Registration) Credentials() (Credentials, error)

Credentials return the service credentials or an error if the service is not registred yet

func (*Registration) Ready

func (w *Registration) Ready() bool

Ready is a non blocking method that return true if the service is registred to the etcd service false otherwise

func (*Registration) UUID

func (w *Registration) UUID() string

UUID of the current host

func (*Registration) WaitRegistration

func (w *Registration) WaitRegistration()

WaitRegistration wait for the first registration to happen, meaning that the service is succesfulled registred to the etcd service

type RegistrationWrapper

type RegistrationWrapper interface {
	Ready() bool                       // Ready return strue if the service registred yet? This method should no be blocking
	WaitRegistration()                 // WaitRegistration wait for the first registration
	Credentials() (Credentials, error) // Credentials return the current credentials or an error if the service is not registred yet
	UUID() string                      // UUID return the host UUID
}

RegistrationWrapper wreap the uuid and the credential channel to provide a more user friendly API for the Register Method

type Service

type Service struct {
	Name     string `json:"name"`               // Name of the service
	Critical bool   `json:"critical"`           // Is the service critical to the infrastructure health?
	Hostname string `json:"hostname,omitempty"` // The service private hostname
	User     string `json:"user,omitempty"`     // The service username
	Password string `json:"password,omitempty"` // The service password
	Ports    Ports  `json:"ports,omitempty"`    // The service private ports
	Public   bool   `json:"public,omitempty"`   // Is the service public?
}

Service store all the informatiosn about a service. This is also used to marshal services present in the /services_infos/ directory.

func (*Service) All

func (s *Service) All() (Hosts, error)

All return all hosts associated to a service

func (*Service) First

func (s *Service) First() (*Host, error)

First return the first host of this service

func (*Service) One

func (s *Service) One() (*Host, error)

One return a random host from all the available hosts of this service.

func (*Service) URL

func (s *Service) URL(scheme, path string) (string, error)

URL return the public url of this service. If this service do not have an public url, this will return an url to a random host.

type ServiceResponse

type ServiceResponse interface {
	// Err is the method used to check if the Response is errored.
	Err() error
	// Service return the Service struct representing the requested service
	Service() (*Service, error)
	// One return a host of the service choosen randomly
	One() HostResponse
	// First return the first host of the serice
	First() HostResponse
	// All return all the hosts registred for this service
	All() (Hosts, error)
	// URL returns a valid url for this service
	URL(scheme, path string) (string, error)
}

ServiceResponse is the interface used to provide a response to the service.Get() Method. This interface provide a standard API used for method chaining like:

url, err := Get("my-service").First().URL()

To provide such API, go errors need to be stored and sent at the last moment. To do so, each "final" method (like Url or All), will check if the Response is errored, before continuing to their own logic.

func Get

func Get(service string) ServiceResponse

Get a service by its name. This method does not directly return the Service, but a ServiceResponse. This permit method chaining like:

url, err := Get("my-service").First().URL()

If there was an error during the acquisition of the service, this error will be stored in the ServiceResponse. Final methods will check for this error before doing actual logic. If the service is not found, we won't render an error, but will return a service with minimal informations. This is done to provide maximal backwerd compatibility since older versions does not register themself to the "/services_infos" directory.

Directories

Path Synopsis
Package servicemock is a generated GoMock package.
Package servicemock is a generated GoMock package.

Jump to

Keyboard shortcuts

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