2017-02-01 00:43:57 +00:00
|
|
|
package consul
|
|
|
|
|
|
|
|
import (
|
2017-09-13 06:15:46 +00:00
|
|
|
"fmt"
|
2021-03-16 18:22:21 +00:00
|
|
|
"sort"
|
2017-09-13 06:15:46 +00:00
|
|
|
"sync"
|
2017-02-01 00:43:57 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/consul/api"
|
2019-12-06 20:46:46 +00:00
|
|
|
"github.com/hashicorp/go-hclog"
|
|
|
|
"github.com/hashicorp/nomad/helper"
|
2017-02-01 00:43:57 +00:00
|
|
|
)
|
|
|
|
|
2021-03-16 18:22:21 +00:00
|
|
|
// MockNamespaces is a mock implementation of NamespaceAPI.
|
|
|
|
type MockNamespaces struct {
|
|
|
|
namespaces []*api.Namespace
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ NamespaceAPI = (*MockNamespaces)(nil)
|
|
|
|
|
|
|
|
// NewMockNamespaces creates a MockNamespaces with the given namespaces, and
|
|
|
|
// will automatically add the "default" namespace if not included.
|
|
|
|
func NewMockNamespaces(namespaces []string) *MockNamespaces {
|
|
|
|
list := helper.CopySliceString(namespaces)
|
|
|
|
if !helper.SliceStringContains(list, "default") {
|
|
|
|
list = append(list, "default")
|
|
|
|
}
|
|
|
|
sort.Strings(list)
|
|
|
|
|
|
|
|
data := make([]*api.Namespace, 0, len(list))
|
|
|
|
for _, namespace := range list {
|
|
|
|
data = append(data, &api.Namespace{
|
|
|
|
Name: namespace,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return &MockNamespaces{
|
|
|
|
namespaces: data,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// List implements NamespaceAPI
|
|
|
|
func (m *MockNamespaces) List(*api.QueryOptions) ([]*api.Namespace, *api.QueryMeta, error) {
|
|
|
|
result := make([]*api.Namespace, len(m.namespaces))
|
|
|
|
copy(result, m.namespaces)
|
|
|
|
return result, new(api.QueryMeta), nil
|
|
|
|
}
|
|
|
|
|
2017-02-01 00:43:57 +00:00
|
|
|
// MockCatalog can be used for testing where the CatalogAPI is needed.
|
|
|
|
type MockCatalog struct {
|
2019-12-06 20:46:46 +00:00
|
|
|
logger hclog.Logger
|
2017-02-01 00:43:57 +00:00
|
|
|
}
|
|
|
|
|
2021-03-16 18:22:21 +00:00
|
|
|
var _ CatalogAPI = (*MockCatalog)(nil)
|
|
|
|
|
2019-12-06 20:46:46 +00:00
|
|
|
func NewMockCatalog(l hclog.Logger) *MockCatalog {
|
|
|
|
return &MockCatalog{logger: l.Named("mock_consul")}
|
2017-02-01 00:43:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MockCatalog) Datacenters() ([]string, error) {
|
|
|
|
dcs := []string{"dc1"}
|
2018-09-13 17:43:40 +00:00
|
|
|
m.logger.Trace("Datacenters()", "dcs", dcs, "error", "nil")
|
2017-02-01 00:43:57 +00:00
|
|
|
return dcs, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MockCatalog) Service(service, tag string, q *api.QueryOptions) ([]*api.CatalogService, *api.QueryMeta, error) {
|
2018-09-13 17:43:40 +00:00
|
|
|
m.logger.Trace("Services()", "service", service, "tag", tag, "query_options", q)
|
2017-02-01 00:43:57 +00:00
|
|
|
return nil, nil, nil
|
|
|
|
}
|
2017-09-13 06:15:46 +00:00
|
|
|
|
|
|
|
// MockAgent is a fake in-memory Consul backend for ServiceClient.
|
|
|
|
type MockAgent struct {
|
2021-03-16 18:22:21 +00:00
|
|
|
// services tracks what services have been registered, per namespace
|
|
|
|
services map[string]map[string]*api.AgentServiceRegistration
|
|
|
|
|
|
|
|
// checks tracks what checks have been registered, per namespace
|
|
|
|
checks map[string]map[string]*api.AgentCheckRegistration
|
2018-04-17 19:36:50 +00:00
|
|
|
|
|
|
|
// hits is the total number of times agent methods have been called
|
|
|
|
hits int
|
|
|
|
|
2021-06-07 15:54:33 +00:00
|
|
|
// ent indicates whether the agent is mocking an enterprise consul
|
|
|
|
ent bool
|
|
|
|
|
|
|
|
// namespaces indicates whether the agent is mocking consul with namespaces
|
|
|
|
// feature enabled
|
|
|
|
namespaces bool
|
|
|
|
|
2018-04-17 19:36:50 +00:00
|
|
|
// mu guards above fields
|
|
|
|
mu sync.Mutex
|
2017-09-13 06:15:46 +00:00
|
|
|
|
2021-03-16 18:22:21 +00:00
|
|
|
// checkTTLS counts calls to UpdateTTL for each check, per namespace
|
|
|
|
checkTTLs map[string]map[string]int
|
2017-09-13 06:15:46 +00:00
|
|
|
|
|
|
|
// What check status to return from Checks()
|
|
|
|
checkStatus string
|
|
|
|
}
|
|
|
|
|
2021-03-16 18:22:21 +00:00
|
|
|
var _ AgentAPI = (*MockAgent)(nil)
|
|
|
|
|
2021-06-07 15:54:33 +00:00
|
|
|
type Features struct {
|
|
|
|
Enterprise bool
|
|
|
|
Namespaces bool
|
|
|
|
}
|
|
|
|
|
2017-09-13 06:15:46 +00:00
|
|
|
// NewMockAgent that returns all checks as passing.
|
2021-06-07 15:54:33 +00:00
|
|
|
func NewMockAgent(f Features) *MockAgent {
|
2017-09-13 06:15:46 +00:00
|
|
|
return &MockAgent{
|
2021-03-16 18:22:21 +00:00
|
|
|
services: make(map[string]map[string]*api.AgentServiceRegistration),
|
|
|
|
checks: make(map[string]map[string]*api.AgentCheckRegistration),
|
|
|
|
checkTTLs: make(map[string]map[string]int),
|
2017-09-13 06:15:46 +00:00
|
|
|
checkStatus: api.HealthPassing,
|
2021-06-07 15:54:33 +00:00
|
|
|
|
|
|
|
ent: f.Enterprise,
|
|
|
|
namespaces: f.Namespaces,
|
2017-09-13 06:15:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-17 19:36:50 +00:00
|
|
|
// getHits returns how many Consul Agent API calls have been made.
|
|
|
|
func (c *MockAgent) getHits() int {
|
|
|
|
c.mu.Lock()
|
|
|
|
defer c.mu.Unlock()
|
|
|
|
return c.hits
|
|
|
|
}
|
|
|
|
|
2017-09-13 06:15:46 +00:00
|
|
|
// SetStatus that Checks() should return. Returns old status value.
|
|
|
|
func (c *MockAgent) SetStatus(s string) string {
|
|
|
|
c.mu.Lock()
|
|
|
|
old := c.checkStatus
|
|
|
|
c.checkStatus = s
|
|
|
|
c.mu.Unlock()
|
|
|
|
return old
|
|
|
|
}
|
|
|
|
|
2018-03-15 00:37:54 +00:00
|
|
|
func (c *MockAgent) Self() (map[string]map[string]interface{}, error) {
|
2018-04-17 19:36:50 +00:00
|
|
|
c.mu.Lock()
|
|
|
|
defer c.mu.Unlock()
|
|
|
|
c.hits++
|
|
|
|
|
2021-06-07 15:54:33 +00:00
|
|
|
version := "1.9.5"
|
|
|
|
build := "1.9.5:22ce6c6a"
|
|
|
|
if c.ent {
|
|
|
|
version = "1.9.5+ent"
|
|
|
|
build = "1.9.5+ent:22ce6c6a"
|
|
|
|
}
|
|
|
|
|
|
|
|
stats := make(map[string]interface{})
|
|
|
|
if c.ent {
|
|
|
|
if c.namespaces {
|
|
|
|
stats = map[string]interface{}{
|
|
|
|
"license": map[string]interface{}{
|
|
|
|
"features": "Namespaces,",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return map[string]map[string]interface{}{
|
|
|
|
"Config": {
|
|
|
|
"Datacenter": "dc1",
|
|
|
|
"NodeName": "x52",
|
|
|
|
"NodeID": "9e7bf42e-a0b4-61b7-24f9-66dead411f0f",
|
|
|
|
"Revision": "22ce6c6ad",
|
|
|
|
"Server": true,
|
|
|
|
"Version": version,
|
|
|
|
},
|
|
|
|
"Stats": stats,
|
2018-03-16 23:31:16 +00:00
|
|
|
"Member": {
|
2018-03-15 00:37:54 +00:00
|
|
|
"Addr": "127.0.0.1",
|
|
|
|
"DelegateCur": 4,
|
|
|
|
"DelegateMax": 5,
|
|
|
|
"DelegateMin": 2,
|
|
|
|
"Name": "rusty",
|
|
|
|
"Port": 8301,
|
|
|
|
"ProtocolCur": 2,
|
|
|
|
"ProtocolMax": 5,
|
|
|
|
"ProtocolMin": 1,
|
|
|
|
"Status": 1,
|
|
|
|
"Tags": map[string]interface{}{
|
2021-06-07 15:54:33 +00:00
|
|
|
"build": build,
|
2018-03-15 00:37:54 +00:00
|
|
|
},
|
|
|
|
},
|
2020-09-04 17:50:11 +00:00
|
|
|
"xDS": {
|
|
|
|
"SupportedProxies": map[string]interface{}{
|
|
|
|
"envoy": []interface{}{
|
|
|
|
"1.14.2",
|
|
|
|
"1.13.2",
|
|
|
|
"1.12.4",
|
|
|
|
"1.11.2",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2021-06-07 15:54:33 +00:00
|
|
|
}, nil
|
2018-03-15 00:37:54 +00:00
|
|
|
}
|
|
|
|
|
2021-03-16 18:22:21 +00:00
|
|
|
func getNamespace(q *api.QueryOptions) string {
|
|
|
|
if q == nil || q.Namespace == "" {
|
|
|
|
return "default"
|
|
|
|
}
|
|
|
|
return q.Namespace
|
|
|
|
}
|
|
|
|
|
|
|
|
// ServicesWithFilterOpts implements AgentAPI
|
|
|
|
func (c *MockAgent) ServicesWithFilterOpts(_ string, q *api.QueryOptions) (map[string]*api.AgentService, error) {
|
2017-09-13 06:15:46 +00:00
|
|
|
c.mu.Lock()
|
|
|
|
defer c.mu.Unlock()
|
2021-03-16 18:22:21 +00:00
|
|
|
|
2018-04-17 19:36:50 +00:00
|
|
|
c.hits++
|
2021-03-16 18:22:21 +00:00
|
|
|
namespace := getNamespace(q)
|
2017-09-13 06:15:46 +00:00
|
|
|
|
|
|
|
r := make(map[string]*api.AgentService, len(c.services))
|
2021-03-16 18:22:21 +00:00
|
|
|
for k, v := range c.services[namespace] {
|
2017-09-13 06:15:46 +00:00
|
|
|
r[k] = &api.AgentService{
|
|
|
|
ID: v.ID,
|
|
|
|
Service: v.Name,
|
|
|
|
Tags: make([]string, len(v.Tags)),
|
2019-11-13 03:27:54 +00:00
|
|
|
Meta: helper.CopyMapStringString(v.Meta),
|
2017-09-13 06:15:46 +00:00
|
|
|
Port: v.Port,
|
|
|
|
Address: v.Address,
|
|
|
|
EnableTagOverride: v.EnableTagOverride,
|
|
|
|
}
|
|
|
|
copy(r[k].Tags, v.Tags)
|
|
|
|
}
|
|
|
|
return r, nil
|
|
|
|
}
|
|
|
|
|
2021-03-16 18:22:21 +00:00
|
|
|
// ChecksWithFilterOpts implements AgentAPI
|
|
|
|
func (c *MockAgent) ChecksWithFilterOpts(_ string, q *api.QueryOptions) (map[string]*api.AgentCheck, error) {
|
2017-09-13 06:15:46 +00:00
|
|
|
c.mu.Lock()
|
|
|
|
defer c.mu.Unlock()
|
2021-03-16 18:22:21 +00:00
|
|
|
|
2018-04-17 19:36:50 +00:00
|
|
|
c.hits++
|
2021-03-16 18:22:21 +00:00
|
|
|
namespace := getNamespace(q)
|
2017-09-13 06:15:46 +00:00
|
|
|
|
|
|
|
r := make(map[string]*api.AgentCheck, len(c.checks))
|
2021-03-16 18:22:21 +00:00
|
|
|
for k, v := range c.checks[namespace] {
|
2017-09-13 06:15:46 +00:00
|
|
|
r[k] = &api.AgentCheck{
|
|
|
|
CheckID: v.ID,
|
|
|
|
Name: v.Name,
|
|
|
|
Status: c.checkStatus,
|
|
|
|
Notes: v.Notes,
|
|
|
|
ServiceID: v.ServiceID,
|
2021-03-16 18:22:21 +00:00
|
|
|
ServiceName: c.services[namespace][v.ServiceID].Name,
|
2017-09-13 06:15:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return r, nil
|
|
|
|
}
|
|
|
|
|
2017-12-06 23:40:04 +00:00
|
|
|
// CheckRegs returns the raw AgentCheckRegistrations registered with this mock
|
2021-03-16 18:22:21 +00:00
|
|
|
// agent, across all namespaces.
|
2017-12-06 23:40:04 +00:00
|
|
|
func (c *MockAgent) CheckRegs() []*api.AgentCheckRegistration {
|
|
|
|
c.mu.Lock()
|
|
|
|
defer c.mu.Unlock()
|
2021-03-16 18:22:21 +00:00
|
|
|
|
2017-12-06 23:40:04 +00:00
|
|
|
regs := make([]*api.AgentCheckRegistration, 0, len(c.checks))
|
2021-03-16 18:22:21 +00:00
|
|
|
for namespace := range c.checks {
|
|
|
|
for _, check := range c.checks[namespace] {
|
|
|
|
regs = append(regs, check)
|
|
|
|
}
|
2017-12-06 23:40:04 +00:00
|
|
|
}
|
|
|
|
return regs
|
|
|
|
}
|
|
|
|
|
2021-03-16 18:22:21 +00:00
|
|
|
// CheckRegister implements AgentAPI
|
2017-09-13 06:15:46 +00:00
|
|
|
func (c *MockAgent) CheckRegister(check *api.AgentCheckRegistration) error {
|
|
|
|
c.mu.Lock()
|
|
|
|
defer c.mu.Unlock()
|
2018-04-17 19:36:50 +00:00
|
|
|
c.hits++
|
|
|
|
|
2021-03-16 18:22:21 +00:00
|
|
|
// Consul will set empty Namespace to default, do the same here
|
|
|
|
if check.Namespace == "" {
|
|
|
|
check.Namespace = "default"
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.checks[check.Namespace] == nil {
|
|
|
|
c.checks[check.Namespace] = make(map[string]*api.AgentCheckRegistration)
|
|
|
|
}
|
|
|
|
c.checks[check.Namespace][check.ID] = check
|
2017-09-13 06:15:46 +00:00
|
|
|
|
|
|
|
// Be nice and make checks reachable-by-service
|
2021-03-16 18:22:21 +00:00
|
|
|
serviceCheck := check.AgentServiceCheck
|
|
|
|
if c.services[check.Namespace] == nil {
|
|
|
|
c.services[check.Namespace] = make(map[string]*api.AgentServiceRegistration)
|
|
|
|
}
|
|
|
|
c.services[check.Namespace][check.ServiceID].Checks = append(c.services[check.Namespace][check.ServiceID].Checks, &serviceCheck)
|
2017-09-13 06:15:46 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-03-16 18:22:21 +00:00
|
|
|
// CheckDeregisterOpts implements AgentAPI
|
|
|
|
func (c *MockAgent) CheckDeregisterOpts(checkID string, q *api.QueryOptions) error {
|
2017-09-13 06:15:46 +00:00
|
|
|
c.mu.Lock()
|
|
|
|
defer c.mu.Unlock()
|
2021-03-16 18:22:21 +00:00
|
|
|
|
2018-04-17 19:36:50 +00:00
|
|
|
c.hits++
|
2021-03-16 18:22:21 +00:00
|
|
|
namespace := getNamespace(q)
|
|
|
|
|
|
|
|
delete(c.checks[namespace], checkID)
|
|
|
|
delete(c.checkTTLs[namespace], checkID)
|
2017-09-13 06:15:46 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-03-16 18:22:21 +00:00
|
|
|
// ServiceRegister implements AgentAPI
|
2017-09-13 06:15:46 +00:00
|
|
|
func (c *MockAgent) ServiceRegister(service *api.AgentServiceRegistration) error {
|
|
|
|
c.mu.Lock()
|
|
|
|
defer c.mu.Unlock()
|
2021-03-16 18:22:21 +00:00
|
|
|
|
2018-04-17 19:36:50 +00:00
|
|
|
c.hits++
|
2021-03-16 18:22:21 +00:00
|
|
|
|
|
|
|
// Consul will set empty Namespace to default, do the same here
|
|
|
|
if service.Namespace == "" {
|
|
|
|
service.Namespace = "default"
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.services[service.Namespace] == nil {
|
|
|
|
c.services[service.Namespace] = make(map[string]*api.AgentServiceRegistration)
|
|
|
|
}
|
|
|
|
c.services[service.Namespace][service.ID] = service
|
2017-09-13 06:15:46 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-03-16 18:22:21 +00:00
|
|
|
// ServiceDeregisterOpts implements AgentAPI
|
|
|
|
func (c *MockAgent) ServiceDeregisterOpts(serviceID string, q *api.QueryOptions) error {
|
2017-09-13 06:15:46 +00:00
|
|
|
c.mu.Lock()
|
|
|
|
defer c.mu.Unlock()
|
2021-03-16 18:22:21 +00:00
|
|
|
|
2018-04-17 19:36:50 +00:00
|
|
|
c.hits++
|
2021-03-16 18:22:21 +00:00
|
|
|
namespace := getNamespace(q)
|
|
|
|
|
|
|
|
delete(c.services[namespace], serviceID)
|
|
|
|
|
|
|
|
for k, v := range c.checks[namespace] {
|
2020-10-06 00:30:29 +00:00
|
|
|
if v.ServiceID == serviceID {
|
2021-03-16 18:22:21 +00:00
|
|
|
delete(c.checks[namespace], k)
|
|
|
|
delete(c.checkTTLs[namespace], k)
|
2020-10-06 00:30:29 +00:00
|
|
|
}
|
|
|
|
}
|
2017-09-13 06:15:46 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-03-16 18:22:21 +00:00
|
|
|
// UpdateTTLOpts implements AgentAPI
|
|
|
|
func (c *MockAgent) UpdateTTLOpts(id string, output string, status string, q *api.QueryOptions) error {
|
2017-09-13 06:15:46 +00:00
|
|
|
c.mu.Lock()
|
|
|
|
defer c.mu.Unlock()
|
2021-03-16 18:22:21 +00:00
|
|
|
|
2018-04-17 19:36:50 +00:00
|
|
|
c.hits++
|
2021-03-16 18:22:21 +00:00
|
|
|
namespace := getNamespace(q)
|
|
|
|
|
|
|
|
checks, nsExists := c.checks[namespace]
|
|
|
|
if !nsExists {
|
|
|
|
return fmt.Errorf("unknown checks namespace: %q", namespace)
|
|
|
|
}
|
2018-04-17 19:36:50 +00:00
|
|
|
|
2021-03-16 18:22:21 +00:00
|
|
|
check, checkExists := checks[id]
|
|
|
|
if !checkExists {
|
|
|
|
return fmt.Errorf("unknown check: %s/%s", namespace, id)
|
2017-09-13 06:15:46 +00:00
|
|
|
}
|
2021-03-16 18:22:21 +00:00
|
|
|
|
2017-09-13 06:15:46 +00:00
|
|
|
// Flip initial status to passing
|
2021-03-16 18:22:21 +00:00
|
|
|
// todo(shoenig) why not just set to the given status?
|
2017-09-13 06:15:46 +00:00
|
|
|
check.Status = "passing"
|
2021-03-16 18:22:21 +00:00
|
|
|
c.checkTTLs[namespace][id]++
|
|
|
|
|
2017-09-13 06:15:46 +00:00
|
|
|
return nil
|
|
|
|
}
|
client: enable configuring enable_tag_override for services
Consul provides a feature of Service Definitions where the tags
associated with a service can be modified through the Catalog API,
overriding the value(s) configured in the agent's service configuration.
To enable this feature, the flag enable_tag_override must be configured
in the service definition.
Previously, Nomad did not allow configuring this flag, and thus the default
value of false was used. Now, it is configurable.
Because Nomad itself acts as a state machine around the the service definitions
of the tasks it manages, it's worth describing what happens when this feature
is enabled and why.
Consider the basic case where there is no Nomad, and your service is provided
to consul as a boring JSON file. The ultimate source of truth for the definition
of that service is the file, and is stored in the agent. Later, Consul performs
"anti-entropy" which synchronizes the Catalog (stored only the leaders). Then
with enable_tag_override=true, the tags field is available for "external"
modification through the Catalog API (rather than directly configuring the
service definition file, or using the Agent API). The important observation
is that if the service definition ever changes (i.e. the file is changed &
config reloaded OR the Agent API is used to modify the service), those
"external" tag values are thrown away, and the new service definition is
once again the source of truth.
In the Nomad case, Nomad itself is the source of truth over the Agent in
the same way the JSON file was the source of truth in the example above.
That means any time Nomad sets a new service definition, any externally
configured tags are going to be replaced. When does this happen? Only on
major lifecycle events, for example when a task is modified because of an
updated job spec from the 'nomad job run <existing>' command. Otherwise,
Nomad's periodic re-sync's with Consul will now no longer try to restore
the externally modified tag values (as long as enable_tag_override=true).
Fixes #2057
2020-02-07 21:22:19 +00:00
|
|
|
|
|
|
|
// a convenience method for looking up a registered service by name
|
2021-03-16 18:22:21 +00:00
|
|
|
func (c *MockAgent) lookupService(namespace, name string) []*api.AgentServiceRegistration {
|
client: enable configuring enable_tag_override for services
Consul provides a feature of Service Definitions where the tags
associated with a service can be modified through the Catalog API,
overriding the value(s) configured in the agent's service configuration.
To enable this feature, the flag enable_tag_override must be configured
in the service definition.
Previously, Nomad did not allow configuring this flag, and thus the default
value of false was used. Now, it is configurable.
Because Nomad itself acts as a state machine around the the service definitions
of the tasks it manages, it's worth describing what happens when this feature
is enabled and why.
Consider the basic case where there is no Nomad, and your service is provided
to consul as a boring JSON file. The ultimate source of truth for the definition
of that service is the file, and is stored in the agent. Later, Consul performs
"anti-entropy" which synchronizes the Catalog (stored only the leaders). Then
with enable_tag_override=true, the tags field is available for "external"
modification through the Catalog API (rather than directly configuring the
service definition file, or using the Agent API). The important observation
is that if the service definition ever changes (i.e. the file is changed &
config reloaded OR the Agent API is used to modify the service), those
"external" tag values are thrown away, and the new service definition is
once again the source of truth.
In the Nomad case, Nomad itself is the source of truth over the Agent in
the same way the JSON file was the source of truth in the example above.
That means any time Nomad sets a new service definition, any externally
configured tags are going to be replaced. When does this happen? Only on
major lifecycle events, for example when a task is modified because of an
updated job spec from the 'nomad job run <existing>' command. Otherwise,
Nomad's periodic re-sync's with Consul will now no longer try to restore
the externally modified tag values (as long as enable_tag_override=true).
Fixes #2057
2020-02-07 21:22:19 +00:00
|
|
|
c.mu.Lock()
|
|
|
|
defer c.mu.Unlock()
|
|
|
|
|
|
|
|
var services []*api.AgentServiceRegistration
|
2021-03-16 18:22:21 +00:00
|
|
|
for _, service := range c.services[namespace] {
|
client: enable configuring enable_tag_override for services
Consul provides a feature of Service Definitions where the tags
associated with a service can be modified through the Catalog API,
overriding the value(s) configured in the agent's service configuration.
To enable this feature, the flag enable_tag_override must be configured
in the service definition.
Previously, Nomad did not allow configuring this flag, and thus the default
value of false was used. Now, it is configurable.
Because Nomad itself acts as a state machine around the the service definitions
of the tasks it manages, it's worth describing what happens when this feature
is enabled and why.
Consider the basic case where there is no Nomad, and your service is provided
to consul as a boring JSON file. The ultimate source of truth for the definition
of that service is the file, and is stored in the agent. Later, Consul performs
"anti-entropy" which synchronizes the Catalog (stored only the leaders). Then
with enable_tag_override=true, the tags field is available for "external"
modification through the Catalog API (rather than directly configuring the
service definition file, or using the Agent API). The important observation
is that if the service definition ever changes (i.e. the file is changed &
config reloaded OR the Agent API is used to modify the service), those
"external" tag values are thrown away, and the new service definition is
once again the source of truth.
In the Nomad case, Nomad itself is the source of truth over the Agent in
the same way the JSON file was the source of truth in the example above.
That means any time Nomad sets a new service definition, any externally
configured tags are going to be replaced. When does this happen? Only on
major lifecycle events, for example when a task is modified because of an
updated job spec from the 'nomad job run <existing>' command. Otherwise,
Nomad's periodic re-sync's with Consul will now no longer try to restore
the externally modified tag values (as long as enable_tag_override=true).
Fixes #2057
2020-02-07 21:22:19 +00:00
|
|
|
if service.Name == name {
|
|
|
|
services = append(services, service)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return services
|
|
|
|
}
|