2016-03-23 07:50:41 +00:00
|
|
|
package consul
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
2016-03-23 17:43:35 +00:00
|
|
|
"net/url"
|
|
|
|
"reflect"
|
2016-03-23 07:50:41 +00:00
|
|
|
"strings"
|
2016-03-24 01:03:02 +00:00
|
|
|
"sync"
|
2016-03-23 07:50:41 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
consul "github.com/hashicorp/consul/api"
|
2016-05-24 06:23:57 +00:00
|
|
|
"github.com/hashicorp/consul/lib"
|
2016-03-23 17:43:35 +00:00
|
|
|
"github.com/hashicorp/go-multierror"
|
2016-03-23 07:50:41 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
2016-05-24 06:23:57 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/structs/config"
|
|
|
|
"github.com/hashicorp/nomad/nomad/types"
|
2016-03-23 07:50:41 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2016-05-24 06:23:57 +00:00
|
|
|
// initialSyncBuffer is the max time an initial sync will sleep
|
|
|
|
// before syncing.
|
|
|
|
initialSyncBuffer = 30 * time.Second
|
|
|
|
|
|
|
|
// initialSyncDelay is the delay before an initial sync.
|
|
|
|
initialSyncDelay = 5 * time.Second
|
|
|
|
|
2016-03-23 21:34:43 +00:00
|
|
|
// The periodic time interval for syncing services and checks with Consul
|
2016-03-23 07:50:41 +00:00
|
|
|
syncInterval = 5 * time.Second
|
2016-03-25 02:30:02 +00:00
|
|
|
|
2016-05-24 06:23:57 +00:00
|
|
|
// syncJitter provides a little variance in the frequency at which
|
|
|
|
// Syncer polls Consul.
|
|
|
|
syncJitter = 8
|
|
|
|
|
2016-03-25 02:30:02 +00:00
|
|
|
// ttlCheckBuffer is the time interval that Nomad can take to report Consul
|
|
|
|
// the check result
|
|
|
|
ttlCheckBuffer = 31 * time.Second
|
2016-05-27 08:35:10 +00:00
|
|
|
|
|
|
|
// ServiceTagHttp is the tag assigned to HTTP services
|
|
|
|
ServiceTagHttp = "http"
|
|
|
|
|
|
|
|
// ServiceTagRpc is the tag assigned to RPC services
|
|
|
|
ServiceTagRpc = "rpc"
|
2016-05-27 22:58:28 +00:00
|
|
|
|
|
|
|
// ServiceTagSerf is the tag assigned to Serf services
|
|
|
|
ServiceTagSerf = "serf"
|
2016-03-23 07:50:41 +00:00
|
|
|
)
|
|
|
|
|
2016-06-02 07:56:55 +00:00
|
|
|
// Syncer allows syncing of services and checks with Consul
|
|
|
|
type Syncer struct {
|
|
|
|
client *consul.Client
|
|
|
|
runChecks bool
|
|
|
|
|
2016-06-10 01:21:22 +00:00
|
|
|
// servicesGroups is a named group of services that will be flattened
|
|
|
|
// and reconciled with Consul when SyncServices() is called. The key
|
|
|
|
// to the servicesGroups map is unique per handler and is used to
|
|
|
|
// allow the Agent's services to be maintained independently of the
|
|
|
|
// Client or Server's services.
|
|
|
|
servicesGroups map[string][]*consul.AgentServiceRegistration
|
|
|
|
servicesGroupsLock sync.RWMutex
|
|
|
|
|
2016-06-07 16:37:39 +00:00
|
|
|
// The "Consul Registry" is a collection of Consul Services and
|
|
|
|
// Checks all guarded by the registryLock.
|
|
|
|
registryLock sync.RWMutex
|
2016-06-02 07:56:55 +00:00
|
|
|
|
|
|
|
checkRunners map[string]*CheckRunner
|
2016-06-07 14:01:13 +00:00
|
|
|
delegateChecks map[string]struct{} // delegateChecks are the checks that the Nomad client runs and reports to Consul
|
2016-06-07 16:37:39 +00:00
|
|
|
|
|
|
|
// serviceRegPrefix is used to namespace the domain of registered
|
|
|
|
// Consul Services and Checks belonging to a single Syncer. A given
|
|
|
|
// Nomad Agent may spawn multiple Syncer tasks between the Agent
|
|
|
|
// Agent and its Executors, all syncing to a single Consul Agent.
|
|
|
|
// The serviceRegPrefix allows multiple Syncers to coexist without
|
|
|
|
// each Syncer clobbering each others Services. The Syncer namespace
|
|
|
|
// protocol is fmt.Sprintf("nomad-%s-%s", serviceRegPrefix, miscID).
|
|
|
|
// serviceRegPrefix is guarded by the registryLock.
|
|
|
|
serviceRegPrefix string
|
|
|
|
|
|
|
|
addrFinder func(portLabel string) (string, int)
|
2016-06-07 14:01:13 +00:00
|
|
|
createDelegatedCheck func(*structs.ServiceCheck, string) (Check, error)
|
2016-06-07 16:37:39 +00:00
|
|
|
// End registryLock guarded attributes.
|
2016-06-02 07:56:55 +00:00
|
|
|
|
|
|
|
logger *log.Logger
|
|
|
|
|
|
|
|
shutdownCh chan struct{}
|
|
|
|
shutdown bool
|
|
|
|
shutdownLock sync.Mutex
|
|
|
|
|
2016-06-02 16:15:30 +00:00
|
|
|
// notifyShutdownCh is used to notify a Syncer it needs to shutdown.
|
|
|
|
// This can happen because there was an explicit call to the Syncer's
|
|
|
|
// Shutdown() method, or because the calling task signaled the
|
|
|
|
// program is going to exit by closing its shutdownCh.
|
|
|
|
notifyShutdownCh chan struct{}
|
|
|
|
|
2016-06-02 07:56:55 +00:00
|
|
|
// periodicCallbacks is walked sequentially when the timer in Run
|
|
|
|
// fires.
|
|
|
|
periodicCallbacks map[string]types.PeriodicCallback
|
|
|
|
notifySyncCh chan struct{}
|
|
|
|
periodicLock sync.RWMutex
|
|
|
|
}
|
|
|
|
|
2016-05-23 14:29:08 +00:00
|
|
|
// NewSyncer returns a new consul.Syncer
|
2016-06-02 16:15:30 +00:00
|
|
|
func NewSyncer(config *config.ConsulConfig, shutdownCh chan struct{}, logger *log.Logger) (*Syncer, error) {
|
2016-03-23 07:50:41 +00:00
|
|
|
var err error
|
|
|
|
var c *consul.Client
|
|
|
|
cfg := consul.DefaultConfig()
|
|
|
|
if config.Addr != "" {
|
|
|
|
cfg.Address = config.Addr
|
|
|
|
}
|
|
|
|
if config.Token != "" {
|
|
|
|
cfg.Token = config.Token
|
|
|
|
}
|
|
|
|
if config.Auth != "" {
|
|
|
|
var username, password string
|
|
|
|
if strings.Contains(config.Auth, ":") {
|
|
|
|
split := strings.SplitN(config.Auth, ":", 2)
|
|
|
|
username = split[0]
|
|
|
|
password = split[1]
|
|
|
|
} else {
|
|
|
|
username = config.Auth
|
|
|
|
}
|
|
|
|
|
|
|
|
cfg.HttpAuth = &consul.HttpBasicAuth{
|
|
|
|
Username: username,
|
|
|
|
Password: password,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if config.EnableSSL {
|
|
|
|
cfg.Scheme = "https"
|
2016-03-28 06:09:31 +00:00
|
|
|
tlsCfg := consul.TLSConfig{
|
|
|
|
Address: cfg.Address,
|
|
|
|
CAFile: config.CAFile,
|
|
|
|
CertFile: config.CertFile,
|
|
|
|
KeyFile: config.KeyFile,
|
|
|
|
InsecureSkipVerify: !config.VerifySSL,
|
|
|
|
}
|
|
|
|
tlsClientCfg, err := consul.SetupTLSConfig(&tlsCfg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error creating tls client config for consul: %v", err)
|
|
|
|
}
|
|
|
|
cfg.HttpClient.Transport = &http.Transport{
|
|
|
|
TLSClientConfig: tlsClientCfg,
|
|
|
|
}
|
2016-03-23 07:50:41 +00:00
|
|
|
}
|
|
|
|
if config.EnableSSL && !config.VerifySSL {
|
|
|
|
cfg.HttpClient.Transport = &http.Transport{
|
|
|
|
TLSClientConfig: &tls.Config{
|
|
|
|
InsecureSkipVerify: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if c, err = consul.NewClient(cfg); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-05-23 18:09:31 +00:00
|
|
|
consulSyncer := Syncer{
|
2016-05-24 06:23:57 +00:00
|
|
|
client: c,
|
|
|
|
logger: logger,
|
2016-06-02 16:15:30 +00:00
|
|
|
shutdownCh: shutdownCh,
|
2016-05-24 06:23:57 +00:00
|
|
|
trackedServices: make(map[string]*consul.AgentService),
|
2016-06-10 01:21:22 +00:00
|
|
|
servicesGroups: make(map[string][]*consul.AgentServiceRegistration),
|
2016-05-24 06:23:57 +00:00
|
|
|
trackedChecks: make(map[string]*consul.AgentCheckRegistration),
|
|
|
|
checkRunners: make(map[string]*CheckRunner),
|
|
|
|
periodicCallbacks: make(map[string]types.PeriodicCallback),
|
2016-03-23 07:50:41 +00:00
|
|
|
}
|
2016-05-23 18:09:31 +00:00
|
|
|
return &consulSyncer, nil
|
2016-03-23 07:50:41 +00:00
|
|
|
}
|
|
|
|
|
2016-03-25 02:30:02 +00:00
|
|
|
// SetDelegatedChecks sets the checks that nomad is going to run and report the
|
|
|
|
// result back to consul
|
2016-06-07 14:01:13 +00:00
|
|
|
func (c *Syncer) SetDelegatedChecks(delegateChecks map[string]struct{}, createDelegatedCheckFn func(*structs.ServiceCheck, string) (Check, error)) *Syncer {
|
2016-03-24 17:06:40 +00:00
|
|
|
c.delegateChecks = delegateChecks
|
2016-06-07 14:01:13 +00:00
|
|
|
c.createDelegatedCheck = createDelegatedCheckFn
|
2016-03-24 17:06:40 +00:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2016-05-14 07:36:26 +00:00
|
|
|
// SetAddrFinder sets a function to find the host and port for a Service given its port label
|
2016-05-22 15:24:54 +00:00
|
|
|
func (c *Syncer) SetAddrFinder(addrFinder func(string) (string, int)) *Syncer {
|
2016-04-12 05:55:19 +00:00
|
|
|
c.addrFinder = addrFinder
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2016-06-07 16:37:39 +00:00
|
|
|
// SetServiceRegPrefix sets the registration prefix used by the Syncer when
|
|
|
|
// registering Services with Consul.
|
|
|
|
func (c *Syncer) SetServiceRegPrefix(servicePrefix string) *Syncer {
|
|
|
|
c.registryLock.Lock()
|
|
|
|
defer c.registryLock.Unlock()
|
|
|
|
c.serviceRegPrefix = servicePrefix
|
2016-04-12 05:55:19 +00:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2016-05-24 06:23:57 +00:00
|
|
|
// SyncNow expires the current timer forcing the list of periodic callbacks
|
|
|
|
// to be synced immediately.
|
|
|
|
func (c *Syncer) SyncNow() {
|
|
|
|
select {
|
2016-06-01 21:13:51 +00:00
|
|
|
case c.notifySyncCh <- struct{}{}:
|
2016-05-24 06:23:57 +00:00
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-10 01:21:22 +00:00
|
|
|
// SetServices assigns the slice of Nomad Services to the provided services
|
|
|
|
// group name.
|
|
|
|
func (c *Syncer) SetServices(groupName string, services []*structs.ConsulService) error {
|
2016-03-23 17:43:35 +00:00
|
|
|
var mErr multierror.Error
|
2016-06-10 01:21:22 +00:00
|
|
|
registeredServices := make([]*consul.AgentServiceRegistration, 0, len(services))
|
2016-04-12 05:55:19 +00:00
|
|
|
for _, service := range services {
|
2016-06-10 01:21:22 +00:00
|
|
|
if service.ServiceID == "" {
|
|
|
|
service.ServiceID = c.GenerateServiceID(groupName, service)
|
|
|
|
}
|
|
|
|
var serviceReg *consul.AgentServiceRegistration
|
|
|
|
var err error
|
|
|
|
if serviceReg, err = c.createService(service); err != nil {
|
2016-03-23 17:43:35 +00:00
|
|
|
mErr.Errors = append(mErr.Errors, err)
|
|
|
|
continue
|
|
|
|
}
|
2016-06-10 01:21:22 +00:00
|
|
|
registeredServices = append(registeredServices, serviceReg)
|
2016-03-23 17:43:35 +00:00
|
|
|
|
2016-06-10 01:21:22 +00:00
|
|
|
// Register the check(s) for this service
|
2016-03-23 17:43:35 +00:00
|
|
|
for _, chk := range service.Checks {
|
2016-06-10 01:21:22 +00:00
|
|
|
// Create a Consul check registration
|
|
|
|
chkReg, err := c.createDelegatedCheckReg(chk, serviceReg)
|
2016-03-24 22:09:50 +00:00
|
|
|
if err != nil {
|
|
|
|
mErr.Errors = append(mErr.Errors, err)
|
|
|
|
continue
|
|
|
|
}
|
2016-03-25 17:36:31 +00:00
|
|
|
// creating a nomad check if we have to handle this particular check type
|
|
|
|
if _, ok := c.delegateChecks[chk.Type]; ok {
|
2016-06-10 01:21:22 +00:00
|
|
|
if _, ok := c.checkRunners[chkReg.ID]; ok {
|
|
|
|
continue
|
|
|
|
}
|
2016-06-07 14:01:13 +00:00
|
|
|
nc, err := c.createDelegatedCheck(chk, chkReg.ID)
|
2016-03-25 17:36:31 +00:00
|
|
|
if err != nil {
|
|
|
|
mErr.Errors = append(mErr.Errors, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
cr := NewCheckRunner(nc, c.runCheck, c.logger)
|
|
|
|
c.checkRunners[nc.ID()] = cr
|
|
|
|
}
|
2016-03-23 17:43:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-10 01:21:22 +00:00
|
|
|
if len(mErr.Errors) > 0 {
|
|
|
|
return mErr.ErrorOrNil()
|
2016-03-23 17:43:35 +00:00
|
|
|
}
|
|
|
|
|
2016-06-10 01:21:22 +00:00
|
|
|
c.servicesGroupsLock.Lock()
|
|
|
|
c.servicesGroups[groupName] = registeredServices
|
|
|
|
c.servicesGroupsLock.Unlock()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SyncNow expires the current timer forcing the list of periodic callbacks
|
|
|
|
// to be synced immediately.
|
|
|
|
func (c *Syncer) SyncNow() {
|
|
|
|
select {
|
|
|
|
case c.notifySyncCh <- struct{}{}:
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// flattenedServices returns a flattened list of services
|
|
|
|
func (c *Syncer) flattenedServices() []*consul.AgentServiceRegistration {
|
|
|
|
const initialNumServices = 8
|
|
|
|
services := make([]*consul.AgentServiceRegistration, 0, initialNumServices)
|
|
|
|
c.servicesGroupsLock.RLock()
|
|
|
|
for _, servicesGroup := range c.servicesGroups {
|
|
|
|
for _, service := range servicesGroup {
|
|
|
|
services = append(services, service)
|
2016-03-23 17:43:35 +00:00
|
|
|
}
|
|
|
|
}
|
2016-06-10 01:21:22 +00:00
|
|
|
c.servicesGroupsLock.RUnlock()
|
|
|
|
|
|
|
|
return services
|
2016-03-23 07:50:41 +00:00
|
|
|
}
|
|
|
|
|
2016-06-02 16:15:30 +00:00
|
|
|
func (c *Syncer) signalShutdown() {
|
|
|
|
select {
|
|
|
|
case c.notifyShutdownCh <- struct{}{}:
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-23 21:34:43 +00:00
|
|
|
// Shutdown de-registers the services and checks and shuts down periodic syncing
|
2016-05-22 15:24:54 +00:00
|
|
|
func (c *Syncer) Shutdown() error {
|
2016-03-23 17:43:35 +00:00
|
|
|
var mErr multierror.Error
|
2016-03-24 01:03:02 +00:00
|
|
|
|
|
|
|
c.shutdownLock.Lock()
|
|
|
|
if !c.shutdown {
|
|
|
|
c.shutdown = true
|
2016-03-23 21:23:32 +00:00
|
|
|
}
|
2016-03-24 01:03:02 +00:00
|
|
|
c.shutdownLock.Unlock()
|
2016-03-25 02:19:13 +00:00
|
|
|
|
2016-06-02 16:15:30 +00:00
|
|
|
c.signalShutdown()
|
|
|
|
|
2016-03-25 02:19:13 +00:00
|
|
|
// Stop all the checks that nomad is running
|
2016-03-25 04:17:33 +00:00
|
|
|
for _, cr := range c.checkRunners {
|
|
|
|
cr.Stop()
|
2016-03-25 02:19:13 +00:00
|
|
|
}
|
|
|
|
|
2016-03-25 23:11:45 +00:00
|
|
|
// De-register all the services from consul
|
2016-03-24 01:21:27 +00:00
|
|
|
for _, service := range c.trackedServices {
|
2016-03-23 17:43:35 +00:00
|
|
|
if err := c.client.Agent().ServiceDeregister(service.ID); err != nil {
|
|
|
|
mErr.Errors = append(mErr.Errors, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return mErr.ErrorOrNil()
|
2016-03-23 07:50:41 +00:00
|
|
|
}
|
|
|
|
|
2016-03-24 01:21:27 +00:00
|
|
|
// KeepServices removes services from consul which are not present in the list
|
|
|
|
// of tasks passed to it
|
2016-05-22 15:24:54 +00:00
|
|
|
func (c *Syncer) KeepServices(services map[string]struct{}) error {
|
2016-03-23 22:28:55 +00:00
|
|
|
var mErr multierror.Error
|
|
|
|
|
2016-03-24 02:14:03 +00:00
|
|
|
// Get the services from Consul
|
2016-03-23 22:28:55 +00:00
|
|
|
cServices, err := c.client.Agent().Services()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
cServices = c.filterConsulServices(cServices)
|
|
|
|
|
2016-03-24 02:14:03 +00:00
|
|
|
// Remove the services from consul which are not in any of the tasks
|
2016-03-23 22:28:55 +00:00
|
|
|
for _, service := range cServices {
|
|
|
|
if _, validService := services[service.ID]; !validService {
|
|
|
|
if err := c.deregisterService(service.ID); err != nil {
|
|
|
|
mErr.Errors = append(mErr.Errors, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return mErr.ErrorOrNil()
|
|
|
|
}
|
|
|
|
|
2016-03-23 21:34:43 +00:00
|
|
|
// registerCheck registers a check definition with Consul
|
2016-05-22 15:24:54 +00:00
|
|
|
func (c *Syncer) registerCheck(chkReg *consul.AgentCheckRegistration) error {
|
2016-03-25 04:17:33 +00:00
|
|
|
if cr, ok := c.checkRunners[chkReg.ID]; ok {
|
|
|
|
cr.Start()
|
2016-03-25 02:00:24 +00:00
|
|
|
}
|
2016-03-24 21:12:09 +00:00
|
|
|
return c.client.Agent().CheckRegister(chkReg)
|
|
|
|
}
|
|
|
|
|
2016-06-07 14:01:13 +00:00
|
|
|
// createDelegatedCheckReg creates a Check that can be registered with
|
|
|
|
// Nomad. It also creates a Nomad check for the check types that it can
|
|
|
|
// handle.
|
|
|
|
func (c *Syncer) createDelegatedCheckReg(check *structs.ServiceCheck, service *consul.AgentService) (*consul.AgentCheckRegistration, error) {
|
2016-03-23 17:43:35 +00:00
|
|
|
chkReg := consul.AgentCheckRegistration{
|
2016-03-24 00:12:53 +00:00
|
|
|
ID: check.Hash(service.ID),
|
2016-03-23 17:43:35 +00:00
|
|
|
Name: check.Name,
|
|
|
|
ServiceID: service.ID,
|
|
|
|
}
|
|
|
|
chkReg.Timeout = check.Timeout.String()
|
2016-03-23 21:23:32 +00:00
|
|
|
chkReg.Interval = check.Interval.String()
|
2016-03-23 17:43:35 +00:00
|
|
|
switch check.Type {
|
|
|
|
case structs.ServiceCheckHTTP:
|
|
|
|
if check.Protocol == "" {
|
|
|
|
check.Protocol = "http"
|
|
|
|
}
|
|
|
|
url := url.URL{
|
|
|
|
Scheme: check.Protocol,
|
|
|
|
Host: fmt.Sprintf("%s:%d", service.Address, service.Port),
|
|
|
|
Path: check.Path,
|
|
|
|
}
|
|
|
|
chkReg.HTTP = url.String()
|
|
|
|
case structs.ServiceCheckTCP:
|
|
|
|
chkReg.TCP = fmt.Sprintf("%s:%d", service.Address, service.Port)
|
|
|
|
case structs.ServiceCheckScript:
|
2016-03-25 02:30:02 +00:00
|
|
|
chkReg.TTL = (check.Interval + ttlCheckBuffer).String()
|
2016-03-24 22:09:50 +00:00
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("check type %q not valid", check.Type)
|
2016-03-24 20:05:08 +00:00
|
|
|
}
|
2016-03-24 22:09:50 +00:00
|
|
|
return &chkReg, nil
|
2016-03-23 07:50:41 +00:00
|
|
|
}
|
|
|
|
|
2016-03-23 17:43:35 +00:00
|
|
|
// createService creates a Consul AgentService from a Nomad Service
|
2016-06-07 15:54:03 +00:00
|
|
|
func (c *Syncer) createService(service *structs.ConsulService) (*consul.AgentServiceRegistration, error) {
|
2016-06-07 16:37:39 +00:00
|
|
|
c.registryLock.RLock()
|
|
|
|
defer c.registryLock.RUnlock()
|
|
|
|
|
|
|
|
srv := consul.AgentServiceRegistration{
|
|
|
|
ID: service.ID(c.serviceRegPrefix),
|
|
|
|
Name: service.Name,
|
|
|
|
Tags: service.Tags,
|
2016-03-23 07:50:41 +00:00
|
|
|
}
|
2016-04-12 05:55:19 +00:00
|
|
|
host, port := c.addrFinder(service.PortLabel)
|
2016-04-14 07:56:39 +00:00
|
|
|
if host != "" {
|
|
|
|
srv.Address = host
|
|
|
|
}
|
|
|
|
|
|
|
|
if port != 0 {
|
|
|
|
srv.Port = port
|
|
|
|
}
|
|
|
|
|
2016-03-23 07:50:41 +00:00
|
|
|
return &srv, nil
|
|
|
|
}
|
|
|
|
|
2016-03-23 17:43:35 +00:00
|
|
|
// deregisterService de-registers a service with the given ID from consul
|
2016-05-22 15:24:54 +00:00
|
|
|
func (c *Syncer) deregisterService(ID string) error {
|
2016-03-23 17:43:35 +00:00
|
|
|
return c.client.Agent().ServiceDeregister(ID)
|
|
|
|
}
|
|
|
|
|
2016-03-23 21:34:43 +00:00
|
|
|
// deregisterCheck de-registers a check with a given ID from Consul.
|
2016-05-22 15:24:54 +00:00
|
|
|
func (c *Syncer) deregisterCheck(ID string) error {
|
2016-03-25 02:00:24 +00:00
|
|
|
// Deleting the nomad check
|
2016-03-25 04:17:33 +00:00
|
|
|
if cr, ok := c.checkRunners[ID]; ok {
|
|
|
|
cr.Stop()
|
|
|
|
delete(c.checkRunners, ID)
|
2016-03-24 20:05:08 +00:00
|
|
|
}
|
2016-03-25 02:00:24 +00:00
|
|
|
|
2016-03-25 23:11:45 +00:00
|
|
|
// Deleting from consul
|
2016-03-23 17:43:35 +00:00
|
|
|
return c.client.Agent().CheckDeregister(ID)
|
|
|
|
}
|
|
|
|
|
2016-05-23 14:24:00 +00:00
|
|
|
// Run triggers periodic syncing of services and checks with Consul. This is
|
|
|
|
// a long lived go-routine which is stopped during shutdown.
|
|
|
|
func (c *Syncer) Run() {
|
2016-05-24 06:23:57 +00:00
|
|
|
d := initialSyncDelay + lib.RandomStagger(initialSyncBuffer-initialSyncDelay)
|
|
|
|
sync := time.NewTimer(d)
|
|
|
|
c.logger.Printf("[DEBUG] consul.sync: sleeping %v before first sync", d)
|
|
|
|
|
2016-03-23 07:50:41 +00:00
|
|
|
for {
|
|
|
|
select {
|
2016-03-25 21:18:04 +00:00
|
|
|
case <-sync.C:
|
2016-05-24 06:23:57 +00:00
|
|
|
d = syncInterval - lib.RandomStagger(syncInterval/syncJitter)
|
|
|
|
sync.Reset(d)
|
|
|
|
|
2016-03-23 18:31:04 +00:00
|
|
|
if err := c.performSync(); err != nil {
|
2016-05-24 04:35:31 +00:00
|
|
|
if c.runChecks {
|
2016-06-07 16:37:39 +00:00
|
|
|
c.logger.Printf("[DEBUG] consul.sync: disabling checks until Consul sync completes for %q: %v", c.serviceRegPrefix, err)
|
2016-04-02 21:48:10 +00:00
|
|
|
}
|
2016-05-24 04:35:31 +00:00
|
|
|
c.runChecks = false
|
2016-04-02 21:48:10 +00:00
|
|
|
} else {
|
2016-05-24 04:35:31 +00:00
|
|
|
c.runChecks = true
|
2016-03-23 07:50:41 +00:00
|
|
|
}
|
2016-05-24 06:23:57 +00:00
|
|
|
case <-c.notifySyncCh:
|
|
|
|
sync.Reset(syncInterval)
|
2016-03-23 07:50:41 +00:00
|
|
|
case <-c.shutdownCh:
|
2016-06-02 16:15:30 +00:00
|
|
|
c.Shutdown()
|
|
|
|
case <-c.notifyShutdownCh:
|
2016-03-25 21:18:04 +00:00
|
|
|
sync.Stop()
|
2016-06-08 17:38:00 +00:00
|
|
|
c.logger.Printf("[INFO] consul.syncer: shutting down sync for %q", c.serviceRegPrefix)
|
2016-03-23 07:50:41 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-24 06:23:57 +00:00
|
|
|
// RunHandlers executes each handler (randomly)
|
2016-06-07 15:59:17 +00:00
|
|
|
func (c *Syncer) RunHandlers() error {
|
2016-05-24 06:23:57 +00:00
|
|
|
c.periodicLock.RLock()
|
|
|
|
handlers := make(map[string]types.PeriodicCallback, len(c.periodicCallbacks))
|
|
|
|
for name, fn := range c.periodicCallbacks {
|
|
|
|
handlers[name] = fn
|
|
|
|
}
|
|
|
|
c.periodicLock.RUnlock()
|
2016-06-07 15:59:17 +00:00
|
|
|
|
|
|
|
var mErr multierror.Error
|
2016-05-24 06:28:42 +00:00
|
|
|
for _, fn := range handlers {
|
2016-06-07 15:59:17 +00:00
|
|
|
if err := fn(); err != nil {
|
|
|
|
mErr.Errors = append(mErr.Errors, err)
|
|
|
|
}
|
2016-05-24 06:23:57 +00:00
|
|
|
}
|
2016-06-07 15:59:17 +00:00
|
|
|
return mErr.ErrorOrNil()
|
2016-05-24 06:23:57 +00:00
|
|
|
}
|
|
|
|
|
2016-03-23 21:34:43 +00:00
|
|
|
// performSync sync the services and checks we are tracking with Consul.
|
2016-05-22 15:24:54 +00:00
|
|
|
func (c *Syncer) performSync() error {
|
2016-03-23 18:31:04 +00:00
|
|
|
var mErr multierror.Error
|
2016-06-07 15:59:17 +00:00
|
|
|
if err := c.RunHandlers(); err != nil {
|
|
|
|
mErr.Errors = append(mErr.Errors, err)
|
2016-03-23 17:43:35 +00:00
|
|
|
}
|
2016-06-07 15:59:17 +00:00
|
|
|
if err := c.syncServices(); err != nil {
|
|
|
|
mErr.Errors = append(mErr.Errors, err)
|
2016-03-23 17:43:35 +00:00
|
|
|
}
|
2016-06-07 15:59:17 +00:00
|
|
|
if err := c.syncChecks(); err != nil {
|
|
|
|
mErr.Errors = append(mErr.Errors, err)
|
2016-03-23 17:43:35 +00:00
|
|
|
}
|
2016-03-23 18:31:04 +00:00
|
|
|
|
|
|
|
return mErr.ErrorOrNil()
|
2016-03-23 17:43:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// filterConsulServices prunes out all the service whose ids are not prefixed
|
|
|
|
// with nomad-
|
2016-06-07 16:37:39 +00:00
|
|
|
func (c *Syncer) filterConsulServices(consulServices map[string]*consul.AgentService) map[string]*consul.AgentService {
|
|
|
|
localServices := make(map[string]*consul.AgentService, len(consulServices))
|
|
|
|
c.registryLock.RLock()
|
|
|
|
defer c.registryLock.RUnlock()
|
|
|
|
for serviceID, service := range consulServices {
|
|
|
|
if strings.HasPrefix(service.ID, c.serviceRegPrefix) {
|
|
|
|
localServices[serviceID] = service
|
2016-03-23 17:43:35 +00:00
|
|
|
}
|
|
|
|
}
|
2016-06-07 16:37:39 +00:00
|
|
|
return localServices
|
2016-03-23 17:43:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// filterConsulChecks prunes out all the consul checks which do not have
|
|
|
|
// services with id prefixed with noamd-
|
2016-05-22 15:24:54 +00:00
|
|
|
func (c *Syncer) filterConsulChecks(chks map[string]*consul.AgentCheck) map[string]*consul.AgentCheck {
|
2016-03-23 17:43:35 +00:00
|
|
|
nomadChecks := make(map[string]*consul.AgentCheck)
|
|
|
|
for _, chk := range chks {
|
|
|
|
if strings.HasPrefix(chk.ServiceID, structs.NomadConsulPrefix) {
|
|
|
|
nomadChecks[chk.CheckID] = chk
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nomadChecks
|
2016-03-23 07:50:41 +00:00
|
|
|
}
|
2016-03-23 22:36:46 +00:00
|
|
|
|
2016-05-28 01:17:37 +00:00
|
|
|
// consulPresent indicates whether the consul agent is responding
|
|
|
|
func (c *Syncer) consulPresent() bool {
|
|
|
|
_, err := c.client.Agent().Self()
|
|
|
|
return err == nil
|
|
|
|
}
|
|
|
|
|
2016-03-24 20:05:08 +00:00
|
|
|
// runCheck runs a check and updates the corresponding ttl check in consul
|
2016-05-22 15:24:54 +00:00
|
|
|
func (c *Syncer) runCheck(check Check) {
|
2016-03-24 20:05:08 +00:00
|
|
|
res := check.Run()
|
2016-05-05 17:01:38 +00:00
|
|
|
if res.Duration >= check.Timeout() {
|
2016-06-08 17:38:00 +00:00
|
|
|
c.logger.Printf("[DEBUG] consul.syncer: check took time: %v, timeout: %v", res.Duration, check.Timeout())
|
2016-05-05 17:01:38 +00:00
|
|
|
}
|
2016-03-25 02:00:24 +00:00
|
|
|
state := consul.HealthCritical
|
|
|
|
output := res.Output
|
2016-03-25 21:26:56 +00:00
|
|
|
switch res.ExitCode {
|
|
|
|
case 0:
|
2016-03-25 02:00:24 +00:00
|
|
|
state = consul.HealthPassing
|
2016-03-25 21:26:56 +00:00
|
|
|
case 1:
|
2016-03-25 02:00:24 +00:00
|
|
|
state = consul.HealthWarning
|
2016-03-25 21:26:56 +00:00
|
|
|
default:
|
|
|
|
state = consul.HealthCritical
|
2016-03-25 02:00:24 +00:00
|
|
|
}
|
2016-04-05 17:15:38 +00:00
|
|
|
if res.Err != nil {
|
|
|
|
state = consul.HealthCritical
|
|
|
|
output = res.Err.Error()
|
|
|
|
}
|
2016-03-25 02:00:24 +00:00
|
|
|
if err := c.client.Agent().UpdateTTL(check.ID(), output, state); err != nil {
|
2016-05-24 04:35:31 +00:00
|
|
|
if c.runChecks {
|
2016-06-08 17:38:00 +00:00
|
|
|
c.logger.Printf("[DEBUG] consul.syncer: check %q failed, disabling Consul checks until until next successful sync: %v", check.ID(), err)
|
2016-05-24 04:35:31 +00:00
|
|
|
c.runChecks = false
|
2016-04-02 21:48:10 +00:00
|
|
|
} else {
|
2016-05-24 04:35:31 +00:00
|
|
|
c.runChecks = true
|
2016-04-02 21:48:10 +00:00
|
|
|
}
|
2016-03-24 20:05:08 +00:00
|
|
|
}
|
|
|
|
}
|
2016-05-14 07:36:26 +00:00
|
|
|
|
2016-06-07 16:37:39 +00:00
|
|
|
// GenerateServicePrefix returns a service prefix based on an allocation id
|
|
|
|
// and task name.
|
|
|
|
func GenerateServicePrefix(allocID string, taskName string) string {
|
2016-05-14 07:36:26 +00:00
|
|
|
return fmt.Sprintf("%s-%s", taskName, allocID)
|
|
|
|
}
|
2016-05-24 06:23:57 +00:00
|
|
|
|
|
|
|
// AddPeriodicHandler adds a uniquely named callback. Returns true if
|
|
|
|
// successful, false if a handler with the same name already exists.
|
|
|
|
func (c *Syncer) AddPeriodicHandler(name string, fn types.PeriodicCallback) bool {
|
|
|
|
c.periodicLock.Lock()
|
|
|
|
defer c.periodicLock.Unlock()
|
|
|
|
if _, found := c.periodicCallbacks[name]; found {
|
2016-06-08 17:38:00 +00:00
|
|
|
c.logger.Printf("[ERROR] consul.syncer: failed adding handler %q", name)
|
2016-05-24 06:23:57 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
c.periodicCallbacks[name] = fn
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Syncer) NumHandlers() int {
|
|
|
|
c.periodicLock.RLock()
|
|
|
|
defer c.periodicLock.RUnlock()
|
|
|
|
return len(c.periodicCallbacks)
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemovePeriodicHandler removes a handler with a given name.
|
|
|
|
func (c *Syncer) RemovePeriodicHandler(name string) {
|
|
|
|
c.periodicLock.Lock()
|
|
|
|
defer c.periodicLock.Unlock()
|
|
|
|
delete(c.periodicCallbacks, name)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Syncer) ConsulClient() *consul.Client {
|
|
|
|
return c.client
|
|
|
|
}
|