discovery

package
v2.1.0+incompatible Latest Latest
Warning

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

Go to latest
Published: May 15, 2019 License: MIT Imports: 14 Imported by: 8

Documentation

Index

Constants

View Source
const (
	CacheDrainInterval = 10 * time.Minute // Drain the cache every 10 mins
)
View Source
const (
	DefaultSleepInterval = 1 * time.Second
)

Variables

This section is empty.

Functions

func RandomHex

func RandomHex(count int) ([]byte, error)

Return a defined number of random bytes as a slice

Types

type ChangeListener added in v1.3.0

type ChangeListener struct {
	Name string // Name to be represented in the Listeners list
	Url  string // Url of the service to send events to
}

A ChangeListener is a service that will receive service change events over the HTTP interface.

type ContainerCache

type ContainerCache struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

A ContainerCache keeps a history of the containers we've inspected in order to do fast lookups of container info when needed.

func NewContainerCache

func NewContainerCache() *ContainerCache

func (*ContainerCache) Drain

func (c *ContainerCache) Drain(newSize int)

On a timed basis, drain the containerCache

func (*ContainerCache) Get

func (c *ContainerCache) Get(svcID string) *docker.Container

Get locks the cache, try to get a service if we have it

func (*ContainerCache) Len

func (c *ContainerCache) Len() int

func (*ContainerCache) Prune

func (c *ContainerCache) Prune(liveContainers map[string]interface{})

Loop through the current cache and remove anything that has disappeared

func (*ContainerCache) Set

func (c *ContainerCache) Set(svc *service.Service, container *docker.Container)

type Discoverer

type Discoverer interface {
	// Returns a slice of services that we discovered
	Services() []service.Service
	// Get the health check and health check args for a service
	HealthCheck(svc *service.Service) (string, string)
	// Services which run on the same host and want to receive
	// Sidecar service change events
	Listeners() []ChangeListener
	// A non-blocking method that runs a discovery loop.
	// The controlling process kicks it off to start discovery.
	Run(director.Looper)
}

A Discoverer is responsible for finding services that we care about. It must have a method to return the list of services, and a Run() method that will be invoked when the discovery mechanism(s) is/are started. It will also return the correct health check for a service and can allow services to subscribe to Sidecar events.

type DockerClient

type DockerClient interface {
	InspectContainer(id string) (*docker.Container, error)
	ListContainers(opts docker.ListContainersOptions) ([]docker.APIContainers, error)
	AddEventListener(listener chan<- *docker.APIEvents) error
	RemoveEventListener(listener chan *docker.APIEvents) error
	Ping() error
}

type DockerDiscovery

type DockerDiscovery struct {
	ClientProvider func() (DockerClient, error) // Return the client we'll use to connect

	sync.RWMutex // Reader/Writer lock
	// contains filtered or unexported fields
}

func NewDockerDiscovery

func NewDockerDiscovery(endpoint string, svcNamer ServiceNamer, ip string) *DockerDiscovery

func (*DockerDiscovery) HealthCheck

func (d *DockerDiscovery) HealthCheck(svc *service.Service) (string, string)

HealthCheck looks up a health check using Docker container labels to pass the type of check and the arguments to pass to it.

func (*DockerDiscovery) Listeners added in v1.3.0

func (d *DockerDiscovery) Listeners() []ChangeListener

Listeners returns any containers we found that had the SidecarListener label set to a valid ServicePort.

func (*DockerDiscovery) Run

func (d *DockerDiscovery) Run(looper director.Looper)

The main loop, poll for containers continuously.

func (*DockerDiscovery) Services

func (d *DockerDiscovery) Services() []service.Service

Services returns the slice of services we found running

type DockerLabelNamer

type DockerLabelNamer struct {
	Label string
}

A ServiceNamer that uses a name provided in a Docker label as the name for the service.

func (*DockerLabelNamer) ServiceName

func (d *DockerLabelNamer) ServiceName(container *docker.APIContainers) string

Return the value of the configured Docker label, or default to the image name.

type MultiDiscovery

type MultiDiscovery struct {
	Discoverers []Discoverer
}

A MultiDiscovery is a wrapper around zero or more Discoverers. It allows the use of potentially multiple Discoverers in place of one.

func (*MultiDiscovery) HealthCheck

func (d *MultiDiscovery) HealthCheck(svc *service.Service) (string, string)

Get the health check and health check args for a service

func (*MultiDiscovery) Listeners added in v1.3.0

func (d *MultiDiscovery) Listeners() []ChangeListener

Aggreates all the Listeners() output from the discoverers

func (*MultiDiscovery) Run

func (d *MultiDiscovery) Run(looper director.Looper)

Kicks off the Run() method for all the discoverers.

func (*MultiDiscovery) Services

func (d *MultiDiscovery) Services() []service.Service

Aggregates all the service slices from the discoverers

type RegexpNamer

type RegexpNamer struct {
	ServiceNameMatch string
	// contains filtered or unexported fields
}

A ServiceNamer that uses a regex to match against the service name or else uses the image as the service name.

func NewRegexpNamer added in v1.3.0

func NewRegexpNamer(exprStr string) (*RegexpNamer, error)

func (*RegexpNamer) ServiceName

func (r *RegexpNamer) ServiceName(container *docker.APIContainers) string

Return a properly regex-matched name for the service, or failing that, the Image ID which we use to stand in for the name of the service.

type ServiceNamer

type ServiceNamer interface {
	ServiceName(*docker.APIContainers) string
}

type StaticCheck

type StaticCheck struct {
	Type string
	Args string
}

type StaticDiscovery

type StaticDiscovery struct {
	Targets    []*Target
	ConfigFile string
	Hostname   string
	DefaultIP  string
}

A StaticDiscovery is an instance of a configuration file based discovery mechanism. It is read on startup and never again, currently, so there is no need for any locking or synchronization mechanism.

func NewStaticDiscovery

func NewStaticDiscovery(filename string, defaultIP string) *StaticDiscovery

func (*StaticDiscovery) HealthCheck

func (d *StaticDiscovery) HealthCheck(svc *service.Service) (string, string)

func (*StaticDiscovery) Listeners added in v1.3.0

func (d *StaticDiscovery) Listeners() []ChangeListener

Listeners returns the list of services configured to be ChangeEvent listeners

func (*StaticDiscovery) ParseConfig

func (d *StaticDiscovery) ParseConfig(filename string) ([]*Target, error)

Parses a JSON config file containing an array of Targets. These are then augmented with a random hex ID and stamped with the current UTC time as the creation time. The same hex ID is applied to the Check and the Service to make sure that they are matched by the healthy package later on.

func (*StaticDiscovery) Run

func (d *StaticDiscovery) Run(looper director.Looper)

Causes the configuration to be parsed and loaded. There is no background processing needed on an ongoing basis.

func (*StaticDiscovery) Services

func (d *StaticDiscovery) Services() []service.Service

Returns the list of services derived from the targets that were parsed out of the config file.

type Target

type Target struct {
	Service    service.Service
	Check      StaticCheck
	ListenPort int64
}

Jump to

Keyboard shortcuts

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