txn: add pre-check operations to txn endpoint

This commit is contained in:
Kyle Havlovitz 2018-12-02 23:11:48 -08:00
parent 8a0d7b65d6
commit bd6b7ad162
3 changed files with 13 additions and 5 deletions

View File

@ -20,6 +20,13 @@ type Catalog struct {
srv *Server 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. // Register is used register that a node is providing a given service.
func (c *Catalog) Register(args *structs.RegisterRequest, reply *struct{}) error { func (c *Catalog) Register(args *structs.RegisterRequest, reply *struct{}) error {
if done, err := c.srv.forward("Catalog.Register", args, args, reply); done { 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 args.Check = nil
} }
for _, check := range args.Checks { for _, check := range args.Checks {
if check.CheckID == "" && check.Name != "" {
check.CheckID = types.CheckID(check.Name)
}
if check.Node == "" { if check.Node == "" {
check.Node = args.Node check.Node = args.Node
} }
checkPreApply(check)
} }
// Check the complete register request against the given ACL policy. // Check the complete register request against the given ACL policy.

View File

@ -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 // 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. // 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) { func (s *Store) deleteCheckCASTxn(tx *memdb.Txn, idx, cidx uint64, node string, checkID types.CheckID) (bool, error) {
// Try to retrieve the existing health check. // Try to retrieve the existing health check.

View File

@ -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. // Perform the pre-apply checks for any KV operations.
for i, op := range ops { 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) ok, err := kvsPreApply(t.srv, authorizer, op.KV.Verb, &op.KV.DirEnt)
if err != nil { if err != nil {
errors = append(errors, &structs.TxnError{ errors = append(errors, &structs.TxnError{
@ -35,6 +36,8 @@ func (t *Txn) preCheck(authorizer acl.Authorizer, ops structs.TxnOps) structs.Tx
What: err.Error(), What: err.Error(),
}) })
} }
case op.Check != nil:
checkPreApply(&op.Check.Check)
} }
} }