From bd6b7ad1623084cee83fe1423c11c7db8680ab02 Mon Sep 17 00:00:00 2001 From: Kyle Havlovitz Date: Sun, 2 Dec 2018 23:11:48 -0800 Subject: [PATCH] txn: add pre-check operations to txn endpoint --- agent/consul/catalog_endpoint.go | 11 ++++++++--- agent/consul/state/catalog.go | 2 +- agent/consul/txn_endpoint.go | 5 ++++- 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/agent/consul/catalog_endpoint.go b/agent/consul/catalog_endpoint.go index da13abf6c..447a6f38f 100644 --- a/agent/consul/catalog_endpoint.go +++ b/agent/consul/catalog_endpoint.go @@ -20,6 +20,13 @@ type Catalog struct { srv *Server } +// checkPreApply does the verification of a check before it is applied to Raft. +func checkPreApply(check *structs.HealthCheck) { + if check.CheckID == "" && check.Name != "" { + check.CheckID = types.CheckID(check.Name) + } +} + // Register is used register that a node is providing a given service. func (c *Catalog) Register(args *structs.RegisterRequest, reply *struct{}) error { if done, err := c.srv.forward("Catalog.Register", args, args, reply); done { @@ -96,12 +103,10 @@ func (c *Catalog) Register(args *structs.RegisterRequest, reply *struct{}) error args.Check = nil } for _, check := range args.Checks { - if check.CheckID == "" && check.Name != "" { - check.CheckID = types.CheckID(check.Name) - } if check.Node == "" { check.Node = args.Node } + checkPreApply(check) } // Check the complete register request against the given ACL policy. diff --git a/agent/consul/state/catalog.go b/agent/consul/state/catalog.go index 1114cd2e3..89d21a267 100644 --- a/agent/consul/state/catalog.go +++ b/agent/consul/state/catalog.go @@ -1592,7 +1592,7 @@ func (s *Store) DeleteCheck(idx uint64, node string, checkID types.CheckID) erro } // deleteCheckCASTxn is used to try doing a check delete operation with a given -// raft index. If the CAS index specified is not equal to the last bserved index for +// raft index. If the CAS index specified is not equal to the last observed index for // the given check, then the call is a noop, otherwise a normal check delete is invoked. func (s *Store) deleteCheckCASTxn(tx *memdb.Txn, idx, cidx uint64, node string, checkID types.CheckID) (bool, error) { // Try to retrieve the existing health check. diff --git a/agent/consul/txn_endpoint.go b/agent/consul/txn_endpoint.go index 236d4da9b..785beadf0 100644 --- a/agent/consul/txn_endpoint.go +++ b/agent/consul/txn_endpoint.go @@ -21,7 +21,8 @@ func (t *Txn) preCheck(authorizer acl.Authorizer, ops structs.TxnOps) structs.Tx // Perform the pre-apply checks for any KV operations. for i, op := range ops { - if op.KV != nil { + switch { + case op.KV != nil: ok, err := kvsPreApply(t.srv, authorizer, op.KV.Verb, &op.KV.DirEnt) if err != nil { errors = append(errors, &structs.TxnError{ @@ -35,6 +36,8 @@ func (t *Txn) preCheck(authorizer acl.Authorizer, ops structs.TxnOps) structs.Tx What: err.Error(), }) } + case op.Check != nil: + checkPreApply(&op.Check.Check) } }