From d9dacb83888b0f8e0fdc96d5b226e707d6dc0bc9 Mon Sep 17 00:00:00 2001 From: Daniel Nephin Date: Mon, 29 Mar 2021 13:17:26 -0400 Subject: [PATCH] state: share more indexer functions for config_entries --- agent/consul/state/config_entry_oss.go | 43 ----------------- agent/consul/state/config_entry_schema.go | 59 +++++++++++++++++++++-- 2 files changed, 54 insertions(+), 48 deletions(-) diff --git a/agent/consul/state/config_entry_oss.go b/agent/consul/state/config_entry_oss.go index 23317572e..5fb2e6afc 100644 --- a/agent/consul/state/config_entry_oss.go +++ b/agent/consul/state/config_entry_oss.go @@ -23,49 +23,6 @@ func indexFromConfigEntryKindName(arg interface{}) ([]byte, error) { return b.Bytes(), nil } -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 -} - -// indexKindFromConfigEntry indexes kinds, it is a shim for enterprise. -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 -} - -func indexFromConfigEntryKindQuery(raw interface{}) ([]byte, error) { - q, ok := raw.(ConfigEntryKindQuery) - if !ok { - return nil, fmt.Errorf("type must be structs.ConfigEntry: %T", raw) - } - - var b indexBuilder - b.String(strings.ToLower(q.Kind)) - return b.Bytes(), nil -} - func validateConfigEntryEnterprise(_ ReadTxn, _ structs.ConfigEntry) error { return nil } diff --git a/agent/consul/state/config_entry_schema.go b/agent/consul/state/config_entry_schema.go index 898b9910c..5e32c4945 100644 --- a/agent/consul/state/config_entry_schema.go +++ b/agent/consul/state/config_entry_schema.go @@ -1,7 +1,12 @@ package state import ( + "fmt" + "strings" + "github.com/hashicorp/go-memdb" + + "github.com/hashicorp/consul/agent/structs" ) const ( @@ -23,9 +28,9 @@ func configTableSchema() *memdb.TableSchema { AllowMissing: false, Unique: true, Indexer: indexerSingleWithPrefix{ - readIndex: readIndex(indexFromConfigEntryKindName), - writeIndex: writeIndex(indexFromConfigEntry), - prefixIndex: prefixIndex(indexFromConfigEntryKindName), + readIndex: indexFromConfigEntryKindName, + writeIndex: indexFromConfigEntry, + prefixIndex: indexFromConfigEntryKindName, }, }, indexKind: { @@ -33,8 +38,8 @@ func configTableSchema() *memdb.TableSchema { AllowMissing: false, Unique: false, Indexer: indexerSingle{ - readIndex: readIndex(indexFromConfigEntryKindQuery), - writeIndex: writeIndex(indexKindFromConfigEntry), + readIndex: indexFromConfigEntryKindQuery, + writeIndex: indexKindFromConfigEntry, }, }, indexLink: { @@ -58,3 +63,47 @@ func configTableSchema() *memdb.TableSchema { }, } } + +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 +}