Skip to content

Commit

Permalink
Fix unnecessary deregistration in consul sync
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Preetha Appan committed Jun 1, 2018
1 parent 5b9b43d commit e27caad
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions command/agent/consul/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand All @@ -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)

Expand Down

0 comments on commit e27caad

Please sign in to comment.