Added service-tags config option to provide additional tags to registered service
This commit is contained in:
parent
e4e2eec14b
commit
765d131b47
|
@ -30,6 +30,7 @@ func StrListSubset(super, sub []string) bool {
|
||||||
// The return slice will be sorted and will not contain duplicate or
|
// The return slice will be sorted and will not contain duplicate or
|
||||||
// empty items. The values will be converted to lower case.
|
// empty items. The values will be converted to lower case.
|
||||||
func ParseStrings(input string) []string {
|
func ParseStrings(input string) []string {
|
||||||
|
input = strings.TrimSpace(input)
|
||||||
var parsed []string
|
var parsed []string
|
||||||
if input == "" {
|
if input == "" {
|
||||||
// Don't return nil
|
// Don't return nil
|
||||||
|
|
|
@ -20,6 +20,7 @@ import (
|
||||||
"github.com/hashicorp/consul/lib"
|
"github.com/hashicorp/consul/lib"
|
||||||
"github.com/hashicorp/errwrap"
|
"github.com/hashicorp/errwrap"
|
||||||
"github.com/hashicorp/go-cleanhttp"
|
"github.com/hashicorp/go-cleanhttp"
|
||||||
|
"github.com/hashicorp/vault/helper/strutil"
|
||||||
"github.com/hashicorp/vault/helper/tlsutil"
|
"github.com/hashicorp/vault/helper/tlsutil"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -62,6 +63,7 @@ type ConsulBackend struct {
|
||||||
advertiseHost string
|
advertiseHost string
|
||||||
advertisePort int64
|
advertisePort int64
|
||||||
serviceName string
|
serviceName string
|
||||||
|
serviceTags []string
|
||||||
disableRegistration bool
|
disableRegistration bool
|
||||||
checkTimeout time.Duration
|
checkTimeout time.Duration
|
||||||
|
|
||||||
|
@ -108,6 +110,11 @@ func newConsulBackend(conf map[string]string, logger *log.Logger) (Backend, erro
|
||||||
}
|
}
|
||||||
logger.Printf("[DEBUG]: consul: config service set to %s", service)
|
logger.Printf("[DEBUG]: consul: config service set to %s", service)
|
||||||
|
|
||||||
|
// Get the additional tags to attach to the registered service name
|
||||||
|
tags := conf["service-tags"]
|
||||||
|
|
||||||
|
logger.Printf("[DEBUG]: consul: config service-tags set to %s", tags)
|
||||||
|
|
||||||
checkTimeout := defaultCheckTimeout
|
checkTimeout := defaultCheckTimeout
|
||||||
checkTimeoutStr, ok := conf["check_timeout"]
|
checkTimeoutStr, ok := conf["check_timeout"]
|
||||||
if ok {
|
if ok {
|
||||||
|
@ -130,11 +137,11 @@ func newConsulBackend(conf map[string]string, logger *log.Logger) (Backend, erro
|
||||||
|
|
||||||
if addr, ok := conf["address"]; ok {
|
if addr, ok := conf["address"]; ok {
|
||||||
consulConf.Address = addr
|
consulConf.Address = addr
|
||||||
logger.Printf("[DEBUG]: consul: config address set to %d", addr)
|
logger.Printf("[DEBUG]: consul: config address set to %s", addr)
|
||||||
}
|
}
|
||||||
if scheme, ok := conf["scheme"]; ok {
|
if scheme, ok := conf["scheme"]; ok {
|
||||||
consulConf.Scheme = scheme
|
consulConf.Scheme = scheme
|
||||||
logger.Printf("[DEBUG]: consul: config scheme set to %d", scheme)
|
logger.Printf("[DEBUG]: consul: config scheme set to %s", scheme)
|
||||||
}
|
}
|
||||||
if token, ok := conf["token"]; ok {
|
if token, ok := conf["token"]; ok {
|
||||||
consulConf.Token = token
|
consulConf.Token = token
|
||||||
|
@ -177,6 +184,7 @@ func newConsulBackend(conf map[string]string, logger *log.Logger) (Backend, erro
|
||||||
kv: client.KV(),
|
kv: client.KV(),
|
||||||
permitPool: NewPermitPool(maxParInt),
|
permitPool: NewPermitPool(maxParInt),
|
||||||
serviceName: service,
|
serviceName: service,
|
||||||
|
serviceTags: strutil.ParseStrings(tags),
|
||||||
checkTimeout: checkTimeout,
|
checkTimeout: checkTimeout,
|
||||||
disableRegistration: disableRegistration,
|
disableRegistration: disableRegistration,
|
||||||
}
|
}
|
||||||
|
@ -462,7 +470,7 @@ shutdown:
|
||||||
go func() {
|
go func() {
|
||||||
defer atomic.CompareAndSwapInt64(&serviceRegLock, 1, 0)
|
defer atomic.CompareAndSwapInt64(&serviceRegLock, 1, 0)
|
||||||
for !shutdown {
|
for !shutdown {
|
||||||
serviceID, err := c.reconcileConsul(registeredServiceID, activeFunc, sealedFunc)
|
serviceID, err := c.reconcileConsul(activeFunc, sealedFunc)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.logger.Printf("[WARN]: consul: reconcile unable to talk with Consul backend: %v", err)
|
c.logger.Printf("[WARN]: consul: reconcile unable to talk with Consul backend: %v", err)
|
||||||
time.Sleep(consulRetryInterval)
|
time.Sleep(consulRetryInterval)
|
||||||
|
@ -527,7 +535,7 @@ func (c *ConsulBackend) serviceID() string {
|
||||||
// without any locks held and can be run concurrently, therefore no changes
|
// without any locks held and can be run concurrently, therefore no changes
|
||||||
// to ConsulBackend can be made in this method (i.e. wtb const receiver for
|
// to ConsulBackend can be made in this method (i.e. wtb const receiver for
|
||||||
// compiler enforced safety).
|
// compiler enforced safety).
|
||||||
func (c *ConsulBackend) reconcileConsul(registeredServiceID string, activeFunc activeFunction, sealedFunc sealedFunction) (serviceID string, err error) {
|
func (c *ConsulBackend) reconcileConsul(activeFunc activeFunction, sealedFunc sealedFunction) (serviceID string, err error) {
|
||||||
// Query vault Core for its current state
|
// Query vault Core for its current state
|
||||||
active := activeFunc()
|
active := activeFunc()
|
||||||
sealed := sealedFunc()
|
sealed := sealedFunc()
|
||||||
|
@ -548,22 +556,9 @@ func (c *ConsulBackend) reconcileConsul(registeredServiceID string, activeFunc a
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
tags := serviceTags(active)
|
tags := c.fetchServiceTags(active)
|
||||||
|
|
||||||
var reregister bool
|
if currentVaultService != nil {
|
||||||
switch {
|
|
||||||
case currentVaultService == nil,
|
|
||||||
registeredServiceID == "":
|
|
||||||
reregister = true
|
|
||||||
default:
|
|
||||||
switch {
|
|
||||||
case len(currentVaultService.ServiceTags) != 1,
|
|
||||||
currentVaultService.ServiceTags[0] != tags[0]:
|
|
||||||
reregister = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if !reregister {
|
|
||||||
// When re-registration is not required, return a valid serviceID
|
// When re-registration is not required, return a valid serviceID
|
||||||
// to avoid registration in the next cycle.
|
// to avoid registration in the next cycle.
|
||||||
return serviceID, nil
|
return serviceID, nil
|
||||||
|
@ -616,13 +611,13 @@ func (c *ConsulBackend) runCheck(sealed bool) error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// serviceTags returns all of the relevant tags for Consul.
|
// fetchServiceTags returns all of the relevant tags for Consul.
|
||||||
func serviceTags(active bool) []string {
|
func (c *ConsulBackend) fetchServiceTags(active bool) []string {
|
||||||
activeTag := "standby"
|
activeTag := "standby"
|
||||||
if active {
|
if active {
|
||||||
activeTag = "active"
|
activeTag = "active"
|
||||||
}
|
}
|
||||||
return []string{activeTag}
|
return append(c.serviceTags, activeTag)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *ConsulBackend) setAdvertiseAddr(addr string) (err error) {
|
func (c *ConsulBackend) setAdvertiseAddr(addr string) (err error) {
|
||||||
|
|
|
@ -215,6 +215,9 @@ For Consul, the following options are supported:
|
||||||
* `service` (optional) - The name of the service to register with Consul.
|
* `service` (optional) - The name of the service to register with Consul.
|
||||||
Defaults to "vault".
|
Defaults to "vault".
|
||||||
|
|
||||||
|
* `service-tags` (optional) - Comma separated list of tags that are to be
|
||||||
|
applied to the service that gets registered with Consul.
|
||||||
|
|
||||||
* `token` (optional) - An access token to use to write data to Consul.
|
* `token` (optional) - An access token to use to write data to Consul.
|
||||||
|
|
||||||
* `max_parallel` (optional) - The maximum number of concurrent connections to Consul.
|
* `max_parallel` (optional) - The maximum number of concurrent connections to Consul.
|
||||||
|
|
Loading…
Reference in New Issue