state: rename config-entries table const to match new pattern

This commit is contained in:
Daniel Nephin 2021-01-28 20:34:34 -05:00
parent 7d17e20270
commit 09425b22a1
6 changed files with 36 additions and 35 deletions

View File

@ -80,7 +80,7 @@ func init() {
// ConfigEntries is used to pull all the config entries for the snapshot. // ConfigEntries is used to pull all the config entries for the snapshot.
func (s *Snapshot) ConfigEntries() ([]structs.ConfigEntry, error) { func (s *Snapshot) ConfigEntries() ([]structs.ConfigEntry, error) {
entries, err := s.tx.Get(configTableName, "id") entries, err := s.tx.Get(tableConfigEntries, "id")
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -107,7 +107,7 @@ func (s *Store) ConfigEntry(ws memdb.WatchSet, kind, name string, entMeta *struc
func configEntryTxn(tx ReadTxn, ws memdb.WatchSet, kind, name string, entMeta *structs.EnterpriseMeta) (uint64, structs.ConfigEntry, error) { func configEntryTxn(tx ReadTxn, ws memdb.WatchSet, kind, name string, entMeta *structs.EnterpriseMeta) (uint64, structs.ConfigEntry, error) {
// Get the index // Get the index
idx := maxIndexTxn(tx, configTableName) idx := maxIndexTxn(tx, tableConfigEntries)
// Get the existing config entry. // Get the existing config entry.
watchCh, existing, err := firstWatchConfigEntryWithTxn(tx, kind, name, entMeta) watchCh, existing, err := firstWatchConfigEntryWithTxn(tx, kind, name, entMeta)
@ -142,7 +142,7 @@ func (s *Store) ConfigEntriesByKind(ws memdb.WatchSet, kind string, entMeta *str
func configEntriesByKindTxn(tx ReadTxn, ws memdb.WatchSet, kind string, entMeta *structs.EnterpriseMeta) (uint64, []structs.ConfigEntry, error) { func configEntriesByKindTxn(tx ReadTxn, ws memdb.WatchSet, kind string, entMeta *structs.EnterpriseMeta) (uint64, []structs.ConfigEntry, error) {
// Get the index and watch for updates // Get the index and watch for updates
idx := maxIndexWatchTxn(tx, ws, configTableName) idx := maxIndexWatchTxn(tx, ws, tableConfigEntries)
// Lookup by kind, or all if kind is empty // Lookup by kind, or all if kind is empty
var iter memdb.ResultIterator var iter memdb.ResultIterator
@ -296,10 +296,10 @@ func deleteConfigEntryTxn(tx WriteTxn, idx uint64, kind, name string, entMeta *s
} }
// Delete the config entry from the DB and update the index. // Delete the config entry from the DB and update the index.
if err := tx.Delete(configTableName, existing); err != nil { if err := tx.Delete(tableConfigEntries, existing); err != nil {
return fmt.Errorf("failed removing config entry: %s", err) return fmt.Errorf("failed removing config entry: %s", err)
} }
if err := tx.Insert("index", &IndexEntry{configTableName, idx}); err != nil { if err := tx.Insert("index", &IndexEntry{tableConfigEntries, idx}); err != nil {
return fmt.Errorf("failed updating index: %s", err) return fmt.Errorf("failed updating index: %s", err)
} }
@ -320,10 +320,10 @@ func insertConfigEntryWithTxn(tx WriteTxn, idx uint64, conf structs.ConfigEntry)
} }
// Insert the config entry and update the index // Insert the config entry and update the index
if err := tx.Insert(configTableName, conf); err != nil { if err := tx.Insert(tableConfigEntries, conf); err != nil {
return fmt.Errorf("failed inserting config entry: %s", err) return fmt.Errorf("failed inserting config entry: %s", err)
} }
if err := indexUpdateMaxTxn(tx, idx, configTableName); err != nil { if err := indexUpdateMaxTxn(tx, idx, tableConfigEntries); err != nil {
return fmt.Errorf("failed updating index: %v", err) return fmt.Errorf("failed updating index: %v", err)
} }
@ -429,7 +429,7 @@ func (s *Store) discoveryChainSourcesTxn(tx ReadTxn, ws memdb.WatchSet, dc strin
queue := []structs.ServiceName{destination} queue := []structs.ServiceName{destination}
for len(queue) > 0 { for len(queue) > 0 {
// The "link" index returns config entries that reference a service // The "link" index returns config entries that reference a service
iter, err := tx.Get(configTableName, "link", queue[0].ToServiceID()) iter, err := tx.Get(tableConfigEntries, "link", queue[0].ToServiceID())
if err != nil { if err != nil {
return 0, nil, err return 0, nil, err
} }
@ -601,7 +601,7 @@ func validateProposedConfigEntryInServiceGraph(
sid := structs.NewServiceID(name, entMeta) sid := structs.NewServiceID(name, entMeta)
checkChains[sid] = struct{}{} checkChains[sid] = struct{}{}
iter, err := tx.Get(configTableName, "link", sid) iter, err := tx.Get(tableConfigEntries, "link", sid)
if err != nil { if err != nil {
return err return err
} }

View File

@ -126,7 +126,7 @@ func (s *ServiceIntentionSourceIndex) FromArgs(args ...interface{}) ([]byte, err
func (s *Store) configIntentionsListTxn(tx ReadTxn, ws memdb.WatchSet, entMeta *structs.EnterpriseMeta) (uint64, structs.Intentions, bool, error) { func (s *Store) configIntentionsListTxn(tx ReadTxn, ws memdb.WatchSet, entMeta *structs.EnterpriseMeta) (uint64, structs.Intentions, bool, error) {
// unrolled part of configEntriesByKindTxn // unrolled part of configEntriesByKindTxn
idx := maxIndexTxn(tx, configTableName) idx := maxIndexTxn(tx, tableConfigEntries)
iter, err := getConfigEntryKindsWithTxn(tx, structs.ServiceIntentions, structs.WildcardEnterpriseMeta()) iter, err := getConfigEntryKindsWithTxn(tx, structs.ServiceIntentions, structs.WildcardEnterpriseMeta())
if err != nil { if err != nil {
@ -145,12 +145,12 @@ func (s *Store) configIntentionsListTxn(tx ReadTxn, ws memdb.WatchSet, entMeta *
} }
func (s *Store) configIntentionGetTxn(tx ReadTxn, ws memdb.WatchSet, id string) (uint64, *structs.ServiceIntentionsConfigEntry, *structs.Intention, error) { func (s *Store) configIntentionGetTxn(tx ReadTxn, ws memdb.WatchSet, id string) (uint64, *structs.ServiceIntentionsConfigEntry, *structs.Intention, error) {
idx := maxIndexTxn(tx, configTableName) idx := maxIndexTxn(tx, tableConfigEntries)
if idx < 1 { if idx < 1 {
idx = 1 idx = 1
} }
watchCh, existing, err := tx.FirstWatch(configTableName, "intention-legacy-id", id) watchCh, existing, err := tx.FirstWatch(tableConfigEntries, "intention-legacy-id", id)
if err != nil { if err != nil {
return 0, nil, nil, fmt.Errorf("failed config entry lookup: %s", err) return 0, nil, nil, fmt.Errorf("failed config entry lookup: %s", err)
} }
@ -239,7 +239,7 @@ func configIntentionMatchOneTxn(
} }
func readSourceIntentionsFromConfigEntriesTxn(tx ReadTxn, ws memdb.WatchSet, serviceName string, entMeta *structs.EnterpriseMeta) (uint64, structs.Intentions, error) { func readSourceIntentionsFromConfigEntriesTxn(tx ReadTxn, ws memdb.WatchSet, serviceName string, entMeta *structs.EnterpriseMeta) (uint64, structs.Intentions, error) {
idx := maxIndexTxn(tx, configTableName) idx := maxIndexTxn(tx, tableConfigEntries)
var ( var (
results structs.Intentions results structs.Intentions
@ -265,7 +265,7 @@ func readSourceIntentionsFromConfigEntriesTxn(tx ReadTxn, ws memdb.WatchSet, ser
func readSourceIntentionsFromConfigEntriesForServiceTxn(tx ReadTxn, ws memdb.WatchSet, serviceName string, entMeta *structs.EnterpriseMeta, results structs.Intentions) (structs.Intentions, error) { func readSourceIntentionsFromConfigEntriesForServiceTxn(tx ReadTxn, ws memdb.WatchSet, serviceName string, entMeta *structs.EnterpriseMeta, results structs.Intentions) (structs.Intentions, error) {
sn := structs.NewServiceName(serviceName, entMeta) sn := structs.NewServiceName(serviceName, entMeta)
iter, err := tx.Get(configTableName, "intention-source", sn) iter, err := tx.Get(tableConfigEntries, "intention-source", sn)
if err != nil { if err != nil {
return nil, fmt.Errorf("failed config entry lookup: %s", err) return nil, fmt.Errorf("failed config entry lookup: %s", err)
} }
@ -284,7 +284,7 @@ func readSourceIntentionsFromConfigEntriesForServiceTxn(tx ReadTxn, ws memdb.Wat
} }
func readDestinationIntentionsFromConfigEntriesTxn(tx ReadTxn, ws memdb.WatchSet, serviceName string, entMeta *structs.EnterpriseMeta) (uint64, structs.Intentions, error) { func readDestinationIntentionsFromConfigEntriesTxn(tx ReadTxn, ws memdb.WatchSet, serviceName string, entMeta *structs.EnterpriseMeta) (uint64, structs.Intentions, error) {
idx := maxIndexTxn(tx, configTableName) idx := maxIndexTxn(tx, tableConfigEntries)
var results structs.Intentions var results structs.Intentions

View File

@ -9,7 +9,7 @@ import (
) )
func firstConfigEntryWithTxn(tx ReadTxn, kind, name string, _ *structs.EnterpriseMeta) (interface{}, error) { func firstConfigEntryWithTxn(tx ReadTxn, kind, name string, _ *structs.EnterpriseMeta) (interface{}, error) {
return tx.First(configTableName, "id", kind, name) return tx.First(tableConfigEntries, "id", kind, name)
} }
func firstWatchConfigEntryWithTxn( func firstWatchConfigEntryWithTxn(
@ -18,7 +18,7 @@ func firstWatchConfigEntryWithTxn(
name string, name string,
_ *structs.EnterpriseMeta, _ *structs.EnterpriseMeta,
) (<-chan struct{}, interface{}, error) { ) (<-chan struct{}, interface{}, error) {
return tx.FirstWatch(configTableName, "id", kind, name) return tx.FirstWatch(tableConfigEntries, "id", kind, name)
} }
func validateConfigEntryEnterprise(_ ReadTxn, _ structs.ConfigEntry) error { func validateConfigEntryEnterprise(_ ReadTxn, _ structs.ConfigEntry) error {
@ -26,11 +26,11 @@ func validateConfigEntryEnterprise(_ ReadTxn, _ structs.ConfigEntry) error {
} }
func getAllConfigEntriesWithTxn(tx ReadTxn, _ *structs.EnterpriseMeta) (memdb.ResultIterator, error) { func getAllConfigEntriesWithTxn(tx ReadTxn, _ *structs.EnterpriseMeta) (memdb.ResultIterator, error) {
return tx.Get(configTableName, "id") return tx.Get(tableConfigEntries, "id")
} }
func getConfigEntryKindsWithTxn(tx ReadTxn, kind string, _ *structs.EnterpriseMeta) (memdb.ResultIterator, error) { func getConfigEntryKindsWithTxn(tx ReadTxn, kind string, _ *structs.EnterpriseMeta) (memdb.ResultIterator, error) {
return tx.Get(configTableName, "kind", kind) return tx.Get(tableConfigEntries, "kind", kind)
} }
func configIntentionsConvertToList(iter memdb.ResultIterator, _ *structs.EnterpriseMeta) structs.Intentions { func configIntentionsConvertToList(iter memdb.ResultIterator, _ *structs.EnterpriseMeta) structs.Intentions {

View File

@ -3,7 +3,7 @@ package state
import "github.com/hashicorp/go-memdb" import "github.com/hashicorp/go-memdb"
const ( const (
configTableName = "config-entries" tableConfigEntries = "config-entries"
indexLink = "link" indexLink = "link"
indexIntentionLegacyID = "intention-legacy-id" indexIntentionLegacyID = "intention-legacy-id"
@ -14,7 +14,7 @@ const (
// config entries. // config entries.
func configTableSchema() *memdb.TableSchema { func configTableSchema() *memdb.TableSchema {
return &memdb.TableSchema{ return &memdb.TableSchema{
Name: configTableName, Name: tableConfigEntries,
Indexes: map[string]*memdb.IndexSchema{ Indexes: map[string]*memdb.IndexSchema{
indexID: { indexID: {
Name: indexID, Name: indexID,

View File

@ -718,7 +718,7 @@ func (s *Store) LegacyIntentionDeleteAll(idx uint64) error {
// Also bump the index for the config entry table so that // Also bump the index for the config entry table so that
// secondaries can correctly know when they've replicated all of the service-intentions // secondaries can correctly know when they've replicated all of the service-intentions
// config entries that USED to exist in the old intentions table. // config entries that USED to exist in the old intentions table.
if err := tx.Insert("index", &IndexEntry{configTableName, idx}); err != nil { if err := tx.Insert("index", &IndexEntry{tableConfigEntries, idx}); err != nil {
return fmt.Errorf("failed updating index: %s", err) return fmt.Errorf("failed updating index: %s", err)
} }

View File

@ -4,13 +4,14 @@ import (
"testing" "testing"
"time" "time"
"github.com/hashicorp/go-memdb"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/hashicorp/consul/acl" "github.com/hashicorp/consul/acl"
"github.com/hashicorp/consul/agent/connect" "github.com/hashicorp/consul/agent/connect"
"github.com/hashicorp/consul/agent/structs" "github.com/hashicorp/consul/agent/structs"
"github.com/hashicorp/consul/sdk/testutil" "github.com/hashicorp/consul/sdk/testutil"
"github.com/hashicorp/go-memdb"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
) )
var ( var (
@ -90,7 +91,7 @@ func TestStore_IntentionSetGet_basic(t *testing.T) {
// Make sure the right index got updated. // Make sure the right index got updated.
require.Equal(t, lastIndex, s.maxIndex(intentionsTableName)) require.Equal(t, lastIndex, s.maxIndex(intentionsTableName))
require.Equal(t, uint64(0), s.maxIndex(configTableName)) require.Equal(t, uint64(0), s.maxIndex(tableConfigEntries))
expected = &structs.Intention{ expected = &structs.Intention{
ID: legacyIxn.ID, ID: legacyIxn.ID,
@ -131,7 +132,7 @@ func TestStore_IntentionSetGet_basic(t *testing.T) {
require.NoError(t, s.EnsureConfigEntry(lastIndex, configEntry.Clone(), nil)) require.NoError(t, s.EnsureConfigEntry(lastIndex, configEntry.Clone(), nil))
// Make sure the config entry index got updated instead of the old intentions one // Make sure the config entry index got updated instead of the old intentions one
require.Equal(t, lastIndex, s.maxIndex(configTableName)) require.Equal(t, lastIndex, s.maxIndex(tableConfigEntries))
require.Equal(t, uint64(0), s.maxIndex(intentionsTableName)) require.Equal(t, uint64(0), s.maxIndex(intentionsTableName))
expected = &structs.Intention{ expected = &structs.Intention{
@ -178,7 +179,7 @@ func TestStore_IntentionSetGet_basic(t *testing.T) {
// Make sure the index got updated. // Make sure the index got updated.
require.Equal(t, lastIndex, s.maxIndex(intentionsTableName)) require.Equal(t, lastIndex, s.maxIndex(intentionsTableName))
require.Equal(t, uint64(0), s.maxIndex(configTableName)) require.Equal(t, uint64(0), s.maxIndex(tableConfigEntries))
expected.SourceNS = legacyIxn.SourceNS expected.SourceNS = legacyIxn.SourceNS
expected.Action = structs.IntentionActionDeny expected.Action = structs.IntentionActionDeny
@ -201,7 +202,7 @@ func TestStore_IntentionSetGet_basic(t *testing.T) {
require.NoError(t, s.EnsureConfigEntry(lastIndex, configEntry.Clone(), nil)) require.NoError(t, s.EnsureConfigEntry(lastIndex, configEntry.Clone(), nil))
// Make sure the config entry index got updated instead of the old intentions one // Make sure the config entry index got updated instead of the old intentions one
require.Equal(t, lastIndex, s.maxIndex(configTableName)) require.Equal(t, lastIndex, s.maxIndex(tableConfigEntries))
require.Equal(t, uint64(0), s.maxIndex(intentionsTableName)) require.Equal(t, uint64(0), s.maxIndex(intentionsTableName))
expected.Description = configEntry.Sources[0].Description expected.Description = configEntry.Sources[0].Description
@ -240,7 +241,7 @@ func TestStore_IntentionSetGet_basic(t *testing.T) {
// Make sure the index did NOT get updated. // Make sure the index did NOT get updated.
require.Equal(t, lastIndex-1, s.maxIndex(intentionsTableName)) require.Equal(t, lastIndex-1, s.maxIndex(intentionsTableName))
require.Equal(t, uint64(0), s.maxIndex(configTableName)) require.Equal(t, uint64(0), s.maxIndex(tableConfigEntries))
require.False(t, watchFired(ws), "watch not fired") require.False(t, watchFired(ws), "watch not fired")
} }
}) })
@ -815,7 +816,7 @@ func TestStore_LegacyIntentionSet_emptyId(t *testing.T) {
// Index is not updated if nothing is saved. // Index is not updated if nothing is saved.
require.Equal(t, s.maxIndex(intentionsTableName), uint64(0)) require.Equal(t, s.maxIndex(intentionsTableName), uint64(0))
require.Equal(t, uint64(0), s.maxIndex(configTableName)) require.Equal(t, uint64(0), s.maxIndex(tableConfigEntries))
require.False(t, watchFired(ws), "watch fired") require.False(t, watchFired(ws), "watch fired")
} }
@ -1005,7 +1006,7 @@ func TestStore_IntentionDelete(t *testing.T) {
// Make sure the index got updated. // Make sure the index got updated.
require.Equal(t, s.maxIndex(intentionsTableName), lastIndex) require.Equal(t, s.maxIndex(intentionsTableName), lastIndex)
require.Equal(t, uint64(0), s.maxIndex(configTableName)) require.Equal(t, uint64(0), s.maxIndex(tableConfigEntries))
} else { } else {
conf := &structs.ServiceIntentionsConfigEntry{ conf := &structs.ServiceIntentionsConfigEntry{
Kind: structs.ServiceIntentions, Kind: structs.ServiceIntentions,
@ -1027,7 +1028,7 @@ func TestStore_IntentionDelete(t *testing.T) {
require.NoError(t, s.EnsureConfigEntry(1, conf.Clone(), nil)) require.NoError(t, s.EnsureConfigEntry(1, conf.Clone(), nil))
// Make sure the index got updated. // Make sure the index got updated.
require.Equal(t, s.maxIndex(configTableName), lastIndex) require.Equal(t, s.maxIndex(tableConfigEntries), lastIndex)
require.Equal(t, uint64(0), s.maxIndex(intentionsTableName)) require.Equal(t, uint64(0), s.maxIndex(intentionsTableName))
} }
require.True(t, watchFired(ws), "watch fired") require.True(t, watchFired(ws), "watch fired")
@ -1045,13 +1046,13 @@ func TestStore_IntentionDelete(t *testing.T) {
// Make sure the index got updated. // Make sure the index got updated.
require.Equal(t, s.maxIndex(intentionsTableName), lastIndex) require.Equal(t, s.maxIndex(intentionsTableName), lastIndex)
require.Equal(t, uint64(0), s.maxIndex(configTableName)) require.Equal(t, uint64(0), s.maxIndex(tableConfigEntries))
} else { } else {
lastIndex++ lastIndex++
require.NoError(t, s.DeleteConfigEntry(lastIndex, structs.ServiceIntentions, "web", nil)) require.NoError(t, s.DeleteConfigEntry(lastIndex, structs.ServiceIntentions, "web", nil))
// Make sure the index got updated. // Make sure the index got updated.
require.Equal(t, s.maxIndex(configTableName), lastIndex) require.Equal(t, s.maxIndex(tableConfigEntries), lastIndex)
require.Equal(t, uint64(0), s.maxIndex(intentionsTableName)) require.Equal(t, uint64(0), s.maxIndex(intentionsTableName))
} }
require.True(t, watchFired(ws), "watch fired") require.True(t, watchFired(ws), "watch fired")