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.
This commit is contained in:
Preetha Appan
2018-06-01 14:48:25 -05:00
parent 5b9b43d81c
commit e27caadca6

View File

@@ -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)