Using a helper method to create service identifiers

This commit is contained in:
Diptanu Choudhury 2016-05-14 00:43:25 -07:00
parent ecd995b283
commit 2e2e2e500e
4 changed files with 14 additions and 10 deletions

View file

@ -412,7 +412,7 @@ func (c *ConsulService) filterConsulServices(srvcs map[string]*consul.AgentServi
nomadServices := make(map[string]*consul.AgentService)
for _, srv := range srvcs {
if strings.HasPrefix(srv.ID, structs.NomadConsulPrefix) &&
!strings.HasPrefix(srv.ID, fmt.Sprintf("%s-%s", structs.NomadConsulPrefix, "agent")) {
!strings.HasPrefix(srv.ID, structs.AgentServicePrefix) {
nomadServices[srv.ID] = srv
}
}
@ -467,8 +467,8 @@ func (c *ConsulService) runCheck(check Check) {
}
}
// generateServiceIdentifier returns a service identifier based on an allocation
// GenerateServiceIdentifier returns a service identifier based on an allocation
// id and task name
func generateServiceIdentifier(allocID string, taskName string) string {
func GenerateServiceIdentifier(allocID string, taskName string) string {
return fmt.Sprintf("%s-%s", taskName, allocID)
}

View file

@ -50,15 +50,15 @@ func TestConsulServiceRegisterServices(t *testing.T) {
return
}
task := mockTask()
cs.SetServiceIdentifier(fmt.Sprintf("%s-%s", allocID, task.Name))
cs.SetServiceIdentifier(GenerateServiceIdentifier(allocID, task.Name))
cs.SetAddrFinder(task.FindHostAndPortFor)
if err := cs.SyncServices(task.Services); err != nil {
t.Fatalf("err: %v", err)
}
defer cs.Shutdown()
service1ID := service1.ID(fmt.Sprintf("%s-%s", allocID, task.Name))
service2ID := service2.ID(fmt.Sprintf("%s-%s", allocID, task.Name))
service1ID := service1.ID(GenerateServiceIdentifier(allocID, task.Name))
service2ID := service2.ID(GenerateServiceIdentifier(allocID, task.Name))
if err := servicesPresent(t, []string{service1ID, service2ID}, cs); err != nil {
t.Fatalf("err : %v", err)
}
@ -78,7 +78,7 @@ func TestConsulServiceUpdateService(t *testing.T) {
}
task := mockTask()
cs.SetServiceIdentifier(fmt.Sprintf("%s-%s", allocID, task.Name))
cs.SetServiceIdentifier(GenerateServiceIdentifier(allocID, task.Name))
cs.SetAddrFinder(task.FindHostAndPortFor)
if err := cs.SyncServices(task.Services); err != nil {
t.Fatalf("err: %v", err)
@ -92,8 +92,8 @@ func TestConsulServiceUpdateService(t *testing.T) {
t.Fatalf("err: %v", err)
}
// Make sure all the services and checks are still present
service1ID := service1.ID(fmt.Sprintf("%s-%s", allocID, task.Name))
service2ID := service2.ID(fmt.Sprintf("%s-%s", allocID, task.Name))
service1ID := service1.ID(GenerateServiceIdentifier(allocID, task.Name))
service2ID := service2.ID(GenerateServiceIdentifier(allocID, task.Name))
if err := servicesPresent(t, []string{service1ID, service2ID}, cs); err != nil {
t.Fatalf("err : %v", err)
}

View file

@ -436,7 +436,7 @@ func (e *UniversalExecutor) SyncServices(ctx *ConsulContext) error {
return err
}
cs.SetDelegatedChecks(e.createCheckMap(), e.createCheck)
cs.SetServiceIdentifier(fmt.Sprintf("%s-%s", e.ctx.AllocID, e.ctx.Task.Name))
cs.SetServiceIdentifier(consul.GenerateServiceIdentifier(e.ctx.AllocID, e.ctx.Task.Name))
cs.SetAddrFinder(e.ctx.Task.FindHostAndPortFor)
e.consulService = cs
}

View file

@ -1474,6 +1474,10 @@ const (
NomadConsulPrefix = "nomad-registered-service"
)
var (
AgentServicePrefix = fmt.Sprintf("%s-%s", NomadConsulPrefix, "agent")
)
// The Service model represents a Consul service defintion
type Service struct {
Name string // Name of the service, defaults to id