2020-12-16 22:12:42 +00:00
|
|
|
package state
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"reflect"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/hashicorp/go-memdb"
|
|
|
|
|
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2021-01-29 01:48:51 +00:00
|
|
|
tableNodes = "nodes"
|
|
|
|
tableServices = "services"
|
|
|
|
tableChecks = "checks"
|
|
|
|
tableGatewayServices = "gateway-services"
|
|
|
|
tableMeshTopology = "mesh-topology"
|
2020-12-16 22:12:42 +00:00
|
|
|
|
2021-02-11 00:40:32 +00:00
|
|
|
indexID = "id"
|
2021-03-16 17:54:05 +00:00
|
|
|
indexService = "service"
|
2021-02-11 00:40:32 +00:00
|
|
|
indexConnect = "connect"
|
|
|
|
indexKind = "kind"
|
|
|
|
indexStatus = "status"
|
|
|
|
indexNodeService = "node_service"
|
2021-02-12 22:31:02 +00:00
|
|
|
indexNode = "node"
|
2021-03-16 17:15:14 +00:00
|
|
|
indexUpstream = "upstream"
|
|
|
|
indexDownstream = "downstream"
|
2021-03-16 17:54:05 +00:00
|
|
|
indexGateway = "gateway"
|
2020-12-16 22:12:42 +00:00
|
|
|
)
|
|
|
|
|
2021-02-19 23:18:25 +00:00
|
|
|
// nodesTableSchema returns a new table schema used for storing struct.Node.
|
2020-12-16 22:12:42 +00:00
|
|
|
func nodesTableSchema() *memdb.TableSchema {
|
|
|
|
return &memdb.TableSchema{
|
|
|
|
Name: tableNodes,
|
|
|
|
Indexes: map[string]*memdb.IndexSchema{
|
2021-01-29 01:30:08 +00:00
|
|
|
indexID: {
|
|
|
|
Name: indexID,
|
2020-12-16 22:12:42 +00:00
|
|
|
AllowMissing: false,
|
|
|
|
Unique: true,
|
2021-02-19 23:18:25 +00:00
|
|
|
Indexer: indexerSingle{
|
2021-03-29 18:14:20 +00:00
|
|
|
readIndex: indexFromQuery,
|
2021-03-29 17:55:13 +00:00
|
|
|
writeIndex: indexFromNode,
|
2020-12-16 22:12:42 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
"uuid": {
|
|
|
|
Name: "uuid",
|
|
|
|
AllowMissing: true,
|
|
|
|
Unique: true,
|
2021-02-19 23:18:25 +00:00
|
|
|
Indexer: &memdb.UUIDFieldIndex{Field: "ID"},
|
2020-12-16 22:12:42 +00:00
|
|
|
},
|
|
|
|
"meta": {
|
|
|
|
Name: "meta",
|
|
|
|
AllowMissing: true,
|
|
|
|
Unique: false,
|
|
|
|
Indexer: &memdb.StringMapFieldIndex{
|
|
|
|
Field: "Meta",
|
|
|
|
Lowercase: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-29 17:55:13 +00:00
|
|
|
func indexFromNode(raw interface{}) ([]byte, error) {
|
|
|
|
n, ok := raw.(*structs.Node)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("unexpected type %T for structs.Node index", raw)
|
|
|
|
}
|
|
|
|
|
|
|
|
if n.Node == "" {
|
|
|
|
return nil, errMissingValueForIndex
|
|
|
|
}
|
|
|
|
|
|
|
|
var b indexBuilder
|
|
|
|
b.String(strings.ToLower(n.Node))
|
|
|
|
return b.Bytes(), nil
|
|
|
|
}
|
|
|
|
|
2020-12-16 22:12:42 +00:00
|
|
|
// servicesTableSchema returns a new table schema used to store information
|
|
|
|
// about services.
|
|
|
|
func servicesTableSchema() *memdb.TableSchema {
|
|
|
|
return &memdb.TableSchema{
|
|
|
|
Name: tableServices,
|
|
|
|
Indexes: map[string]*memdb.IndexSchema{
|
|
|
|
indexID: {
|
|
|
|
Name: indexID,
|
|
|
|
AllowMissing: false,
|
|
|
|
Unique: true,
|
2021-02-12 21:52:09 +00:00
|
|
|
Indexer: indexerSingleWithPrefix{
|
2021-03-29 17:55:13 +00:00
|
|
|
readIndex: indexFromNodeServiceQuery,
|
|
|
|
writeIndex: indexFromServiceNode,
|
|
|
|
prefixIndex: prefixIndexFromQuery,
|
2020-12-16 22:12:42 +00:00
|
|
|
},
|
|
|
|
},
|
2021-02-12 22:31:02 +00:00
|
|
|
indexNode: {
|
|
|
|
Name: indexNode,
|
2020-12-16 22:12:42 +00:00
|
|
|
AllowMissing: false,
|
|
|
|
Unique: false,
|
2021-02-12 22:31:02 +00:00
|
|
|
Indexer: indexerSingle{
|
2021-03-29 18:14:20 +00:00
|
|
|
readIndex: indexFromQuery,
|
2021-03-29 17:55:13 +00:00
|
|
|
writeIndex: indexFromNodeIdentity,
|
2020-12-16 22:12:42 +00:00
|
|
|
},
|
|
|
|
},
|
2021-03-16 17:54:05 +00:00
|
|
|
indexService: {
|
|
|
|
Name: indexService,
|
2020-12-16 22:12:42 +00:00
|
|
|
AllowMissing: true,
|
|
|
|
Unique: false,
|
|
|
|
Indexer: &memdb.StringFieldIndex{
|
|
|
|
Field: "ServiceName",
|
|
|
|
Lowercase: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
indexConnect: {
|
|
|
|
Name: indexConnect,
|
|
|
|
AllowMissing: true,
|
|
|
|
Unique: false,
|
|
|
|
Indexer: &IndexConnectService{},
|
|
|
|
},
|
|
|
|
indexKind: {
|
|
|
|
Name: indexKind,
|
|
|
|
AllowMissing: false,
|
|
|
|
Unique: false,
|
|
|
|
Indexer: &IndexServiceKind{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-29 17:55:13 +00:00
|
|
|
func indexFromNodeServiceQuery(arg interface{}) ([]byte, error) {
|
|
|
|
q, ok := arg.(NodeServiceQuery)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("unexpected type %T for NodeServiceQuery index", arg)
|
|
|
|
}
|
|
|
|
|
|
|
|
var b indexBuilder
|
|
|
|
b.String(strings.ToLower(q.Node))
|
|
|
|
b.String(strings.ToLower(q.Service))
|
|
|
|
return b.Bytes(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func indexFromServiceNode(raw interface{}) ([]byte, error) {
|
|
|
|
n, ok := raw.(*structs.ServiceNode)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("unexpected type %T for structs.ServiceNode index", raw)
|
|
|
|
}
|
|
|
|
|
|
|
|
if n.Node == "" {
|
|
|
|
return nil, errMissingValueForIndex
|
|
|
|
}
|
|
|
|
|
|
|
|
var b indexBuilder
|
|
|
|
b.String(strings.ToLower(n.Node))
|
|
|
|
b.String(strings.ToLower(n.ServiceID))
|
|
|
|
return b.Bytes(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func indexFromNodeIdentity(raw interface{}) ([]byte, error) {
|
|
|
|
n, ok := raw.(interface {
|
|
|
|
NodeIdentity() structs.Identity
|
|
|
|
})
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("unexpected type %T for index, type must provide NodeIdentity()", raw)
|
|
|
|
}
|
|
|
|
|
|
|
|
id := n.NodeIdentity()
|
|
|
|
if id.ID == "" {
|
|
|
|
return nil, errMissingValueForIndex
|
|
|
|
}
|
|
|
|
|
|
|
|
var b indexBuilder
|
|
|
|
b.String(strings.ToLower(id.ID))
|
|
|
|
return b.Bytes(), nil
|
|
|
|
}
|
|
|
|
|
2020-12-16 22:12:42 +00:00
|
|
|
// checksTableSchema returns a new table schema used for storing and indexing
|
|
|
|
// health check information. Health checks have a number of different attributes
|
|
|
|
// we want to filter by, so this table is a bit more complex.
|
|
|
|
func checksTableSchema() *memdb.TableSchema {
|
|
|
|
return &memdb.TableSchema{
|
|
|
|
Name: tableChecks,
|
|
|
|
Indexes: map[string]*memdb.IndexSchema{
|
|
|
|
indexID: {
|
|
|
|
Name: indexID,
|
|
|
|
AllowMissing: false,
|
|
|
|
Unique: true,
|
2021-02-12 23:06:56 +00:00
|
|
|
Indexer: indexerSingleWithPrefix{
|
2021-03-29 17:55:13 +00:00
|
|
|
readIndex: indexFromNodeCheckQuery,
|
|
|
|
writeIndex: indexFromHealthCheck,
|
|
|
|
prefixIndex: prefixIndexFromQuery,
|
2020-12-16 22:12:42 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
indexStatus: {
|
|
|
|
Name: indexStatus,
|
|
|
|
AllowMissing: false,
|
|
|
|
Unique: false,
|
|
|
|
Indexer: &memdb.StringFieldIndex{
|
|
|
|
Field: "Status",
|
|
|
|
Lowercase: false,
|
|
|
|
},
|
|
|
|
},
|
2021-03-16 17:54:05 +00:00
|
|
|
indexService: {
|
|
|
|
Name: indexService,
|
2020-12-16 22:12:42 +00:00
|
|
|
AllowMissing: true,
|
|
|
|
Unique: false,
|
|
|
|
Indexer: &memdb.StringFieldIndex{
|
|
|
|
Field: "ServiceName",
|
|
|
|
Lowercase: true,
|
|
|
|
},
|
|
|
|
},
|
2021-02-12 22:31:02 +00:00
|
|
|
indexNode: {
|
|
|
|
Name: indexNode,
|
2020-12-16 22:12:42 +00:00
|
|
|
AllowMissing: true,
|
|
|
|
Unique: false,
|
2021-02-12 22:31:02 +00:00
|
|
|
Indexer: indexerSingle{
|
2021-03-29 18:14:20 +00:00
|
|
|
readIndex: indexFromQuery,
|
2021-03-29 17:55:13 +00:00
|
|
|
writeIndex: indexFromNodeIdentity,
|
2020-12-16 22:12:42 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
indexNodeService: {
|
|
|
|
Name: indexNodeService,
|
|
|
|
AllowMissing: true,
|
|
|
|
Unique: false,
|
2021-02-11 00:40:32 +00:00
|
|
|
Indexer: indexerSingle{
|
2021-03-29 17:55:13 +00:00
|
|
|
readIndex: indexFromNodeServiceQuery,
|
|
|
|
writeIndex: indexNodeServiceFromHealthCheck,
|
2020-12-16 22:12:42 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-29 17:55:13 +00:00
|
|
|
func indexFromNodeCheckQuery(raw interface{}) ([]byte, error) {
|
|
|
|
hc, ok := raw.(NodeCheckQuery)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("unexpected type %T for NodeCheckQuery index", raw)
|
|
|
|
}
|
|
|
|
|
|
|
|
if hc.Node == "" || hc.CheckID == "" {
|
|
|
|
return nil, errMissingValueForIndex
|
|
|
|
}
|
|
|
|
|
|
|
|
var b indexBuilder
|
|
|
|
b.String(strings.ToLower(hc.Node))
|
|
|
|
b.String(strings.ToLower(hc.CheckID))
|
|
|
|
return b.Bytes(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func indexFromHealthCheck(raw interface{}) ([]byte, error) {
|
|
|
|
hc, ok := raw.(*structs.HealthCheck)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("unexpected type %T for structs.HealthCheck index", raw)
|
|
|
|
}
|
|
|
|
|
|
|
|
if hc.Node == "" || hc.CheckID == "" {
|
|
|
|
return nil, errMissingValueForIndex
|
|
|
|
}
|
|
|
|
|
|
|
|
var b indexBuilder
|
|
|
|
b.String(strings.ToLower(hc.Node))
|
|
|
|
b.String(strings.ToLower(string(hc.CheckID)))
|
|
|
|
return b.Bytes(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func indexNodeServiceFromHealthCheck(raw interface{}) ([]byte, error) {
|
|
|
|
hc, ok := raw.(*structs.HealthCheck)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("unexpected type %T for structs.HealthCheck index", raw)
|
|
|
|
}
|
|
|
|
|
|
|
|
if hc.Node == "" {
|
|
|
|
return nil, errMissingValueForIndex
|
|
|
|
}
|
|
|
|
|
|
|
|
var b indexBuilder
|
|
|
|
b.String(strings.ToLower(hc.Node))
|
|
|
|
b.String(strings.ToLower(hc.ServiceID))
|
|
|
|
return b.Bytes(), nil
|
|
|
|
}
|
|
|
|
|
2021-01-29 01:48:51 +00:00
|
|
|
// gatewayServicesTableSchema returns a new table schema used to store information
|
2020-12-16 22:12:42 +00:00
|
|
|
// about services associated with terminating gateways.
|
2021-01-29 01:48:51 +00:00
|
|
|
func gatewayServicesTableSchema() *memdb.TableSchema {
|
2020-12-16 22:12:42 +00:00
|
|
|
return &memdb.TableSchema{
|
2021-01-29 01:48:51 +00:00
|
|
|
Name: tableGatewayServices,
|
2020-12-16 22:12:42 +00:00
|
|
|
Indexes: map[string]*memdb.IndexSchema{
|
2021-01-29 01:30:08 +00:00
|
|
|
indexID: {
|
|
|
|
Name: indexID,
|
2020-12-16 22:12:42 +00:00
|
|
|
AllowMissing: false,
|
|
|
|
Unique: true,
|
|
|
|
Indexer: &memdb.CompoundIndex{
|
|
|
|
Indexes: []memdb.Indexer{
|
|
|
|
&ServiceNameIndex{
|
|
|
|
Field: "Gateway",
|
|
|
|
},
|
|
|
|
&ServiceNameIndex{
|
|
|
|
Field: "Service",
|
|
|
|
},
|
|
|
|
&memdb.IntFieldIndex{
|
|
|
|
Field: "Port",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2021-03-16 17:54:05 +00:00
|
|
|
indexGateway: {
|
|
|
|
Name: indexGateway,
|
2020-12-16 22:12:42 +00:00
|
|
|
AllowMissing: false,
|
|
|
|
Unique: false,
|
|
|
|
Indexer: &ServiceNameIndex{
|
|
|
|
Field: "Gateway",
|
|
|
|
},
|
|
|
|
},
|
2021-03-16 17:54:05 +00:00
|
|
|
indexService: {
|
|
|
|
Name: indexService,
|
2020-12-16 22:12:42 +00:00
|
|
|
AllowMissing: true,
|
|
|
|
Unique: false,
|
|
|
|
Indexer: &ServiceNameIndex{
|
|
|
|
Field: "Service",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-29 01:48:51 +00:00
|
|
|
// meshTopologyTableSchema returns a new table schema used to store information
|
2020-12-16 22:12:42 +00:00
|
|
|
// relating upstream and downstream services
|
2021-01-29 01:48:51 +00:00
|
|
|
func meshTopologyTableSchema() *memdb.TableSchema {
|
2020-12-16 22:12:42 +00:00
|
|
|
return &memdb.TableSchema{
|
2021-01-29 01:48:51 +00:00
|
|
|
Name: tableMeshTopology,
|
2020-12-16 22:12:42 +00:00
|
|
|
Indexes: map[string]*memdb.IndexSchema{
|
2021-01-29 01:30:08 +00:00
|
|
|
indexID: {
|
|
|
|
Name: indexID,
|
2020-12-16 22:12:42 +00:00
|
|
|
AllowMissing: false,
|
|
|
|
Unique: true,
|
|
|
|
Indexer: &memdb.CompoundIndex{
|
|
|
|
Indexes: []memdb.Indexer{
|
|
|
|
&ServiceNameIndex{
|
|
|
|
Field: "Upstream",
|
|
|
|
},
|
|
|
|
&ServiceNameIndex{
|
|
|
|
Field: "Downstream",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2021-03-16 17:15:14 +00:00
|
|
|
indexUpstream: {
|
|
|
|
Name: indexUpstream,
|
2020-12-16 22:12:42 +00:00
|
|
|
AllowMissing: true,
|
|
|
|
Unique: false,
|
|
|
|
Indexer: &ServiceNameIndex{
|
|
|
|
Field: "Upstream",
|
|
|
|
},
|
|
|
|
},
|
2021-03-16 17:15:14 +00:00
|
|
|
indexDownstream: {
|
|
|
|
Name: indexDownstream,
|
2020-12-16 22:12:42 +00:00
|
|
|
AllowMissing: false,
|
|
|
|
Unique: false,
|
|
|
|
Indexer: &ServiceNameIndex{
|
|
|
|
Field: "Downstream",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type ServiceNameIndex struct {
|
|
|
|
Field string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (index *ServiceNameIndex) FromObject(obj interface{}) (bool, []byte, error) {
|
|
|
|
v := reflect.ValueOf(obj)
|
|
|
|
v = reflect.Indirect(v) // Dereference the pointer if any
|
|
|
|
|
|
|
|
fv := v.FieldByName(index.Field)
|
|
|
|
isPtr := fv.Kind() == reflect.Ptr
|
|
|
|
fv = reflect.Indirect(fv)
|
|
|
|
if !isPtr && !fv.IsValid() || !fv.CanInterface() {
|
|
|
|
return false, nil,
|
|
|
|
fmt.Errorf("field '%s' for %#v is invalid %v ", index.Field, obj, isPtr)
|
|
|
|
}
|
|
|
|
|
|
|
|
name, ok := fv.Interface().(structs.ServiceName)
|
|
|
|
if !ok {
|
|
|
|
return false, nil, fmt.Errorf("Field 'ServiceName' is not of type structs.ServiceName")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Enforce lowercase and add null character as terminator
|
|
|
|
id := strings.ToLower(name.String()) + "\x00"
|
|
|
|
|
|
|
|
return true, []byte(id), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (index *ServiceNameIndex) FromArgs(args ...interface{}) ([]byte, error) {
|
|
|
|
if len(args) != 1 {
|
|
|
|
return nil, fmt.Errorf("must provide only a single argument")
|
|
|
|
}
|
|
|
|
name, ok := args[0].(structs.ServiceName)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("argument must be of type structs.ServiceName: %#v", args[0])
|
|
|
|
}
|
|
|
|
|
|
|
|
// Enforce lowercase and add null character as terminator
|
|
|
|
id := strings.ToLower(name.String()) + "\x00"
|
|
|
|
|
|
|
|
return []byte(strings.ToLower(id)), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (index *ServiceNameIndex) PrefixFromArgs(args ...interface{}) ([]byte, error) {
|
|
|
|
val, err := index.FromArgs(args...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Strip the null terminator, the rest is a prefix
|
|
|
|
n := len(val)
|
|
|
|
if n > 0 {
|
|
|
|
return val[:n-1], nil
|
|
|
|
}
|
|
|
|
return val, nil
|
|
|
|
}
|
2021-03-16 17:37:44 +00:00
|
|
|
|
|
|
|
// upstreamDownstream pairs come from individual proxy registrations, which can be updated independently.
|
|
|
|
type upstreamDownstream struct {
|
|
|
|
Upstream structs.ServiceName
|
|
|
|
Downstream structs.ServiceName
|
|
|
|
|
|
|
|
// Refs stores the registrations that contain this pairing.
|
|
|
|
// When there are no remaining Refs, the upstreamDownstream can be deleted.
|
|
|
|
//
|
|
|
|
// Note: This map must be treated as immutable when accessed in MemDB.
|
|
|
|
// The entire upstreamDownstream structure must be deep copied on updates.
|
|
|
|
Refs map[string]struct{}
|
|
|
|
|
|
|
|
structs.RaftIndex
|
|
|
|
}
|
2021-02-12 23:06:56 +00:00
|
|
|
|
2021-03-22 21:29:58 +00:00
|
|
|
// NodeCheckQuery is used to query the ID index of the checks table.
|
|
|
|
type NodeCheckQuery struct {
|
2021-02-12 23:06:56 +00:00
|
|
|
Node string
|
|
|
|
CheckID string
|
|
|
|
structs.EnterpriseMeta
|
|
|
|
}
|
2021-03-29 17:55:13 +00:00
|
|
|
|
|
|
|
// NamespaceOrDefault exists because structs.EnterpriseMeta uses a pointer
|
|
|
|
// receiver for this method. Remove once that is fixed.
|
|
|
|
func (q NodeCheckQuery) NamespaceOrDefault() string {
|
|
|
|
return q.EnterpriseMeta.NamespaceOrDefault()
|
|
|
|
}
|