diff --git a/client/consul/sync.go b/client/consul/sync.go index 9713adc72..08da6e89d 100644 --- a/client/consul/sync.go +++ b/client/consul/sync.go @@ -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) } diff --git a/client/consul/sync_test.go b/client/consul/sync_test.go index d4fb5fad4..80907c787 100644 --- a/client/consul/sync_test.go +++ b/client/consul/sync_test.go @@ -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) } diff --git a/client/driver/executor/executor.go b/client/driver/executor/executor.go index 9552c2892..c95437dca 100644 --- a/client/driver/executor/executor.go +++ b/client/driver/executor/executor.go @@ -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 } diff --git a/nomad/structs/structs.go b/nomad/structs/structs.go index 7f3428b3e..f0bfc4510 100644 --- a/nomad/structs/structs.go +++ b/nomad/structs/structs.go @@ -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