2015-11-18 08:50:45 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/hashicorp/consul/api"
|
|
|
|
"github.com/hashicorp/go-multierror"
|
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
2015-11-18 10:14:07 +00:00
|
|
|
"log"
|
2015-11-18 08:50:45 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
consulPort = 8080
|
|
|
|
)
|
|
|
|
|
|
|
|
type ConsulClient struct {
|
|
|
|
client *api.Client
|
2015-11-18 10:14:07 +00:00
|
|
|
|
|
|
|
logger *log.Logger
|
2015-11-18 08:50:45 +00:00
|
|
|
}
|
|
|
|
|
2015-11-18 10:14:07 +00:00
|
|
|
func NewConsulClient(logger *log.Logger) (*ConsulClient, error) {
|
2015-11-18 08:50:45 +00:00
|
|
|
var err error
|
|
|
|
var c *api.Client
|
|
|
|
if c, err = api.NewClient(api.DefaultConfig()); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
consulClient := ConsulClient{
|
|
|
|
client: c,
|
2015-11-18 10:14:07 +00:00
|
|
|
logger: logger,
|
2015-11-18 08:50:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &consulClient, nil
|
|
|
|
}
|
|
|
|
|
2015-11-18 09:18:29 +00:00
|
|
|
func (c *ConsulClient) Register(task *structs.Task, allocID string) error {
|
2015-11-18 08:50:45 +00:00
|
|
|
var mErr multierror.Error
|
2015-11-18 09:18:29 +00:00
|
|
|
var serviceDefns []*api.AgentServiceRegistration
|
2015-11-18 10:37:34 +00:00
|
|
|
for _, service := range task.Services {
|
2015-11-18 08:50:45 +00:00
|
|
|
service.Id = fmt.Sprintf("%s-%s", allocID, task.Name)
|
2015-11-18 09:18:29 +00:00
|
|
|
host, port := c.findPortAndHostForLabel(service.PortLabel, task)
|
|
|
|
if host == "" || port == 0 {
|
|
|
|
continue
|
|
|
|
}
|
2015-11-18 08:50:45 +00:00
|
|
|
asr := &api.AgentServiceRegistration{
|
|
|
|
ID: service.Id,
|
|
|
|
Name: service.Name,
|
|
|
|
Tags: service.Tags,
|
|
|
|
Port: port,
|
|
|
|
Address: host,
|
|
|
|
}
|
2015-11-18 10:37:34 +00:00
|
|
|
serviceDefns = append(serviceDefns, asr)
|
2015-11-18 08:50:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, serviceDefn := range serviceDefns {
|
2015-11-18 10:37:34 +00:00
|
|
|
c.logger.Printf("[INFO] Registering service %v with Consul", serviceDefn.Name)
|
2015-11-18 08:50:45 +00:00
|
|
|
if err := c.client.Agent().ServiceRegister(serviceDefn); err != nil {
|
2015-11-18 10:37:34 +00:00
|
|
|
c.logger.Printf("[ERROR] Error while registering service %v with Consul: %v", serviceDefn.Name, err)
|
2015-11-18 08:50:45 +00:00
|
|
|
mErr.Errors = append(mErr.Errors, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return mErr.ErrorOrNil()
|
|
|
|
}
|
|
|
|
|
2015-11-18 10:37:34 +00:00
|
|
|
func (c *ConsulClient) Deregister(task *structs.Task) error {
|
2015-11-18 08:50:45 +00:00
|
|
|
var mErr multierror.Error
|
|
|
|
for _, service := range task.Services {
|
2015-11-18 10:37:34 +00:00
|
|
|
c.logger.Printf("[INFO] De-Registering service %v with Consul", service.Name)
|
2015-11-18 08:50:45 +00:00
|
|
|
if err := c.client.Agent().ServiceDeregister(service.Id); err != nil {
|
2015-11-18 10:37:34 +00:00
|
|
|
c.logger.Printf("[ERROR] Error in de-registering 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
|
|
|
|
|
|
|
func (c *ConsulClient) findPortAndHostForLabel(portLabel string, task *structs.Task) (string, int) {
|
|
|
|
for _, network := range task.Resources.Networks {
|
|
|
|
if p, ok := network.MapLabelToValues()[portLabel]; ok {
|
2015-11-18 09:20:53 +00:00
|
|
|
return network.IP, p
|
2015-11-18 09:18:29 +00:00
|
|
|
}
|
|
|
|
}
|
2015-11-18 09:20:53 +00:00
|
|
|
return "", 0
|
2015-11-18 09:18:29 +00:00
|
|
|
}
|