2021-01-29 01:34:15 +00:00
|
|
|
package state
|
|
|
|
|
2021-02-09 17:37:57 +00:00
|
|
|
import (
|
2021-03-29 17:17:26 +00:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
2021-02-09 17:37:57 +00:00
|
|
|
"github.com/hashicorp/go-memdb"
|
2021-03-29 17:17:26 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
2021-02-09 17:37:57 +00:00
|
|
|
)
|
2021-01-29 01:34:15 +00:00
|
|
|
|
|
|
|
const (
|
2021-01-29 01:34:34 +00:00
|
|
|
tableConfigEntries = "config-entries"
|
2021-01-29 01:34:15 +00:00
|
|
|
|
|
|
|
indexLink = "link"
|
|
|
|
indexIntentionLegacyID = "intention-legacy-id"
|
|
|
|
indexSource = "intention-source"
|
|
|
|
)
|
|
|
|
|
|
|
|
// configTableSchema returns a new table schema used to store global
|
|
|
|
// config entries.
|
|
|
|
func configTableSchema() *memdb.TableSchema {
|
|
|
|
return &memdb.TableSchema{
|
2021-01-29 01:34:34 +00:00
|
|
|
Name: tableConfigEntries,
|
2021-01-29 01:34:15 +00:00
|
|
|
Indexes: map[string]*memdb.IndexSchema{
|
|
|
|
indexID: {
|
|
|
|
Name: indexID,
|
|
|
|
AllowMissing: false,
|
|
|
|
Unique: true,
|
2021-03-10 18:37:17 +00:00
|
|
|
Indexer: indexerSingleWithPrefix{
|
2021-03-29 17:17:26 +00:00
|
|
|
readIndex: indexFromConfigEntryKindName,
|
|
|
|
writeIndex: indexFromConfigEntry,
|
|
|
|
prefixIndex: indexFromConfigEntryKindName,
|
2021-01-29 01:34:15 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
indexKind: {
|
|
|
|
Name: indexKind,
|
|
|
|
AllowMissing: false,
|
|
|
|
Unique: false,
|
2021-03-10 19:05:43 +00:00
|
|
|
Indexer: indexerSingle{
|
2021-03-29 17:17:26 +00:00
|
|
|
readIndex: indexFromConfigEntryKindQuery,
|
|
|
|
writeIndex: indexKindFromConfigEntry,
|
2021-01-29 01:34:15 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
indexLink: {
|
|
|
|
Name: indexLink,
|
|
|
|
AllowMissing: true,
|
|
|
|
Unique: false,
|
|
|
|
Indexer: &ConfigEntryLinkIndex{},
|
|
|
|
},
|
|
|
|
indexIntentionLegacyID: {
|
|
|
|
Name: indexIntentionLegacyID,
|
|
|
|
AllowMissing: true,
|
|
|
|
Unique: true,
|
|
|
|
Indexer: &ServiceIntentionLegacyIDIndex{},
|
|
|
|
},
|
|
|
|
indexSource: {
|
|
|
|
Name: indexSource,
|
|
|
|
AllowMissing: true,
|
|
|
|
Unique: false,
|
|
|
|
Indexer: &ServiceIntentionSourceIndex{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2021-03-29 17:17:26 +00:00
|
|
|
|
|
|
|
func indexFromConfigEntry(raw interface{}) ([]byte, error) {
|
|
|
|
c, ok := raw.(structs.ConfigEntry)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("type must be structs.ConfigEntry: %T", raw)
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.GetName() == "" || c.GetKind() == "" {
|
|
|
|
return nil, errMissingValueForIndex
|
|
|
|
}
|
|
|
|
|
|
|
|
var b indexBuilder
|
|
|
|
b.String(strings.ToLower(c.GetKind()))
|
|
|
|
b.String(strings.ToLower(c.GetName()))
|
|
|
|
return b.Bytes(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func indexFromConfigEntryKindQuery(raw interface{}) ([]byte, error) {
|
|
|
|
q, ok := raw.(ConfigEntryKindQuery)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("type must be ConfigEntryKindQuery: %T", raw)
|
|
|
|
}
|
|
|
|
|
|
|
|
var b indexBuilder
|
|
|
|
b.String(strings.ToLower(q.Kind))
|
|
|
|
return b.Bytes(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// indexKindFromConfigEntry indexes kinds without a namespace for any config
|
|
|
|
// entries that span all namespaces.
|
|
|
|
func indexKindFromConfigEntry(raw interface{}) ([]byte, error) {
|
|
|
|
c, ok := raw.(structs.ConfigEntry)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("type must be structs.ConfigEntry: %T", raw)
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.GetKind() == "" {
|
|
|
|
return nil, errMissingValueForIndex
|
|
|
|
}
|
|
|
|
|
|
|
|
var b indexBuilder
|
|
|
|
b.String(strings.ToLower(c.GetKind()))
|
|
|
|
return b.Bytes(), nil
|
|
|
|
}
|