From c749c6c92760f2dc3d4b07c4aff525b313be3a68 Mon Sep 17 00:00:00 2001 From: Daniel Nephin Date: Tue, 16 Mar 2021 13:15:14 -0400 Subject: [PATCH] state: use constants for mesh-topology table operations --- agent/consul/state/catalog.go | 14 +++++++------- agent/consul/state/catalog_schema.go | 10 ++++++---- agent/consul/state/config_entry.go | 2 +- 3 files changed, 14 insertions(+), 12 deletions(-) diff --git a/agent/consul/state/catalog.go b/agent/consul/state/catalog.go index e04f7ebc9c..df5ac87467 100644 --- a/agent/consul/state/catalog.go +++ b/agent/consul/state/catalog.go @@ -2992,9 +2992,9 @@ func downstreamsFromRegistrationTxn(tx ReadTxn, ws memdb.WatchSet, sn structs.Se func linkedFromRegistrationTxn(tx ReadTxn, ws memdb.WatchSet, service structs.ServiceName, downstreams bool) (uint64, []structs.ServiceName, error) { // To fetch upstreams we query services that have the input listed as a downstream // To fetch downstreams we query services that have the input listed as an upstream - index := "downstream" + index := indexDownstream if downstreams { - index = "upstream" + index = indexUpstream } iter, err := tx.Get(tableMeshTopology, index, service) @@ -3053,7 +3053,7 @@ func updateMeshTopology(tx WriteTxn, idx uint64, node string, svc *structs.NodeS upstreamMeta := structs.NewEnterpriseMeta(u.DestinationNamespace) upstream := structs.NewServiceName(u.DestinationName, &upstreamMeta) - obj, err := tx.First(tableMeshTopology, "id", upstream, downstream) + obj, err := tx.First(tableMeshTopology, indexID, upstream, downstream) if err != nil { return fmt.Errorf("%q lookup failed: %v", tableMeshTopology, err) } @@ -3097,7 +3097,7 @@ func updateMeshTopology(tx WriteTxn, idx uint64, node string, svc *structs.NodeS for u := range oldUpstreams { if !inserted[u] { - if _, err := tx.DeleteAll(tableMeshTopology, "id", u, downstream); err != nil { + if _, err := tx.DeleteAll(tableMeshTopology, indexID, u, downstream); err != nil { return fmt.Errorf("failed to truncate %s table: %v", tableMeshTopology, err) } if err := indexUpdateMaxTxn(tx, idx, tableMeshTopology); err != nil { @@ -3119,7 +3119,7 @@ func cleanupMeshTopology(tx WriteTxn, idx uint64, service *structs.ServiceNode) sid := service.CompoundServiceID() uid := structs.UniqueID(service.Node, sid.String()) - iter, err := tx.Get(tableMeshTopology, "downstream", sn) + iter, err := tx.Get(tableMeshTopology, indexDownstream, sn) if err != nil { return fmt.Errorf("%q lookup failed: %v", tableMeshTopology, err) } @@ -3190,7 +3190,7 @@ func deleteGatewayServiceTopologyMapping(tx WriteTxn, idx uint64, gs *structs.Ga return nil } - if _, err := tx.DeleteAll(tableMeshTopology, "id", gs.Service, gs.Gateway); err != nil { + if _, err := tx.DeleteAll(tableMeshTopology, indexID, gs.Service, gs.Gateway); err != nil { return fmt.Errorf("failed to truncate %s table: %v", tableMeshTopology, err) } if err := indexUpdateMaxTxn(tx, idx, tableMeshTopology); err != nil { @@ -3206,7 +3206,7 @@ func truncateGatewayServiceTopologyMappings(tx WriteTxn, idx uint64, gateway str return nil } - if _, err := tx.DeleteAll(tableMeshTopology, "downstream", gateway); err != nil { + if _, err := tx.DeleteAll(tableMeshTopology, indexDownstream, gateway); err != nil { return fmt.Errorf("failed to truncate %s table: %v", tableMeshTopology, err) } if err := indexUpdateMaxTxn(tx, idx, tableMeshTopology); err != nil { diff --git a/agent/consul/state/catalog_schema.go b/agent/consul/state/catalog_schema.go index 8769d1e75b..d0fdd0e798 100644 --- a/agent/consul/state/catalog_schema.go +++ b/agent/consul/state/catalog_schema.go @@ -24,6 +24,8 @@ const ( indexStatus = "status" indexNodeService = "node_service" indexNode = "node" + indexUpstream = "upstream" + indexDownstream = "downstream" ) // nodesTableSchema returns a new table schema used for storing struct.Node. @@ -245,16 +247,16 @@ func meshTopologyTableSchema() *memdb.TableSchema { }, }, }, - "upstream": { - Name: "upstream", + indexUpstream: { + Name: indexUpstream, AllowMissing: true, Unique: false, Indexer: &ServiceNameIndex{ Field: "Upstream", }, }, - "downstream": { - Name: "downstream", + indexDownstream: { + Name: indexDownstream, AllowMissing: false, Unique: false, Indexer: &ServiceNameIndex{ diff --git a/agent/consul/state/config_entry.go b/agent/consul/state/config_entry.go index 41fe9f7520..2fabc2af5e 100644 --- a/agent/consul/state/config_entry.go +++ b/agent/consul/state/config_entry.go @@ -278,7 +278,7 @@ func deleteConfigEntryTxn(tx WriteTxn, idx uint64, kind, name string, entMeta *s } // Also clean up associations in the mesh topology table for ingress gateways if kind == structs.IngressGateway { - if _, err := tx.DeleteAll(tableMeshTopology, "downstream", sn); err != nil { + if _, err := tx.DeleteAll(tableMeshTopology, indexDownstream, sn); err != nil { return fmt.Errorf("failed to truncate %s table: %v", tableMeshTopology, err) } if err := indexUpdateMaxTxn(tx, idx, tableMeshTopology); err != nil {