open-nomad/client/consul/sync.go

444 lines
12 KiB
Go
Raw Normal View History

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-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-03-23 21:34:43 +00:00
// ConsulService allows syncing of services and checks with Consul
2016-03-23 07:50:41 +00:00
type ConsulService struct {
client *consul.Client
task *structs.Task
allocID string
delegateChecks map[string]struct{}
2016-03-24 20:05:08 +00:00
createCheck func(*structs.ServiceCheck, string) (Check, error)
2016-03-23 07:50:41 +00:00
2016-03-24 01:21:27 +00:00
trackedServices map[string]*consul.AgentService
trackedChecks map[string]*consul.AgentCheckRegistration
2016-03-25 04:17:33 +00:00
checkRunners map[string]*CheckRunner
2016-03-23 07:50:41 +00:00
2016-03-24 01:03:02 +00:00
logger *log.Logger
shutdownCh chan struct{}
shutdown bool
shutdownLock sync.Mutex
2016-03-23 07:50:41 +00:00
}
2016-03-23 21:34:43 +00:00
// ConsulConfig is the configuration used to create a new ConsulService client
2016-03-23 07:50:41 +00:00
type ConsulConfig struct {
Addr string
Token string
Auth string
EnableSSL bool
VerifySSL bool
CAFile string
CertFile string
KeyFile string
2016-03-23 07:50:41 +00:00
}
const (
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
// ttlCheckBuffer is the time interval that Nomad can take to report Consul
// the check result
ttlCheckBuffer = 31 * time.Second
2016-03-23 07:50:41 +00:00
)
2016-03-23 21:34:43 +00:00
// NewConsulService returns a new ConsulService
func NewConsulService(config *ConsulConfig, logger *log.Logger, allocID string) (*ConsulService, 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"
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
}
consulService := ConsulService{
2016-03-24 01:21:27 +00:00
client: c,
2016-03-24 01:39:17 +00:00
allocID: allocID,
2016-03-24 01:21:27 +00:00
logger: logger,
trackedServices: make(map[string]*consul.AgentService),
trackedChecks: make(map[string]*consul.AgentCheckRegistration),
checkRunners: make(map[string]*CheckRunner),
2016-03-23 07:50:41 +00:00
shutdownCh: make(chan struct{}),
}
return &consulService, nil
}
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-03-24 20:05:08 +00:00
func (c *ConsulService) SetDelegatedChecks(delegateChecks map[string]struct{}, createCheck func(*structs.ServiceCheck, string) (Check, error)) *ConsulService {
c.delegateChecks = delegateChecks
c.createCheck = createCheck
return c
}
2016-03-23 21:34:43 +00:00
// SyncTask sync the services and task with consul
2016-03-23 17:43:35 +00:00
func (c *ConsulService) SyncTask(task *structs.Task) error {
var mErr multierror.Error
2016-03-23 07:50:41 +00:00
c.task = task
2016-03-24 01:21:27 +00:00
taskServices := make(map[string]*consul.AgentService)
taskChecks := make(map[string]*consul.AgentCheckRegistration)
2016-03-23 17:43:35 +00:00
// Register Services and Checks that we don't know about or has changed
for _, service := range task.Services {
srv, err := c.createService(service)
if err != nil {
mErr.Errors = append(mErr.Errors, err)
continue
}
2016-03-24 01:21:27 +00:00
trackedService, ok := c.trackedServices[srv.ID]
2016-03-23 17:43:35 +00:00
if (ok && !reflect.DeepEqual(trackedService, srv)) || !ok {
if err := c.registerService(srv); err != nil {
mErr.Errors = append(mErr.Errors, err)
}
2016-03-23 17:43:35 +00:00
}
2016-03-24 01:21:27 +00:00
c.trackedServices[srv.ID] = srv
taskServices[srv.ID] = srv
2016-03-23 17:43:35 +00:00
for _, chk := range service.Checks {
// Create a consul check registration
2016-03-24 22:09:50 +00:00
chkReg, err := c.createCheckReg(chk, srv)
if err != nil {
mErr.Errors = append(mErr.Errors, err)
continue
}
// creating a nomad check if we have to handle this particular check type
if _, ok := c.delegateChecks[chk.Type]; ok {
nc, err := c.createCheck(chk, chkReg.ID)
if err != nil {
mErr.Errors = append(mErr.Errors, err)
continue
}
cr := NewCheckRunner(nc, c.runCheck, c.logger)
c.checkRunners[nc.ID()] = cr
}
if _, ok := c.trackedChecks[chkReg.ID]; !ok {
if err := c.registerCheck(chkReg); err != nil {
mErr.Errors = append(mErr.Errors, err)
}
2016-03-23 17:43:35 +00:00
}
c.trackedChecks[chkReg.ID] = chkReg
taskChecks[chkReg.ID] = chkReg
2016-03-23 17:43:35 +00:00
}
}
// Remove services that are not present anymore
2016-03-24 01:21:27 +00:00
for _, service := range c.trackedServices {
if _, ok := taskServices[service.ID]; !ok {
if err := c.deregisterService(service.ID); err != nil {
mErr.Errors = append(mErr.Errors, err)
}
2016-03-24 01:21:27 +00:00
delete(c.trackedServices, service.ID)
2016-03-23 17:43:35 +00:00
}
}
// Remove the checks that are not present anymore
2016-03-24 01:21:27 +00:00
for checkID, _ := range c.trackedChecks {
if _, ok := taskChecks[checkID]; !ok {
if err := c.deregisterCheck(checkID); err != nil {
mErr.Errors = append(mErr.Errors, err)
}
2016-03-24 01:21:27 +00:00
delete(c.trackedChecks, checkID)
2016-03-23 17:43:35 +00:00
}
}
return mErr.ErrorOrNil()
2016-03-23 07:50:41 +00:00
}
2016-03-23 21:34:43 +00:00
// Shutdown de-registers the services and checks and shuts down periodic syncing
func (c *ConsulService) 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 {
close(c.shutdownCh)
c.shutdown = true
}
2016-03-24 01:03:02 +00:00
c.shutdownLock.Unlock()
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-03-25 02:19:13 +00:00
func (c *ConsulService) KeepServices(services map[string]struct{}) error {
var mErr multierror.Error
// Get the services from Consul
cServices, err := c.client.Agent().Services()
if err != nil {
return err
}
cServices = c.filterConsulServices(cServices)
// Remove the services from consul which are not in any of the tasks
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
func (c *ConsulService) 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
}
return c.client.Agent().CheckRegister(chkReg)
}
2016-03-25 02:30:02 +00:00
// createCheckReg creates a Check that can be registered with Nomad. It also
// creates a Nomad check for the check types that it can handle.
2016-03-24 22:09:50 +00:00
func (c *ConsulService) createCheckReg(check *structs.ServiceCheck, service *consul.AgentService) (*consul.AgentCheckRegistration, error) {
2016-03-23 17:43:35 +00:00
chkReg := consul.AgentCheckRegistration{
ID: check.Hash(service.ID),
2016-03-23 17:43:35 +00:00
Name: check.Name,
ServiceID: service.ID,
}
chkReg.Timeout = check.Timeout.String()
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-03-23 07:50:41 +00:00
func (c *ConsulService) createService(service *structs.Service) (*consul.AgentService, error) {
host, port := c.task.FindHostAndPortFor(service.PortLabel)
if host == "" {
return nil, fmt.Errorf("host for the service %q couldn't be found", service.Name)
}
if port == 0 {
return nil, fmt.Errorf("port for the service %q couldn't be found", service.Name)
}
srv := consul.AgentService{
ID: service.ID(c.allocID, c.task.Name),
2016-03-23 07:50:41 +00:00
Service: service.Name,
Tags: service.Tags,
Address: host,
Port: port,
}
return &srv, nil
}
2016-03-23 17:43:35 +00:00
// registerService registers a service with Consul
func (c *ConsulService) registerService(service *consul.AgentService) error {
srvReg := consul.AgentServiceRegistration{
ID: service.ID,
Name: service.Service,
Tags: service.Tags,
Port: service.Port,
Address: service.Address,
}
return c.client.Agent().ServiceRegister(&srvReg)
}
// deregisterService de-registers a service with the given ID from consul
func (c *ConsulService) deregisterService(ID string) error {
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-03-23 17:43:35 +00:00
func (c *ConsulService) 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-03-24 02:20:08 +00:00
// PeriodicSync triggers periodic syncing of services and checks with Consul.
// This is a long lived go-routine which is stopped during shutdown
2016-03-24 01:13:13 +00:00
func (c *ConsulService) PeriodicSync() {
sync := time.NewTicker(syncInterval)
2016-03-23 07:50:41 +00:00
for {
select {
case <-sync.C:
2016-03-23 18:31:04 +00:00
if err := c.performSync(); err != nil {
2016-03-23 07:50:41 +00:00
c.logger.Printf("[DEBUG] consul: error in syncing task %q: %v", c.task.Name, err)
}
case <-c.shutdownCh:
sync.Stop()
2016-03-23 07:50:41 +00:00
c.logger.Printf("[INFO] consul: shutting down sync for task %q", c.task.Name)
return
}
}
}
2016-03-23 21:34:43 +00:00
// performSync sync the services and checks we are tracking with Consul.
2016-03-23 18:31:04 +00:00
func (c *ConsulService) performSync() error {
var mErr multierror.Error
2016-03-23 17:43:35 +00:00
cServices, err := c.client.Agent().Services()
if err != nil {
2016-03-23 18:31:04 +00:00
return err
2016-03-23 17:43:35 +00:00
}
cChecks, err := c.client.Agent().Checks()
if err != nil {
2016-03-23 18:31:04 +00:00
return err
2016-03-23 17:43:35 +00:00
}
// Add services and checks that consul doesn't have but we do
2016-03-24 01:21:27 +00:00
for serviceID, service := range c.trackedServices {
2016-03-23 17:43:35 +00:00
if _, ok := cServices[serviceID]; !ok {
2016-03-23 18:31:04 +00:00
if err := c.registerService(service); err != nil {
mErr.Errors = append(mErr.Errors, err)
}
2016-03-23 17:43:35 +00:00
}
}
2016-03-24 01:21:27 +00:00
for checkID, check := range c.trackedChecks {
if _, ok := cChecks[checkID]; !ok {
if err := c.registerCheck(check); err != nil {
2016-03-23 18:31:04 +00:00
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-
func (c *ConsulService) filterConsulServices(srvcs map[string]*consul.AgentService) map[string]*consul.AgentService {
nomadServices := make(map[string]*consul.AgentService)
for _, srv := range srvcs {
if strings.HasPrefix(srv.ID, structs.NomadConsulPrefix) {
nomadServices[srv.ID] = srv
}
}
return nomadServices
}
// filterConsulChecks prunes out all the consul checks which do not have
// services with id prefixed with noamd-
func (c *ConsulService) filterConsulChecks(chks map[string]*consul.AgentCheck) map[string]*consul.AgentCheck {
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-03-24 20:05:08 +00:00
// consulPresent indicates whether the consul agent is responding
2016-03-23 22:36:46 +00:00
func (c *ConsulService) 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
func (c *ConsulService) runCheck(check Check) {
res := check.Run()
2016-03-25 02:00:24 +00:00
state := consul.HealthCritical
output := res.Output
2016-03-24 20:05:08 +00:00
if res.Err != nil {
2016-03-25 02:00:24 +00:00
output = res.Err.Error()
2016-03-24 20:05:08 +00:00
}
switch res.ExitCode {
case 0:
2016-03-25 02:00:24 +00:00
state = consul.HealthPassing
case 1:
2016-03-25 02:00:24 +00:00
state = consul.HealthWarning
default:
state = consul.HealthCritical
2016-03-25 02:00:24 +00:00
}
if err := c.client.Agent().UpdateTTL(check.ID(), output, state); err != nil {
c.logger.Printf("[DEBUG] error updating ttl check for check %q: %v", check.ID(), err)
2016-03-24 20:05:08 +00:00
}
}