2013-12-19 20:03:57 +00:00
|
|
|
package structs
|
2013-12-11 22:04:44 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
2015-11-07 00:59:32 +00:00
|
|
|
"math/rand"
|
2015-10-28 21:32:00 +00:00
|
|
|
"reflect"
|
2017-04-18 12:02:24 +00:00
|
|
|
"regexp"
|
2018-04-08 13:45:55 +00:00
|
|
|
"strconv"
|
2017-04-18 12:02:24 +00:00
|
|
|
"strings"
|
2014-02-05 18:21:31 +00:00
|
|
|
"time"
|
2014-08-08 22:32:43 +00:00
|
|
|
|
2018-04-08 14:08:34 +00:00
|
|
|
"github.com/hashicorp/consul/agent/cache"
|
2017-04-19 23:00:11 +00:00
|
|
|
"github.com/hashicorp/consul/api"
|
2016-06-06 20:19:31 +00:00
|
|
|
"github.com/hashicorp/consul/types"
|
2014-10-18 01:26:19 +00:00
|
|
|
"github.com/hashicorp/go-msgpack/codec"
|
2018-03-09 06:13:35 +00:00
|
|
|
"github.com/hashicorp/go-multierror"
|
2015-03-28 18:52:04 +00:00
|
|
|
"github.com/hashicorp/serf/coordinate"
|
2018-04-08 13:45:55 +00:00
|
|
|
"github.com/mitchellh/hashstructure"
|
2013-12-11 22:04:44 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type MessageType uint8
|
|
|
|
|
2015-08-25 01:10:23 +00:00
|
|
|
// RaftIndex is used to track the index used while creating
|
2015-08-22 19:44:33 +00:00
|
|
|
// or modifying a given struct type.
|
2015-08-25 01:10:23 +00:00
|
|
|
type RaftIndex struct {
|
2015-08-22 19:44:33 +00:00
|
|
|
CreateIndex uint64
|
|
|
|
ModifyIndex uint64
|
|
|
|
}
|
|
|
|
|
2017-08-03 00:05:18 +00:00
|
|
|
// These are serialized between Consul servers and stored in Consul snapshots,
|
|
|
|
// so entries must only ever be added.
|
2013-12-11 22:04:44 +00:00
|
|
|
const (
|
2017-08-03 00:05:18 +00:00
|
|
|
RegisterRequestType MessageType = 0
|
|
|
|
DeregisterRequestType = 1
|
|
|
|
KVSRequestType = 2
|
|
|
|
SessionRequestType = 3
|
|
|
|
ACLRequestType = 4
|
|
|
|
TombstoneRequestType = 5
|
|
|
|
CoordinateBatchUpdateType = 6
|
|
|
|
PreparedQueryRequestType = 7
|
|
|
|
TxnRequestType = 8
|
|
|
|
AutopilotRequestType = 9
|
|
|
|
AreaRequestType = 10
|
|
|
|
ACLBootstrapRequestType = 11 // FSM snapshots only.
|
2018-02-28 18:28:07 +00:00
|
|
|
IntentionRequestType = 12
|
2018-03-21 17:10:53 +00:00
|
|
|
ConnectCARequestType = 13
|
2013-12-11 22:04:44 +00:00
|
|
|
)
|
|
|
|
|
2015-05-06 02:44:21 +00:00
|
|
|
const (
|
|
|
|
// IgnoreUnknownTypeFlag is set along with a MessageType
|
|
|
|
// to indicate that the message type can be safely ignored
|
|
|
|
// if it is not recognized. This is for future proofing, so
|
|
|
|
// that new commands can be added in a way that won't cause
|
|
|
|
// old servers to crash when the FSM attempts to process them.
|
|
|
|
IgnoreUnknownTypeFlag MessageType = 128
|
|
|
|
|
2016-11-29 21:15:20 +00:00
|
|
|
// NodeMaint is the special key set by a node in maintenance mode.
|
|
|
|
NodeMaint = "_node_maintenance"
|
|
|
|
|
|
|
|
// ServiceMaintPrefix is the prefix for a service in maintenance mode.
|
|
|
|
ServiceMaintPrefix = "_service_maintenance:"
|
2014-01-08 19:21:29 +00:00
|
|
|
|
2017-01-23 23:53:45 +00:00
|
|
|
// The meta key prefix reserved for Consul's internal use
|
|
|
|
metaKeyReservedPrefix = "consul-"
|
|
|
|
|
2017-04-21 03:14:10 +00:00
|
|
|
// metaMaxKeyPairs is maximum number of metadata key pairs allowed to be registered
|
2017-01-23 23:53:45 +00:00
|
|
|
metaMaxKeyPairs = 64
|
|
|
|
|
2017-04-21 03:14:10 +00:00
|
|
|
// metaKeyMaxLength is the maximum allowed length of a metadata key
|
2017-01-23 23:53:45 +00:00
|
|
|
metaKeyMaxLength = 128
|
|
|
|
|
2017-04-21 03:14:10 +00:00
|
|
|
// metaValueMaxLength is the maximum allowed length of a metadata value
|
2017-01-23 23:53:45 +00:00
|
|
|
metaValueMaxLength = 512
|
2015-04-12 00:53:48 +00:00
|
|
|
|
2017-08-14 14:36:07 +00:00
|
|
|
// MetaSegmentKey is the node metadata key used to store the node's network segment
|
|
|
|
MetaSegmentKey = "consul-network-segment"
|
|
|
|
|
2014-05-19 19:50:29 +00:00
|
|
|
// MaxLockDelay provides a maximum LockDelay value for
|
|
|
|
// a session. Any value above this will not be respected.
|
|
|
|
MaxLockDelay = 60 * time.Second
|
|
|
|
)
|
|
|
|
|
2017-04-21 03:14:10 +00:00
|
|
|
// metaKeyFormat checks if a metadata key string is valid
|
|
|
|
var metaKeyFormat = regexp.MustCompile(`^[a-zA-Z0-9_-]+$`).MatchString
|
|
|
|
|
|
|
|
func ValidStatus(s string) bool {
|
|
|
|
return s == api.HealthPassing || s == api.HealthWarning || s == api.HealthCritical
|
|
|
|
}
|
|
|
|
|
2014-04-19 00:14:00 +00:00
|
|
|
// RPCInfo is used to describe common information about query
|
|
|
|
type RPCInfo interface {
|
|
|
|
RequestDatacenter() string
|
|
|
|
IsRead() bool
|
|
|
|
AllowStaleRead() bool
|
2014-08-05 22:48:28 +00:00
|
|
|
ACLToken() string
|
2014-04-19 00:14:00 +00:00
|
|
|
}
|
|
|
|
|
2014-04-21 18:31:15 +00:00
|
|
|
// QueryOptions is used to specify various flags for read queries
|
|
|
|
type QueryOptions struct {
|
2014-08-05 22:48:28 +00:00
|
|
|
// Token is the ACL token ID. If not provided, the 'anonymous'
|
|
|
|
// token is assumed for backwards compatibility.
|
|
|
|
Token string
|
|
|
|
|
2014-04-21 18:31:15 +00:00
|
|
|
// If set, wait until query exceeds given index. Must be provided
|
|
|
|
// with MaxQueryTime.
|
2014-02-05 18:21:31 +00:00
|
|
|
MinQueryIndex uint64
|
|
|
|
|
2014-04-21 18:31:15 +00:00
|
|
|
// Provided with MinQueryIndex to wait for change.
|
2014-02-05 18:21:31 +00:00
|
|
|
MaxQueryTime time.Duration
|
|
|
|
|
2014-04-18 23:46:51 +00:00
|
|
|
// If set, any follower can service the request. Results
|
|
|
|
// may be arbitrarily stale.
|
|
|
|
AllowStale bool
|
|
|
|
|
|
|
|
// If set, the leader must verify leadership prior to
|
|
|
|
// servicing the request. Prevents a stale read.
|
|
|
|
RequireConsistent bool
|
2018-03-30 15:14:44 +00:00
|
|
|
|
|
|
|
// If set and AllowStale is true, will try first a stale
|
|
|
|
// read, and then will perform a consistent read if stale
|
|
|
|
// read is older than value
|
|
|
|
MaxStaleDuration time.Duration
|
2014-04-18 23:46:51 +00:00
|
|
|
}
|
|
|
|
|
2017-04-21 03:14:10 +00:00
|
|
|
// IsRead is always true for QueryOption.
|
2014-04-19 00:14:00 +00:00
|
|
|
func (q QueryOptions) IsRead() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2018-03-30 15:14:44 +00:00
|
|
|
// ConsistencyLevel display the consistency required by a request
|
|
|
|
func (q QueryOptions) ConsistencyLevel() string {
|
|
|
|
if q.RequireConsistent {
|
|
|
|
return "consistent"
|
|
|
|
} else if q.AllowStale {
|
|
|
|
return "stale"
|
|
|
|
} else {
|
|
|
|
return "leader"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-19 00:14:00 +00:00
|
|
|
func (q QueryOptions) AllowStaleRead() bool {
|
|
|
|
return q.AllowStale
|
|
|
|
}
|
|
|
|
|
2014-08-05 22:48:28 +00:00
|
|
|
func (q QueryOptions) ACLToken() string {
|
|
|
|
return q.Token
|
|
|
|
}
|
|
|
|
|
|
|
|
type WriteRequest struct {
|
|
|
|
// Token is the ACL token ID. If not provided, the 'anonymous'
|
|
|
|
// token is assumed for backwards compatibility.
|
|
|
|
Token string
|
|
|
|
}
|
2014-04-19 00:14:00 +00:00
|
|
|
|
|
|
|
// WriteRequest only applies to writes, always false
|
|
|
|
func (w WriteRequest) IsRead() bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w WriteRequest) AllowStaleRead() bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2014-08-05 22:48:28 +00:00
|
|
|
func (w WriteRequest) ACLToken() string {
|
|
|
|
return w.Token
|
|
|
|
}
|
|
|
|
|
2014-04-18 23:46:51 +00:00
|
|
|
// QueryMeta allows a query response to include potentially
|
|
|
|
// useful metadata about a query
|
|
|
|
type QueryMeta struct {
|
2014-04-21 18:13:36 +00:00
|
|
|
// This is the index associated with the read
|
|
|
|
Index uint64
|
|
|
|
|
2014-04-18 23:46:51 +00:00
|
|
|
// If AllowStale is used, this is time elapsed since
|
|
|
|
// last contact between the follower and leader. This
|
|
|
|
// can be used to gauge staleness.
|
|
|
|
LastContact time.Duration
|
|
|
|
|
|
|
|
// Used to indicate if there is a known leader node
|
|
|
|
KnownLeader bool
|
2018-03-30 15:14:44 +00:00
|
|
|
|
|
|
|
// Consistencylevel returns the consistency used to serve the query
|
|
|
|
// Having `discovery_max_stale` on the agent can affect whether
|
|
|
|
// the request was served by a leader.
|
|
|
|
ConsistencyLevel string
|
2014-04-18 23:46:51 +00:00
|
|
|
}
|
|
|
|
|
2013-12-11 22:04:44 +00:00
|
|
|
// RegisterRequest is used for the Catalog.Register endpoint
|
|
|
|
// to register a node as providing a service. If no service
|
|
|
|
// is provided, the node is registered.
|
|
|
|
type RegisterRequest struct {
|
2016-02-07 18:37:34 +00:00
|
|
|
Datacenter string
|
2017-01-18 22:26:42 +00:00
|
|
|
ID types.NodeID
|
2016-02-07 18:37:34 +00:00
|
|
|
Node string
|
|
|
|
Address string
|
|
|
|
TaggedAddresses map[string]string
|
2017-01-05 22:10:26 +00:00
|
|
|
NodeMeta map[string]string
|
2016-02-07 18:37:34 +00:00
|
|
|
Service *NodeService
|
|
|
|
Check *HealthCheck
|
|
|
|
Checks HealthChecks
|
2017-03-23 22:01:46 +00:00
|
|
|
|
|
|
|
// SkipNodeUpdate can be used when a register request is intended for
|
|
|
|
// updating a service and/or checks, but doesn't want to overwrite any
|
|
|
|
// node information if the node is already registered. If the node
|
|
|
|
// doesn't exist, it will still be created, but if the node exists, any
|
|
|
|
// node portion of this update will not apply.
|
|
|
|
SkipNodeUpdate bool
|
|
|
|
|
2014-04-19 00:14:00 +00:00
|
|
|
WriteRequest
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RegisterRequest) RequestDatacenter() string {
|
|
|
|
return r.Datacenter
|
2013-12-11 22:04:44 +00:00
|
|
|
}
|
|
|
|
|
2016-12-09 00:01:01 +00:00
|
|
|
// ChangesNode returns true if the given register request changes the given
|
|
|
|
// node, which can be nil. This only looks for changes to the node record itself,
|
|
|
|
// not any of the health checks.
|
|
|
|
func (r *RegisterRequest) ChangesNode(node *Node) bool {
|
|
|
|
// This means it's creating the node.
|
|
|
|
if node == nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2017-03-23 22:01:46 +00:00
|
|
|
// If we've been asked to skip the node update, then say there are no
|
|
|
|
// changes.
|
|
|
|
if r.SkipNodeUpdate {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2016-12-09 00:01:01 +00:00
|
|
|
// Check if any of the node-level fields are being changed.
|
2017-01-18 22:26:42 +00:00
|
|
|
if r.ID != node.ID ||
|
|
|
|
r.Node != node.Node ||
|
2016-12-09 00:01:01 +00:00
|
|
|
r.Address != node.Address ||
|
2017-04-18 12:02:24 +00:00
|
|
|
r.Datacenter != node.Datacenter ||
|
2017-01-05 22:10:26 +00:00
|
|
|
!reflect.DeepEqual(r.TaggedAddresses, node.TaggedAddresses) ||
|
|
|
|
!reflect.DeepEqual(r.NodeMeta, node.Meta) {
|
2016-12-09 00:01:01 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2013-12-11 22:04:44 +00:00
|
|
|
// DeregisterRequest is used for the Catalog.Deregister endpoint
|
|
|
|
// to deregister a node as providing a service. If no service is
|
|
|
|
// provided the entire node is deregistered.
|
|
|
|
type DeregisterRequest struct {
|
2014-01-06 22:18:38 +00:00
|
|
|
Datacenter string
|
|
|
|
Node string
|
|
|
|
ServiceID string
|
2016-06-06 20:19:31 +00:00
|
|
|
CheckID types.CheckID
|
2014-04-19 00:14:00 +00:00
|
|
|
WriteRequest
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *DeregisterRequest) RequestDatacenter() string {
|
|
|
|
return r.Datacenter
|
2013-12-11 22:04:44 +00:00
|
|
|
}
|
|
|
|
|
2015-06-30 21:25:40 +00:00
|
|
|
// QuerySource is used to pass along information about the source node
|
|
|
|
// in queries so that we can adjust the response based on its network
|
|
|
|
// coordinates.
|
|
|
|
type QuerySource struct {
|
|
|
|
Datacenter string
|
2017-08-30 00:02:50 +00:00
|
|
|
Segment string
|
2015-06-30 21:25:40 +00:00
|
|
|
Node string
|
2018-04-10 18:50:50 +00:00
|
|
|
Ip string
|
2015-06-30 21:25:40 +00:00
|
|
|
}
|
|
|
|
|
2014-02-05 18:21:31 +00:00
|
|
|
// DCSpecificRequest is used to query about a specific DC
|
|
|
|
type DCSpecificRequest struct {
|
2017-01-11 19:41:12 +00:00
|
|
|
Datacenter string
|
|
|
|
NodeMetaFilters map[string]string
|
|
|
|
Source QuerySource
|
2014-04-18 23:46:51 +00:00
|
|
|
QueryOptions
|
2014-02-05 18:21:31 +00:00
|
|
|
}
|
|
|
|
|
2014-04-19 00:14:00 +00:00
|
|
|
func (r *DCSpecificRequest) RequestDatacenter() string {
|
|
|
|
return r.Datacenter
|
|
|
|
}
|
|
|
|
|
2018-04-08 14:08:34 +00:00
|
|
|
func (r *DCSpecificRequest) CacheInfo() cache.RequestInfo {
|
|
|
|
info := cache.RequestInfo{
|
2018-04-16 10:28:18 +00:00
|
|
|
Token: r.Token,
|
|
|
|
Datacenter: r.Datacenter,
|
|
|
|
MinIndex: r.MinQueryIndex,
|
|
|
|
Timeout: r.MaxQueryTime,
|
2018-04-08 14:08:34 +00:00
|
|
|
}
|
|
|
|
|
2018-04-08 13:45:55 +00:00
|
|
|
// To calculate the cache key we only hash the node filters. The
|
|
|
|
// datacenter is handled by the cache framework. The other fields are
|
|
|
|
// not, but should not be used in any cache types.
|
|
|
|
v, err := hashstructure.Hash(r.NodeMetaFilters, nil)
|
2018-04-08 14:08:34 +00:00
|
|
|
if err == nil {
|
|
|
|
// If there is an error, we don't set the key. A blank key forces
|
|
|
|
// no cache for this request so the request is forwarded directly
|
|
|
|
// to the server.
|
|
|
|
info.Key = strconv.FormatUint(v, 10)
|
2018-04-08 13:45:55 +00:00
|
|
|
}
|
|
|
|
|
2018-04-08 14:08:34 +00:00
|
|
|
return info
|
2018-04-08 13:45:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *DCSpecificRequest) CacheMinIndex() uint64 {
|
|
|
|
return r.QueryOptions.MinQueryIndex
|
|
|
|
}
|
|
|
|
|
2015-07-27 21:41:46 +00:00
|
|
|
// ServiceSpecificRequest is used to query about a specific service
|
2014-01-08 22:43:36 +00:00
|
|
|
type ServiceSpecificRequest struct {
|
2017-01-14 01:08:43 +00:00
|
|
|
Datacenter string
|
|
|
|
NodeMetaFilters map[string]string
|
|
|
|
ServiceName string
|
|
|
|
ServiceTag string
|
2018-05-03 20:54:14 +00:00
|
|
|
ServiceAddress string
|
2017-01-14 01:08:43 +00:00
|
|
|
TagFilter bool // Controls tag filtering
|
|
|
|
Source QuerySource
|
2018-03-09 16:34:55 +00:00
|
|
|
|
|
|
|
// Connect if true will only search for Connect-compatible services.
|
|
|
|
Connect bool
|
|
|
|
|
2014-04-18 23:46:51 +00:00
|
|
|
QueryOptions
|
2014-01-08 22:43:36 +00:00
|
|
|
}
|
|
|
|
|
2014-04-19 00:14:00 +00:00
|
|
|
func (r *ServiceSpecificRequest) RequestDatacenter() string {
|
|
|
|
return r.Datacenter
|
|
|
|
}
|
|
|
|
|
2014-01-08 22:43:36 +00:00
|
|
|
// NodeSpecificRequest is used to request the information about a single node
|
|
|
|
type NodeSpecificRequest struct {
|
|
|
|
Datacenter string
|
|
|
|
Node string
|
2014-04-18 23:46:51 +00:00
|
|
|
QueryOptions
|
2014-01-08 22:43:36 +00:00
|
|
|
}
|
|
|
|
|
2014-04-19 00:14:00 +00:00
|
|
|
func (r *NodeSpecificRequest) RequestDatacenter() string {
|
|
|
|
return r.Datacenter
|
|
|
|
}
|
|
|
|
|
2014-01-08 22:43:36 +00:00
|
|
|
// ChecksInStateRequest is used to query for nodes in a state
|
|
|
|
type ChecksInStateRequest struct {
|
2017-01-14 01:08:43 +00:00
|
|
|
Datacenter string
|
|
|
|
NodeMetaFilters map[string]string
|
|
|
|
State string
|
|
|
|
Source QuerySource
|
2014-04-18 23:46:51 +00:00
|
|
|
QueryOptions
|
2014-01-08 22:43:36 +00:00
|
|
|
}
|
|
|
|
|
2014-04-19 00:14:00 +00:00
|
|
|
func (r *ChecksInStateRequest) RequestDatacenter() string {
|
|
|
|
return r.Datacenter
|
|
|
|
}
|
|
|
|
|
2013-12-12 18:48:36 +00:00
|
|
|
// Used to return information about a node
|
|
|
|
type Node struct {
|
2017-01-18 22:26:42 +00:00
|
|
|
ID types.NodeID
|
2016-02-07 18:37:34 +00:00
|
|
|
Node string
|
|
|
|
Address string
|
2017-04-18 12:02:24 +00:00
|
|
|
Datacenter string
|
2016-02-07 18:37:34 +00:00
|
|
|
TaggedAddresses map[string]string
|
2017-01-05 22:10:26 +00:00
|
|
|
Meta map[string]string
|
2015-08-22 19:44:33 +00:00
|
|
|
|
2015-08-25 01:10:23 +00:00
|
|
|
RaftIndex
|
2013-12-12 18:48:36 +00:00
|
|
|
}
|
2015-08-25 01:10:23 +00:00
|
|
|
type Nodes []*Node
|
2013-12-12 18:48:36 +00:00
|
|
|
|
2017-01-23 23:53:45 +00:00
|
|
|
// ValidateMeta validates a set of key/value pairs from the agent config
|
2017-08-30 00:02:50 +00:00
|
|
|
func ValidateMetadata(meta map[string]string, allowConsulPrefix bool) error {
|
2017-01-23 23:53:45 +00:00
|
|
|
if len(meta) > metaMaxKeyPairs {
|
|
|
|
return fmt.Errorf("Node metadata cannot contain more than %d key/value pairs", metaMaxKeyPairs)
|
|
|
|
}
|
|
|
|
|
|
|
|
for key, value := range meta {
|
2017-08-30 00:02:50 +00:00
|
|
|
if err := validateMetaPair(key, value, allowConsulPrefix); err != nil {
|
2017-01-23 23:53:45 +00:00
|
|
|
return fmt.Errorf("Couldn't load metadata pair ('%s', '%s'): %s", key, value, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// validateMetaPair checks that the given key/value pair is in a valid format
|
2017-08-30 00:02:50 +00:00
|
|
|
func validateMetaPair(key, value string, allowConsulPrefix bool) error {
|
2017-01-23 23:53:45 +00:00
|
|
|
if key == "" {
|
|
|
|
return fmt.Errorf("Key cannot be blank")
|
|
|
|
}
|
|
|
|
if !metaKeyFormat(key) {
|
|
|
|
return fmt.Errorf("Key contains invalid characters")
|
|
|
|
}
|
|
|
|
if len(key) > metaKeyMaxLength {
|
|
|
|
return fmt.Errorf("Key is too long (limit: %d characters)", metaKeyMaxLength)
|
|
|
|
}
|
2017-08-30 00:02:50 +00:00
|
|
|
if strings.HasPrefix(key, metaKeyReservedPrefix) && !allowConsulPrefix {
|
2017-01-23 23:53:45 +00:00
|
|
|
return fmt.Errorf("Key prefix '%s' is reserved for internal use", metaKeyReservedPrefix)
|
|
|
|
}
|
|
|
|
if len(value) > metaValueMaxLength {
|
|
|
|
return fmt.Errorf("Value is too long (limit: %d characters)", metaValueMaxLength)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-01-18 00:20:29 +00:00
|
|
|
// SatisfiesMetaFilters returns true if the metadata map contains the given filters
|
2017-01-14 01:08:43 +00:00
|
|
|
func SatisfiesMetaFilters(meta map[string]string, filters map[string]string) bool {
|
|
|
|
for key, value := range filters {
|
|
|
|
if v, ok := meta[key]; !ok || v != value {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2013-12-12 19:07:14 +00:00
|
|
|
// Used to return information about a provided services.
|
|
|
|
// Maps service name to available tags
|
|
|
|
type Services map[string][]string
|
|
|
|
|
2017-01-18 22:26:42 +00:00
|
|
|
// ServiceNode represents a node that is part of a service. ID, Address,
|
|
|
|
// TaggedAddresses, and NodeMeta are node-related fields that are always empty
|
|
|
|
// in the state store and are filled in on the way out by parseServiceNodes().
|
|
|
|
// This is also why PartialClone() skips them, because we know they are blank
|
|
|
|
// already so it would be a waste of time to copy them.
|
2013-12-12 19:37:19 +00:00
|
|
|
type ServiceNode struct {
|
2017-01-18 22:26:42 +00:00
|
|
|
ID types.NodeID
|
2015-10-19 20:55:35 +00:00
|
|
|
Node string
|
|
|
|
Address string
|
2017-04-18 12:02:24 +00:00
|
|
|
Datacenter string
|
2016-06-15 18:02:51 +00:00
|
|
|
TaggedAddresses map[string]string
|
2017-01-05 22:10:26 +00:00
|
|
|
NodeMeta map[string]string
|
2018-03-06 03:56:52 +00:00
|
|
|
ServiceKind ServiceKind
|
2015-10-19 20:55:35 +00:00
|
|
|
ServiceID string
|
|
|
|
ServiceName string
|
|
|
|
ServiceTags []string
|
|
|
|
ServiceAddress string
|
2018-02-07 00:54:42 +00:00
|
|
|
ServiceMeta map[string]string
|
2015-10-19 20:55:35 +00:00
|
|
|
ServicePort int
|
|
|
|
ServiceEnableTagOverride bool
|
2018-03-06 03:56:52 +00:00
|
|
|
ServiceProxyDestination string
|
2018-06-04 05:14:01 +00:00
|
|
|
ServiceConnectNative bool
|
2015-08-23 21:17:48 +00:00
|
|
|
|
2015-08-25 01:10:23 +00:00
|
|
|
RaftIndex
|
2013-12-12 19:37:19 +00:00
|
|
|
}
|
2015-10-14 06:38:07 +00:00
|
|
|
|
2016-08-12 23:28:56 +00:00
|
|
|
// PartialClone() returns a clone of the given service node, minus the node-
|
|
|
|
// related fields that get filled in later, Address and TaggedAddresses.
|
|
|
|
func (s *ServiceNode) PartialClone() *ServiceNode {
|
2015-10-14 06:38:07 +00:00
|
|
|
tags := make([]string, len(s.ServiceTags))
|
|
|
|
copy(tags, s.ServiceTags)
|
2018-02-09 00:37:45 +00:00
|
|
|
nsmeta := make(map[string]string)
|
|
|
|
for k, v := range s.ServiceMeta {
|
|
|
|
nsmeta[k] = v
|
|
|
|
}
|
2015-10-14 06:38:07 +00:00
|
|
|
|
|
|
|
return &ServiceNode{
|
2017-01-18 22:26:42 +00:00
|
|
|
// Skip ID, see above.
|
2016-08-12 23:28:56 +00:00
|
|
|
Node: s.Node,
|
|
|
|
// Skip Address, see above.
|
|
|
|
// Skip TaggedAddresses, see above.
|
2018-03-07 01:32:41 +00:00
|
|
|
ServiceKind: s.ServiceKind,
|
2015-10-19 20:55:35 +00:00
|
|
|
ServiceID: s.ServiceID,
|
|
|
|
ServiceName: s.ServiceName,
|
|
|
|
ServiceTags: tags,
|
|
|
|
ServiceAddress: s.ServiceAddress,
|
|
|
|
ServicePort: s.ServicePort,
|
2018-02-09 00:37:45 +00:00
|
|
|
ServiceMeta: nsmeta,
|
2015-10-19 20:55:35 +00:00
|
|
|
ServiceEnableTagOverride: s.ServiceEnableTagOverride,
|
2018-03-07 01:32:41 +00:00
|
|
|
ServiceProxyDestination: s.ServiceProxyDestination,
|
2018-06-04 05:14:01 +00:00
|
|
|
ServiceConnectNative: s.ServiceConnectNative,
|
2015-10-19 20:55:35 +00:00
|
|
|
RaftIndex: RaftIndex{
|
|
|
|
CreateIndex: s.CreateIndex,
|
|
|
|
ModifyIndex: s.ModifyIndex,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ToNodeService converts the given service node to a node service.
|
|
|
|
func (s *ServiceNode) ToNodeService() *NodeService {
|
|
|
|
return &NodeService{
|
2018-03-06 03:56:52 +00:00
|
|
|
Kind: s.ServiceKind,
|
2015-10-19 20:55:35 +00:00
|
|
|
ID: s.ServiceID,
|
|
|
|
Service: s.ServiceName,
|
|
|
|
Tags: s.ServiceTags,
|
|
|
|
Address: s.ServiceAddress,
|
|
|
|
Port: s.ServicePort,
|
2018-03-28 14:04:50 +00:00
|
|
|
Meta: s.ServiceMeta,
|
2015-10-19 20:55:35 +00:00
|
|
|
EnableTagOverride: s.ServiceEnableTagOverride,
|
2018-03-06 03:56:52 +00:00
|
|
|
ProxyDestination: s.ServiceProxyDestination,
|
2018-06-04 05:14:01 +00:00
|
|
|
ConnectNative: s.ServiceConnectNative,
|
2015-10-14 06:38:07 +00:00
|
|
|
RaftIndex: RaftIndex{
|
|
|
|
CreateIndex: s.CreateIndex,
|
|
|
|
ModifyIndex: s.ModifyIndex,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-25 01:10:23 +00:00
|
|
|
type ServiceNodes []*ServiceNode
|
2013-12-12 19:37:19 +00:00
|
|
|
|
2018-03-06 03:56:52 +00:00
|
|
|
// ServiceKind is the kind of service being registered.
|
|
|
|
type ServiceKind string
|
|
|
|
|
|
|
|
const (
|
2018-03-11 16:11:10 +00:00
|
|
|
// ServiceKindTypical is a typical, classic Consul service. This is
|
2018-03-26 15:51:43 +00:00
|
|
|
// represented by the absence of a value. This was chosen for ease of
|
2018-03-11 16:11:10 +00:00
|
|
|
// backwards compatibility: existing services in the catalog would
|
|
|
|
// default to the typical service.
|
|
|
|
ServiceKindTypical ServiceKind = ""
|
2018-03-06 03:56:52 +00:00
|
|
|
|
|
|
|
// ServiceKindConnectProxy is a proxy for the Connect feature. This
|
|
|
|
// service proxies another service within Consul and speaks the connect
|
|
|
|
// protocol.
|
|
|
|
ServiceKindConnectProxy ServiceKind = "connect-proxy"
|
|
|
|
)
|
|
|
|
|
2013-12-12 19:46:25 +00:00
|
|
|
// NodeService is a service provided by a node
|
|
|
|
type NodeService struct {
|
2018-03-06 03:56:52 +00:00
|
|
|
// Kind is the kind of service this is. Different kinds of services may
|
|
|
|
// have differing validation, DNS behavior, etc. An empty kind will default
|
|
|
|
// to the Default kind. See ServiceKind for the full list of kinds.
|
|
|
|
Kind ServiceKind
|
|
|
|
|
2015-09-11 15:35:29 +00:00
|
|
|
ID string
|
|
|
|
Service string
|
|
|
|
Tags []string
|
|
|
|
Address string
|
2018-03-28 14:04:50 +00:00
|
|
|
Meta map[string]string
|
2015-09-11 15:35:29 +00:00
|
|
|
Port int
|
|
|
|
EnableTagOverride bool
|
2015-08-22 20:21:38 +00:00
|
|
|
|
2018-03-06 03:56:52 +00:00
|
|
|
// ProxyDestination is the name of the service that this service is
|
|
|
|
// a Connect proxy for. This is only valid if Kind is "connect-proxy".
|
2018-03-12 17:13:44 +00:00
|
|
|
// The destination may be a service that isn't present in the catalog.
|
|
|
|
// This is expected and allowed to allow for proxies to come up
|
|
|
|
// earlier than their target services.
|
2018-03-06 03:56:52 +00:00
|
|
|
ProxyDestination string
|
|
|
|
|
2018-06-04 05:14:01 +00:00
|
|
|
// ConnectNative is true if this service speaks the Connect protocol.
|
|
|
|
ConnectNative bool
|
|
|
|
|
2015-08-22 20:21:38 +00:00
|
|
|
RaftIndex
|
2013-12-12 19:46:25 +00:00
|
|
|
}
|
2015-08-22 20:21:38 +00:00
|
|
|
|
2018-03-09 06:13:35 +00:00
|
|
|
// Validate validates the node service configuration.
|
|
|
|
//
|
|
|
|
// NOTE(mitchellh): This currently only validates fields for a ConnectProxy.
|
|
|
|
// Historically validation has been directly in the Catalog.Register RPC.
|
|
|
|
// ConnectProxy validation was moved here for easier table testing, but
|
|
|
|
// other validation still exists in Catalog.Register.
|
|
|
|
func (s *NodeService) Validate() error {
|
|
|
|
var result error
|
|
|
|
|
|
|
|
// ConnectProxy validation
|
|
|
|
if s.Kind == ServiceKindConnectProxy {
|
|
|
|
if strings.TrimSpace(s.ProxyDestination) == "" {
|
|
|
|
result = multierror.Append(result, fmt.Errorf(
|
|
|
|
"ProxyDestination must be non-empty for Connect proxy services"))
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.Port == 0 {
|
|
|
|
result = multierror.Append(result, fmt.Errorf(
|
|
|
|
"Port must be set for a Connect proxy"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2015-10-28 21:32:00 +00:00
|
|
|
// IsSame checks if one NodeService is the same as another, without looking
|
|
|
|
// at the Raft information (that's why we didn't call it IsEqual). This is
|
|
|
|
// useful for seeing if an update would be idempotent for all the functional
|
|
|
|
// parts of the structure.
|
|
|
|
func (s *NodeService) IsSame(other *NodeService) bool {
|
|
|
|
if s.ID != other.ID ||
|
|
|
|
s.Service != other.Service ||
|
|
|
|
!reflect.DeepEqual(s.Tags, other.Tags) ||
|
|
|
|
s.Address != other.Address ||
|
|
|
|
s.Port != other.Port ||
|
2018-03-28 14:04:50 +00:00
|
|
|
!reflect.DeepEqual(s.Meta, other.Meta) ||
|
2018-03-10 01:21:26 +00:00
|
|
|
s.EnableTagOverride != other.EnableTagOverride ||
|
|
|
|
s.Kind != other.Kind ||
|
2018-06-04 05:14:01 +00:00
|
|
|
s.ProxyDestination != other.ProxyDestination ||
|
|
|
|
s.ConnectNative != other.ConnectNative {
|
2015-10-28 21:32:00 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2015-10-19 20:55:35 +00:00
|
|
|
// ToServiceNode converts the given node service to a service node.
|
2016-08-12 23:28:56 +00:00
|
|
|
func (s *NodeService) ToServiceNode(node string) *ServiceNode {
|
2015-10-19 20:55:35 +00:00
|
|
|
return &ServiceNode{
|
2017-01-18 22:26:42 +00:00
|
|
|
// Skip ID, see ServiceNode definition.
|
2016-08-12 23:28:56 +00:00
|
|
|
Node: node,
|
|
|
|
// Skip Address, see ServiceNode definition.
|
|
|
|
// Skip TaggedAddresses, see ServiceNode definition.
|
2018-03-06 03:56:52 +00:00
|
|
|
ServiceKind: s.Kind,
|
2015-10-19 20:55:35 +00:00
|
|
|
ServiceID: s.ID,
|
|
|
|
ServiceName: s.Service,
|
|
|
|
ServiceTags: s.Tags,
|
|
|
|
ServiceAddress: s.Address,
|
|
|
|
ServicePort: s.Port,
|
2018-03-28 14:04:50 +00:00
|
|
|
ServiceMeta: s.Meta,
|
2015-10-19 20:55:35 +00:00
|
|
|
ServiceEnableTagOverride: s.EnableTagOverride,
|
2018-03-06 03:56:52 +00:00
|
|
|
ServiceProxyDestination: s.ProxyDestination,
|
2018-06-04 05:14:01 +00:00
|
|
|
ServiceConnectNative: s.ConnectNative,
|
2015-10-19 20:55:35 +00:00
|
|
|
RaftIndex: RaftIndex{
|
|
|
|
CreateIndex: s.CreateIndex,
|
|
|
|
ModifyIndex: s.ModifyIndex,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-03 01:29:39 +00:00
|
|
|
type NodeServices struct {
|
2015-08-25 01:10:23 +00:00
|
|
|
Node *Node
|
2014-01-08 18:29:29 +00:00
|
|
|
Services map[string]*NodeService
|
2014-01-03 01:29:39 +00:00
|
|
|
}
|
2013-12-12 19:46:25 +00:00
|
|
|
|
2014-01-08 19:21:29 +00:00
|
|
|
// HealthCheck represents a single check on a given node
|
|
|
|
type HealthCheck struct {
|
|
|
|
Node string
|
2016-06-06 20:19:31 +00:00
|
|
|
CheckID types.CheckID // Unique per-node ID
|
|
|
|
Name string // Check name
|
|
|
|
Status string // The current check status
|
|
|
|
Notes string // Additional notes with the status
|
|
|
|
Output string // Holds output of script runs
|
|
|
|
ServiceID string // optional associated service
|
|
|
|
ServiceName string // optional service name
|
2017-04-27 23:03:05 +00:00
|
|
|
ServiceTags []string // optional service tags
|
2015-08-25 05:32:18 +00:00
|
|
|
|
2017-11-01 21:25:46 +00:00
|
|
|
Definition HealthCheckDefinition
|
|
|
|
|
|
|
|
RaftIndex
|
|
|
|
}
|
|
|
|
|
|
|
|
type HealthCheckDefinition struct {
|
2017-10-26 02:17:41 +00:00
|
|
|
HTTP string `json:",omitempty"`
|
|
|
|
TLSSkipVerify bool `json:",omitempty"`
|
|
|
|
Header map[string][]string `json:",omitempty"`
|
|
|
|
Method string `json:",omitempty"`
|
|
|
|
TCP string `json:",omitempty"`
|
|
|
|
Interval api.ReadableDuration `json:",omitempty"`
|
|
|
|
Timeout api.ReadableDuration `json:",omitempty"`
|
|
|
|
DeregisterCriticalServiceAfter api.ReadableDuration `json:",omitempty"`
|
2014-01-08 19:21:29 +00:00
|
|
|
}
|
2015-10-28 21:32:00 +00:00
|
|
|
|
|
|
|
// IsSame checks if one HealthCheck is the same as another, without looking
|
|
|
|
// at the Raft information (that's why we didn't call it IsEqual). This is
|
|
|
|
// useful for seeing if an update would be idempotent for all the functional
|
|
|
|
// parts of the structure.
|
|
|
|
func (c *HealthCheck) IsSame(other *HealthCheck) bool {
|
|
|
|
if c.Node != other.Node ||
|
|
|
|
c.CheckID != other.CheckID ||
|
|
|
|
c.Name != other.Name ||
|
|
|
|
c.Status != other.Status ||
|
|
|
|
c.Notes != other.Notes ||
|
|
|
|
c.Output != other.Output ||
|
|
|
|
c.ServiceID != other.ServiceID ||
|
2017-04-27 23:03:05 +00:00
|
|
|
c.ServiceName != other.ServiceName ||
|
|
|
|
!reflect.DeepEqual(c.ServiceTags, other.ServiceTags) {
|
2015-10-28 21:32:00 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2016-04-11 07:05:39 +00:00
|
|
|
// Clone returns a distinct clone of the HealthCheck.
|
|
|
|
func (c *HealthCheck) Clone() *HealthCheck {
|
|
|
|
clone := new(HealthCheck)
|
|
|
|
*clone = *c
|
|
|
|
return clone
|
|
|
|
}
|
|
|
|
|
2016-11-29 21:15:20 +00:00
|
|
|
// HealthChecks is a collection of HealthCheck structs.
|
2014-01-08 19:35:27 +00:00
|
|
|
type HealthChecks []*HealthCheck
|
2014-01-08 19:21:29 +00:00
|
|
|
|
2015-09-10 21:40:11 +00:00
|
|
|
// CheckServiceNode is used to provide the node, its service
|
2015-07-27 21:41:46 +00:00
|
|
|
// definition, as well as a HealthCheck that is associated.
|
2014-01-08 22:58:53 +00:00
|
|
|
type CheckServiceNode struct {
|
2015-09-01 04:04:25 +00:00
|
|
|
Node *Node
|
|
|
|
Service *NodeService
|
2014-01-08 22:58:53 +00:00
|
|
|
Checks HealthChecks
|
2014-01-08 21:52:09 +00:00
|
|
|
}
|
2014-01-08 22:58:53 +00:00
|
|
|
type CheckServiceNodes []CheckServiceNode
|
2014-01-08 21:52:09 +00:00
|
|
|
|
2015-11-07 00:59:32 +00:00
|
|
|
// Shuffle does an in-place random shuffle using the Fisher-Yates algorithm.
|
|
|
|
func (nodes CheckServiceNodes) Shuffle() {
|
|
|
|
for i := len(nodes) - 1; i > 0; i-- {
|
2016-02-18 23:17:42 +00:00
|
|
|
j := rand.Int31n(int32(i + 1))
|
2015-11-07 00:59:32 +00:00
|
|
|
nodes[i], nodes[j] = nodes[j], nodes[i]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Filter removes nodes that are failing health checks (and any non-passing
|
|
|
|
// check if that option is selected). Note that this returns the filtered
|
|
|
|
// results AND modifies the receiver for performance.
|
|
|
|
func (nodes CheckServiceNodes) Filter(onlyPassing bool) CheckServiceNodes {
|
2018-04-10 12:28:27 +00:00
|
|
|
return nodes.FilterIgnore(onlyPassing, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// FilterIgnore removes nodes that are failing health checks just like Filter.
|
|
|
|
// It also ignores the status of any check with an ID present in ignoreCheckIDs
|
|
|
|
// as if that check didn't exist. Note that this returns the filtered results
|
|
|
|
// AND modifies the receiver for performance.
|
|
|
|
func (nodes CheckServiceNodes) FilterIgnore(onlyPassing bool,
|
|
|
|
ignoreCheckIDs []types.CheckID) CheckServiceNodes {
|
2015-11-07 00:59:32 +00:00
|
|
|
n := len(nodes)
|
|
|
|
OUTER:
|
|
|
|
for i := 0; i < n; i++ {
|
|
|
|
node := nodes[i]
|
2018-04-10 12:28:27 +00:00
|
|
|
INNER:
|
2015-11-07 00:59:32 +00:00
|
|
|
for _, check := range node.Checks {
|
2018-04-10 12:28:27 +00:00
|
|
|
for _, ignore := range ignoreCheckIDs {
|
|
|
|
if check.CheckID == ignore {
|
|
|
|
// Skip this _check_ but keep looking at other checks for this node.
|
|
|
|
continue INNER
|
|
|
|
}
|
|
|
|
}
|
2017-04-19 23:00:11 +00:00
|
|
|
if check.Status == api.HealthCritical ||
|
|
|
|
(onlyPassing && check.Status != api.HealthPassing) {
|
2015-11-07 00:59:32 +00:00
|
|
|
nodes[i], nodes[n-1] = nodes[n-1], CheckServiceNode{}
|
|
|
|
n--
|
|
|
|
i--
|
2018-04-10 12:28:27 +00:00
|
|
|
// Skip this _node_ now we've swapped it off the end of the list.
|
2015-11-07 00:59:32 +00:00
|
|
|
continue OUTER
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nodes[:n]
|
|
|
|
}
|
|
|
|
|
2014-04-27 19:49:20 +00:00
|
|
|
// NodeInfo is used to dump all associated information about
|
|
|
|
// a node. This is currently used for the UI only, as it is
|
|
|
|
// rather expensive to generate.
|
|
|
|
type NodeInfo struct {
|
2017-01-18 22:26:42 +00:00
|
|
|
ID types.NodeID
|
2016-02-07 18:37:34 +00:00
|
|
|
Node string
|
|
|
|
Address string
|
|
|
|
TaggedAddresses map[string]string
|
2017-01-05 22:10:26 +00:00
|
|
|
Meta map[string]string
|
2016-02-07 18:37:34 +00:00
|
|
|
Services []*NodeService
|
2016-11-29 21:15:20 +00:00
|
|
|
Checks HealthChecks
|
2014-04-27 19:49:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NodeDump is used to dump all the nodes with all their
|
|
|
|
// associated data. This is currently used for the UI only,
|
|
|
|
// as it is rather expensive to generate.
|
|
|
|
type NodeDump []*NodeInfo
|
|
|
|
|
2014-02-05 18:44:28 +00:00
|
|
|
type IndexedNodes struct {
|
|
|
|
Nodes Nodes
|
2014-04-18 23:46:51 +00:00
|
|
|
QueryMeta
|
2014-02-05 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type IndexedServices struct {
|
2014-02-05 22:27:24 +00:00
|
|
|
Services Services
|
2014-04-18 23:46:51 +00:00
|
|
|
QueryMeta
|
2014-02-05 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type IndexedServiceNodes struct {
|
|
|
|
ServiceNodes ServiceNodes
|
2014-04-18 23:46:51 +00:00
|
|
|
QueryMeta
|
2014-02-05 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type IndexedNodeServices struct {
|
2018-02-07 15:02:10 +00:00
|
|
|
// TODO: This should not be a pointer, see comments in
|
|
|
|
// agent/catalog_endpoint.go.
|
2014-02-05 18:44:28 +00:00
|
|
|
NodeServices *NodeServices
|
2014-04-18 23:46:51 +00:00
|
|
|
QueryMeta
|
2014-02-05 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type IndexedHealthChecks struct {
|
|
|
|
HealthChecks HealthChecks
|
2014-04-18 23:46:51 +00:00
|
|
|
QueryMeta
|
2014-02-05 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
2014-02-05 21:30:18 +00:00
|
|
|
type IndexedCheckServiceNodes struct {
|
|
|
|
Nodes CheckServiceNodes
|
2014-04-18 23:46:51 +00:00
|
|
|
QueryMeta
|
2014-02-05 21:30:18 +00:00
|
|
|
}
|
|
|
|
|
2014-04-27 19:56:06 +00:00
|
|
|
type IndexedNodeDump struct {
|
|
|
|
Dump NodeDump
|
|
|
|
QueryMeta
|
|
|
|
}
|
|
|
|
|
2014-03-31 18:47:10 +00:00
|
|
|
// DirEntry is used to represent a directory entry. This is
|
|
|
|
// used for values in our Key-Value store.
|
|
|
|
type DirEntry struct {
|
2015-09-01 23:33:52 +00:00
|
|
|
LockIndex uint64
|
|
|
|
Key string
|
|
|
|
Flags uint64
|
|
|
|
Value []byte
|
|
|
|
Session string `json:",omitempty"`
|
|
|
|
|
|
|
|
RaftIndex
|
2014-03-31 18:47:10 +00:00
|
|
|
}
|
2015-09-25 19:01:46 +00:00
|
|
|
|
|
|
|
// Returns a clone of the given directory entry.
|
|
|
|
func (d *DirEntry) Clone() *DirEntry {
|
|
|
|
return &DirEntry{
|
|
|
|
LockIndex: d.LockIndex,
|
|
|
|
Key: d.Key,
|
|
|
|
Flags: d.Flags,
|
|
|
|
Value: d.Value,
|
|
|
|
Session: d.Session,
|
|
|
|
RaftIndex: RaftIndex{
|
|
|
|
CreateIndex: d.CreateIndex,
|
|
|
|
ModifyIndex: d.ModifyIndex,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-31 18:47:10 +00:00
|
|
|
type DirEntries []*DirEntry
|
|
|
|
|
|
|
|
// KVSRequest is used to operate on the Key-Value store
|
|
|
|
type KVSRequest struct {
|
2014-03-31 21:13:03 +00:00
|
|
|
Datacenter string
|
2017-04-19 23:00:11 +00:00
|
|
|
Op api.KVOp // Which operation are we performing
|
2014-03-31 21:13:03 +00:00
|
|
|
DirEnt DirEntry // Which directory entry
|
2014-04-19 00:14:00 +00:00
|
|
|
WriteRequest
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *KVSRequest) RequestDatacenter() string {
|
|
|
|
return r.Datacenter
|
2014-03-31 18:47:10 +00:00
|
|
|
}
|
|
|
|
|
2014-03-31 23:00:23 +00:00
|
|
|
// KeyRequest is used to request a key, or key prefix
|
|
|
|
type KeyRequest struct {
|
|
|
|
Datacenter string
|
|
|
|
Key string
|
2014-04-18 23:46:51 +00:00
|
|
|
QueryOptions
|
2014-03-31 23:00:23 +00:00
|
|
|
}
|
|
|
|
|
2014-04-19 00:14:00 +00:00
|
|
|
func (r *KeyRequest) RequestDatacenter() string {
|
|
|
|
return r.Datacenter
|
|
|
|
}
|
|
|
|
|
2014-04-28 23:33:54 +00:00
|
|
|
// KeyListRequest is used to list keys
|
|
|
|
type KeyListRequest struct {
|
|
|
|
Datacenter string
|
|
|
|
Prefix string
|
|
|
|
Seperator string
|
|
|
|
QueryOptions
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *KeyListRequest) RequestDatacenter() string {
|
|
|
|
return r.Datacenter
|
|
|
|
}
|
|
|
|
|
2014-03-31 23:00:23 +00:00
|
|
|
type IndexedDirEntries struct {
|
|
|
|
Entries DirEntries
|
2014-04-18 23:46:51 +00:00
|
|
|
QueryMeta
|
2014-03-31 23:00:23 +00:00
|
|
|
}
|
|
|
|
|
2014-04-28 23:33:54 +00:00
|
|
|
type IndexedKeyList struct {
|
|
|
|
Keys []string
|
|
|
|
QueryMeta
|
|
|
|
}
|
|
|
|
|
2014-11-20 02:11:41 +00:00
|
|
|
type SessionBehavior string
|
|
|
|
|
|
|
|
const (
|
|
|
|
SessionKeysRelease SessionBehavior = "release"
|
|
|
|
SessionKeysDelete = "delete"
|
|
|
|
)
|
|
|
|
|
2014-11-25 16:06:14 +00:00
|
|
|
const (
|
2015-11-14 23:30:53 +00:00
|
|
|
SessionTTLMax = 24 * time.Hour
|
2014-11-25 16:06:14 +00:00
|
|
|
SessionTTLMultiplier = 2
|
|
|
|
)
|
|
|
|
|
2014-05-08 22:01:02 +00:00
|
|
|
// Session is used to represent an open session in the KV store.
|
|
|
|
// This issued to associate node checks with acquired locks.
|
|
|
|
type Session struct {
|
2015-09-04 02:11:12 +00:00
|
|
|
ID string
|
|
|
|
Name string
|
|
|
|
Node string
|
2016-06-07 20:24:51 +00:00
|
|
|
Checks []types.CheckID
|
2015-09-04 02:11:12 +00:00
|
|
|
LockDelay time.Duration
|
|
|
|
Behavior SessionBehavior // What to do when session is invalidated
|
|
|
|
TTL string
|
|
|
|
|
|
|
|
RaftIndex
|
2014-05-08 22:01:02 +00:00
|
|
|
}
|
2014-05-16 21:36:14 +00:00
|
|
|
type Sessions []*Session
|
2014-05-08 22:01:02 +00:00
|
|
|
|
2014-05-16 02:22:31 +00:00
|
|
|
type SessionOp string
|
|
|
|
|
|
|
|
const (
|
|
|
|
SessionCreate SessionOp = "create"
|
|
|
|
SessionDestroy = "destroy"
|
|
|
|
)
|
|
|
|
|
|
|
|
// SessionRequest is used to operate on sessions
|
|
|
|
type SessionRequest struct {
|
|
|
|
Datacenter string
|
|
|
|
Op SessionOp // Which operation are we performing
|
|
|
|
Session Session // Which session
|
|
|
|
WriteRequest
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *SessionRequest) RequestDatacenter() string {
|
|
|
|
return r.Datacenter
|
|
|
|
}
|
|
|
|
|
2014-05-16 22:49:17 +00:00
|
|
|
// SessionSpecificRequest is used to request a session by ID
|
|
|
|
type SessionSpecificRequest struct {
|
2014-05-16 21:36:14 +00:00
|
|
|
Datacenter string
|
|
|
|
Session string
|
|
|
|
QueryOptions
|
|
|
|
}
|
|
|
|
|
2014-05-16 22:49:17 +00:00
|
|
|
func (r *SessionSpecificRequest) RequestDatacenter() string {
|
2014-05-16 21:36:14 +00:00
|
|
|
return r.Datacenter
|
|
|
|
}
|
|
|
|
|
|
|
|
type IndexedSessions struct {
|
|
|
|
Sessions Sessions
|
|
|
|
QueryMeta
|
|
|
|
}
|
|
|
|
|
2015-06-19 15:26:56 +00:00
|
|
|
// Coordinate stores a node name with its associated network coordinate.
|
2015-03-28 18:52:04 +00:00
|
|
|
type Coordinate struct {
|
2017-08-14 14:36:07 +00:00
|
|
|
Node string
|
|
|
|
Segment string
|
|
|
|
Coord *coordinate.Coordinate
|
2015-03-28 18:52:04 +00:00
|
|
|
}
|
|
|
|
|
2015-10-23 22:19:14 +00:00
|
|
|
type Coordinates []*Coordinate
|
|
|
|
|
2015-06-06 03:31:33 +00:00
|
|
|
// IndexedCoordinate is used to represent a single node's coordinate from the state
|
2015-07-29 23:33:25 +00:00
|
|
|
// store.
|
2015-04-18 21:05:29 +00:00
|
|
|
type IndexedCoordinate struct {
|
|
|
|
Coord *coordinate.Coordinate
|
|
|
|
QueryMeta
|
|
|
|
}
|
|
|
|
|
2015-07-29 23:33:25 +00:00
|
|
|
// IndexedCoordinates is used to represent a list of nodes and their
|
|
|
|
// corresponding raw coordinates.
|
|
|
|
type IndexedCoordinates struct {
|
2015-10-23 22:19:14 +00:00
|
|
|
Coordinates Coordinates
|
2015-07-29 23:33:25 +00:00
|
|
|
QueryMeta
|
|
|
|
}
|
|
|
|
|
|
|
|
// DatacenterMap is used to represent a list of nodes with their raw coordinates,
|
2017-03-14 01:54:34 +00:00
|
|
|
// associated with a datacenter. Coordinates are only compatible between nodes in
|
|
|
|
// the same area.
|
2015-07-29 23:33:25 +00:00
|
|
|
type DatacenterMap struct {
|
|
|
|
Datacenter string
|
2017-03-14 01:54:34 +00:00
|
|
|
AreaID types.AreaID
|
2015-10-23 22:19:14 +00:00
|
|
|
Coordinates Coordinates
|
2015-07-29 23:33:25 +00:00
|
|
|
}
|
|
|
|
|
2015-06-06 03:31:33 +00:00
|
|
|
// CoordinateUpdateRequest is used to update the network coordinate of a given
|
|
|
|
// node.
|
2015-03-28 18:52:04 +00:00
|
|
|
type CoordinateUpdateRequest struct {
|
2015-04-18 21:05:29 +00:00
|
|
|
Datacenter string
|
|
|
|
Node string
|
2017-08-14 14:36:07 +00:00
|
|
|
Segment string
|
2015-04-18 21:05:29 +00:00
|
|
|
Coord *coordinate.Coordinate
|
2015-04-09 20:23:14 +00:00
|
|
|
WriteRequest
|
2015-03-28 18:52:04 +00:00
|
|
|
}
|
|
|
|
|
2015-06-06 03:31:33 +00:00
|
|
|
// RequestDatacenter returns the datacenter for a given update request.
|
2015-04-18 21:05:29 +00:00
|
|
|
func (c *CoordinateUpdateRequest) RequestDatacenter() string {
|
|
|
|
return c.Datacenter
|
|
|
|
}
|
|
|
|
|
2014-08-28 22:00:49 +00:00
|
|
|
// EventFireRequest is used to ask a server to fire
|
|
|
|
// a Serf event. It is a bit odd, since it doesn't depend on
|
|
|
|
// the catalog or leader. Any node can respond, so it's not quite
|
|
|
|
// like a standard write request. This is used only internally.
|
|
|
|
type EventFireRequest struct {
|
|
|
|
Datacenter string
|
|
|
|
Name string
|
|
|
|
Payload []byte
|
|
|
|
|
|
|
|
// Not using WriteRequest so that any server can process
|
|
|
|
// the request. It is a bit unusual...
|
|
|
|
QueryOptions
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *EventFireRequest) RequestDatacenter() string {
|
|
|
|
return r.Datacenter
|
|
|
|
}
|
|
|
|
|
|
|
|
// EventFireResponse is used to respond to a fire request.
|
|
|
|
type EventFireResponse struct {
|
|
|
|
QueryMeta
|
|
|
|
}
|
|
|
|
|
2014-12-15 23:26:46 +00:00
|
|
|
type TombstoneOp string
|
|
|
|
|
|
|
|
const (
|
|
|
|
TombstoneReap TombstoneOp = "reap"
|
|
|
|
)
|
|
|
|
|
|
|
|
// TombstoneRequest is used to trigger a reaping of the tombstones
|
|
|
|
type TombstoneRequest struct {
|
2014-12-15 23:01:04 +00:00
|
|
|
Datacenter string
|
2014-12-15 23:26:46 +00:00
|
|
|
Op TombstoneOp
|
2014-12-15 23:01:04 +00:00
|
|
|
ReapIndex uint64
|
|
|
|
WriteRequest
|
|
|
|
}
|
|
|
|
|
2014-12-15 23:26:46 +00:00
|
|
|
func (r *TombstoneRequest) RequestDatacenter() string {
|
2014-12-15 23:01:04 +00:00
|
|
|
return r.Datacenter
|
|
|
|
}
|
|
|
|
|
2014-06-08 21:02:42 +00:00
|
|
|
// msgpackHandle is a shared handle for encoding/decoding of structs
|
|
|
|
var msgpackHandle = &codec.MsgpackHandle{}
|
2014-06-07 07:59:27 +00:00
|
|
|
|
2013-12-11 22:04:44 +00:00
|
|
|
// Decode is used to decode a MsgPack encoded object
|
|
|
|
func Decode(buf []byte, out interface{}) error {
|
2014-06-08 21:02:42 +00:00
|
|
|
return codec.NewDecoder(bytes.NewReader(buf), msgpackHandle).Decode(out)
|
2013-12-11 22:04:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Encode is used to encode a MsgPack object with type prefix
|
|
|
|
func Encode(t MessageType, msg interface{}) ([]byte, error) {
|
2014-06-08 21:02:42 +00:00
|
|
|
var buf bytes.Buffer
|
2013-12-11 22:04:44 +00:00
|
|
|
buf.WriteByte(uint8(t))
|
2014-06-08 21:02:42 +00:00
|
|
|
err := codec.NewEncoder(&buf, msgpackHandle).Encode(msg)
|
2013-12-11 22:04:44 +00:00
|
|
|
return buf.Bytes(), err
|
|
|
|
}
|
2014-09-24 23:39:14 +00:00
|
|
|
|
2014-10-02 06:09:00 +00:00
|
|
|
// CompoundResponse is an interface for gathering multiple responses. It is
|
|
|
|
// used in cross-datacenter RPC calls where more than 1 datacenter is
|
|
|
|
// expected to reply.
|
|
|
|
type CompoundResponse interface {
|
|
|
|
// Add adds a new response to the compound response
|
|
|
|
Add(interface{})
|
|
|
|
|
|
|
|
// New returns an empty response object which can be passed around by
|
|
|
|
// reference, and then passed to Add() later on.
|
|
|
|
New() interface{}
|
|
|
|
}
|
|
|
|
|
2014-10-03 00:10:54 +00:00
|
|
|
type KeyringOp string
|
|
|
|
|
|
|
|
const (
|
|
|
|
KeyringList KeyringOp = "list"
|
|
|
|
KeyringInstall = "install"
|
|
|
|
KeyringUse = "use"
|
|
|
|
KeyringRemove = "remove"
|
|
|
|
)
|
|
|
|
|
2014-09-24 23:39:14 +00:00
|
|
|
// KeyringRequest encapsulates a request to modify an encryption keyring.
|
|
|
|
// It can be used for install, remove, or use key type operations.
|
|
|
|
type KeyringRequest struct {
|
2017-02-02 02:42:41 +00:00
|
|
|
Operation KeyringOp
|
|
|
|
Key string
|
|
|
|
Datacenter string
|
|
|
|
Forwarded bool
|
|
|
|
RelayFactor uint8
|
2014-09-24 23:39:14 +00:00
|
|
|
QueryOptions
|
|
|
|
}
|
|
|
|
|
2014-10-02 06:09:00 +00:00
|
|
|
func (r *KeyringRequest) RequestDatacenter() string {
|
|
|
|
return r.Datacenter
|
|
|
|
}
|
|
|
|
|
2014-09-24 23:39:14 +00:00
|
|
|
// KeyringResponse is a unified key response and can be used for install,
|
|
|
|
// remove, use, as well as listing key queries.
|
|
|
|
type KeyringResponse struct {
|
2014-09-28 19:35:51 +00:00
|
|
|
WAN bool
|
2014-09-25 01:30:34 +00:00
|
|
|
Datacenter string
|
2017-09-07 19:17:39 +00:00
|
|
|
Segment string
|
2016-11-15 02:54:37 +00:00
|
|
|
Messages map[string]string `json:",omitempty"`
|
2014-09-25 01:30:34 +00:00
|
|
|
Keys map[string]int
|
|
|
|
NumNodes int
|
2016-11-15 02:54:37 +00:00
|
|
|
Error string `json:",omitempty"`
|
2014-09-25 01:30:34 +00:00
|
|
|
}
|
|
|
|
|
2014-09-30 22:31:07 +00:00
|
|
|
// KeyringResponses holds multiple responses to keyring queries. Each
|
|
|
|
// datacenter replies independently, and KeyringResponses is used as a
|
|
|
|
// container for the set of all responses.
|
2014-09-25 01:30:34 +00:00
|
|
|
type KeyringResponses struct {
|
|
|
|
Responses []*KeyringResponse
|
2014-09-28 19:35:51 +00:00
|
|
|
QueryMeta
|
2014-09-24 23:39:14 +00:00
|
|
|
}
|
2014-10-02 06:09:00 +00:00
|
|
|
|
|
|
|
func (r *KeyringResponses) Add(v interface{}) {
|
|
|
|
val := v.(*KeyringResponses)
|
|
|
|
r.Responses = append(r.Responses, val.Responses...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *KeyringResponses) New() interface{} {
|
|
|
|
return new(KeyringResponses)
|
|
|
|
}
|