2015-11-18 08:50:45 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
2015-11-25 21:39:16 +00:00
|
|
|
"crypto/tls"
|
2015-11-18 08:50:45 +00:00
|
|
|
"fmt"
|
2015-11-18 10:14:07 +00:00
|
|
|
"log"
|
2015-11-25 21:39:16 +00:00
|
|
|
"net/http"
|
2015-11-18 22:19:58 +00:00
|
|
|
"net/url"
|
2015-11-25 21:39:16 +00:00
|
|
|
"strings"
|
2015-11-18 17:36:37 +00:00
|
|
|
"sync"
|
2015-11-18 12:34:23 +00:00
|
|
|
"time"
|
2015-11-20 06:18:19 +00:00
|
|
|
|
|
|
|
consul "github.com/hashicorp/consul/api"
|
|
|
|
"github.com/hashicorp/go-multierror"
|
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
2015-11-18 08:50:45 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2015-11-18 12:34:23 +00:00
|
|
|
syncInterval = 5 * time.Second
|
2015-11-18 08:50:45 +00:00
|
|
|
)
|
|
|
|
|
2015-11-26 19:25:09 +00:00
|
|
|
// consulApi is the interface which wraps the actual consul api client
|
2015-11-26 09:03:16 +00:00
|
|
|
type consulApi interface {
|
|
|
|
CheckRegister(check *consul.AgentCheckRegistration) error
|
|
|
|
CheckDeregister(checkID string) error
|
|
|
|
ServiceRegister(service *consul.AgentServiceRegistration) error
|
|
|
|
ServiceDeregister(ServiceID string) error
|
|
|
|
Services() (map[string]*consul.AgentService, error)
|
|
|
|
Checks() (map[string]*consul.AgentCheck, error)
|
|
|
|
}
|
|
|
|
|
2015-11-26 19:25:09 +00:00
|
|
|
// consulApiClient is the actual implementation of the consulApi which
|
|
|
|
// talks to the consul agent
|
2015-11-26 09:03:16 +00:00
|
|
|
type consulApiClient struct {
|
|
|
|
client *consul.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *consulApiClient) CheckRegister(check *consul.AgentCheckRegistration) error {
|
|
|
|
return a.client.Agent().CheckRegister(check)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *consulApiClient) CheckDeregister(checkID string) error {
|
|
|
|
return a.client.Agent().CheckDeregister(checkID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *consulApiClient) ServiceRegister(service *consul.AgentServiceRegistration) error {
|
|
|
|
return a.client.Agent().ServiceRegister(service)
|
|
|
|
}
|
|
|
|
|
2015-12-14 23:57:56 +00:00
|
|
|
func (a *consulApiClient) ServiceDeregister(serviceID string) error {
|
|
|
|
return a.client.Agent().ServiceDeregister(serviceID)
|
2015-11-26 09:03:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *consulApiClient) Services() (map[string]*consul.AgentService, error) {
|
|
|
|
return a.client.Agent().Services()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *consulApiClient) Checks() (map[string]*consul.AgentCheck, error) {
|
|
|
|
return a.client.Agent().Checks()
|
|
|
|
}
|
|
|
|
|
2015-11-26 19:25:09 +00:00
|
|
|
// trackedTask is a Task that we are tracking for changes in service and check
|
|
|
|
// definitions and keep them sycned with Consul Agent
|
2015-11-25 01:26:30 +00:00
|
|
|
type trackedTask struct {
|
2015-12-14 22:53:49 +00:00
|
|
|
task *structs.Task
|
|
|
|
alloc *structs.Allocation
|
2015-11-18 12:34:23 +00:00
|
|
|
}
|
|
|
|
|
2015-11-26 19:25:09 +00:00
|
|
|
// ConsulService is the service which tracks tasks and syncs the services and
|
|
|
|
// checks defined in them with Consul Agent
|
2015-11-24 20:34:26 +00:00
|
|
|
type ConsulService struct {
|
2015-11-26 09:03:16 +00:00
|
|
|
client consulApi
|
2015-11-18 12:34:23 +00:00
|
|
|
logger *log.Logger
|
|
|
|
shutdownCh chan struct{}
|
2015-12-09 23:24:44 +00:00
|
|
|
node *structs.Node
|
2015-11-18 10:14:07 +00:00
|
|
|
|
2016-03-02 21:57:08 +00:00
|
|
|
trackedTasks map[string]*trackedTask
|
|
|
|
serviceStates map[string]string
|
|
|
|
allocToService map[string][]string
|
|
|
|
trackedTaskLock sync.Mutex
|
2015-11-18 08:50:45 +00:00
|
|
|
}
|
|
|
|
|
2015-12-11 19:02:23 +00:00
|
|
|
type consulServiceConfig struct {
|
|
|
|
logger *log.Logger
|
|
|
|
consulAddr string
|
|
|
|
token string
|
|
|
|
auth string
|
|
|
|
enableSSL bool
|
|
|
|
verifySSL bool
|
|
|
|
node *structs.Node
|
|
|
|
}
|
|
|
|
|
2015-11-26 02:31:11 +00:00
|
|
|
// A factory method to create new consul service
|
2015-12-11 19:02:23 +00:00
|
|
|
func NewConsulService(config *consulServiceConfig) (*ConsulService, error) {
|
2015-11-18 08:50:45 +00:00
|
|
|
var err error
|
2015-11-18 12:34:23 +00:00
|
|
|
var c *consul.Client
|
2015-11-18 13:15:52 +00:00
|
|
|
cfg := consul.DefaultConfig()
|
2015-12-11 19:02:23 +00:00
|
|
|
cfg.Address = config.consulAddr
|
|
|
|
if config.token != "" {
|
|
|
|
cfg.Token = config.token
|
2015-11-25 21:39:16 +00:00
|
|
|
}
|
|
|
|
|
2015-12-11 19:02:23 +00:00
|
|
|
if config.auth != "" {
|
2015-11-25 21:39:16 +00:00
|
|
|
var username, password string
|
2015-12-11 19:02:23 +00:00
|
|
|
if strings.Contains(config.auth, ":") {
|
|
|
|
split := strings.SplitN(config.auth, ":", 2)
|
2015-11-25 21:39:16 +00:00
|
|
|
username = split[0]
|
|
|
|
password = split[1]
|
|
|
|
} else {
|
2015-12-11 19:02:23 +00:00
|
|
|
username = config.auth
|
2015-11-25 21:39:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cfg.HttpAuth = &consul.HttpBasicAuth{
|
|
|
|
Username: username,
|
|
|
|
Password: password,
|
|
|
|
}
|
|
|
|
}
|
2015-12-11 19:02:23 +00:00
|
|
|
if config.enableSSL {
|
2015-11-25 21:39:16 +00:00
|
|
|
cfg.Scheme = "https"
|
|
|
|
}
|
2015-12-11 19:02:23 +00:00
|
|
|
if config.enableSSL && !config.verifySSL {
|
2015-11-25 21:39:16 +00:00
|
|
|
cfg.HttpClient.Transport = &http.Transport{
|
|
|
|
TLSClientConfig: &tls.Config{
|
|
|
|
InsecureSkipVerify: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2015-11-18 13:15:52 +00:00
|
|
|
if c, err = consul.NewClient(cfg); err != nil {
|
2015-11-18 08:50:45 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-11-24 20:34:26 +00:00
|
|
|
consulService := ConsulService{
|
2016-02-05 20:07:56 +00:00
|
|
|
client: &consulApiClient{client: c},
|
|
|
|
logger: config.logger,
|
|
|
|
node: config.node,
|
|
|
|
trackedTasks: make(map[string]*trackedTask),
|
|
|
|
serviceStates: make(map[string]string),
|
|
|
|
allocToService: make(map[string][]string),
|
|
|
|
shutdownCh: make(chan struct{}),
|
2015-11-18 08:50:45 +00:00
|
|
|
}
|
|
|
|
|
2015-11-24 20:34:26 +00:00
|
|
|
return &consulService, nil
|
2015-11-18 08:50:45 +00:00
|
|
|
}
|
|
|
|
|
2015-11-26 21:47:02 +00:00
|
|
|
// Register starts tracking a task for changes to it's services and tasks and
|
|
|
|
// adds/removes services and checks associated with it.
|
2015-12-14 22:53:49 +00:00
|
|
|
func (c *ConsulService) Register(task *structs.Task, alloc *structs.Allocation) error {
|
2015-11-18 08:50:45 +00:00
|
|
|
var mErr multierror.Error
|
2016-03-02 21:57:08 +00:00
|
|
|
c.trackedTaskLock.Lock()
|
2015-12-14 22:53:49 +00:00
|
|
|
tt := &trackedTask{task: task, alloc: alloc}
|
|
|
|
c.trackedTasks[fmt.Sprintf("%s-%s", alloc.ID, task.Name)] = tt
|
2016-02-05 20:07:56 +00:00
|
|
|
|
|
|
|
// Delete any previously registered service as the same alloc is being
|
|
|
|
// re-registered.
|
|
|
|
for _, service := range c.allocToService[alloc.ID] {
|
|
|
|
delete(c.serviceStates, service)
|
|
|
|
}
|
2016-03-02 21:57:08 +00:00
|
|
|
c.trackedTaskLock.Unlock()
|
2016-02-05 20:07:56 +00:00
|
|
|
|
2015-11-18 10:37:34 +00:00
|
|
|
for _, service := range task.Services {
|
2016-02-05 20:07:56 +00:00
|
|
|
// Track the services this alloc is registering.
|
|
|
|
c.allocToService[alloc.ID] = append(c.allocToService[alloc.ID], service.Name)
|
|
|
|
|
2015-12-11 00:04:04 +00:00
|
|
|
c.logger.Printf("[INFO] consul: registering service %s with consul.", service.Name)
|
2015-12-14 22:53:49 +00:00
|
|
|
if err := c.registerService(service, task, alloc); err != nil {
|
2015-11-18 12:34:23 +00:00
|
|
|
mErr.Errors = append(mErr.Errors, err)
|
2015-11-18 09:18:29 +00:00
|
|
|
}
|
2015-11-18 08:50:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return mErr.ErrorOrNil()
|
|
|
|
}
|
|
|
|
|
2015-11-26 21:47:02 +00:00
|
|
|
// Deregister stops tracking a task for changes to it's services and checks and
|
|
|
|
// removes all the services and checks associated with the Task
|
2015-12-14 22:53:49 +00:00
|
|
|
func (c *ConsulService) Deregister(task *structs.Task, alloc *structs.Allocation) error {
|
2015-11-18 08:50:45 +00:00
|
|
|
var mErr multierror.Error
|
2016-03-02 21:57:08 +00:00
|
|
|
c.trackedTaskLock.Lock()
|
2015-12-14 22:53:49 +00:00
|
|
|
delete(c.trackedTasks, fmt.Sprintf("%s-%s", alloc.ID, task.Name))
|
2016-02-05 20:07:56 +00:00
|
|
|
delete(c.allocToService, alloc.ID)
|
2016-03-02 21:57:08 +00:00
|
|
|
c.trackedTaskLock.Unlock()
|
2015-11-18 08:50:45 +00:00
|
|
|
for _, service := range task.Services {
|
2015-12-14 23:57:56 +00:00
|
|
|
serviceID := alloc.Services[service.Name]
|
|
|
|
if serviceID == "" {
|
2015-11-19 03:31:29 +00:00
|
|
|
continue
|
|
|
|
}
|
2015-12-11 00:04:04 +00:00
|
|
|
c.logger.Printf("[INFO] consul: deregistering service %v with consul", service.Name)
|
2015-12-14 23:57:56 +00:00
|
|
|
if err := c.deregisterService(serviceID); err != nil {
|
2015-12-11 17:22:47 +00:00
|
|
|
c.printLogMessage("[DEBUG] consul: error in deregistering service %v from consul", service.Name)
|
2015-11-18 08:50:45 +00:00
|
|
|
mErr.Errors = append(mErr.Errors, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return mErr.ErrorOrNil()
|
|
|
|
}
|
2015-11-18 09:18:29 +00:00
|
|
|
|
2015-11-24 20:34:26 +00:00
|
|
|
func (c *ConsulService) ShutDown() {
|
2015-11-18 12:59:57 +00:00
|
|
|
close(c.shutdownCh)
|
|
|
|
}
|
|
|
|
|
2015-11-26 08:21:25 +00:00
|
|
|
// SyncWithConsul is a long lived function that performs calls to sync
|
|
|
|
// checks and services periodically with Consul Agent
|
2015-11-24 20:34:26 +00:00
|
|
|
func (c *ConsulService) SyncWithConsul() {
|
2015-11-18 12:34:23 +00:00
|
|
|
sync := time.After(syncInterval)
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-sync:
|
2015-11-26 09:03:16 +00:00
|
|
|
c.performSync()
|
2015-11-19 02:47:12 +00:00
|
|
|
sync = time.After(syncInterval)
|
2015-11-18 12:34:23 +00:00
|
|
|
case <-c.shutdownCh:
|
2015-12-11 00:04:04 +00:00
|
|
|
c.logger.Printf("[INFO] consul: shutting down consul service")
|
2015-11-18 12:34:23 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-26 08:21:25 +00:00
|
|
|
// performSync syncs checks and services with Consul and removed tracked
|
|
|
|
// services which are no longer present in tasks
|
2015-11-26 09:03:16 +00:00
|
|
|
func (c *ConsulService) performSync() {
|
2015-11-26 01:28:44 +00:00
|
|
|
// Get the list of the services and that Consul knows about
|
2015-12-11 18:08:57 +00:00
|
|
|
srvcs, err := c.client.Services()
|
2015-12-11 21:10:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2015-12-11 18:08:57 +00:00
|
|
|
chks, err := c.client.Checks()
|
2015-12-11 21:10:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2015-12-11 18:08:57 +00:00
|
|
|
|
|
|
|
// Filter the services and checks that isn't managed by consul
|
|
|
|
consulServices := c.filterConsulServices(srvcs)
|
|
|
|
consulChecks := c.filterConsulChecks(chks)
|
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{})
|
|
|
|
|
2016-03-02 21:57:08 +00:00
|
|
|
c.trackedTaskLock.Lock()
|
|
|
|
tasks := make([]*trackedTask, 0, len(c.trackedTasks))
|
2015-11-25 01:26:30 +00:00
|
|
|
for _, trackedTask := range c.trackedTasks {
|
2016-03-02 21:57:08 +00:00
|
|
|
tasks = append(tasks, trackedTask)
|
|
|
|
}
|
|
|
|
c.trackedTaskLock.Unlock()
|
|
|
|
|
|
|
|
// Add services and checks which Consul doesn't know about
|
|
|
|
for _, trackedTask := range tasks {
|
2015-11-25 01:26:30 +00:00
|
|
|
for _, service := range trackedTask.task.Services {
|
2015-12-14 23:57:56 +00:00
|
|
|
serviceID := trackedTask.alloc.Services[service.Name]
|
2015-11-26 08:21:25 +00:00
|
|
|
|
|
|
|
// Add new services which Consul agent isn't aware of
|
2015-12-14 23:57:56 +00:00
|
|
|
knownServices[serviceID] = struct{}{}
|
|
|
|
if _, ok := consulServices[serviceID]; !ok {
|
2016-02-05 20:07:56 +00:00
|
|
|
c.printLogMessage("[INFO] consul: perform sync, registering service %s with consul.", service.Name)
|
2015-12-14 22:53:49 +00:00
|
|
|
c.registerService(service, trackedTask.task, trackedTask.alloc)
|
2015-11-26 01:28:44 +00:00
|
|
|
continue
|
2015-11-25 01:26:30 +00:00
|
|
|
}
|
|
|
|
|
2015-11-26 08:21:25 +00:00
|
|
|
// If a service has changed, re-register it with Consul agent
|
2015-12-14 23:57:56 +00:00
|
|
|
if service.Hash() != c.serviceStates[serviceID] {
|
2016-02-05 20:07:56 +00:00
|
|
|
c.printLogMessage("[INFO] consul: perform sync hash change, reregistering service %s with consul.", service.Name)
|
2015-12-14 22:53:49 +00:00
|
|
|
c.registerService(service, trackedTask.task, trackedTask.alloc)
|
2015-11-26 01:28:44 +00:00
|
|
|
continue
|
|
|
|
}
|
2015-11-26 08:21:25 +00:00
|
|
|
|
|
|
|
// Add new checks that Consul isn't aware of
|
2015-11-26 01:28:44 +00:00
|
|
|
for _, check := range service.Checks {
|
2015-12-14 23:57:56 +00:00
|
|
|
checkID := check.Hash(serviceID)
|
|
|
|
knownChecks[checkID] = struct{}{}
|
|
|
|
if _, ok := consulChecks[checkID]; !ok {
|
2015-11-26 01:28:44 +00:00
|
|
|
host, port := trackedTask.task.FindHostAndPortFor(service.PortLabel)
|
2015-12-14 23:57:56 +00:00
|
|
|
cr := c.makeCheck(serviceID, check, host, port)
|
2016-01-08 01:51:57 +00:00
|
|
|
if err := c.registerCheck(cr); err != nil {
|
|
|
|
c.printLogMessage("[DEBUG] consul: error registering check %q: %v", cr.ID, err)
|
|
|
|
}
|
|
|
|
|
2015-11-26 01:28:44 +00:00
|
|
|
}
|
2015-11-24 22:37:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-26 08:21:25 +00:00
|
|
|
// Remove services from the service tracker which no longer exists
|
2015-12-14 23:57:56 +00:00
|
|
|
for serviceID := range c.serviceStates {
|
|
|
|
if _, ok := knownServices[serviceID]; !ok {
|
|
|
|
delete(c.serviceStates, serviceID)
|
2015-11-26 08:21:25 +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)
|
2016-02-05 20:07:56 +00:00
|
|
|
c.printLogMessage("[INFO] consul: perform sync, deregistering service %v with consul", consulService.Service)
|
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-24 22:37:14 +00:00
|
|
|
}
|
|
|
|
|
2015-11-26 08:21:25 +00:00
|
|
|
// registerService registers a Service with Consul
|
2015-12-14 22:53:49 +00:00
|
|
|
func (c *ConsulService) registerService(service *structs.Service, task *structs.Task, alloc *structs.Allocation) error {
|
2015-11-18 12:34:23 +00:00
|
|
|
var mErr multierror.Error
|
2015-11-25 02:39:38 +00:00
|
|
|
host, port := task.FindHostAndPortFor(service.PortLabel)
|
2015-11-18 12:34:23 +00:00
|
|
|
if host == "" || port == 0 {
|
2015-12-11 00:47:43 +00:00
|
|
|
return fmt.Errorf("consul: the port:%q marked for registration of service: %q couldn't be found", service.PortLabel, service.Name)
|
2015-11-18 12:34:23 +00:00
|
|
|
}
|
2015-12-14 23:57:56 +00:00
|
|
|
serviceID := alloc.Services[service.Name]
|
|
|
|
c.serviceStates[serviceID] = service.Hash()
|
2015-11-19 02:35:22 +00:00
|
|
|
|
2015-11-25 02:58:53 +00:00
|
|
|
asr := &consul.AgentServiceRegistration{
|
2015-12-14 23:57:56 +00:00
|
|
|
ID: serviceID,
|
2015-11-25 02:58:53 +00:00
|
|
|
Name: service.Name,
|
|
|
|
Tags: service.Tags,
|
|
|
|
Port: port,
|
|
|
|
Address: host,
|
|
|
|
}
|
|
|
|
|
2015-11-26 09:03:16 +00:00
|
|
|
if err := c.client.ServiceRegister(asr); err != nil {
|
2015-12-11 17:22:47 +00:00
|
|
|
c.printLogMessage("[DEBUG] consul: error while registering service %v with consul: %v", service.Name, err)
|
2015-11-18 12:34:23 +00:00
|
|
|
mErr.Errors = append(mErr.Errors, err)
|
|
|
|
}
|
2015-11-26 01:28:44 +00:00
|
|
|
for _, check := range service.Checks {
|
2015-12-14 23:57:56 +00:00
|
|
|
cr := c.makeCheck(serviceID, check, host, port)
|
2015-11-26 01:28:44 +00:00
|
|
|
if err := c.registerCheck(cr); err != nil {
|
2016-01-27 15:49:32 +00:00
|
|
|
c.printLogMessage("[DEBUG] consul: error while registering check %v with consul: %v", check.Name, err)
|
2015-11-23 07:27:59 +00:00
|
|
|
mErr.Errors = append(mErr.Errors, err)
|
|
|
|
}
|
2015-11-26 01:28:44 +00:00
|
|
|
|
2015-11-23 07:27:59 +00:00
|
|
|
}
|
2015-11-18 12:34:23 +00:00
|
|
|
return mErr.ErrorOrNil()
|
|
|
|
}
|
|
|
|
|
2015-11-26 08:21:25 +00:00
|
|
|
// registerCheck registers a check with Consul
|
2015-11-25 02:39:38 +00:00
|
|
|
func (c *ConsulService) registerCheck(check *consul.AgentCheckRegistration) error {
|
2015-12-11 23:45:49 +00:00
|
|
|
c.printLogMessage("[INFO] consul: registering check with ID: %s for service: %s", check.ID, check.ServiceID)
|
2015-11-26 09:03:16 +00:00
|
|
|
return c.client.CheckRegister(check)
|
2015-11-25 02:39:38 +00:00
|
|
|
}
|
|
|
|
|
2015-11-26 08:21:25 +00:00
|
|
|
// deregisterCheck de-registers a check with a specific ID from Consul
|
2015-11-25 02:39:38 +00:00
|
|
|
func (c *ConsulService) deregisterCheck(checkID string) error {
|
2015-12-11 17:22:47 +00:00
|
|
|
c.printLogMessage("[INFO] consul: removing check with ID: %v", checkID)
|
2015-11-26 09:03:16 +00:00
|
|
|
return c.client.CheckDeregister(checkID)
|
2015-11-25 02:39:38 +00:00
|
|
|
}
|
|
|
|
|
2015-11-26 08:21:25 +00:00
|
|
|
// deregisterService de-registers a Service with a specific id from Consul
|
2015-12-14 23:57:56 +00:00
|
|
|
func (c *ConsulService) deregisterService(serviceID string) error {
|
|
|
|
delete(c.serviceStates, serviceID)
|
|
|
|
if err := c.client.ServiceDeregister(serviceID); err != nil {
|
2015-11-18 12:34:23 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-11-26 08:21:25 +00:00
|
|
|
// makeCheck creates a Consul Check Registration struct
|
2015-12-14 23:57:56 +00:00
|
|
|
func (c *ConsulService) makeCheck(serviceID string, check *structs.ServiceCheck, ip string, port int) *consul.AgentCheckRegistration {
|
|
|
|
checkID := check.Hash(serviceID)
|
2015-11-26 01:28:44 +00:00
|
|
|
cr := &consul.AgentCheckRegistration{
|
2015-12-14 23:57:56 +00:00
|
|
|
ID: checkID,
|
2015-11-26 01:28:44 +00:00
|
|
|
Name: check.Name,
|
2015-12-14 23:57:56 +00:00
|
|
|
ServiceID: serviceID,
|
2015-11-26 01:28:44 +00:00
|
|
|
}
|
|
|
|
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
|
|
|
}
|
2015-12-09 23:24:44 +00:00
|
|
|
|
2015-12-11 18:08:57 +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)
|
|
|
|
delete(srvcs, "consul")
|
|
|
|
for _, srv := range srvcs {
|
2015-12-11 22:02:09 +00:00
|
|
|
if strings.HasPrefix(srv.ID, structs.NomadConsulPrefix) {
|
2015-12-11 18:08:57 +00:00
|
|
|
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 {
|
2015-12-11 22:02:09 +00:00
|
|
|
if strings.HasPrefix(chk.ServiceID, structs.NomadConsulPrefix) {
|
2015-12-11 18:08:57 +00:00
|
|
|
nomadChecks[chk.CheckID] = chk
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nomadChecks
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// printLogMessage prints log messages only when the node attributes have consul
|
|
|
|
// related information
|
2015-12-11 17:22:47 +00:00
|
|
|
func (c *ConsulService) printLogMessage(message string, v ...interface{}) {
|
2015-12-09 23:24:44 +00:00
|
|
|
if _, ok := c.node.Attributes["consul.version"]; ok {
|
2015-12-11 23:45:49 +00:00
|
|
|
c.logger.Println(fmt.Sprintf(message, v...))
|
2015-12-09 23:24:44 +00:00
|
|
|
}
|
|
|
|
}
|