diff --git a/agent/consul/state/catalog.go b/agent/consul/state/catalog.go index aa8034506..d8d1b5a21 100644 --- a/agent/consul/state/catalog.go +++ b/agent/consul/state/catalog.go @@ -194,7 +194,7 @@ func ensureNoNodeWithSimilarNameTxn(tx ReadTxn, node *structs.Node, allowClashWi if strings.EqualFold(node.Node, enode.Node) && node.ID != enode.ID { // Look up the existing node's Serf health check to see if it's failed. // If it is, the node can be renamed. - enodeCheck, err := tx.First(tableChecks, indexID, NodeCheckID{EnterpriseMeta: *structs.DefaultEnterpriseMeta(), Node: enode.Node, CheckID: string(structs.SerfCheckID)}) + enodeCheck, err := tx.First(tableChecks, indexID, NodeCheckQuery{EnterpriseMeta: *structs.DefaultEnterpriseMeta(), Node: enode.Node, CheckID: string(structs.SerfCheckID)}) if err != nil { return fmt.Errorf("Cannot get status of node %s: %s", enode.Node, err) } @@ -1470,7 +1470,7 @@ func (s *Store) ensureCheckCASTxn(tx WriteTxn, idx uint64, hc *structs.HealthChe // checks with no matching node or service. func (s *Store) ensureCheckTxn(tx WriteTxn, idx uint64, preserveIndexes bool, hc *structs.HealthCheck) error { // Check if we have an existing health check - existing, err := tx.First(tableChecks, indexID, NodeCheckID{EnterpriseMeta: hc.EnterpriseMeta, Node: hc.Node, CheckID: string(hc.CheckID)}) + existing, err := tx.First(tableChecks, indexID, NodeCheckQuery{EnterpriseMeta: hc.EnterpriseMeta, Node: hc.Node, CheckID: string(hc.CheckID)}) if err != nil { return fmt.Errorf("failed health check lookup: %s", err) } @@ -1583,7 +1583,7 @@ func getNodeCheckTxn(tx ReadTxn, nodeName string, checkID types.CheckID, entMeta } // Return the check. - check, err := tx.First(tableChecks, indexID, NodeCheckID{EnterpriseMeta: *entMeta, Node: nodeName, CheckID: string(checkID)}) + check, err := tx.First(tableChecks, indexID, NodeCheckQuery{EnterpriseMeta: *entMeta, Node: nodeName, CheckID: string(checkID)}) if err != nil { return 0, nil, fmt.Errorf("failed check lookup: %s", err) } @@ -1809,7 +1809,7 @@ func (s *Store) deleteCheckTxn(tx WriteTxn, idx uint64, node string, checkID typ } // Try to retrieve the existing health check. - hc, err := tx.First(tableChecks, indexID, NodeCheckID{EnterpriseMeta: *entMeta, Node: node, CheckID: string(checkID)}) + hc, err := tx.First(tableChecks, indexID, NodeCheckQuery{EnterpriseMeta: *entMeta, Node: node, CheckID: string(checkID)}) if err != nil { return fmt.Errorf("check lookup failed: %s", err) } diff --git a/agent/consul/state/catalog_oss.go b/agent/consul/state/catalog_oss.go index 8a187066c..ab2d87bf3 100644 --- a/agent/consul/state/catalog_oss.go +++ b/agent/consul/state/catalog_oss.go @@ -120,9 +120,9 @@ func indexFromHealthCheck(raw interface{}) ([]byte, error) { } func indexFromNodeCheckID(raw interface{}) ([]byte, error) { - hc, ok := raw.(NodeCheckID) + hc, ok := raw.(NodeCheckQuery) if !ok { - return nil, fmt.Errorf("unexpected type %T for NodeCheckID index", raw) + return nil, fmt.Errorf("unexpected type %T for NodeCheckQuery index", raw) } if hc.Node == "" || hc.CheckID == "" { diff --git a/agent/consul/state/catalog_oss_test.go b/agent/consul/state/catalog_oss_test.go index 39d61ac10..925c130c4 100644 --- a/agent/consul/state/catalog_oss_test.go +++ b/agent/consul/state/catalog_oss_test.go @@ -2,10 +2,40 @@ package state -import "github.com/hashicorp/consul/agent/structs" +import ( + "github.com/hashicorp/consul/agent/structs" +) func testIndexerTableChecks() map[string]indexerTestCase { + obj := &structs.HealthCheck{ + Node: "NoDe", + ServiceID: "SeRvIcE", + CheckID: "CheckID", + } return map[string]indexerTestCase{ + indexID: { + read: indexValue{ + source: NodeCheckQuery{ + Node: "NoDe", + CheckID: "CheckId", + }, + expected: []byte("node\x00checkid\x00"), + }, + write: indexValue{ + source: obj, + expected: []byte("node\x00checkid\x00"), + }, + prefix: []indexValue{ + { + source: structs.EnterpriseMeta{}, + expected: nil, + }, + { + source: Query{Value: "nOdE"}, + expected: []byte("node\x00"), + }, + }, + }, indexNodeService: { read: indexValue{ source: NodeServiceQuery{ @@ -15,10 +45,7 @@ func testIndexerTableChecks() map[string]indexerTestCase { expected: []byte("node\x00service\x00"), }, write: indexValue{ - source: &structs.HealthCheck{ - Node: "NoDe", - ServiceID: "SeRvIcE", - }, + source: obj, expected: []byte("node\x00service\x00"), }, }, @@ -30,10 +57,7 @@ func testIndexerTableChecks() map[string]indexerTestCase { expected: []byte("node\x00"), }, write: indexValue{ - source: &structs.HealthCheck{ - Node: "NoDe", - ServiceID: "SeRvIcE", - }, + source: obj, expected: []byte("node\x00"), }, }, diff --git a/agent/consul/state/catalog_schema.go b/agent/consul/state/catalog_schema.go index eef6c6d2a..5c10c8e34 100644 --- a/agent/consul/state/catalog_schema.go +++ b/agent/consul/state/catalog_schema.go @@ -325,8 +325,8 @@ type upstreamDownstream struct { structs.RaftIndex } -// NodeCheckID is used to query the ID index of the checks table. -type NodeCheckID struct { +// NodeCheckQuery is used to query the ID index of the checks table. +type NodeCheckQuery struct { Node string CheckID string structs.EnterpriseMeta diff --git a/agent/consul/state/catalog_test.go b/agent/consul/state/catalog_test.go index 3302ffa8e..b68a76aca 100644 --- a/agent/consul/state/catalog_test.go +++ b/agent/consul/state/catalog_test.go @@ -1307,7 +1307,7 @@ func TestStateStore_DeleteNode(t *testing.T) { } // Associated health check was removed. - checks, err := tx.Get(tableChecks, indexID, NodeCheckID{Node: "node1", CheckID: "check1"}) + checks, err := tx.Get(tableChecks, indexID, NodeCheckQuery{Node: "node1", CheckID: "check1"}) if err != nil { t.Fatalf("err: %s", err) } @@ -2067,7 +2067,7 @@ func TestStateStore_DeleteService(t *testing.T) { // that it actually is removed in the state store. tx := s.db.Txn(false) defer tx.Abort() - check, err := tx.First(tableChecks, indexID, NodeCheckID{Node: "node1", CheckID: "check1"}) + check, err := tx.First(tableChecks, indexID, NodeCheckQuery{Node: "node1", CheckID: "check1"}) if err != nil || check != nil { t.Fatalf("bad: %#v (err: %s)", check, err) } diff --git a/agent/consul/state/session_oss.go b/agent/consul/state/session_oss.go index cd4cad3ad..483ff840d 100644 --- a/agent/consul/state/session_oss.go +++ b/agent/consul/state/session_oss.go @@ -108,7 +108,7 @@ func sessionMaxIndex(tx ReadTxn, entMeta *structs.EnterpriseMeta) uint64 { func validateSessionChecksTxn(tx *txn, session *structs.Session) error { // Go over the session checks and ensure they exist. for _, checkID := range session.CheckIDs() { - check, err := tx.First(tableChecks, indexID, NodeCheckID{Node: session.Node, CheckID: string(checkID)}) + check, err := tx.First(tableChecks, indexID, NodeCheckQuery{Node: session.Node, CheckID: string(checkID)}) if err != nil { return fmt.Errorf("failed check lookup: %s", err) } diff --git a/agent/consul/state/state_store_test.go b/agent/consul/state/state_store_test.go index 0e0db1f2b..985e7540b 100644 --- a/agent/consul/state/state_store_test.go +++ b/agent/consul/state/state_store_test.go @@ -163,7 +163,7 @@ func testRegisterCheck(t *testing.T, s *Store, idx uint64, tx := s.db.Txn(false) defer tx.Abort() - c, err := tx.First(tableChecks, indexID, NodeCheckID{Node: nodeID, CheckID: string(checkID)}) + c, err := tx.First(tableChecks, indexID, NodeCheckQuery{Node: nodeID, CheckID: string(checkID)}) if err != nil { t.Fatalf("err: %s", err) }