Skip to content

Commit

Permalink
consul: allow stale namespace results
Browse files Browse the repository at this point in the history
Nomad reconciles services it expects to be registered in Consul with
what is actually registered in the local Consul agent. This is necessary
to prevent leaking service registrations if Nomad crashes at certain
points (or if there are bugs).

When Consul has namespaces enabled, we must iterate over each available
namespace to be sure no services were leaked into non-default
namespaces.

Since this reconciliation happens often, there's no need to require
results from the Consul leader server. In large clusters this creates
far more load than the "freshness" of the response is worth.

Therefore this patch switches the request to AllowStale=true
  • Loading branch information
schmichael committed May 11, 2022
1 parent e7924e3 commit 7bf77c5
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions command/agent/consul/namespaces_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"sort"
"sync"
"time"

"github.com/hashicorp/consul/api"
)

const (
Expand Down Expand Up @@ -66,17 +68,17 @@ func (ns *NamespacesClient) allowable(now time.Time) bool {
}

// 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)
qo := &api.QueryOptions{
AllowStale: true,
}
namespaces, _, err := ns.namespacesAPI.List(qo)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 7bf77c5

Please sign in to comment.