Documentation ¶
Overview ¶
Package consul is used by Nomad to register all services both static services and dynamic via allocations.
Consul Service IDs have the following format: ${nomadServicePrefix}-${groupName}-${serviceKey} groupName takes on one of the following values: - server - client - executor-${alloc-id}-${task-name}
serviceKey should be generated by service registrators. If the serviceKey is being generated by the executor for a Nomad Task.Services the following helper should be used:
NOTE: Executor should interpolate the service prior to calling func GenerateTaskServiceKey(service *structs.Service) string
The Nomad Client reaps services registered from dead allocations that were not properly cleaned up by the executor (this is not the expected case).
TODO fix this comment The Consul ServiceIDs generated by the executor will contain the allocation ID. Thus the client can generate the list of Consul ServiceIDs to keep by calling the following method on all running allocations the client is aware of: func GenerateExecutorServiceKeyPrefixFromAlloc(allocID string) string
Index ¶
- Constants
- type Check
- type CheckRunner
- type ServiceDomain
- type ServiceKey
- type Syncer
- func (c *Syncer) AddPeriodicHandler(name string, fn types.PeriodicCallback) bool
- func (c *Syncer) ConsulClient() *consul.Client
- func (c *Syncer) NumHandlers() int
- func (c *Syncer) ReapUnmatched(domains []ServiceDomain) error
- func (c *Syncer) RemovePeriodicHandler(name string)
- func (c *Syncer) Run()
- func (c *Syncer) RunHandlers() error
- func (c *Syncer) SetAddrFinder(addrFinder func(string) (string, int)) *Syncer
- func (c *Syncer) SetDelegatedChecks(delegateChecks map[string]struct{}, ...) *Syncer
- func (c *Syncer) SetServices(domain ServiceDomain, services map[ServiceKey]*structs.Service) error
- func (c *Syncer) Shutdown() error
- func (c *Syncer) SyncNow()
- func (c *Syncer) SyncServices() error
Constants ¶
const ( // DefaultQueryWaitDuration is the max duration the Consul Agent will // spend waiting for a response from a Consul Query. DefaultQueryWaitDuration = 2 * time.Second // ServiceTagHTTP is the tag assigned to HTTP services ServiceTagHTTP = "http" // ServiceTagRPC is the tag assigned to RPC services ServiceTagRPC = "rpc" // ServiceTagSerf is the tag assigned to Serf services ServiceTagSerf = "serf" )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Check ¶
type Check interface { Run() *cstructs.CheckResult ID() string Interval() time.Duration Timeout() time.Duration }
Check is an interface which check providers can implement for Nomad to run
type CheckRunner ¶
type CheckRunner struct {
// contains filtered or unexported fields
}
CheckRunner runs a given check in a specific interval and update a corresponding Consul TTL check
func NewCheckRunner ¶
func NewCheckRunner(check Check, runCheck func(Check), logger *log.Logger) *CheckRunner
NewCheckRunner configures and returns a CheckRunner
func (*CheckRunner) Start ¶
func (r *CheckRunner) Start()
Start is used to start the check. The check runs until stop is called
type ServiceDomain ¶
type ServiceDomain string
ServiceDomain is the domain of services registered by Nomad
const ( ClientDomain ServiceDomain = "client" ServerDomain ServiceDomain = "server" )
func NewExecutorDomain ¶
func NewExecutorDomain(allocID, task string) ServiceDomain
NewExecutorDomain returns a domain specific to the alloc ID and task
type ServiceKey ¶
type ServiceKey string
ServiceKey is the generated service key that is used to build the Consul ServiceID
func GenerateServiceKey ¶
func GenerateServiceKey(service *structs.Service) ServiceKey
GenerateServiceKey should be called to generate a serviceKey based on the Service.
type Syncer ¶
type Syncer struct {
// contains filtered or unexported fields
}
Syncer allows syncing of services and checks with Consul
func NewSyncer ¶
func NewSyncer(consulConfig *config.ConsulConfig, shutdownCh chan struct{}, logger *log.Logger) (*Syncer, error)
NewSyncer returns a new consul.Syncer
func (*Syncer) AddPeriodicHandler ¶
func (c *Syncer) AddPeriodicHandler(name string, fn types.PeriodicCallback) bool
AddPeriodicHandler adds a uniquely named callback. Returns true if successful, false if a handler with the same name already exists.
func (*Syncer) ConsulClient ¶
ConsulClient returns the Consul client used by the Syncer.
func (*Syncer) NumHandlers ¶
NumHandlers returns the number of callbacks registered with the syncer
func (*Syncer) ReapUnmatched ¶
func (c *Syncer) ReapUnmatched(domains []ServiceDomain) error
ReapUnmatched prunes all services that do not exist in the passed domains
func (*Syncer) RemovePeriodicHandler ¶
RemovePeriodicHandler removes a handler with a given name.
func (*Syncer) Run ¶
func (c *Syncer) Run()
Run triggers periodic syncing of services and checks with Consul. This is a long lived go-routine which is stopped during shutdown.
func (*Syncer) RunHandlers ¶
RunHandlers executes each handler (randomly)
func (*Syncer) SetAddrFinder ¶
SetAddrFinder sets a function to find the host and port for a Service given its port label
func (*Syncer) SetDelegatedChecks ¶
func (c *Syncer) SetDelegatedChecks(delegateChecks map[string]struct{}, createDelegatedCheckFn func(*structs.ServiceCheck, string) (Check, error)) *Syncer
SetDelegatedChecks sets the checks that nomad is going to run and report the result back to consul
func (*Syncer) SetServices ¶
func (c *Syncer) SetServices(domain ServiceDomain, services map[ServiceKey]*structs.Service) error
SetServices stores the map of Nomad Services to the provided service domain name.
func (*Syncer) Shutdown ¶
Shutdown de-registers the services and checks and shuts down periodic syncing
func (*Syncer) SyncNow ¶
func (c *Syncer) SyncNow()
SyncNow expires the current timer forcing the list of periodic callbacks to be synced immediately.
func (*Syncer) SyncServices ¶
SyncServices sync the services with the Consul Agent