open-nomad/client/consul.go

285 lines
7.8 KiB
Go
Raw Normal View History

package client
import (
"crypto/tls"
"fmt"
2015-11-18 10:14:07 +00:00
"log"
"net/http"
2015-11-18 22:19:58 +00:00
"net/url"
"strings"
"sync"
"time"
consul "github.com/hashicorp/consul/api"
"github.com/hashicorp/go-multierror"
"github.com/hashicorp/nomad/nomad/structs"
)
const (
syncInterval = 5 * time.Second
)
2015-11-25 01:26:30 +00:00
type trackedTask struct {
allocID string
task *structs.Task
}
2015-11-24 20:34:26 +00:00
type ConsulService struct {
client *consul.Client
logger *log.Logger
shutdownCh chan struct{}
2015-11-18 10:14:07 +00:00
2015-11-26 01:28:44 +00:00
trackedTasks map[string]*trackedTask
serviceStates map[string]string
trackedTskLock sync.Mutex
}
2015-11-26 02:31:11 +00:00
// A factory method to create new consul service
func NewConsulService(logger *log.Logger, consulAddr string, token string,
auth string, enableSSL bool, verifySSL bool) (*ConsulService, error) {
var err error
var c *consul.Client
cfg := consul.DefaultConfig()
cfg.Address = consulAddr
if token != "" {
cfg.Token = token
}
if auth != "" {
var username, password string
if strings.Contains(auth, ":") {
split := strings.SplitN(auth, ":", 2)
username = split[0]
password = split[1]
} else {
username = auth
}
cfg.HttpAuth = &consul.HttpBasicAuth{
Username: username,
Password: password,
}
}
if enableSSL {
cfg.Scheme = "https"
}
if enableSSL && !verifySSL {
cfg.HttpClient.Transport = &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
}
}
if c, err = consul.NewClient(cfg); err != nil {
return nil, err
}
2015-11-24 20:34:26 +00:00
consulService := ConsulService{
2015-11-26 01:28:44 +00:00
client: c,
logger: logger,
trackedTasks: make(map[string]*trackedTask),
serviceStates: make(map[string]string),
shutdownCh: make(chan struct{}),
}
2015-11-24 20:34:26 +00:00
return &consulService, nil
}
2015-11-26 02:31:11 +00:00
// Starts tracking a task for changes to it's services and tasks
2015-11-24 20:34:26 +00:00
func (c *ConsulService) Register(task *structs.Task, allocID string) error {
var mErr multierror.Error
2015-11-25 01:26:30 +00:00
c.trackedTskLock.Lock()
tt := &trackedTask{allocID: allocID, task: task}
c.trackedTasks[fmt.Sprintf("%s-%s", allocID, task.Name)] = tt
c.trackedTskLock.Unlock()
for _, service := range task.Services {
c.logger.Printf("[INFO] consul: Registering service %s with Consul.", service.Name)
if err := c.registerService(service, task, allocID); err != nil {
mErr.Errors = append(mErr.Errors, err)
}
}
return mErr.ErrorOrNil()
}
2015-11-26 02:31:11 +00:00
// Stops tracking a task for changes to it's services and checks
2015-11-25 01:26:30 +00:00
func (c *ConsulService) Deregister(task *structs.Task, allocID string) error {
var mErr multierror.Error
2015-11-25 01:26:30 +00:00
c.trackedTskLock.Lock()
delete(c.trackedTasks, fmt.Sprintf("%s-%s", allocID, task.Name))
c.trackedTskLock.Unlock()
for _, service := range task.Services {
if service.Id == "" {
continue
}
c.logger.Printf("[INFO] consul: De-Registering service %v with Consul", service.Name)
if err := c.deregisterService(service.Id); err != nil {
2015-11-24 20:34:26 +00:00
c.logger.Printf("[DEBUG] consul: Error in de-registering service %v from Consul", service.Name)
mErr.Errors = append(mErr.Errors, err)
}
}
return mErr.ErrorOrNil()
}
2015-11-24 20:34:26 +00:00
func (c *ConsulService) ShutDown() {
close(c.shutdownCh)
}
2015-11-26 02:31:11 +00:00
// Performs calls to sync checks and services periodically
2015-11-24 20:34:26 +00:00
func (c *ConsulService) SyncWithConsul() {
sync := time.After(syncInterval)
agent := c.client.Agent()
for {
select {
case <-sync:
2015-11-24 22:37:14 +00:00
c.performSync(agent)
sync = time.After(syncInterval)
case <-c.shutdownCh:
c.logger.Printf("[INFO] Shutting down Consul Client")
return
}
}
}
2015-11-26 02:31:11 +00:00
// Sync checks and services with Consul
2015-11-26 02:23:47 +00:00
func (c *ConsulService) performSync(agent *consul.Agent) (int, int) {
2015-11-26 01:28:44 +00:00
// Get the list of the services and that Consul knows about
consulServices, _ := agent.Services()
consulChecks, _ := agent.Checks()
delete(consulServices, "consul")
2015-11-24 22:37:14 +00:00
2015-11-26 01:28:44 +00:00
knownChecks := make(map[string]struct{})
2015-11-26 02:23:47 +00:00
knownServices := make(map[string]struct{})
// Add services and checks which Consul doesn't know about
2015-11-25 01:26:30 +00:00
for _, trackedTask := range c.trackedTasks {
for _, service := range trackedTask.task.Services {
2015-11-26 02:23:47 +00:00
knownServices[service.Id] = struct{}{}
2015-11-26 01:28:44 +00:00
if _, ok := consulServices[service.Id]; !ok {
2015-11-25 01:26:30 +00:00
c.registerService(service, trackedTask.task, trackedTask.allocID)
2015-11-26 01:28:44 +00:00
continue
2015-11-25 01:26:30 +00:00
}
2015-11-26 01:28:44 +00:00
if service.Hash() != c.serviceStates[service.Id] {
c.registerService(service, trackedTask.task, trackedTask.allocID)
continue
}
for _, check := range service.Checks {
knownChecks[check.Id] = struct{}{}
if _, ok := consulChecks[check.Id]; !ok {
2015-11-26 01:28:44 +00:00
host, port := trackedTask.task.FindHostAndPortFor(service.PortLabel)
cr := c.makeCheck(service, check, host, port)
2015-11-26 01:28:44 +00:00
c.registerCheck(cr)
}
2015-11-24 22:37:14 +00:00
}
}
}
2015-11-26 02:23:47 +00:00
// Remove services that are not present anymore
2015-11-26 01:28:44 +00:00
for _, consulService := range consulServices {
2015-11-26 02:23:47 +00:00
if _, ok := knownServices[consulService.ID]; !ok {
delete(c.serviceStates, consulService.ID)
2015-11-26 01:28:44 +00:00
c.deregisterService(consulService.ID)
2015-11-25 02:39:38 +00:00
}
}
2015-11-26 02:23:47 +00:00
// Remove checks that are not present anymore
2015-11-26 01:28:44 +00:00
for _, consulCheck := range consulChecks {
if _, ok := knownChecks[consulCheck.CheckID]; !ok {
c.deregisterCheck(consulCheck.CheckID)
2015-11-25 02:39:38 +00:00
}
}
2015-11-26 01:28:44 +00:00
2015-11-26 02:23:47 +00:00
return len(c.serviceStates), len(knownChecks)
2015-11-24 22:37:14 +00:00
}
2015-11-26 02:31:11 +00:00
// Registers a Service with Consul
2015-11-24 20:34:26 +00:00
func (c *ConsulService) registerService(service *structs.Service, task *structs.Task, allocID string) error {
var mErr multierror.Error
2015-11-24 20:34:26 +00:00
service.Id = fmt.Sprintf("%s-%s", allocID, service.Name)
2015-11-25 02:39:38 +00:00
host, port := task.FindHostAndPortFor(service.PortLabel)
if host == "" || port == 0 {
return fmt.Errorf("consul: The port:%s marked for registration of service: %s couldn't be found", service.PortLabel, service.Name)
}
2015-11-26 01:28:44 +00:00
c.serviceStates[service.Id] = service.Hash()
asr := &consul.AgentServiceRegistration{
ID: service.Id,
Name: service.Name,
Tags: service.Tags,
Port: port,
Address: host,
}
if err := c.client.Agent().ServiceRegister(asr); err != nil {
2015-11-24 20:34:26 +00:00
c.logger.Printf("[DEBUG] consul: Error while registering service %v with Consul: %v", service.Name, err)
mErr.Errors = append(mErr.Errors, err)
}
2015-11-26 01:28:44 +00:00
for _, check := range service.Checks {
cr := c.makeCheck(service, check, host, port)
2015-11-26 01:28:44 +00:00
if err := c.registerCheck(cr); err != nil {
2015-11-23 07:27:59 +00:00
c.logger.Printf("[ERROR] consul: Error while registerting check %v with Consul: %v", check.Name, err)
mErr.Errors = append(mErr.Errors, err)
}
2015-11-26 01:28:44 +00:00
2015-11-23 07:27:59 +00:00
}
return mErr.ErrorOrNil()
}
2015-11-26 02:31:11 +00:00
// Registers a check with Consul
2015-11-25 02:39:38 +00:00
func (c *ConsulService) registerCheck(check *consul.AgentCheckRegistration) error {
c.logger.Printf("[DEBUG] Registering Check with ID: %v for Service: %v", check.ID, check.ServiceID)
return c.client.Agent().CheckRegister(check)
}
2015-11-26 02:31:11 +00:00
// Deregisters a check with a specific ID from Consul
2015-11-25 02:39:38 +00:00
func (c *ConsulService) deregisterCheck(checkID string) error {
2015-11-25 02:43:23 +00:00
c.logger.Printf("[DEBUG] Removing check with ID: %v", checkID)
2015-11-25 02:39:38 +00:00
return c.client.Agent().CheckDeregister(checkID)
}
2015-11-26 02:31:11 +00:00
// De-Registers a Service with a specific id from Consul
2015-11-24 20:34:26 +00:00
func (c *ConsulService) deregisterService(serviceId string) error {
2015-11-26 01:28:44 +00:00
delete(c.serviceStates, serviceId)
if err := c.client.Agent().ServiceDeregister(serviceId); err != nil {
return err
}
return nil
}
2015-11-26 02:31:11 +00:00
// Creates a Consul Check Registration struct
2015-11-26 01:28:44 +00:00
func (c *ConsulService) makeCheck(service *structs.Service, check *structs.ServiceCheck, ip string, port int) *consul.AgentCheckRegistration {
if check.Name == "" {
2015-11-26 02:31:11 +00:00
check.Name = fmt.Sprintf("service: %q%s%q check", service.Name)
2015-11-26 01:28:44 +00:00
}
check.Id = check.Hash(service.Id)
2015-11-26 02:31:11 +00:00
2015-11-26 01:28:44 +00:00
cr := &consul.AgentCheckRegistration{
ID: check.Id,
2015-11-26 01:28:44 +00:00
Name: check.Name,
ServiceID: service.Id,
}
cr.Interval = check.Interval.String()
cr.Timeout = check.Timeout.String()
2015-11-26 02:31:11 +00:00
2015-11-26 01:28:44 +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", ip, port),
Path: check.Path,
}
cr.HTTP = url.String()
case structs.ServiceCheckTCP:
cr.TCP = fmt.Sprintf("%s:%d", ip, port)
case structs.ServiceCheckScript:
cr.Script = check.Script // TODO This needs to include the path of the alloc dir and based on driver types
}
return cr
2015-11-18 11:08:53 +00:00
}