Merge pull request #1628 from hashicorp/fix-consul-registrations

Fixed re-registrations and health-check flatenning issue
This commit is contained in:
Vishal Nayak 2016-07-19 14:18:15 -04:00 committed by GitHub
commit 57a8e4d3a2
2 changed files with 22 additions and 22 deletions

View File

@ -513,7 +513,13 @@ shutdown:
// checkID returns the ID used for a Consul Check. Assume at least a read // checkID returns the ID used for a Consul Check. Assume at least a read
// lock is held. // lock is held.
func (c *ConsulBackend) checkID() string { func (c *ConsulBackend) checkID() string {
return "vault-sealed-check" return fmt.Sprintf("%s:vault-sealed-check", c.serviceID())
}
// serviceID returns the Vault ServiceID for use in Consul. Assume at least
// a read lock is held.
func (c *ConsulBackend) serviceID() string {
return fmt.Sprintf("%s:%s:%d", c.serviceName, c.advertiseHost, c.advertisePort)
} }
// reconcileConsul queries the state of Vault Core and Consul and fixes up // reconcileConsul queries the state of Vault Core and Consul and fixes up
@ -527,16 +533,21 @@ func (c *ConsulBackend) reconcileConsul(registeredServiceID string, activeFunc a
sealed := sealedFunc() sealed := sealedFunc()
agent := c.client.Agent() agent := c.client.Agent()
catalog := c.client.Catalog()
serviceID = c.serviceID()
// Get the current state of Vault from Consul // Get the current state of Vault from Consul
var currentVaultService *api.AgentService var currentVaultService *api.CatalogService
if services, err := agent.Services(); err == nil { if services, _, err := catalog.Service(c.serviceName, "", &api.QueryOptions{AllowStale: true}); err == nil {
if service, ok := services[c.serviceName]; ok { for _, service := range services {
currentVaultService = service if serviceID == service.ServiceID {
currentVaultService = service
break
}
} }
} }
serviceID = c.serviceID()
tags := serviceTags(active) tags := serviceTags(active)
var reregister bool var reregister bool
@ -546,14 +557,16 @@ func (c *ConsulBackend) reconcileConsul(registeredServiceID string, activeFunc a
reregister = true reregister = true
default: default:
switch { switch {
case len(currentVaultService.Tags) != 1, case len(currentVaultService.ServiceTags) != 1,
currentVaultService.Tags[0] != tags[0]: currentVaultService.ServiceTags[0] != tags[0]:
reregister = true reregister = true
} }
} }
if !reregister { if !reregister {
return "", nil // When re-registration is not required, return a valid serviceID
// to avoid registration in the next cycle.
return serviceID, nil
} }
service := &api.AgentServiceRegistration{ service := &api.AgentServiceRegistration{
@ -603,12 +616,6 @@ func (c *ConsulBackend) runCheck(sealed bool) error {
} }
} }
// serviceID returns the Vault ServiceID for use in Consul. Assume at least
// a read lock is held.
func (c *ConsulBackend) serviceID() string {
return fmt.Sprintf("%s:%s:%d", c.serviceName, c.advertiseHost, c.advertisePort)
}
// serviceTags returns all of the relevant tags for Consul. // serviceTags returns all of the relevant tags for Consul.
func serviceTags(active bool) []string { func serviceTags(active bool) []string {
activeTag := "standby" activeTag := "standby"

View File

@ -295,13 +295,6 @@ func TestConsul_NotifySealedStateChange(t *testing.T) {
} }
} }
func TestConsul_checkID(t *testing.T) {
c := testConsulBackend(t)
if c.checkID() != "vault-sealed-check" {
t.Errorf("bad")
}
}
func TestConsul_serviceID(t *testing.T) { func TestConsul_serviceID(t *testing.T) {
passingTests := []struct { passingTests := []struct {
name string name string