removing ids of services and checks on the server side

This commit is contained in:
Diptanu Choudhury 2016-03-23 17:12:53 -07:00
parent 66e35b0b96
commit 83f41993aa
2 changed files with 15 additions and 14 deletions

View file

@ -117,13 +117,14 @@ func (c *ConsulService) SyncTask(task *structs.Task) error {
services[srv.ID] = srv services[srv.ID] = srv
for _, chk := range service.Checks { for _, chk := range service.Checks {
if _, ok := c.checks[chk.ID]; !ok { checkID := chk.Hash(srv.ID)
if _, ok := c.checks[checkID]; !ok {
if err := c.registerCheck(chk, srv); err != nil { if err := c.registerCheck(chk, srv); err != nil {
mErr.Errors = append(mErr.Errors, err) mErr.Errors = append(mErr.Errors, err)
} }
} }
c.checks[chk.ID] = chk c.checks[checkID] = chk
checks[chk.ID] = chk checks[checkID] = chk
} }
} }
@ -138,12 +139,12 @@ func (c *ConsulService) SyncTask(task *structs.Task) error {
} }
// Remove the checks that are not present anymore // Remove the checks that are not present anymore
for _, check := range c.checks { for checkID, _ := range c.checks {
if _, ok := checks[check.ID]; !ok { if _, ok := checks[checkID]; !ok {
if err := c.deregisterCheck(check.ID); err != nil { if err := c.deregisterCheck(checkID); err != nil {
mErr.Errors = append(mErr.Errors, err) mErr.Errors = append(mErr.Errors, err)
} }
delete(c.checks, check.ID) delete(c.checks, checkID)
} }
} }
return mErr.ErrorOrNil() return mErr.ErrorOrNil()
@ -169,7 +170,7 @@ func (c *ConsulService) RemoveServices(tasks []*structs.Task) error {
var services map[string]struct{} var services map[string]struct{}
for _, task := range tasks { for _, task := range tasks {
for _, service := range task.Services { for _, service := range task.Services {
services[service.ID] = struct{}{} services[service.ID()] = struct{}{}
} }
} }
@ -192,7 +193,7 @@ func (c *ConsulService) RemoveServices(tasks []*structs.Task) error {
// registerCheck registers a check definition with Consul // registerCheck registers a check definition with Consul
func (c *ConsulService) registerCheck(check *structs.ServiceCheck, service *consul.AgentService) error { func (c *ConsulService) registerCheck(check *structs.ServiceCheck, service *consul.AgentService) error {
chkReg := consul.AgentCheckRegistration{ chkReg := consul.AgentCheckRegistration{
ID: check.ID, ID: check.Hash(service.ID),
Name: check.Name, Name: check.Name,
ServiceID: service.ID, ServiceID: service.ID,
} }
@ -228,7 +229,7 @@ func (c *ConsulService) createService(service *structs.Service) (*consul.AgentSe
return nil, fmt.Errorf("port for the service %q couldn't be found", service.Name) return nil, fmt.Errorf("port for the service %q couldn't be found", service.Name)
} }
srv := consul.AgentService{ srv := consul.AgentService{
ID: service.ID, ID: service.ID(),
Service: service.Name, Service: service.Name,
Tags: service.Tags, Tags: service.Tags,
Address: host, Address: host,

View file

@ -1418,7 +1418,6 @@ const (
// The ServiceCheck data model represents the consul health check that // The ServiceCheck data model represents the consul health check that
// Nomad registers for a Task // Nomad registers for a Task
type ServiceCheck struct { type ServiceCheck struct {
ID string
Name string // Name of the check, defaults to id Name string // Name of the check, defaults to id
Type string // Type of the check - tcp, http, docker and script Type string // Type of the check - tcp, http, docker and script
Script string // Script to invoke for script check Script string // Script to invoke for script check
@ -1476,7 +1475,6 @@ const (
// The Service model represents a Consul service defintion // The Service model represents a Consul service defintion
type Service struct { type Service struct {
ID string // ID of the service
Name string // Name of the service, defaults to id Name string // Name of the service, defaults to id
Tags []string // List of tags for the service Tags []string // List of tags for the service
PortLabel string `mapstructure:"port"` // port for the service PortLabel string `mapstructure:"port"` // port for the service
@ -1512,16 +1510,18 @@ func (s *Service) InitFields(job string, taskGroup string, task string) {
"BASE": fmt.Sprintf("%s-%s-%s", job, taskGroup, task), "BASE": fmt.Sprintf("%s-%s-%s", job, taskGroup, task),
}, },
) )
s.ID = fmt.Sprintf("%s-%s", NomadConsulPrefix, s.Hash())
for _, check := range s.Checks { for _, check := range s.Checks {
if check.Name == "" { if check.Name == "" {
check.Name = fmt.Sprintf("service: %q check", s.Name) check.Name = fmt.Sprintf("service: %q check", s.Name)
} }
check.ID = check.Hash(s.ID)
} }
} }
func (s *Service) ID() string {
return fmt.Sprintf("%s-%s", NomadConsulPrefix, s.Hash())
}
// Validate checks if the Check definition is valid // Validate checks if the Check definition is valid
func (s *Service) Validate() error { func (s *Service) Validate() error {
var mErr multierror.Error var mErr multierror.Error