2023-03-28 18:39:22 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
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
|
|
|
"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
|
|
|
|
2023-03-13 21:19:11 +00:00
|
|
|
indexLink = "link"
|
|
|
|
indexIntentionLegacyID = "intention-legacy-id"
|
|
|
|
indexSource = "intention-source"
|
2023-03-31 18:35:56 +00:00
|
|
|
indexSamenessGroupDefault = "sameness-group-default-for-failover"
|
2021-01-29 01:34:15 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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,
|
2022-06-23 15:07:19 +00:00
|
|
|
Indexer: indexerSingleWithPrefix[any, structs.ConfigEntry, any]{
|
2021-03-29 17:17:26 +00:00
|
|
|
readIndex: indexFromConfigEntryKindName,
|
|
|
|
writeIndex: indexFromConfigEntry,
|
|
|
|
prefixIndex: indexFromConfigEntryKindName,
|
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{},
|
|
|
|
},
|
2023-03-13 21:19:11 +00:00
|
|
|
indexSamenessGroupDefault: {
|
|
|
|
Name: indexSamenessGroupDefault,
|
|
|
|
AllowMissing: true,
|
|
|
|
Unique: true,
|
|
|
|
Indexer: &SamenessGroupDefaultIndex{},
|
|
|
|
},
|
2021-01-29 01:34:15 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2021-03-29 17:17:26 +00:00
|
|
|
|
2022-06-23 15:07:19 +00:00
|
|
|
// configEntryIndexable is required because while structs.ConfigEntry
|
|
|
|
// has a GetEnterpriseMeta method, it does not directly expose the
|
|
|
|
// required NamespaceOrDefault and PartitionOrDefault methods of
|
|
|
|
// enterpriseIndexable.
|
|
|
|
//
|
|
|
|
// Config entries that embed *acl.EnterpriseMeta will automatically
|
|
|
|
// implement this interface.
|
|
|
|
type configEntryIndexable interface {
|
|
|
|
structs.ConfigEntry
|
|
|
|
enterpriseIndexable
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ configEntryIndexable = (*structs.ExportedServicesConfigEntry)(nil)
|
2023-03-13 21:19:11 +00:00
|
|
|
var _ configEntryIndexable = (*structs.SamenessGroupConfigEntry)(nil)
|
2022-06-23 15:07:19 +00:00
|
|
|
var _ configEntryIndexable = (*structs.IngressGatewayConfigEntry)(nil)
|
|
|
|
var _ configEntryIndexable = (*structs.MeshConfigEntry)(nil)
|
|
|
|
var _ configEntryIndexable = (*structs.ProxyConfigEntry)(nil)
|
|
|
|
var _ configEntryIndexable = (*structs.ServiceConfigEntry)(nil)
|
|
|
|
var _ configEntryIndexable = (*structs.ServiceIntentionsConfigEntry)(nil)
|
|
|
|
var _ configEntryIndexable = (*structs.ServiceResolverConfigEntry)(nil)
|
|
|
|
var _ configEntryIndexable = (*structs.ServiceRouterConfigEntry)(nil)
|
|
|
|
var _ configEntryIndexable = (*structs.ServiceSplitterConfigEntry)(nil)
|
|
|
|
var _ configEntryIndexable = (*structs.TerminatingGatewayConfigEntry)(nil)
|
2021-03-29 17:17:26 +00:00
|
|
|
|
2022-06-23 15:07:19 +00:00
|
|
|
func indexFromConfigEntry(c structs.ConfigEntry) ([]byte, error) {
|
2021-03-29 17:17:26 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// indexKindFromConfigEntry indexes kinds without a namespace for any config
|
|
|
|
// entries that span all namespaces.
|
2022-06-23 15:07:19 +00:00
|
|
|
func indexKindFromConfigEntry(c configEntryIndexable) ([]byte, error) {
|
2021-03-29 17:17:26 +00:00
|
|
|
if c.GetKind() == "" {
|
|
|
|
return nil, errMissingValueForIndex
|
|
|
|
}
|
|
|
|
|
|
|
|
var b indexBuilder
|
|
|
|
b.String(strings.ToLower(c.GetKind()))
|
|
|
|
return b.Bytes(), nil
|
|
|
|
}
|