Skip to content

Commit

Permalink
Fix old services not getting removed from consul on update
Browse files Browse the repository at this point in the history
Fixes #1661
  • Loading branch information
schmichael committed Aug 30, 2016
1 parent 53d7dfb commit a63b2cc
Show file tree
Hide file tree
Showing 2 changed files with 180 additions and 150 deletions.
49 changes: 38 additions & 11 deletions command/agent/consul/syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,8 @@ func NewSyncer(consulConfig *config.ConsulConfig, shutdownCh chan struct{}, logg
trackedChecks: make(map[consulCheckID]*consul.AgentCheckRegistration),
checkRunners: make(map[consulCheckID]*CheckRunner),
periodicCallbacks: make(map[string]types.PeriodicCallback),
// default noop implementation of addrFinder
addrFinder: func(string) (string, int) { return "", 0 },
}

return &consulSyncer, nil
Expand Down Expand Up @@ -264,22 +266,47 @@ func (c *Syncer) SetServices(domain ServiceDomain, services map[ServiceKey]*stru
return mErr.ErrorOrNil()
}

// Update the services and checks groups for this domain
c.groupsLock.Lock()
for serviceKey, service := range registeredServices {
serviceKeys, ok := c.servicesGroups[domain]
if !ok {
serviceKeys = make(map[ServiceKey]*consul.AgentServiceRegistration, len(registeredServices))
c.servicesGroups[domain] = serviceKeys

// Create map for service group if it doesn't exist
serviceKeys, ok := c.servicesGroups[domain]
if !ok {
serviceKeys = make(map[ServiceKey]*consul.AgentServiceRegistration, len(registeredServices))
c.servicesGroups[domain] = serviceKeys
}

// Remove stale services
for existingServiceKey := range serviceKeys {
if _, ok := registeredServices[existingServiceKey]; !ok {
// Exisitng service needs to be removed
delete(serviceKeys, existingServiceKey)
}
}

// Add registered services
for serviceKey, service := range registeredServices {
serviceKeys[serviceKey] = service
}
for serviceKey, checks := range registeredChecks {
serviceKeys, ok := c.checkGroups[domain]
if !ok {
serviceKeys = make(map[ServiceKey][]*consul.AgentCheckRegistration, len(registeredChecks))
c.checkGroups[domain] = serviceKeys

// Create map for check group if it doesn't exist
checkKeys, ok := c.checkGroups[domain]
if !ok {
checkKeys = make(map[ServiceKey][]*consul.AgentCheckRegistration, len(registeredChecks))
c.checkGroups[domain] = checkKeys
}

// Remove stale checks
for existingCheckKey := range checkKeys {
if _, ok := registeredChecks[existingCheckKey]; !ok {
// Exisitng check needs to be removed
delete(checkKeys, existingCheckKey)
}
serviceKeys[serviceKey] = checks
}

// Add registered checks
for checkKey, checks := range registeredChecks {
checkKeys[checkKey] = checks
}
c.groupsLock.Unlock()

Expand Down
Loading

0 comments on commit a63b2cc

Please sign in to comment.