2017-08-28 12:17:12 +00:00
|
|
|
package local
|
2014-01-16 01:14:50 +00:00
|
|
|
|
|
|
|
import (
|
2015-01-27 09:11:57 +00:00
|
|
|
"fmt"
|
2016-02-07 21:12:42 +00:00
|
|
|
"reflect"
|
2017-08-28 12:17:12 +00:00
|
|
|
"strconv"
|
2014-12-01 19:43:01 +00:00
|
|
|
"strings"
|
2014-01-16 01:14:50 +00:00
|
|
|
"sync"
|
2014-02-07 19:58:24 +00:00
|
|
|
"sync/atomic"
|
2014-01-16 01:14:50 +00:00
|
|
|
"time"
|
2014-12-01 19:43:01 +00:00
|
|
|
|
2020-11-13 02:12:12 +00:00
|
|
|
"github.com/armon/go-metrics"
|
|
|
|
"github.com/armon/go-metrics/prometheus"
|
2020-11-25 23:24:13 +00:00
|
|
|
"github.com/hashicorp/go-hclog"
|
|
|
|
|
2017-08-23 14:52:48 +00:00
|
|
|
"github.com/hashicorp/consul/acl"
|
2017-07-06 10:34:00 +00:00
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
2017-07-26 18:03:43 +00:00
|
|
|
"github.com/hashicorp/consul/agent/token"
|
2017-04-19 23:00:11 +00:00
|
|
|
"github.com/hashicorp/consul/api"
|
2016-01-29 19:42:34 +00:00
|
|
|
"github.com/hashicorp/consul/lib"
|
2016-06-06 20:19:31 +00:00
|
|
|
"github.com/hashicorp/consul/types"
|
2014-01-16 01:14:50 +00:00
|
|
|
)
|
|
|
|
|
2020-11-13 02:12:12 +00:00
|
|
|
var StateCounters = []prometheus.CounterDefinition{
|
|
|
|
{
|
2020-11-14 00:26:08 +00:00
|
|
|
Name: []string{"acl", "blocked", "service", "registration"},
|
2020-11-16 19:02:11 +00:00
|
|
|
Help: "Increments whenever a registration fails for a service (blocked by an ACL)",
|
2020-11-13 02:12:12 +00:00
|
|
|
},
|
|
|
|
{
|
2020-11-14 00:26:08 +00:00
|
|
|
Name: []string{"acl", "blocked", "service", "deregistration"},
|
2020-11-16 19:02:11 +00:00
|
|
|
Help: "Increments whenever a deregistration fails for a service (blocked by an ACL)",
|
2020-11-13 02:12:12 +00:00
|
|
|
},
|
|
|
|
{
|
2020-11-14 00:26:08 +00:00
|
|
|
Name: []string{"acl", "blocked", "check", "registration"},
|
2020-11-16 19:02:11 +00:00
|
|
|
Help: "Increments whenever a registration fails for a check (blocked by an ACL)",
|
2020-11-13 02:12:12 +00:00
|
|
|
},
|
|
|
|
{
|
2020-11-14 00:26:08 +00:00
|
|
|
Name: []string{"acl", "blocked", "check", "deregistration"},
|
2020-11-16 19:02:11 +00:00
|
|
|
Help: "Increments whenever a deregistration fails for a check (blocked by an ACL)",
|
2020-11-13 02:12:12 +00:00
|
|
|
},
|
|
|
|
{
|
2020-11-13 21:18:04 +00:00
|
|
|
Name: []string{"acl", "blocked", "node", "registration"},
|
2020-11-16 19:02:11 +00:00
|
|
|
Help: "Increments whenever a registration fails for a node (blocked by an ACL)",
|
2020-11-14 00:26:08 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: []string{"acl", "blocked", "node", "deregistration"},
|
2020-11-16 19:02:11 +00:00
|
|
|
Help: "Increments whenever a deregistration fails for a node (blocked by an ACL)",
|
2020-11-13 02:12:12 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-06-17 16:05:47 +00:00
|
|
|
const fullSyncReadMaxStale = 2 * time.Second
|
|
|
|
|
2017-10-18 13:05:57 +00:00
|
|
|
// Config is the configuration for the State.
|
2017-08-28 12:17:12 +00:00
|
|
|
type Config struct {
|
2017-06-30 20:37:20 +00:00
|
|
|
AdvertiseAddr string
|
|
|
|
CheckUpdateInterval time.Duration
|
|
|
|
Datacenter string
|
2017-08-28 12:17:12 +00:00
|
|
|
DiscardCheckOutput bool
|
2017-06-30 20:37:20 +00:00
|
|
|
NodeID types.NodeID
|
|
|
|
NodeName string
|
2021-08-19 20:09:42 +00:00
|
|
|
Partition string // this defaults if empty
|
2017-06-30 20:37:20 +00:00
|
|
|
TaggedAddresses map[string]string
|
|
|
|
}
|
|
|
|
|
2017-08-28 12:17:12 +00:00
|
|
|
// ServiceState describes the state of a service record.
|
|
|
|
type ServiceState struct {
|
|
|
|
// Service is the local copy of the service record.
|
|
|
|
Service *structs.NodeService
|
|
|
|
|
2017-08-30 10:25:49 +00:00
|
|
|
// Token is the ACL to update or delete the service record on the
|
|
|
|
// server.
|
2017-08-28 12:17:12 +00:00
|
|
|
Token string
|
|
|
|
|
|
|
|
// InSync contains whether the local state of the service record
|
|
|
|
// is in sync with the remote state on the server.
|
|
|
|
InSync bool
|
|
|
|
|
|
|
|
// Deleted is true when the service record has been marked as deleted
|
|
|
|
// but has not been removed on the server yet.
|
|
|
|
Deleted bool
|
2018-09-27 14:00:51 +00:00
|
|
|
|
2019-09-26 02:55:52 +00:00
|
|
|
// WatchCh is closed when the service state changes. Suitable for use in a
|
2018-09-27 14:00:51 +00:00
|
|
|
// memdb.WatchSet when watching agent local changes with hash-based blocking.
|
|
|
|
WatchCh chan struct{}
|
2017-08-28 12:17:12 +00:00
|
|
|
}
|
|
|
|
|
2018-09-27 14:00:51 +00:00
|
|
|
// Clone returns a shallow copy of the object. The service record still points
|
|
|
|
// to the original service record and must not be modified. The WatchCh is also
|
|
|
|
// still pointing to the original so the clone will be update when the original
|
|
|
|
// is.
|
2017-08-28 12:17:13 +00:00
|
|
|
func (s *ServiceState) Clone() *ServiceState {
|
|
|
|
s2 := new(ServiceState)
|
|
|
|
*s2 = *s
|
|
|
|
return s2
|
|
|
|
}
|
|
|
|
|
2017-08-28 12:17:12 +00:00
|
|
|
// CheckState describes the state of a health check record.
|
|
|
|
type CheckState struct {
|
|
|
|
// Check is the local copy of the health check record.
|
2019-05-24 18:36:56 +00:00
|
|
|
//
|
|
|
|
// Must Clone() the overall CheckState before mutating this. After mutation
|
2020-01-23 16:38:32 +00:00
|
|
|
// reinstall into the checks map. If Deleted is true, this field can be nil.
|
2017-08-28 12:17:12 +00:00
|
|
|
Check *structs.HealthCheck
|
|
|
|
|
2017-08-30 10:25:49 +00:00
|
|
|
// Token is the ACL record to update or delete the health check
|
|
|
|
// record on the server.
|
2017-08-28 12:17:12 +00:00
|
|
|
Token string
|
|
|
|
|
|
|
|
// CriticalTime is the last time the health check status went
|
|
|
|
// from non-critical to critical. When the health check is not
|
|
|
|
// in critical state the value is the zero value.
|
|
|
|
CriticalTime time.Time
|
|
|
|
|
|
|
|
// DeferCheck is used to delay the sync of a health check when
|
2017-08-30 10:25:49 +00:00
|
|
|
// only the output has changed. This rate limits changes which
|
|
|
|
// do not affect the state of the node and/or service.
|
2017-08-28 12:17:12 +00:00
|
|
|
DeferCheck *time.Timer
|
|
|
|
|
|
|
|
// InSync contains whether the local state of the health check
|
|
|
|
// record is in sync with the remote state on the server.
|
|
|
|
InSync bool
|
|
|
|
|
|
|
|
// Deleted is true when the health check record has been marked as
|
|
|
|
// deleted but has not been removed on the server yet.
|
|
|
|
Deleted bool
|
|
|
|
}
|
|
|
|
|
2019-05-24 18:36:56 +00:00
|
|
|
// Clone returns a shallow copy of the object.
|
|
|
|
//
|
|
|
|
// The defer timer still points to the original value and must not be modified.
|
2017-08-28 12:17:13 +00:00
|
|
|
func (c *CheckState) Clone() *CheckState {
|
|
|
|
c2 := new(CheckState)
|
|
|
|
*c2 = *c
|
2019-05-24 18:36:56 +00:00
|
|
|
if c.Check != nil {
|
|
|
|
c2.Check = c.Check.Clone()
|
|
|
|
}
|
2017-08-28 12:17:13 +00:00
|
|
|
return c2
|
|
|
|
}
|
|
|
|
|
2017-08-28 12:17:12 +00:00
|
|
|
// Critical returns true when the health check is in critical state.
|
|
|
|
func (c *CheckState) Critical() bool {
|
|
|
|
return !c.CriticalTime.IsZero()
|
|
|
|
}
|
|
|
|
|
|
|
|
// CriticalFor returns the amount of time the service has been in critical
|
|
|
|
// state. Its value is undefined when the service is not in critical state.
|
|
|
|
func (c *CheckState) CriticalFor() time.Duration {
|
|
|
|
return time.Since(c.CriticalTime)
|
|
|
|
}
|
|
|
|
|
2017-08-30 10:25:49 +00:00
|
|
|
type rpc interface {
|
2017-08-28 12:17:12 +00:00
|
|
|
RPC(method string, args interface{}, reply interface{}) error
|
2020-05-13 17:00:08 +00:00
|
|
|
ResolveTokenToIdentity(secretID string) (structs.ACLIdentity, error)
|
2017-08-28 12:17:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// State is used to represent the node's services,
|
2017-10-18 13:05:57 +00:00
|
|
|
// and checks. We use it to perform anti-entropy with the
|
2014-01-16 01:14:50 +00:00
|
|
|
// catalog representation
|
2017-08-28 12:17:12 +00:00
|
|
|
type State struct {
|
2015-04-28 05:01:01 +00:00
|
|
|
sync.RWMutex
|
2017-08-30 10:25:49 +00:00
|
|
|
|
|
|
|
// Delegate the RPC interface to the consul server or agent.
|
|
|
|
//
|
|
|
|
// It is set after both the state and the consul server/agent have
|
|
|
|
// been created.
|
|
|
|
Delegate rpc
|
|
|
|
|
|
|
|
// TriggerSyncChanges is used to notify the state syncer that a
|
|
|
|
// partial sync should be performed.
|
|
|
|
//
|
|
|
|
// It is set after both the state and the state syncer have been
|
|
|
|
// created.
|
|
|
|
TriggerSyncChanges func()
|
|
|
|
|
2020-01-28 23:50:41 +00:00
|
|
|
logger hclog.Logger
|
2014-01-16 01:14:50 +00:00
|
|
|
|
2014-01-21 19:52:25 +00:00
|
|
|
// Config is the agent config
|
2017-08-28 12:17:12 +00:00
|
|
|
config Config
|
2014-01-21 19:52:25 +00:00
|
|
|
|
2021-08-19 20:09:42 +00:00
|
|
|
agentEnterpriseMeta structs.EnterpriseMeta
|
|
|
|
|
2016-02-07 21:12:42 +00:00
|
|
|
// nodeInfoInSync tracks whether the server has our correct top-level
|
2017-01-18 22:26:42 +00:00
|
|
|
// node information in sync
|
2016-02-07 21:12:42 +00:00
|
|
|
nodeInfoInSync bool
|
|
|
|
|
2014-01-16 01:14:50 +00:00
|
|
|
// Services tracks the local services
|
2019-12-10 02:26:41 +00:00
|
|
|
services map[structs.ServiceID]*ServiceState
|
2014-01-16 01:14:50 +00:00
|
|
|
|
2018-06-30 14:23:47 +00:00
|
|
|
// Checks tracks the local checks. checkAliases are aliased checks.
|
2019-12-10 02:26:41 +00:00
|
|
|
checks map[structs.CheckID]*CheckState
|
|
|
|
checkAliases map[structs.ServiceID]map[structs.CheckID]chan<- struct{}
|
2014-06-10 17:42:55 +00:00
|
|
|
|
2017-08-30 10:25:49 +00:00
|
|
|
// metadata tracks the node metadata fields
|
2017-01-05 22:10:26 +00:00
|
|
|
metadata map[string]string
|
|
|
|
|
2017-10-11 00:04:52 +00:00
|
|
|
// discardCheckOutput stores whether the output of health checks
|
|
|
|
// is stored in the raft log.
|
|
|
|
discardCheckOutput atomic.Value // bool
|
2017-08-28 12:17:12 +00:00
|
|
|
|
2017-08-28 12:17:12 +00:00
|
|
|
// tokens contains the ACL tokens
|
2017-08-28 12:17:12 +00:00
|
|
|
tokens *token.Store
|
2018-04-16 15:00:20 +00:00
|
|
|
|
2018-10-03 12:36:38 +00:00
|
|
|
// notifyHandlers is a map of registered channel listeners that are sent
|
|
|
|
// messages whenever state changes occur. For now these events only include
|
|
|
|
// service registration and deregistration since that is all that is needed
|
2019-08-09 19:19:30 +00:00
|
|
|
// but the same mechanism could be used for other state changes. Any
|
|
|
|
// future notifications should re-use this mechanism.
|
2018-10-03 12:36:38 +00:00
|
|
|
notifyHandlers map[chan<- struct{}]struct{}
|
2014-01-16 01:14:50 +00:00
|
|
|
}
|
|
|
|
|
2018-04-16 15:00:20 +00:00
|
|
|
// NewState creates a new local state for the agent.
|
2020-01-28 23:50:41 +00:00
|
|
|
func NewState(c Config, logger hclog.Logger, tokens *token.Store) *State {
|
2017-08-28 12:17:12 +00:00
|
|
|
l := &State{
|
2021-08-19 20:09:42 +00:00
|
|
|
config: c,
|
|
|
|
logger: logger,
|
|
|
|
services: make(map[structs.ServiceID]*ServiceState),
|
|
|
|
checks: make(map[structs.CheckID]*CheckState),
|
|
|
|
checkAliases: make(map[structs.ServiceID]map[structs.CheckID]chan<- struct{}),
|
|
|
|
metadata: make(map[string]string),
|
|
|
|
tokens: tokens,
|
|
|
|
notifyHandlers: make(map[chan<- struct{}]struct{}),
|
|
|
|
agentEnterpriseMeta: *structs.NodeEnterpriseMetaInPartition(c.Partition),
|
2017-08-30 10:25:49 +00:00
|
|
|
}
|
|
|
|
l.SetDiscardCheckOutput(c.DiscardCheckOutput)
|
2017-10-11 00:04:52 +00:00
|
|
|
return l
|
2014-01-21 19:52:25 +00:00
|
|
|
}
|
|
|
|
|
2017-10-18 13:05:57 +00:00
|
|
|
// SetDiscardCheckOutput configures whether the check output
|
|
|
|
// is discarded. This can be changed at runtime.
|
2017-08-28 12:17:12 +00:00
|
|
|
func (l *State) SetDiscardCheckOutput(b bool) {
|
2017-10-11 00:04:52 +00:00
|
|
|
l.discardCheckOutput.Store(b)
|
|
|
|
}
|
|
|
|
|
2021-05-04 20:43:44 +00:00
|
|
|
// ServiceToken returns the ACL token associated with the service. If the service is
|
|
|
|
// not found, or does not have a token, the empty string is returned.
|
2019-12-10 02:26:41 +00:00
|
|
|
func (l *State) ServiceToken(id structs.ServiceID) string {
|
2015-04-28 18:53:53 +00:00
|
|
|
l.RLock()
|
|
|
|
defer l.RUnlock()
|
2021-05-04 20:43:44 +00:00
|
|
|
if s := l.services[id]; s != nil {
|
|
|
|
return s.Token
|
|
|
|
}
|
|
|
|
return ""
|
2015-04-28 18:53:53 +00:00
|
|
|
}
|
|
|
|
|
2021-05-04 20:43:44 +00:00
|
|
|
// aclTokenForServiceSync returns an ACL token associated with a service. If there is
|
2021-05-05 21:37:02 +00:00
|
|
|
// no ACL token associated with the service, fallback is used to return a value.
|
2017-10-18 13:05:57 +00:00
|
|
|
// This method is not synchronized and the lock must already be held.
|
2021-05-05 21:37:02 +00:00
|
|
|
func (l *State) aclTokenForServiceSync(id structs.ServiceID, fallback func() string) string {
|
|
|
|
if s := l.services[id]; s != nil && s.Token != "" {
|
|
|
|
return s.Token
|
2015-04-28 05:01:01 +00:00
|
|
|
}
|
2021-05-05 21:37:02 +00:00
|
|
|
return fallback()
|
2015-04-28 05:01:01 +00:00
|
|
|
}
|
|
|
|
|
2014-01-16 01:14:50 +00:00
|
|
|
// AddService is used to add a service entry to the local state.
|
|
|
|
// This entry is persistent and the agent will make a best effort to
|
|
|
|
// ensure it is registered
|
2017-08-28 12:17:12 +00:00
|
|
|
func (l *State) AddService(service *structs.NodeService, token string) error {
|
2019-03-04 14:34:05 +00:00
|
|
|
l.Lock()
|
|
|
|
defer l.Unlock()
|
|
|
|
return l.addServiceLocked(service, token)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *State) addServiceLocked(service *structs.NodeService, token string) error {
|
2017-08-28 12:17:12 +00:00
|
|
|
if service == nil {
|
|
|
|
return fmt.Errorf("no service")
|
|
|
|
}
|
|
|
|
|
|
|
|
// use the service name as id if the id was omitted
|
|
|
|
if service.ID == "" {
|
|
|
|
service.ID = service.Service
|
|
|
|
}
|
|
|
|
|
2021-08-19 20:09:42 +00:00
|
|
|
if l.agentEnterpriseMeta.PartitionOrDefault() != service.PartitionOrDefault() {
|
2021-08-21 02:03:24 +00:00
|
|
|
return fmt.Errorf("cannot add service ID %q to node in partition %q", service.CompoundServiceID(), l.config.Partition)
|
2021-08-19 20:09:42 +00:00
|
|
|
}
|
|
|
|
|
2019-03-04 14:34:05 +00:00
|
|
|
l.setServiceStateLocked(&ServiceState{
|
2017-08-28 12:17:12 +00:00
|
|
|
Service: service,
|
|
|
|
Token: token,
|
2017-08-28 12:17:13 +00:00
|
|
|
})
|
2017-08-28 12:17:12 +00:00
|
|
|
return nil
|
2017-10-23 08:08:33 +00:00
|
|
|
}
|
|
|
|
|
2019-03-04 14:34:05 +00:00
|
|
|
// AddServiceWithChecks adds a service and its check tp the local state atomically
|
|
|
|
func (l *State) AddServiceWithChecks(service *structs.NodeService, checks []*structs.HealthCheck, token string) error {
|
|
|
|
l.Lock()
|
|
|
|
defer l.Unlock()
|
|
|
|
|
|
|
|
if err := l.addServiceLocked(service, token); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, check := range checks {
|
|
|
|
if err := l.addCheckLocked(check, token); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-01-16 01:14:50 +00:00
|
|
|
// RemoveService is used to remove a service entry from the local state.
|
2017-08-28 12:17:12 +00:00
|
|
|
// The agent will make a best effort to ensure it is deregistered.
|
2019-12-10 02:26:41 +00:00
|
|
|
func (l *State) RemoveService(id structs.ServiceID) error {
|
2014-01-21 19:52:25 +00:00
|
|
|
l.Lock()
|
|
|
|
defer l.Unlock()
|
2019-03-04 14:34:05 +00:00
|
|
|
return l.removeServiceLocked(id)
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveServiceWithChecks removes a service and its check from the local state atomically
|
2019-12-10 02:26:41 +00:00
|
|
|
func (l *State) RemoveServiceWithChecks(serviceID structs.ServiceID, checkIDs []structs.CheckID) error {
|
2019-03-04 14:34:05 +00:00
|
|
|
l.Lock()
|
|
|
|
defer l.Unlock()
|
|
|
|
|
|
|
|
if err := l.removeServiceLocked(serviceID); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, id := range checkIDs {
|
|
|
|
if err := l.removeCheckLocked(id); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-12-10 02:26:41 +00:00
|
|
|
func (l *State) removeServiceLocked(id structs.ServiceID) error {
|
2017-08-28 12:17:12 +00:00
|
|
|
s := l.services[id]
|
|
|
|
if s == nil || s.Deleted {
|
2021-08-21 02:03:24 +00:00
|
|
|
// Take care if modifying this error message.
|
|
|
|
// deleteService assumes the Catalog.Deregister RPC call will include "Unknown service"
|
|
|
|
// in the error if deregistration fails due to a service with that ID not existing.
|
|
|
|
|
|
|
|
// When the service register endpoint is called, this error message is also typically
|
|
|
|
// shadowed by vetServiceUpdateWithAuthorizer, which checks for the existence of the
|
|
|
|
// service and, if none is found, returns an error before this function is ever called.
|
|
|
|
return fmt.Errorf("Unknown service ID %q. Ensure that the service ID is passed, not the service name.", id)
|
2016-11-09 21:56:54 +00:00
|
|
|
}
|
|
|
|
|
2017-08-28 12:17:12 +00:00
|
|
|
// To remove the service on the server we need the token.
|
|
|
|
// Therefore, we mark the service as deleted and keep the
|
|
|
|
// entry around until it is actually removed.
|
|
|
|
s.InSync = false
|
|
|
|
s.Deleted = true
|
2018-09-27 14:00:51 +00:00
|
|
|
if s.WatchCh != nil {
|
|
|
|
close(s.WatchCh)
|
|
|
|
s.WatchCh = nil
|
|
|
|
}
|
2020-08-12 15:47:41 +00:00
|
|
|
|
|
|
|
l.notifyIfAliased(id)
|
2017-08-30 10:25:49 +00:00
|
|
|
l.TriggerSyncChanges()
|
2018-10-03 12:36:38 +00:00
|
|
|
l.broadcastUpdateLocked()
|
2017-08-28 12:17:12 +00:00
|
|
|
|
2016-11-09 21:56:54 +00:00
|
|
|
return nil
|
2014-01-16 01:14:50 +00:00
|
|
|
}
|
|
|
|
|
2021-08-19 20:09:42 +00:00
|
|
|
// Service returns the locally registered service that the agent is aware of
|
|
|
|
// with this ID and are being kept in sync with the server.
|
2019-12-10 02:26:41 +00:00
|
|
|
func (l *State) Service(id structs.ServiceID) *structs.NodeService {
|
2017-08-28 12:17:12 +00:00
|
|
|
l.RLock()
|
|
|
|
defer l.RUnlock()
|
2017-08-28 12:17:12 +00:00
|
|
|
|
|
|
|
s := l.services[id]
|
|
|
|
if s == nil || s.Deleted {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return s.Service
|
2017-08-28 12:17:12 +00:00
|
|
|
}
|
|
|
|
|
2021-08-19 20:09:42 +00:00
|
|
|
// ServicesByName returns all the locally registered service instances that the
|
|
|
|
// agent is aware of with this name and are being kept in sync with the server
|
|
|
|
func (l *State) ServicesByName(sn structs.ServiceName) []*structs.NodeService {
|
|
|
|
l.RLock()
|
|
|
|
defer l.RUnlock()
|
|
|
|
|
|
|
|
var found []*structs.NodeService
|
|
|
|
for id, s := range l.services {
|
|
|
|
if s.Deleted {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if !sn.EnterpriseMeta.Matches(&id.EnterpriseMeta) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if s.Service.Service == sn.Name {
|
|
|
|
found = append(found, s.Service)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return found
|
|
|
|
}
|
|
|
|
|
|
|
|
// AllServices returns the locally registered services that the
|
2014-01-21 01:00:52 +00:00
|
|
|
// agent is aware of and are being kept in sync with the server
|
2021-08-19 20:09:42 +00:00
|
|
|
func (l *State) AllServices() map[structs.ServiceID]*structs.NodeService {
|
|
|
|
return l.listServices(false, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Services returns the locally registered services that the agent is aware of
|
|
|
|
// and are being kept in sync with the server
|
|
|
|
//
|
|
|
|
// Results are scoped to the provided namespace and partition.
|
2019-12-10 02:26:41 +00:00
|
|
|
func (l *State) Services(entMeta *structs.EnterpriseMeta) map[structs.ServiceID]*structs.NodeService {
|
2021-08-19 20:09:42 +00:00
|
|
|
return l.listServices(true, entMeta)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *State) listServices(filtered bool, entMeta *structs.EnterpriseMeta) map[structs.ServiceID]*structs.NodeService {
|
2015-04-28 05:01:01 +00:00
|
|
|
l.RLock()
|
|
|
|
defer l.RUnlock()
|
2014-01-21 01:00:52 +00:00
|
|
|
|
2019-12-10 02:26:41 +00:00
|
|
|
m := make(map[structs.ServiceID]*structs.NodeService)
|
2017-08-28 12:17:12 +00:00
|
|
|
for id, s := range l.services {
|
|
|
|
if s.Deleted {
|
|
|
|
continue
|
|
|
|
}
|
2019-12-10 02:26:41 +00:00
|
|
|
|
2021-08-19 20:09:42 +00:00
|
|
|
if filtered && !entMeta.Matches(&id.EnterpriseMeta) {
|
2019-12-10 02:26:41 +00:00
|
|
|
continue
|
|
|
|
}
|
2017-08-28 12:17:12 +00:00
|
|
|
m[id] = s.Service
|
2014-01-21 01:00:52 +00:00
|
|
|
}
|
2017-08-28 12:17:12 +00:00
|
|
|
return m
|
2014-01-21 01:00:52 +00:00
|
|
|
}
|
|
|
|
|
2018-09-27 14:00:51 +00:00
|
|
|
// ServiceState returns a shallow copy of the current service state record. The
|
|
|
|
// service record still points to the original service record and must not be
|
|
|
|
// modified. The WatchCh for the copy returned will also be closed when the
|
|
|
|
// actual service state is changed.
|
2019-12-10 02:26:41 +00:00
|
|
|
func (l *State) ServiceState(id structs.ServiceID) *ServiceState {
|
2017-08-28 12:17:13 +00:00
|
|
|
l.RLock()
|
|
|
|
defer l.RUnlock()
|
|
|
|
|
|
|
|
s := l.services[id]
|
|
|
|
if s == nil || s.Deleted {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return s.Clone()
|
|
|
|
}
|
|
|
|
|
2017-10-18 13:07:19 +00:00
|
|
|
// SetServiceState is used to overwrite a raw service state with the given
|
|
|
|
// state. This method is safe to be called concurrently but should only be used
|
|
|
|
// during testing. You should most likely call AddService instead.
|
|
|
|
func (l *State) SetServiceState(s *ServiceState) {
|
|
|
|
l.Lock()
|
|
|
|
defer l.Unlock()
|
|
|
|
|
2021-08-19 20:09:42 +00:00
|
|
|
if l.agentEnterpriseMeta.PartitionOrDefault() != s.Service.PartitionOrDefault() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-03-04 14:34:05 +00:00
|
|
|
l.setServiceStateLocked(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *State) setServiceStateLocked(s *ServiceState) {
|
2019-12-10 02:26:41 +00:00
|
|
|
key := s.Service.CompoundServiceID()
|
|
|
|
old, hasOld := l.services[key]
|
2020-11-25 23:24:13 +00:00
|
|
|
if hasOld {
|
|
|
|
s.InSync = s.Service.IsSame(old.Service)
|
|
|
|
}
|
2019-12-10 02:26:41 +00:00
|
|
|
l.services[key] = s
|
2018-09-27 14:00:51 +00:00
|
|
|
|
2020-11-25 23:24:13 +00:00
|
|
|
s.WatchCh = make(chan struct{}, 1)
|
2018-09-27 14:00:51 +00:00
|
|
|
if hasOld && old.WatchCh != nil {
|
|
|
|
close(old.WatchCh)
|
|
|
|
}
|
2020-08-12 15:47:41 +00:00
|
|
|
if !hasOld {
|
|
|
|
// The status of an alias check is updated if the alias service is added/removed
|
|
|
|
// Only try notify alias checks if service didn't already exist (!hasOld)
|
|
|
|
l.notifyIfAliased(key)
|
|
|
|
}
|
2018-09-27 14:00:51 +00:00
|
|
|
|
2017-10-18 13:07:19 +00:00
|
|
|
l.TriggerSyncChanges()
|
2018-10-03 12:36:38 +00:00
|
|
|
l.broadcastUpdateLocked()
|
2017-10-18 13:07:19 +00:00
|
|
|
}
|
|
|
|
|
2017-08-28 12:17:13 +00:00
|
|
|
// ServiceStates returns a shallow copy of all service state records.
|
|
|
|
// The service record still points to the original service record and
|
|
|
|
// must not be modified.
|
2019-12-10 02:26:41 +00:00
|
|
|
func (l *State) ServiceStates(entMeta *structs.EnterpriseMeta) map[structs.ServiceID]*ServiceState {
|
2017-08-28 12:17:13 +00:00
|
|
|
l.RLock()
|
|
|
|
defer l.RUnlock()
|
|
|
|
|
2019-12-10 02:26:41 +00:00
|
|
|
m := make(map[structs.ServiceID]*ServiceState)
|
2017-08-28 12:17:13 +00:00
|
|
|
for id, s := range l.services {
|
|
|
|
if s.Deleted {
|
|
|
|
continue
|
|
|
|
}
|
2019-12-10 02:26:41 +00:00
|
|
|
if !entMeta.Matches(&id.EnterpriseMeta) {
|
|
|
|
continue
|
|
|
|
}
|
2017-08-28 12:17:13 +00:00
|
|
|
m[id] = s.Clone()
|
|
|
|
}
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
2021-05-04 20:43:44 +00:00
|
|
|
// CheckToken returns the ACL token associated with the check. If the check is
|
|
|
|
// not found, or does not have a token, the empty string is returned.
|
|
|
|
func (l *State) CheckToken(id structs.CheckID) string {
|
2015-04-28 18:53:53 +00:00
|
|
|
l.RLock()
|
|
|
|
defer l.RUnlock()
|
2021-05-04 20:43:44 +00:00
|
|
|
if c := l.checks[id]; c != nil {
|
|
|
|
return c.Token
|
|
|
|
}
|
|
|
|
return ""
|
2015-04-28 18:53:53 +00:00
|
|
|
}
|
|
|
|
|
2021-05-04 20:43:44 +00:00
|
|
|
// aclTokenForCheckSync returns an ACL token associated with a check. If there is
|
2021-05-05 21:37:02 +00:00
|
|
|
// no ACL token associated with the check, the callback is used to return a value.
|
2017-10-18 13:05:57 +00:00
|
|
|
// This method is not synchronized and the lock must already be held.
|
2021-05-05 21:37:02 +00:00
|
|
|
func (l *State) aclTokenForCheckSync(id structs.CheckID, fallback func() string) string {
|
|
|
|
if c := l.checks[id]; c != nil && c.Token != "" {
|
|
|
|
return c.Token
|
2015-04-28 05:01:01 +00:00
|
|
|
}
|
2021-05-05 21:37:02 +00:00
|
|
|
return fallback()
|
2015-04-28 05:01:01 +00:00
|
|
|
}
|
|
|
|
|
2014-01-16 01:14:50 +00:00
|
|
|
// AddCheck is used to add a health check to the local state.
|
|
|
|
// This entry is persistent and the agent will make a best effort to
|
|
|
|
// ensure it is registered
|
2017-08-28 12:17:12 +00:00
|
|
|
func (l *State) AddCheck(check *structs.HealthCheck, token string) error {
|
2019-03-04 14:34:05 +00:00
|
|
|
l.Lock()
|
|
|
|
defer l.Unlock()
|
|
|
|
|
|
|
|
return l.addCheckLocked(check, token)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *State) addCheckLocked(check *structs.HealthCheck, token string) error {
|
2017-08-28 12:17:12 +00:00
|
|
|
if check == nil {
|
|
|
|
return fmt.Errorf("no check")
|
|
|
|
}
|
2014-01-21 01:06:44 +00:00
|
|
|
|
2017-10-20 04:41:12 +00:00
|
|
|
// clone the check since we will be modifying it.
|
|
|
|
check = check.Clone()
|
|
|
|
|
2017-10-11 00:04:52 +00:00
|
|
|
if l.discardCheckOutput.Load().(bool) {
|
|
|
|
check.Output = ""
|
|
|
|
}
|
2017-07-18 21:06:37 +00:00
|
|
|
|
2021-08-19 20:09:42 +00:00
|
|
|
// hard-set the node name and partition
|
|
|
|
check.Node = l.config.NodeName
|
|
|
|
check.EnterpriseMeta = structs.NewEnterpriseMetaWithPartition(
|
|
|
|
l.agentEnterpriseMeta.PartitionOrEmpty(),
|
|
|
|
check.NamespaceOrEmpty(),
|
|
|
|
)
|
|
|
|
|
2017-07-18 21:06:37 +00:00
|
|
|
// if there is a serviceID associated with the check, make sure it exists before adding it
|
|
|
|
// NOTE - This logic may be moved to be handled within the Agent's Addcheck method after a refactor
|
2019-12-10 02:26:41 +00:00
|
|
|
if _, ok := l.services[check.CompoundServiceID()]; check.ServiceID != "" && !ok {
|
2021-08-21 02:03:24 +00:00
|
|
|
return fmt.Errorf("Check ID %q refers to non-existent service ID %q", check.CheckID, check.ServiceID)
|
2017-07-18 19:09:19 +00:00
|
|
|
}
|
2017-07-18 21:06:37 +00:00
|
|
|
|
2019-03-04 14:34:05 +00:00
|
|
|
l.setCheckStateLocked(&CheckState{
|
2017-08-28 12:17:12 +00:00
|
|
|
Check: check,
|
|
|
|
Token: token,
|
2017-08-28 12:17:13 +00:00
|
|
|
})
|
2017-10-23 08:08:34 +00:00
|
|
|
return nil
|
2017-10-23 08:08:33 +00:00
|
|
|
}
|
|
|
|
|
2018-09-27 14:00:51 +00:00
|
|
|
// AddAliasCheck creates an alias check. When any check for the srcServiceID is
|
|
|
|
// changed, checkID will reflect that using the same semantics as
|
2018-06-30 14:23:47 +00:00
|
|
|
// checks.CheckAlias.
|
|
|
|
//
|
2018-09-27 14:00:51 +00:00
|
|
|
// This is a local optimization so that the Alias check doesn't need to use
|
|
|
|
// blocking queries against the remote server for check updates for local
|
|
|
|
// services.
|
2019-12-10 02:26:41 +00:00
|
|
|
func (l *State) AddAliasCheck(checkID structs.CheckID, srcServiceID structs.ServiceID, notifyCh chan<- struct{}) error {
|
2018-06-30 14:23:47 +00:00
|
|
|
l.Lock()
|
|
|
|
defer l.Unlock()
|
|
|
|
|
2021-08-19 20:09:42 +00:00
|
|
|
if l.agentEnterpriseMeta.PartitionOrDefault() != checkID.PartitionOrDefault() {
|
2021-08-21 02:03:24 +00:00
|
|
|
return fmt.Errorf("cannot add alias check ID %q to node in partition %q", checkID.String(), l.config.Partition)
|
2021-08-19 20:09:42 +00:00
|
|
|
}
|
|
|
|
if l.agentEnterpriseMeta.PartitionOrDefault() != srcServiceID.PartitionOrDefault() {
|
|
|
|
return fmt.Errorf("cannot add alias check for %q to node in partition %q", srcServiceID.String(), l.config.Partition)
|
|
|
|
}
|
|
|
|
|
2018-06-30 14:23:47 +00:00
|
|
|
m, ok := l.checkAliases[srcServiceID]
|
|
|
|
if !ok {
|
2019-12-10 02:26:41 +00:00
|
|
|
m = make(map[structs.CheckID]chan<- struct{})
|
2018-06-30 14:23:47 +00:00
|
|
|
l.checkAliases[srcServiceID] = m
|
|
|
|
}
|
|
|
|
m[checkID] = notifyCh
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-06-04 12:50:52 +00:00
|
|
|
// ServiceExists return true if the given service does exists
|
|
|
|
func (l *State) ServiceExists(serviceID structs.ServiceID) bool {
|
2020-06-05 08:41:39 +00:00
|
|
|
serviceID.EnterpriseMeta.Normalize()
|
|
|
|
|
2020-06-04 12:50:52 +00:00
|
|
|
l.Lock()
|
|
|
|
defer l.Unlock()
|
|
|
|
return l.services[serviceID] != nil
|
|
|
|
}
|
|
|
|
|
2018-06-30 14:23:47 +00:00
|
|
|
// RemoveAliasCheck removes the mapping for the alias check.
|
2019-12-10 02:26:41 +00:00
|
|
|
func (l *State) RemoveAliasCheck(checkID structs.CheckID, srcServiceID structs.ServiceID) {
|
2018-06-30 14:23:47 +00:00
|
|
|
l.Lock()
|
|
|
|
defer l.Unlock()
|
|
|
|
|
|
|
|
if m, ok := l.checkAliases[srcServiceID]; ok {
|
|
|
|
delete(m, checkID)
|
|
|
|
if len(m) == 0 {
|
|
|
|
delete(l.checkAliases, srcServiceID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-16 01:14:50 +00:00
|
|
|
// RemoveCheck is used to remove a health check from the local state.
|
|
|
|
// The agent will make a best effort to ensure it is deregistered
|
2019-03-06 17:13:28 +00:00
|
|
|
// todo(fs): RemoveService returns an error for a non-existent service. RemoveCheck should as well.
|
2017-08-28 12:17:12 +00:00
|
|
|
// todo(fs): Check code that calls this to handle the error.
|
2019-12-10 02:26:41 +00:00
|
|
|
func (l *State) RemoveCheck(id structs.CheckID) error {
|
2014-01-21 19:52:25 +00:00
|
|
|
l.Lock()
|
|
|
|
defer l.Unlock()
|
2019-03-04 14:34:05 +00:00
|
|
|
return l.removeCheckLocked(id)
|
|
|
|
}
|
2014-01-16 01:14:50 +00:00
|
|
|
|
2019-12-10 02:26:41 +00:00
|
|
|
func (l *State) removeCheckLocked(id structs.CheckID) error {
|
2017-08-28 12:17:12 +00:00
|
|
|
c := l.checks[id]
|
|
|
|
if c == nil || c.Deleted {
|
2021-08-21 02:03:24 +00:00
|
|
|
return fmt.Errorf("Check ID %q does not exist", id)
|
2017-08-28 12:17:12 +00:00
|
|
|
}
|
|
|
|
|
2019-04-24 18:17:06 +00:00
|
|
|
// If this is a check for an aliased service, then notify the waiters.
|
2019-12-10 02:26:41 +00:00
|
|
|
l.notifyIfAliased(c.Check.CompoundServiceID())
|
2019-04-24 18:17:06 +00:00
|
|
|
|
2017-08-28 12:17:12 +00:00
|
|
|
// To remove the check on the server we need the token.
|
|
|
|
// Therefore, we mark the service as deleted and keep the
|
|
|
|
// entry around until it is actually removed.
|
|
|
|
c.InSync = false
|
|
|
|
c.Deleted = true
|
2017-08-30 10:25:49 +00:00
|
|
|
l.TriggerSyncChanges()
|
2017-08-28 12:17:12 +00:00
|
|
|
|
|
|
|
return nil
|
2014-01-16 01:14:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateCheck is used to update the status of a check
|
2019-12-10 02:26:41 +00:00
|
|
|
func (l *State) UpdateCheck(id structs.CheckID, status, output string) {
|
2014-01-21 19:52:25 +00:00
|
|
|
l.Lock()
|
|
|
|
defer l.Unlock()
|
2014-01-16 01:14:50 +00:00
|
|
|
|
2017-08-28 12:17:12 +00:00
|
|
|
c := l.checks[id]
|
|
|
|
if c == nil || c.Deleted {
|
2014-01-16 01:14:50 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-10-11 00:04:52 +00:00
|
|
|
if l.discardCheckOutput.Load().(bool) {
|
|
|
|
output = ""
|
|
|
|
}
|
|
|
|
|
2016-08-16 07:05:55 +00:00
|
|
|
// Update the critical time tracking (this doesn't cause a server updates
|
|
|
|
// so we can always keep this up to date).
|
2017-04-19 23:00:11 +00:00
|
|
|
if status == api.HealthCritical {
|
2017-08-28 12:17:12 +00:00
|
|
|
if !c.Critical() {
|
|
|
|
c.CriticalTime = time.Now()
|
2016-08-16 07:05:55 +00:00
|
|
|
}
|
|
|
|
} else {
|
2017-08-28 12:17:12 +00:00
|
|
|
c.CriticalTime = time.Time{}
|
2016-08-16 07:05:55 +00:00
|
|
|
}
|
|
|
|
|
2014-01-16 01:14:50 +00:00
|
|
|
// Do nothing if update is idempotent
|
2017-08-28 12:17:12 +00:00
|
|
|
if c.Check.Status == status && c.Check.Output == output {
|
2014-01-16 01:14:50 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-05-24 18:36:56 +00:00
|
|
|
// Ensure we only mutate a copy of the check state and put the finalized
|
|
|
|
// version into the checks map when complete.
|
|
|
|
//
|
|
|
|
// Note that we are relying upon the earlier deferred mutex unlock to
|
|
|
|
// happen AFTER this defer. As per the Go spec this is true, but leaving
|
|
|
|
// this note here for the future in case of any refactorings which may not
|
|
|
|
// notice this relationship.
|
|
|
|
c = c.Clone()
|
|
|
|
defer func(c *CheckState) {
|
|
|
|
l.checks[id] = c
|
|
|
|
}(c)
|
|
|
|
|
2014-06-09 19:46:29 +00:00
|
|
|
// Defer a sync if the output has changed. This is an optimization around
|
|
|
|
// frequent updates of output. Instead, we update the output internally,
|
|
|
|
// and periodically do a write-back to the servers. If there is a status
|
|
|
|
// change we do the write immediately.
|
2017-08-28 12:17:12 +00:00
|
|
|
if l.config.CheckUpdateInterval > 0 && c.Check.Status == status {
|
|
|
|
c.Check.Output = output
|
|
|
|
if c.DeferCheck == nil {
|
|
|
|
d := l.config.CheckUpdateInterval
|
|
|
|
intv := time.Duration(uint64(d)/2) + lib.RandomStagger(d)
|
|
|
|
c.DeferCheck = time.AfterFunc(intv, func() {
|
2014-06-09 19:46:29 +00:00
|
|
|
l.Lock()
|
2017-08-28 12:17:12 +00:00
|
|
|
defer l.Unlock()
|
|
|
|
|
|
|
|
c := l.checks[id]
|
|
|
|
if c == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c.DeferCheck = nil
|
|
|
|
if c.Deleted {
|
|
|
|
return
|
2014-06-09 23:00:25 +00:00
|
|
|
}
|
2017-08-28 12:17:12 +00:00
|
|
|
c.InSync = false
|
2017-08-30 10:25:49 +00:00
|
|
|
l.TriggerSyncChanges()
|
2014-06-09 19:46:29 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-06-30 14:23:47 +00:00
|
|
|
// If this is a check for an aliased service, then notify the waiters.
|
2019-12-10 02:26:41 +00:00
|
|
|
l.notifyIfAliased(c.Check.CompoundServiceID())
|
2018-06-30 14:23:47 +00:00
|
|
|
|
2014-01-16 01:14:50 +00:00
|
|
|
// Update status and mark out of sync
|
2017-08-28 12:17:12 +00:00
|
|
|
c.Check.Status = status
|
|
|
|
c.Check.Output = output
|
|
|
|
c.InSync = false
|
2017-08-30 10:25:49 +00:00
|
|
|
l.TriggerSyncChanges()
|
2014-01-16 01:14:50 +00:00
|
|
|
}
|
|
|
|
|
2017-08-28 12:17:12 +00:00
|
|
|
// Check returns the locally registered check that the
|
|
|
|
// agent is aware of and are being kept in sync with the server
|
2019-12-10 02:26:41 +00:00
|
|
|
func (l *State) Check(id structs.CheckID) *structs.HealthCheck {
|
2017-08-28 12:17:12 +00:00
|
|
|
l.RLock()
|
|
|
|
defer l.RUnlock()
|
2017-08-28 12:17:12 +00:00
|
|
|
|
|
|
|
c := l.checks[id]
|
|
|
|
if c == nil || c.Deleted {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return c.Check
|
2017-08-28 12:17:12 +00:00
|
|
|
}
|
|
|
|
|
2021-08-19 20:09:42 +00:00
|
|
|
// AllChecks returns the locally registered checks that the
|
|
|
|
// agent is aware of and are being kept in sync with the server
|
|
|
|
func (l *State) AllChecks() map[structs.CheckID]*structs.HealthCheck {
|
|
|
|
return l.listChecks(false, nil)
|
|
|
|
}
|
|
|
|
|
2014-01-21 01:00:52 +00:00
|
|
|
// Checks returns the locally registered checks that the
|
|
|
|
// agent is aware of and are being kept in sync with the server
|
2021-08-19 20:09:42 +00:00
|
|
|
//
|
|
|
|
// Results are scoped to the provided namespace and partition.
|
2019-12-10 02:26:41 +00:00
|
|
|
func (l *State) Checks(entMeta *structs.EnterpriseMeta) map[structs.CheckID]*structs.HealthCheck {
|
2021-08-19 20:09:42 +00:00
|
|
|
return l.listChecks(true, entMeta)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *State) listChecks(filtered bool, entMeta *structs.EnterpriseMeta) map[structs.CheckID]*structs.HealthCheck {
|
2019-12-10 02:26:41 +00:00
|
|
|
m := make(map[structs.CheckID]*structs.HealthCheck)
|
2021-08-19 20:09:42 +00:00
|
|
|
for id, c := range l.listCheckStates(filtered, entMeta) {
|
2017-08-28 12:17:13 +00:00
|
|
|
m[id] = c.Check
|
|
|
|
}
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
2019-12-10 02:26:41 +00:00
|
|
|
func (l *State) ChecksForService(serviceID structs.ServiceID, includeNodeChecks bool) map[structs.CheckID]*structs.HealthCheck {
|
|
|
|
m := make(map[structs.CheckID]*structs.HealthCheck)
|
|
|
|
|
|
|
|
l.RLock()
|
|
|
|
defer l.RUnlock()
|
|
|
|
|
|
|
|
for id, c := range l.checks {
|
|
|
|
if c.Deleted {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.Check.ServiceID != "" {
|
|
|
|
sid := c.Check.CompoundServiceID()
|
2020-12-11 21:10:00 +00:00
|
|
|
if !serviceID.Matches(sid) {
|
2019-12-10 02:26:41 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
} else if !includeNodeChecks {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
m[id] = c.Check.Clone()
|
|
|
|
}
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
2019-05-24 18:36:56 +00:00
|
|
|
// CheckState returns a shallow copy of the current health check state record.
|
|
|
|
//
|
|
|
|
// The defer timer still points to the original value and must not be modified.
|
2019-12-10 02:26:41 +00:00
|
|
|
func (l *State) CheckState(id structs.CheckID) *CheckState {
|
2017-08-28 12:17:13 +00:00
|
|
|
l.RLock()
|
|
|
|
defer l.RUnlock()
|
|
|
|
|
2017-08-28 12:17:13 +00:00
|
|
|
c := l.checks[id]
|
|
|
|
if c == nil || c.Deleted {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return c.Clone()
|
|
|
|
}
|
|
|
|
|
2017-10-18 13:07:19 +00:00
|
|
|
// SetCheckState is used to overwrite a raw check state with the given
|
|
|
|
// state. This method is safe to be called concurrently but should only be used
|
|
|
|
// during testing. You should most likely call AddCheck instead.
|
|
|
|
func (l *State) SetCheckState(c *CheckState) {
|
|
|
|
l.Lock()
|
|
|
|
defer l.Unlock()
|
|
|
|
|
2021-08-19 20:09:42 +00:00
|
|
|
if l.agentEnterpriseMeta.PartitionOrDefault() != c.Check.PartitionOrDefault() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-03-04 14:34:05 +00:00
|
|
|
l.setCheckStateLocked(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *State) setCheckStateLocked(c *CheckState) {
|
2020-11-25 23:24:13 +00:00
|
|
|
id := c.Check.CompoundCheckID()
|
|
|
|
existing := l.checks[id]
|
|
|
|
if existing != nil {
|
|
|
|
c.InSync = c.Check.IsSame(existing.Check)
|
|
|
|
}
|
|
|
|
|
|
|
|
l.checks[id] = c
|
2019-04-24 18:17:06 +00:00
|
|
|
|
|
|
|
// If this is a check for an aliased service, then notify the waiters.
|
2019-12-10 02:26:41 +00:00
|
|
|
l.notifyIfAliased(c.Check.CompoundServiceID())
|
2019-04-24 18:17:06 +00:00
|
|
|
|
2017-10-18 13:07:19 +00:00
|
|
|
l.TriggerSyncChanges()
|
|
|
|
}
|
|
|
|
|
2021-08-19 20:09:42 +00:00
|
|
|
// AllCheckStates returns a shallow copy of all health check state records.
|
|
|
|
// The map contains a shallow copy of the current check states.
|
|
|
|
//
|
|
|
|
// The defer timers still point to the original values and must not be modified.
|
|
|
|
func (l *State) AllCheckStates() map[structs.CheckID]*CheckState {
|
|
|
|
return l.listCheckStates(false, nil)
|
|
|
|
}
|
|
|
|
|
2017-08-28 12:17:13 +00:00
|
|
|
// CheckStates returns a shallow copy of all health check state records.
|
2019-05-24 18:36:56 +00:00
|
|
|
// The map contains a shallow copy of the current check states.
|
|
|
|
//
|
|
|
|
// The defer timers still point to the original values and must not be modified.
|
2021-08-19 20:09:42 +00:00
|
|
|
//
|
|
|
|
// Results are scoped to the provided namespace and partition.
|
2019-12-10 02:26:41 +00:00
|
|
|
func (l *State) CheckStates(entMeta *structs.EnterpriseMeta) map[structs.CheckID]*CheckState {
|
2021-08-19 20:09:42 +00:00
|
|
|
return l.listCheckStates(true, entMeta)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *State) listCheckStates(filtered bool, entMeta *structs.EnterpriseMeta) map[structs.CheckID]*CheckState {
|
2017-08-28 12:17:13 +00:00
|
|
|
l.RLock()
|
|
|
|
defer l.RUnlock()
|
|
|
|
|
2019-12-10 02:26:41 +00:00
|
|
|
m := make(map[structs.CheckID]*CheckState)
|
2017-09-26 11:42:10 +00:00
|
|
|
for id, c := range l.checks {
|
2017-08-28 12:17:12 +00:00
|
|
|
if c.Deleted {
|
|
|
|
continue
|
|
|
|
}
|
2021-08-19 20:09:42 +00:00
|
|
|
if filtered && !entMeta.Matches(&id.EnterpriseMeta) {
|
2019-12-10 02:26:41 +00:00
|
|
|
continue
|
|
|
|
}
|
2017-08-28 12:17:13 +00:00
|
|
|
m[id] = c.Clone()
|
2014-01-21 01:00:52 +00:00
|
|
|
}
|
2017-08-28 12:17:12 +00:00
|
|
|
return m
|
2017-10-23 08:08:34 +00:00
|
|
|
}
|
|
|
|
|
2021-08-19 20:09:42 +00:00
|
|
|
// AllCriticalCheckStates returns the locally registered checks that the
|
|
|
|
// agent is aware of and are being kept in sync with the server.
|
|
|
|
// The map contains a shallow copy of the current check states.
|
|
|
|
//
|
|
|
|
// The defer timers still point to the original values and must not be modified.
|
|
|
|
func (l *State) AllCriticalCheckStates() map[structs.CheckID]*CheckState {
|
|
|
|
return l.listCriticalCheckStates(false, nil)
|
|
|
|
}
|
|
|
|
|
2017-08-28 12:17:12 +00:00
|
|
|
// CriticalCheckStates returns the locally registered checks that the
|
2017-10-18 13:05:57 +00:00
|
|
|
// agent is aware of and are being kept in sync with the server.
|
2019-05-24 18:36:56 +00:00
|
|
|
// The map contains a shallow copy of the current check states.
|
|
|
|
//
|
|
|
|
// The defer timers still point to the original values and must not be modified.
|
2021-08-19 20:09:42 +00:00
|
|
|
//
|
|
|
|
// Results are scoped to the provided namespace and partition.
|
2019-12-10 02:26:41 +00:00
|
|
|
func (l *State) CriticalCheckStates(entMeta *structs.EnterpriseMeta) map[structs.CheckID]*CheckState {
|
2021-08-19 20:09:42 +00:00
|
|
|
return l.listCriticalCheckStates(true, entMeta)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *State) listCriticalCheckStates(filtered bool, entMeta *structs.EnterpriseMeta) map[structs.CheckID]*CheckState {
|
2016-08-16 07:05:55 +00:00
|
|
|
l.RLock()
|
|
|
|
defer l.RUnlock()
|
|
|
|
|
2019-12-10 02:26:41 +00:00
|
|
|
m := make(map[structs.CheckID]*CheckState)
|
2017-08-28 12:17:12 +00:00
|
|
|
for id, c := range l.checks {
|
|
|
|
if c.Deleted || !c.Critical() {
|
|
|
|
continue
|
2016-08-16 07:05:55 +00:00
|
|
|
}
|
2021-08-19 20:09:42 +00:00
|
|
|
if filtered && !entMeta.Matches(&id.EnterpriseMeta) {
|
2019-12-10 02:26:41 +00:00
|
|
|
continue
|
|
|
|
}
|
2017-08-28 12:17:13 +00:00
|
|
|
m[id] = c.Clone()
|
2016-08-16 07:05:55 +00:00
|
|
|
}
|
2017-08-28 12:17:12 +00:00
|
|
|
return m
|
2016-08-16 07:05:55 +00:00
|
|
|
}
|
|
|
|
|
2018-10-03 12:36:38 +00:00
|
|
|
// broadcastUpdateLocked assumes l is locked and delivers an update to all
|
|
|
|
// registered watchers.
|
|
|
|
func (l *State) broadcastUpdateLocked() {
|
|
|
|
for ch := range l.notifyHandlers {
|
|
|
|
// Do not block
|
|
|
|
select {
|
|
|
|
case ch <- struct{}{}:
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Notify will register a channel to receive messages when the local state
|
|
|
|
// changes. Only service add/remove are supported for now. See notes on
|
|
|
|
// l.notifyHandlers for more details.
|
|
|
|
//
|
|
|
|
// This will not block on channel send so ensure the channel has a buffer. Note
|
|
|
|
// that any buffer size is generally fine since actual data is not sent over the
|
|
|
|
// channel, so a dropped send due to a full buffer does not result in any loss
|
|
|
|
// of data. The fact that a buffer already contains a notification means that
|
|
|
|
// the receiver will still be notified that changes occurred.
|
|
|
|
func (l *State) Notify(ch chan<- struct{}) {
|
|
|
|
l.Lock()
|
|
|
|
defer l.Unlock()
|
|
|
|
l.notifyHandlers[ch] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// StopNotify will deregister a channel receiving state change notifications.
|
|
|
|
// Pair this with all calls to Notify to clean up state.
|
|
|
|
func (l *State) StopNotify(ch chan<- struct{}) {
|
|
|
|
l.Lock()
|
|
|
|
defer l.Unlock()
|
|
|
|
delete(l.notifyHandlers, ch)
|
|
|
|
}
|
|
|
|
|
2017-01-05 22:10:26 +00:00
|
|
|
// Metadata returns the local node metadata fields that the
|
|
|
|
// agent is aware of and are being kept in sync with the server
|
2017-08-28 12:17:12 +00:00
|
|
|
func (l *State) Metadata() map[string]string {
|
2017-01-05 22:10:26 +00:00
|
|
|
l.RLock()
|
|
|
|
defer l.RUnlock()
|
2017-10-18 13:11:49 +00:00
|
|
|
|
2017-08-28 12:17:12 +00:00
|
|
|
m := make(map[string]string)
|
|
|
|
for k, v := range l.metadata {
|
|
|
|
m[k] = v
|
2017-01-05 22:10:26 +00:00
|
|
|
}
|
2017-08-28 12:17:12 +00:00
|
|
|
return m
|
2017-01-05 22:10:26 +00:00
|
|
|
}
|
|
|
|
|
2017-10-18 13:12:28 +00:00
|
|
|
// LoadMetadata loads node metadata fields from the agent config and
|
|
|
|
// updates them on the local agent.
|
|
|
|
func (l *State) LoadMetadata(data map[string]string) error {
|
|
|
|
l.Lock()
|
|
|
|
defer l.Unlock()
|
|
|
|
|
|
|
|
for k, v := range data {
|
|
|
|
l.metadata[k] = v
|
|
|
|
}
|
|
|
|
l.TriggerSyncChanges()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnloadMetadata resets the local metadata state
|
|
|
|
func (l *State) UnloadMetadata() {
|
|
|
|
l.Lock()
|
|
|
|
defer l.Unlock()
|
|
|
|
l.metadata = make(map[string]string)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stats is used to get various debugging state from the sub-systems
|
|
|
|
func (l *State) Stats() map[string]string {
|
|
|
|
l.RLock()
|
|
|
|
defer l.RUnlock()
|
|
|
|
|
|
|
|
services := 0
|
|
|
|
for _, s := range l.services {
|
|
|
|
if s.Deleted {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
services++
|
|
|
|
}
|
|
|
|
|
|
|
|
checks := 0
|
|
|
|
for _, c := range l.checks {
|
|
|
|
if c.Deleted {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
checks++
|
|
|
|
}
|
|
|
|
|
|
|
|
return map[string]string{
|
|
|
|
"services": strconv.Itoa(services),
|
|
|
|
"checks": strconv.Itoa(checks),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-25 23:24:13 +00:00
|
|
|
// updateSyncState queries the server for all the services and checks in the catalog
|
|
|
|
// registered to this node, and updates the local entries as InSync or Deleted.
|
2017-08-28 12:17:16 +00:00
|
|
|
func (l *State) updateSyncState() error {
|
2017-10-18 13:11:49 +00:00
|
|
|
// Get all checks and services from the master
|
2014-01-16 01:14:50 +00:00
|
|
|
req := structs.NodeSpecificRequest{
|
2019-06-17 16:05:47 +00:00
|
|
|
Datacenter: l.config.Datacenter,
|
|
|
|
Node: l.config.NodeName,
|
|
|
|
QueryOptions: structs.QueryOptions{
|
|
|
|
Token: l.tokens.AgentToken(),
|
|
|
|
AllowStale: true,
|
|
|
|
MaxStaleDuration: fullSyncReadMaxStale,
|
|
|
|
},
|
2021-09-17 23:36:20 +00:00
|
|
|
EnterpriseMeta: *l.agentEnterpriseMeta.WithWildcardNamespace(),
|
2014-01-16 01:14:50 +00:00
|
|
|
}
|
2017-08-28 12:17:12 +00:00
|
|
|
|
2019-12-10 02:26:41 +00:00
|
|
|
var out1 structs.IndexedNodeServiceList
|
|
|
|
remoteServices := make(map[structs.ServiceID]*structs.NodeService)
|
|
|
|
var svcNode *structs.Node
|
|
|
|
|
|
|
|
if err := l.Delegate.RPC("Catalog.NodeServiceList", &req, &out1); err == nil {
|
|
|
|
for _, svc := range out1.NodeServices.Services {
|
|
|
|
remoteServices[svc.CompoundServiceID()] = svc
|
|
|
|
}
|
|
|
|
|
|
|
|
svcNode = out1.NodeServices.Node
|
|
|
|
} else if errMsg := err.Error(); strings.Contains(errMsg, "rpc: can't find method") {
|
|
|
|
// fallback to the old RPC
|
|
|
|
var out1 structs.IndexedNodeServices
|
|
|
|
if err := l.Delegate.RPC("Catalog.NodeServices", &req, &out1); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if out1.NodeServices != nil {
|
|
|
|
for _, svc := range out1.NodeServices.Services {
|
|
|
|
remoteServices[svc.CompoundServiceID()] = svc
|
|
|
|
}
|
|
|
|
|
|
|
|
svcNode = out1.NodeServices.Node
|
|
|
|
}
|
|
|
|
} else {
|
2017-08-28 12:17:12 +00:00
|
|
|
return err
|
2017-10-23 08:08:34 +00:00
|
|
|
}
|
2017-08-28 12:17:12 +00:00
|
|
|
|
|
|
|
var out2 structs.IndexedHealthChecks
|
2017-08-30 10:25:49 +00:00
|
|
|
if err := l.Delegate.RPC("Health.NodeChecks", &req, &out2); err != nil {
|
2014-01-16 01:14:50 +00:00
|
|
|
return err
|
|
|
|
}
|
2017-08-28 12:17:12 +00:00
|
|
|
|
2019-12-10 02:26:41 +00:00
|
|
|
remoteChecks := make(map[structs.CheckID]*structs.HealthCheck, len(out2.HealthChecks))
|
2017-08-28 12:17:12 +00:00
|
|
|
for _, rc := range out2.HealthChecks {
|
2019-12-10 02:26:41 +00:00
|
|
|
remoteChecks[rc.CompoundCheckID()] = rc
|
2017-08-28 12:17:12 +00:00
|
|
|
}
|
|
|
|
|
2017-10-18 13:11:49 +00:00
|
|
|
// Traverse all checks, services and the node info to determine
|
|
|
|
// which entries need to be updated on or removed from the server
|
2014-01-16 01:14:50 +00:00
|
|
|
|
2014-01-21 19:52:25 +00:00
|
|
|
l.Lock()
|
|
|
|
defer l.Unlock()
|
2014-01-16 01:14:50 +00:00
|
|
|
|
2017-10-18 13:11:49 +00:00
|
|
|
// Check if node info needs syncing
|
2019-12-10 02:26:41 +00:00
|
|
|
if svcNode == nil || svcNode.ID != l.config.NodeID ||
|
|
|
|
!reflect.DeepEqual(svcNode.TaggedAddresses, l.config.TaggedAddresses) ||
|
|
|
|
!reflect.DeepEqual(svcNode.Meta, l.metadata) {
|
2016-02-07 21:12:42 +00:00
|
|
|
l.nodeInfoInSync = false
|
|
|
|
}
|
2017-10-18 13:11:49 +00:00
|
|
|
// Check which services need syncing
|
2015-04-10 18:04:15 +00:00
|
|
|
|
2017-10-18 13:11:49 +00:00
|
|
|
// Look for local services that do not exist remotely and mark them for
|
|
|
|
// syncing so that they will be pushed to the server later
|
2017-08-28 12:17:12 +00:00
|
|
|
for id, s := range l.services {
|
|
|
|
if remoteServices[id] == nil {
|
|
|
|
s.InSync = false
|
2015-04-08 19:20:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-18 13:11:49 +00:00
|
|
|
// Traverse the list of services from the server.
|
|
|
|
// Remote services which do not exist locally have been deregistered.
|
|
|
|
// Otherwise, check whether the two definitions are still in sync.
|
2017-08-28 12:17:12 +00:00
|
|
|
for id, rs := range remoteServices {
|
|
|
|
ls := l.services[id]
|
|
|
|
if ls == nil {
|
2017-10-18 13:11:49 +00:00
|
|
|
// The consul service is managed automatically and does
|
|
|
|
// not need to be deregistered
|
2021-08-19 20:09:42 +00:00
|
|
|
if structs.IsConsulServiceID(id) {
|
2017-07-14 05:33:47 +00:00
|
|
|
continue
|
|
|
|
}
|
2017-08-28 12:17:12 +00:00
|
|
|
|
2017-10-18 13:11:49 +00:00
|
|
|
// Mark a remote service that does not exist locally as deleted so
|
|
|
|
// that it will be removed on the server later.
|
2017-08-28 12:17:12 +00:00
|
|
|
l.services[id] = &ServiceState{Deleted: true}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2017-08-30 10:25:49 +00:00
|
|
|
// If the service is already scheduled for removal skip it
|
2017-08-28 12:17:12 +00:00
|
|
|
if ls.Deleted {
|
2015-04-10 18:04:15 +00:00
|
|
|
continue
|
2014-03-05 23:03:23 +00:00
|
|
|
}
|
2015-04-10 18:04:15 +00:00
|
|
|
|
2016-04-11 21:53:18 +00:00
|
|
|
// If our definition is different, we need to update it. Make a
|
|
|
|
// copy so that we don't retain a pointer to any actual state
|
|
|
|
// store info for in-memory RPCs.
|
2022-01-11 19:28:51 +00:00
|
|
|
if ls.Service.EnableTagOverride {
|
|
|
|
ls.Service.Tags = make([]string, len(rs.Tags))
|
|
|
|
copy(ls.Service.Tags, rs.Tags)
|
2015-08-18 21:03:48 +00:00
|
|
|
}
|
2021-12-02 23:42:47 +00:00
|
|
|
|
|
|
|
// Merge any tagged addresses with the consul- prefix (set by the server)
|
|
|
|
// back into the local state.
|
2022-01-11 19:28:51 +00:00
|
|
|
if !reflect.DeepEqual(ls.Service.TaggedAddresses, rs.TaggedAddresses) {
|
|
|
|
// Make a copy of TaggedAddresses to prevent races when writing
|
|
|
|
// since other goroutines may be reading from the map
|
|
|
|
m := make(map[string]structs.ServiceAddress)
|
|
|
|
for k, v := range ls.Service.TaggedAddresses {
|
|
|
|
m[k] = v
|
2021-12-02 23:42:47 +00:00
|
|
|
}
|
|
|
|
for k, v := range rs.TaggedAddresses {
|
|
|
|
if strings.HasPrefix(k, structs.MetaKeyReservedPrefix) {
|
2022-01-11 19:28:51 +00:00
|
|
|
m[k] = v
|
2021-12-02 23:42:47 +00:00
|
|
|
}
|
|
|
|
}
|
2022-01-11 19:28:51 +00:00
|
|
|
ls.Service.TaggedAddresses = m
|
2021-12-02 23:42:47 +00:00
|
|
|
}
|
2022-01-11 19:28:51 +00:00
|
|
|
ls.InSync = ls.Service.IsSame(rs)
|
2014-01-16 01:14:50 +00:00
|
|
|
}
|
|
|
|
|
2017-10-18 13:11:49 +00:00
|
|
|
// Check which checks need syncing
|
2015-10-13 03:30:11 +00:00
|
|
|
|
2017-10-18 13:11:49 +00:00
|
|
|
// Look for local checks that do not exist remotely and mark them for
|
|
|
|
// syncing so that they will be pushed to the server later
|
2017-08-28 12:17:12 +00:00
|
|
|
for id, c := range l.checks {
|
|
|
|
if remoteChecks[id] == nil {
|
|
|
|
c.InSync = false
|
2015-04-08 19:20:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-18 13:11:49 +00:00
|
|
|
// Traverse the list of checks from the server.
|
|
|
|
// Remote checks which do not exist locally have been deregistered.
|
|
|
|
// Otherwise, check whether the two definitions are still in sync.
|
2017-08-28 12:17:12 +00:00
|
|
|
for id, rc := range remoteChecks {
|
|
|
|
lc := l.checks[id]
|
|
|
|
|
|
|
|
if lc == nil {
|
|
|
|
// The Serf check is created automatically and does not
|
2017-07-14 05:33:47 +00:00
|
|
|
// need to be deregistered.
|
2021-08-19 20:09:42 +00:00
|
|
|
if structs.IsSerfCheckID(id) {
|
2020-01-28 23:50:41 +00:00
|
|
|
l.logger.Debug("Skipping remote check since it is managed automatically", "check", structs.SerfCheckID)
|
2014-01-16 03:28:23 +00:00
|
|
|
continue
|
|
|
|
}
|
2017-08-28 12:17:12 +00:00
|
|
|
|
2017-10-18 13:11:49 +00:00
|
|
|
// Mark a remote check that does not exist locally as deleted so
|
|
|
|
// that it will be removed on the server later.
|
2017-08-28 12:17:12 +00:00
|
|
|
l.checks[id] = &CheckState{Deleted: true}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2017-08-30 10:25:49 +00:00
|
|
|
// If the check is already scheduled for removal skip it.
|
2017-08-28 12:17:12 +00:00
|
|
|
if lc.Deleted {
|
2014-01-16 01:14:50 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// If our definition is different, we need to update it
|
2014-06-09 23:00:25 +00:00
|
|
|
if l.config.CheckUpdateInterval == 0 {
|
2017-08-28 12:17:12 +00:00
|
|
|
lc.InSync = lc.Check.IsSame(rc)
|
|
|
|
continue
|
2014-06-09 23:00:25 +00:00
|
|
|
}
|
|
|
|
|
2017-08-28 12:17:12 +00:00
|
|
|
// Copy the existing check before potentially modifying
|
|
|
|
// it before the compare operation.
|
|
|
|
lcCopy := lc.Check.Clone()
|
|
|
|
|
|
|
|
// Copy the server's check before modifying, otherwise
|
|
|
|
// in-memory RPCs will have side effects.
|
|
|
|
rcCopy := rc.Clone()
|
|
|
|
|
|
|
|
// If there's a defer timer active then we've got a
|
|
|
|
// potentially spammy check so we don't sync the output
|
|
|
|
// during this sweep since the timer will mark the check
|
|
|
|
// out of sync for us. Otherwise, it is safe to sync the
|
|
|
|
// output now. This is especially important for checks
|
|
|
|
// that don't change state after they are created, in
|
|
|
|
// which case we'd never see their output synced back ever.
|
|
|
|
if lc.DeferCheck != nil {
|
|
|
|
lcCopy.Output = ""
|
|
|
|
rcCopy.Output = ""
|
|
|
|
}
|
|
|
|
lc.InSync = lcCopy.IsSame(rcCopy)
|
2014-01-16 01:14:50 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-08-28 12:17:16 +00:00
|
|
|
// SyncFull determines the delta between the local and remote state
|
|
|
|
// and synchronizes the changes.
|
|
|
|
func (l *State) SyncFull() error {
|
|
|
|
// note that we do not acquire the lock here since the methods
|
2018-03-19 16:56:00 +00:00
|
|
|
// we are calling will do that themselves.
|
2017-08-30 10:25:49 +00:00
|
|
|
//
|
|
|
|
// Also note that we don't hold the lock for the entire operation
|
|
|
|
// but release it between the two calls. This is not an issue since
|
|
|
|
// the algorithm is best-effort to achieve eventual consistency.
|
|
|
|
// SyncChanges will sync whatever updateSyncState() has determined
|
|
|
|
// needs updating.
|
2017-08-28 12:17:16 +00:00
|
|
|
|
|
|
|
if err := l.updateSyncState(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return l.SyncChanges()
|
|
|
|
}
|
|
|
|
|
2017-10-18 13:11:49 +00:00
|
|
|
// SyncChanges pushes checks, services and node info data which has been
|
|
|
|
// marked out of sync or deleted to the server.
|
2017-08-28 12:17:12 +00:00
|
|
|
func (l *State) SyncChanges() error {
|
2014-01-21 19:52:25 +00:00
|
|
|
l.Lock()
|
|
|
|
defer l.Unlock()
|
2014-01-16 01:14:50 +00:00
|
|
|
|
2020-02-06 14:30:58 +00:00
|
|
|
// Sync the node level info if we need to.
|
2021-11-16 19:16:11 +00:00
|
|
|
// At the start to guarantee sync even if services or checks fail,
|
|
|
|
// which is more likely because there are more syncs happening for them.
|
|
|
|
|
2020-02-06 14:30:58 +00:00
|
|
|
if l.nodeInfoInSync {
|
|
|
|
l.logger.Debug("Node info in sync")
|
|
|
|
} else {
|
|
|
|
if err := l.syncNodeInfo(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-14 19:48:36 +00:00
|
|
|
// Sync the services
|
2017-10-18 13:11:49 +00:00
|
|
|
// (logging happens in the helper methods)
|
2017-08-28 12:17:12 +00:00
|
|
|
for id, s := range l.services {
|
|
|
|
var err error
|
|
|
|
switch {
|
|
|
|
case s.Deleted:
|
|
|
|
err = l.deleteService(id)
|
|
|
|
case !s.InSync:
|
|
|
|
err = l.syncService(id)
|
|
|
|
default:
|
2020-01-28 23:50:41 +00:00
|
|
|
l.logger.Debug("Service in sync", "service", id.String())
|
2014-01-16 01:14:50 +00:00
|
|
|
}
|
2017-08-28 12:17:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-01-16 01:14:50 +00:00
|
|
|
}
|
|
|
|
|
2017-10-18 13:11:49 +00:00
|
|
|
// Sync the checks
|
|
|
|
// (logging happens in the helper methods)
|
2017-08-28 12:17:12 +00:00
|
|
|
for id, c := range l.checks {
|
|
|
|
var err error
|
|
|
|
switch {
|
|
|
|
case c.Deleted:
|
|
|
|
err = l.deleteCheck(id)
|
|
|
|
case !c.InSync:
|
|
|
|
if c.DeferCheck != nil {
|
|
|
|
c.DeferCheck.Stop()
|
|
|
|
c.DeferCheck = nil
|
2017-10-23 08:08:34 +00:00
|
|
|
}
|
2017-08-28 12:17:12 +00:00
|
|
|
err = l.syncCheck(id)
|
|
|
|
default:
|
2020-01-28 23:50:41 +00:00
|
|
|
l.logger.Debug("Check in sync", "check", id.String())
|
2014-01-16 01:14:50 +00:00
|
|
|
}
|
2017-08-28 12:17:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-01-16 01:14:50 +00:00
|
|
|
}
|
2020-02-06 14:30:58 +00:00
|
|
|
return nil
|
2014-01-16 01:14:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// deleteService is used to delete a service from the server
|
2019-12-10 02:26:41 +00:00
|
|
|
func (l *State) deleteService(key structs.ServiceID) error {
|
|
|
|
if key.ID == "" {
|
2015-01-27 09:11:57 +00:00
|
|
|
return fmt.Errorf("ServiceID missing")
|
|
|
|
}
|
|
|
|
|
2021-05-05 21:37:02 +00:00
|
|
|
st := l.aclTokenForServiceSync(key, l.tokens.AgentToken)
|
2014-01-16 01:14:50 +00:00
|
|
|
req := structs.DeregisterRequest{
|
2019-12-10 02:26:41 +00:00
|
|
|
Datacenter: l.config.Datacenter,
|
|
|
|
Node: l.config.NodeName,
|
|
|
|
ServiceID: key.ID,
|
|
|
|
EnterpriseMeta: key.EnterpriseMeta,
|
2020-01-27 19:54:32 +00:00
|
|
|
WriteRequest: structs.WriteRequest{Token: st},
|
2014-01-16 01:14:50 +00:00
|
|
|
}
|
|
|
|
var out struct{}
|
2017-08-30 10:25:49 +00:00
|
|
|
err := l.Delegate.RPC("Catalog.Deregister", &req, &out)
|
|
|
|
switch {
|
|
|
|
case err == nil || strings.Contains(err.Error(), "Unknown service"):
|
2019-12-10 02:26:41 +00:00
|
|
|
delete(l.services, key)
|
2020-01-17 13:26:53 +00:00
|
|
|
// service deregister also deletes associated checks
|
|
|
|
for _, c := range l.checks {
|
2020-04-23 16:16:50 +00:00
|
|
|
if c.Deleted && c.Check != nil {
|
|
|
|
sid := c.Check.CompoundServiceID()
|
2020-12-11 21:10:00 +00:00
|
|
|
if sid.Matches(key) {
|
2020-04-23 16:16:50 +00:00
|
|
|
l.pruneCheck(c.Check.CompoundCheckID())
|
|
|
|
}
|
2020-01-17 13:26:53 +00:00
|
|
|
}
|
|
|
|
}
|
2020-01-28 23:50:41 +00:00
|
|
|
l.logger.Info("Deregistered service", "service", key.ID)
|
2017-03-25 00:15:20 +00:00
|
|
|
return nil
|
2017-08-30 10:25:49 +00:00
|
|
|
|
2019-01-04 15:01:50 +00:00
|
|
|
case acl.IsErrPermissionDenied(err), acl.IsErrNotFound(err):
|
2017-08-30 10:25:49 +00:00
|
|
|
// todo(fs): mark the service to be in sync to prevent excessive retrying before next full sync
|
|
|
|
// todo(fs): some backoff strategy might be a better solution
|
2019-12-10 02:26:41 +00:00
|
|
|
l.services[key].InSync = true
|
2020-01-27 19:54:32 +00:00
|
|
|
accessorID := l.aclAccessorID(st)
|
2020-01-28 23:50:41 +00:00
|
|
|
l.logger.Warn("Service deregistration blocked by ACLs", "service", key.String(), "accessorID", accessorID)
|
2018-06-08 09:56:46 +00:00
|
|
|
metrics.IncrCounter([]string{"acl", "blocked", "service", "deregistration"}, 1)
|
2017-03-25 00:15:20 +00:00
|
|
|
return nil
|
2017-08-30 10:25:49 +00:00
|
|
|
|
|
|
|
default:
|
2020-01-28 23:50:41 +00:00
|
|
|
l.logger.Warn("Deregistering service failed.",
|
|
|
|
"service", key.String(),
|
|
|
|
"error", err,
|
|
|
|
)
|
2017-08-30 10:25:49 +00:00
|
|
|
return err
|
2014-01-16 01:14:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-16 07:05:55 +00:00
|
|
|
// deleteCheck is used to delete a check from the server
|
2019-12-10 02:26:41 +00:00
|
|
|
func (l *State) deleteCheck(key structs.CheckID) error {
|
|
|
|
if key.ID == "" {
|
2015-01-27 09:11:57 +00:00
|
|
|
return fmt.Errorf("CheckID missing")
|
|
|
|
}
|
|
|
|
|
2021-05-05 21:37:02 +00:00
|
|
|
ct := l.aclTokenForCheckSync(key, l.tokens.AgentToken)
|
2014-01-16 01:14:50 +00:00
|
|
|
req := structs.DeregisterRequest{
|
2019-12-10 02:26:41 +00:00
|
|
|
Datacenter: l.config.Datacenter,
|
|
|
|
Node: l.config.NodeName,
|
|
|
|
CheckID: key.ID,
|
|
|
|
EnterpriseMeta: key.EnterpriseMeta,
|
2020-01-27 19:54:32 +00:00
|
|
|
WriteRequest: structs.WriteRequest{Token: ct},
|
2014-01-16 01:14:50 +00:00
|
|
|
}
|
|
|
|
var out struct{}
|
2017-08-30 10:25:49 +00:00
|
|
|
err := l.Delegate.RPC("Catalog.Deregister", &req, &out)
|
|
|
|
switch {
|
|
|
|
case err == nil || strings.Contains(err.Error(), "Unknown check"):
|
2020-01-17 13:26:53 +00:00
|
|
|
l.pruneCheck(key)
|
2020-01-28 23:50:41 +00:00
|
|
|
l.logger.Info("Deregistered check", "check", key.String())
|
2017-03-25 00:15:20 +00:00
|
|
|
return nil
|
2017-08-30 10:25:49 +00:00
|
|
|
|
2019-01-04 15:01:50 +00:00
|
|
|
case acl.IsErrPermissionDenied(err), acl.IsErrNotFound(err):
|
2017-08-30 10:25:49 +00:00
|
|
|
// todo(fs): mark the check to be in sync to prevent excessive retrying before next full sync
|
|
|
|
// todo(fs): some backoff strategy might be a better solution
|
2019-12-10 02:26:41 +00:00
|
|
|
l.checks[key].InSync = true
|
2020-01-27 19:54:32 +00:00
|
|
|
accessorID := l.aclAccessorID(ct)
|
2020-01-28 23:50:41 +00:00
|
|
|
l.logger.Warn("Check deregistration blocked by ACLs", "check", key.String(), "accessorID", accessorID)
|
2018-06-08 09:56:46 +00:00
|
|
|
metrics.IncrCounter([]string{"acl", "blocked", "check", "deregistration"}, 1)
|
2017-03-25 00:15:20 +00:00
|
|
|
return nil
|
2017-08-30 10:25:49 +00:00
|
|
|
|
|
|
|
default:
|
2020-01-28 23:50:41 +00:00
|
|
|
l.logger.Warn("Deregistering check failed.",
|
|
|
|
"check", key.String(),
|
|
|
|
"error", err,
|
|
|
|
)
|
2017-08-30 10:25:49 +00:00
|
|
|
return err
|
2014-01-16 01:14:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-17 13:26:53 +00:00
|
|
|
func (l *State) pruneCheck(id structs.CheckID) {
|
|
|
|
c := l.checks[id]
|
|
|
|
if c != nil && c.DeferCheck != nil {
|
|
|
|
c.DeferCheck.Stop()
|
|
|
|
}
|
|
|
|
delete(l.checks, id)
|
|
|
|
}
|
|
|
|
|
2014-01-16 01:14:50 +00:00
|
|
|
// syncService is used to sync a service to the server
|
2019-12-10 02:26:41 +00:00
|
|
|
func (l *State) syncService(key structs.ServiceID) error {
|
2021-05-05 21:37:02 +00:00
|
|
|
st := l.aclTokenForServiceSync(key, l.tokens.UserToken)
|
2020-01-27 19:54:32 +00:00
|
|
|
|
2017-10-23 08:08:34 +00:00
|
|
|
// If the service has associated checks that are out of sync,
|
|
|
|
// piggyback them on the service sync so they are part of the
|
|
|
|
// same transaction and are registered atomically. We only let
|
|
|
|
// checks ride on service registrations with the same token,
|
|
|
|
// otherwise we need to register them separately so they don't
|
|
|
|
// pick up privileges from the service token.
|
|
|
|
var checks structs.HealthChecks
|
2019-12-10 02:26:41 +00:00
|
|
|
for checkKey, c := range l.checks {
|
2017-08-28 12:17:12 +00:00
|
|
|
if c.Deleted || c.InSync {
|
|
|
|
continue
|
|
|
|
}
|
2020-12-11 21:10:00 +00:00
|
|
|
if !key.Matches(c.Check.CompoundServiceID()) {
|
2017-08-28 12:17:12 +00:00
|
|
|
continue
|
2017-10-23 08:08:34 +00:00
|
|
|
}
|
2021-05-05 21:37:02 +00:00
|
|
|
if st != l.aclTokenForCheckSync(checkKey, l.tokens.UserToken) {
|
2017-08-28 12:17:12 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
checks = append(checks, c.Check)
|
|
|
|
}
|
|
|
|
|
|
|
|
req := structs.RegisterRequest{
|
|
|
|
Datacenter: l.config.Datacenter,
|
|
|
|
ID: l.config.NodeID,
|
|
|
|
Node: l.config.NodeName,
|
|
|
|
Address: l.config.AdvertiseAddr,
|
|
|
|
TaggedAddresses: l.config.TaggedAddresses,
|
|
|
|
NodeMeta: l.metadata,
|
2019-12-10 02:26:41 +00:00
|
|
|
Service: l.services[key].Service,
|
|
|
|
EnterpriseMeta: key.EnterpriseMeta,
|
2020-01-27 19:54:32 +00:00
|
|
|
WriteRequest: structs.WriteRequest{Token: st},
|
2020-02-06 14:30:58 +00:00
|
|
|
SkipNodeUpdate: l.nodeInfoInSync,
|
2017-10-23 08:08:34 +00:00
|
|
|
}
|
|
|
|
|
2015-01-15 07:09:42 +00:00
|
|
|
// Backwards-compatibility for Consul < 0.5
|
2015-01-14 19:48:36 +00:00
|
|
|
if len(checks) == 1 {
|
|
|
|
req.Check = checks[0]
|
|
|
|
} else {
|
|
|
|
req.Checks = checks
|
|
|
|
}
|
|
|
|
|
2014-01-16 01:14:50 +00:00
|
|
|
var out struct{}
|
2017-08-30 10:25:49 +00:00
|
|
|
err := l.Delegate.RPC("Catalog.Register", &req, &out)
|
|
|
|
switch {
|
|
|
|
case err == nil:
|
2019-12-10 02:26:41 +00:00
|
|
|
l.services[key].InSync = true
|
2016-02-07 21:12:42 +00:00
|
|
|
// Given how the register API works, this info is also updated
|
|
|
|
// every time we sync a service.
|
|
|
|
l.nodeInfoInSync = true
|
2015-01-14 19:48:36 +00:00
|
|
|
for _, check := range checks {
|
2020-04-15 16:03:29 +00:00
|
|
|
checkKey := structs.NewCheckID(check.CheckID, &check.EnterpriseMeta)
|
2019-12-10 02:26:41 +00:00
|
|
|
l.checks[checkKey].InSync = true
|
2015-01-14 19:48:36 +00:00
|
|
|
}
|
2020-01-28 23:50:41 +00:00
|
|
|
l.logger.Info("Synced service", "service", key.String())
|
2017-08-28 12:17:12 +00:00
|
|
|
return nil
|
2017-08-30 10:25:49 +00:00
|
|
|
|
2019-01-04 15:01:50 +00:00
|
|
|
case acl.IsErrPermissionDenied(err), acl.IsErrNotFound(err):
|
2017-08-30 10:25:49 +00:00
|
|
|
// todo(fs): mark the service and the checks to be in sync to prevent excessive retrying before next full sync
|
|
|
|
// todo(fs): some backoff strategy might be a better solution
|
2019-12-10 02:26:41 +00:00
|
|
|
l.services[key].InSync = true
|
2017-10-23 08:08:34 +00:00
|
|
|
for _, check := range checks {
|
2020-04-15 16:03:29 +00:00
|
|
|
checkKey := structs.NewCheckID(check.CheckID, &check.EnterpriseMeta)
|
2019-12-10 02:26:41 +00:00
|
|
|
l.checks[checkKey].InSync = true
|
2017-10-23 08:08:34 +00:00
|
|
|
}
|
2020-01-27 19:54:32 +00:00
|
|
|
accessorID := l.aclAccessorID(st)
|
2020-01-28 23:50:41 +00:00
|
|
|
l.logger.Warn("Service registration blocked by ACLs", "service", key.String(), "accessorID", accessorID)
|
2018-06-08 09:56:46 +00:00
|
|
|
metrics.IncrCounter([]string{"acl", "blocked", "service", "registration"}, 1)
|
2014-12-01 19:43:01 +00:00
|
|
|
return nil
|
2017-08-30 10:25:49 +00:00
|
|
|
|
|
|
|
default:
|
2020-01-28 23:50:41 +00:00
|
|
|
l.logger.Warn("Syncing service failed.",
|
|
|
|
"service", key.String(),
|
|
|
|
"error", err,
|
|
|
|
)
|
2017-08-30 10:25:49 +00:00
|
|
|
return err
|
2014-01-16 01:14:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-08 09:45:01 +00:00
|
|
|
// syncCheck is used to sync a check to the server
|
2019-12-10 02:26:41 +00:00
|
|
|
func (l *State) syncCheck(key structs.CheckID) error {
|
|
|
|
c := l.checks[key]
|
2021-05-05 21:37:02 +00:00
|
|
|
ct := l.aclTokenForCheckSync(key, l.tokens.UserToken)
|
2015-01-14 19:48:36 +00:00
|
|
|
req := structs.RegisterRequest{
|
2016-02-07 18:37:34 +00:00
|
|
|
Datacenter: l.config.Datacenter,
|
2017-01-18 22:26:42 +00:00
|
|
|
ID: l.config.NodeID,
|
2016-02-07 18:37:34 +00:00
|
|
|
Node: l.config.NodeName,
|
|
|
|
Address: l.config.AdvertiseAddr,
|
|
|
|
TaggedAddresses: l.config.TaggedAddresses,
|
2017-01-05 22:10:26 +00:00
|
|
|
NodeMeta: l.metadata,
|
2017-08-28 12:17:12 +00:00
|
|
|
Check: c.Check,
|
2019-12-10 02:26:41 +00:00
|
|
|
EnterpriseMeta: c.Check.EnterpriseMeta,
|
2020-01-27 19:54:32 +00:00
|
|
|
WriteRequest: structs.WriteRequest{Token: ct},
|
2020-02-06 14:30:58 +00:00
|
|
|
SkipNodeUpdate: l.nodeInfoInSync,
|
2014-01-16 01:14:50 +00:00
|
|
|
}
|
2017-08-28 12:17:12 +00:00
|
|
|
|
2020-04-15 16:03:29 +00:00
|
|
|
serviceKey := structs.NewServiceID(c.Check.ServiceID, &key.EnterpriseMeta)
|
2019-12-10 02:26:41 +00:00
|
|
|
|
2017-08-28 12:17:12 +00:00
|
|
|
// Pull in the associated service if any
|
2019-12-10 02:26:41 +00:00
|
|
|
s := l.services[serviceKey]
|
2017-08-28 12:17:12 +00:00
|
|
|
if s != nil && !s.Deleted {
|
|
|
|
req.Service = s.Service
|
|
|
|
}
|
|
|
|
|
2015-01-14 19:48:36 +00:00
|
|
|
var out struct{}
|
2017-08-30 10:25:49 +00:00
|
|
|
err := l.Delegate.RPC("Catalog.Register", &req, &out)
|
|
|
|
switch {
|
|
|
|
case err == nil:
|
2019-12-10 02:26:41 +00:00
|
|
|
l.checks[key].InSync = true
|
2016-02-07 21:12:42 +00:00
|
|
|
// Given how the register API works, this info is also updated
|
2017-03-25 00:15:20 +00:00
|
|
|
// every time we sync a check.
|
2016-02-07 21:12:42 +00:00
|
|
|
l.nodeInfoInSync = true
|
2020-01-28 23:50:41 +00:00
|
|
|
l.logger.Info("Synced check", "check", key.String())
|
2017-08-28 12:17:12 +00:00
|
|
|
return nil
|
2017-08-30 10:25:49 +00:00
|
|
|
|
2019-01-04 15:01:50 +00:00
|
|
|
case acl.IsErrPermissionDenied(err), acl.IsErrNotFound(err):
|
2017-08-30 10:25:49 +00:00
|
|
|
// todo(fs): mark the check to be in sync to prevent excessive retrying before next full sync
|
|
|
|
// todo(fs): some backoff strategy might be a better solution
|
2019-12-10 02:26:41 +00:00
|
|
|
l.checks[key].InSync = true
|
2020-01-27 19:54:32 +00:00
|
|
|
accessorID := l.aclAccessorID(ct)
|
2020-01-28 23:50:41 +00:00
|
|
|
l.logger.Warn("Check registration blocked by ACLs", "check", key.String(), "accessorID", accessorID)
|
2018-06-08 09:56:46 +00:00
|
|
|
metrics.IncrCounter([]string{"acl", "blocked", "check", "registration"}, 1)
|
2015-01-14 19:48:36 +00:00
|
|
|
return nil
|
2017-08-30 10:25:49 +00:00
|
|
|
|
|
|
|
default:
|
2020-01-28 23:50:41 +00:00
|
|
|
l.logger.Warn("Syncing check failed.",
|
|
|
|
"check", key.String(),
|
|
|
|
"error", err,
|
|
|
|
)
|
2017-08-30 10:25:49 +00:00
|
|
|
return err
|
2015-01-14 19:48:36 +00:00
|
|
|
}
|
2014-01-16 01:14:50 +00:00
|
|
|
}
|
2016-02-07 21:12:42 +00:00
|
|
|
|
2017-08-28 12:17:12 +00:00
|
|
|
func (l *State) syncNodeInfo() error {
|
2020-01-27 19:54:32 +00:00
|
|
|
at := l.tokens.AgentToken()
|
2016-02-07 21:12:42 +00:00
|
|
|
req := structs.RegisterRequest{
|
|
|
|
Datacenter: l.config.Datacenter,
|
2017-01-18 22:26:42 +00:00
|
|
|
ID: l.config.NodeID,
|
2016-02-07 21:12:42 +00:00
|
|
|
Node: l.config.NodeName,
|
|
|
|
Address: l.config.AdvertiseAddr,
|
|
|
|
TaggedAddresses: l.config.TaggedAddresses,
|
2017-01-05 22:10:26 +00:00
|
|
|
NodeMeta: l.metadata,
|
2021-08-19 20:09:42 +00:00
|
|
|
EnterpriseMeta: l.agentEnterpriseMeta,
|
2020-01-27 19:54:32 +00:00
|
|
|
WriteRequest: structs.WriteRequest{Token: at},
|
2016-02-07 21:12:42 +00:00
|
|
|
}
|
|
|
|
var out struct{}
|
2017-08-30 10:25:49 +00:00
|
|
|
err := l.Delegate.RPC("Catalog.Register", &req, &out)
|
|
|
|
switch {
|
|
|
|
case err == nil:
|
2016-02-07 21:12:42 +00:00
|
|
|
l.nodeInfoInSync = true
|
2020-01-28 23:50:41 +00:00
|
|
|
l.logger.Info("Synced node info")
|
2017-08-28 12:17:12 +00:00
|
|
|
return nil
|
2017-08-30 10:25:49 +00:00
|
|
|
|
2019-01-04 15:01:50 +00:00
|
|
|
case acl.IsErrPermissionDenied(err), acl.IsErrNotFound(err):
|
2017-08-30 10:25:49 +00:00
|
|
|
// todo(fs): mark the node info to be in sync to prevent excessive retrying before next full sync
|
|
|
|
// todo(fs): some backoff strategy might be a better solution
|
2016-02-07 21:12:42 +00:00
|
|
|
l.nodeInfoInSync = true
|
2020-01-27 19:54:32 +00:00
|
|
|
accessorID := l.aclAccessorID(at)
|
2020-01-28 23:50:41 +00:00
|
|
|
l.logger.Warn("Node info update blocked by ACLs", "node", l.config.NodeID, "accessorID", accessorID)
|
2018-06-08 09:56:46 +00:00
|
|
|
metrics.IncrCounter([]string{"acl", "blocked", "node", "registration"}, 1)
|
2016-02-07 21:12:42 +00:00
|
|
|
return nil
|
2017-08-30 10:25:49 +00:00
|
|
|
|
|
|
|
default:
|
2020-01-28 23:50:41 +00:00
|
|
|
l.logger.Warn("Syncing node info failed.", "error", err)
|
2017-08-30 10:25:49 +00:00
|
|
|
return err
|
2016-02-07 21:12:42 +00:00
|
|
|
}
|
|
|
|
}
|
2019-04-24 18:17:06 +00:00
|
|
|
|
2020-08-12 15:47:41 +00:00
|
|
|
// notifyIfAliased will notify waiters of changes to an aliased service
|
2019-12-10 02:26:41 +00:00
|
|
|
func (l *State) notifyIfAliased(serviceID structs.ServiceID) {
|
2019-04-24 18:17:06 +00:00
|
|
|
if aliases, ok := l.checkAliases[serviceID]; ok && len(aliases) > 0 {
|
|
|
|
for _, notifyCh := range aliases {
|
|
|
|
// Do not block. All notify channels should be buffered to at
|
|
|
|
// least 1 in which case not-blocking does not result in loss
|
|
|
|
// of data because a failed send means a notification is
|
|
|
|
// already queued. This must be called with the lock held.
|
|
|
|
select {
|
|
|
|
case notifyCh <- struct{}{}:
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-01-27 19:54:32 +00:00
|
|
|
|
|
|
|
// aclAccessorID is used to convert an ACLToken's secretID to its accessorID for non-
|
|
|
|
// critical purposes, such as logging. Therefore we interpret all errors as empty-string
|
|
|
|
// so we can safely log it without handling non-critical errors at the usage site.
|
|
|
|
func (l *State) aclAccessorID(secretID string) string {
|
2020-05-13 17:00:08 +00:00
|
|
|
ident, err := l.Delegate.ResolveTokenToIdentity(secretID)
|
2020-01-29 17:16:08 +00:00
|
|
|
if acl.IsErrNotFound(err) {
|
|
|
|
return ""
|
|
|
|
}
|
2020-01-27 19:54:32 +00:00
|
|
|
if err != nil {
|
2020-01-29 17:16:08 +00:00
|
|
|
l.logger.Debug("non-critical error resolving acl token accessor for logging", "error", err)
|
2020-01-27 19:54:32 +00:00
|
|
|
return ""
|
|
|
|
}
|
|
|
|
if ident == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return ident.ID()
|
|
|
|
}
|