From e27caadca6ed14bf67318098b658377778f50157 Mon Sep 17 00:00:00 2001 From: Preetha Appan Date: Fri, 1 Jun 2018 14:48:25 -0500 Subject: [PATCH] Fix unnecessary deregistration in consul sync This commit fixes an issue where if a nomad client and server shared the same consul instance, the server would deregister any services and checks registered by clients for running tasks. --- command/agent/consul/client.go | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/command/agent/consul/client.go b/command/agent/consul/client.go index 6a0c789dc..6d5614021 100644 --- a/command/agent/consul/client.go +++ b/command/agent/consul/client.go @@ -232,6 +232,10 @@ type ServiceClient struct { // checkWatcher restarts checks that are unhealthy. checkWatcher *checkWatcher + + // agentRoleLock guards state about whether this agent is a client + agentRoleLock sync.Mutex + isClientAgent bool } // NewServiceClient creates a new Consul ServiceClient from an existing Consul API @@ -433,7 +437,7 @@ func (c *ServiceClient) sync() error { // Known service, skip continue } - if !isNomadService(id) { + if !isNomadService(id) || !c.IsClient() { // Not managed by Nomad, skip continue } @@ -470,7 +474,7 @@ func (c *ServiceClient) sync() error { // Known check, leave it continue } - if !isNomadService(check.ServiceID) { + if !isNomadService(check.ServiceID) || !c.IsClient() { // Service not managed by Nomad, skip continue } @@ -519,6 +523,12 @@ func (c *ServiceClient) sync() error { return nil } +func (c *ServiceClient) IsClient() bool { + c.agentRoleLock.Lock() + defer c.agentRoleLock.Unlock() + return c.isClientAgent +} + // RegisterAgent registers Nomad agents (client or server). The // Service.PortLabel should be a literal port to be parsed with SplitHostPort. // Script checks are not supported and will return an error. Registration is @@ -528,6 +538,12 @@ func (c *ServiceClient) sync() error { func (c *ServiceClient) RegisterAgent(role string, services []*structs.Service) error { ops := operations{} + if role == "client" { + c.agentRoleLock.Lock() + c.isClientAgent = true + c.agentRoleLock.Unlock() + } + for _, service := range services { id := makeAgentServiceID(role, service)