Files
nomad/command/agent/consul/namespaces_client.go
Seth Hoenig 0bc8a33084 consul: probe consul namespace feature before using namespace api
This PR changes Nomad's wrapper around the Consul NamespaceAPI so that
it will detect if the Consul Namespaces feature is enabled before making
a request to the Namespaces API. Namespaces are not enabled in Consul OSS,
and require a suitable license to be used with Consul ENT.

Previously Nomad would check for a 404 status code when makeing a request
to the Namespaces API to "detect" if Consul OSS was being used. This does
not work for Consul ENT with Namespaces disabled, which returns a 500.

Now we avoid requesting the namespace API altogether if Consul is detected
to be the OSS sku, or if the Namespaces feature is not licensed. Since
Consul can be upgraded from OSS to ENT, or a new license applied, we cache
the value for 1 minute, refreshing on demand if expired.

Fixes https://github.com/hashicorp/nomad-enterprise/issues/575

Note that the ticket originally describes using attributes from https://github.com/hashicorp/nomad/issues/10688.
This turns out not to be possible due to a chicken-egg situation between
bootstrapping the agent and setting up the consul client. Also fun: the
Consul fingerprinter creates its own Consul client, because there is no
[currently] no way to pass the agent's client through the fingerprint factory.
2021-06-07 12:19:25 -05:00

96 lines
2.2 KiB
Go

package consul
import (
"sort"
"sync"
"time"
)
const (
// namespaceEnabledCacheTTL is how long to cache the response from Consul
// /v1/agent/self API, which is used to determine whether namespaces are
// available.
namespaceEnabledCacheTTL = 1 * time.Minute
)
// NamespacesClient is a wrapper for the Consul NamespacesAPI, that is used to
// deal with Consul OSS vs Consul Enterprise behavior in listing namespaces.
type NamespacesClient struct {
namespacesAPI NamespaceAPI
agentAPI AgentAPI
lock sync.Mutex
enabled bool // namespaces requires Ent + Namespaces feature
updated time.Time // memoize response for a while
}
// NewNamespacesClient returns a NamespacesClient backed by a NamespaceAPI.
func NewNamespacesClient(namespacesAPI NamespaceAPI, agentAPI AgentAPI) *NamespacesClient {
return &NamespacesClient{
namespacesAPI: namespacesAPI,
agentAPI: agentAPI,
}
}
func stale(updated, now time.Time) bool {
return now.After(updated.Add(namespaceEnabledCacheTTL))
}
func (ns *NamespacesClient) allowable(now time.Time) bool {
ns.lock.Lock()
defer ns.lock.Unlock()
if !stale(ns.updated, now) {
return ns.enabled
}
self, err := ns.agentAPI.Self()
if err != nil {
return ns.enabled
}
sku, ok := SKU(self)
if !ok {
return ns.enabled
}
if sku != "ent" {
ns.enabled = false
ns.updated = now
return ns.enabled
}
enabledStr, ok := Namespaces(self)
if !ok {
return ns.enabled
}
ns.enabled = enabledStr == "true"
ns.updated = now
return ns.enabled
}
// List returns a list of Consul Namespaces.
//
// TODO(shoenig): return empty string instead of "default" when namespaces are not
// enabled. (Coming in followup PR).
func (ns *NamespacesClient) List() ([]string, error) {
if !ns.allowable(time.Now()) {
// TODO(shoenig): lets return the empty string instead, that way we do not
// need to normalize at call sites later on
return []string{"default"}, nil
}
namespaces, _, err := ns.namespacesAPI.List(nil)
if err != nil {
return nil, err
}
result := make([]string, 0, len(namespaces))
for _, namespace := range namespaces {
result = append(result, namespace.Name)
}
sort.Strings(result)
return result, nil
}