github.com/Ilhicas/nomad@v1.0.4-0.20210304152020-e86851182bc3/nomad/drainer_shims.go (about) 1 package nomad 2 3 import ( 4 "time" 5 6 "github.com/hashicorp/nomad/nomad/structs" 7 ) 8 9 // drainerShim implements the drainer.RaftApplier interface required by the 10 // NodeDrainer. 11 type drainerShim struct { 12 s *Server 13 } 14 15 func (d drainerShim) NodesDrainComplete(nodes []string, event *structs.NodeEvent) (uint64, error) { 16 args := &structs.BatchNodeUpdateDrainRequest{ 17 Updates: make(map[string]*structs.DrainUpdate, len(nodes)), 18 NodeEvents: make(map[string]*structs.NodeEvent, len(nodes)), 19 WriteRequest: structs.WriteRequest{Region: d.s.config.Region}, 20 UpdatedAt: time.Now().Unix(), 21 } 22 23 update := &structs.DrainUpdate{} 24 for _, node := range nodes { 25 args.Updates[node] = update 26 if event != nil { 27 args.NodeEvents[node] = event 28 } 29 } 30 31 resp, index, err := d.s.raftApply(structs.BatchNodeUpdateDrainRequestType, args) 32 return d.convertApplyErrors(resp, index, err) 33 } 34 35 func (d drainerShim) AllocUpdateDesiredTransition(allocs map[string]*structs.DesiredTransition, evals []*structs.Evaluation) (uint64, error) { 36 args := &structs.AllocUpdateDesiredTransitionRequest{ 37 Allocs: allocs, 38 Evals: evals, 39 WriteRequest: structs.WriteRequest{Region: d.s.config.Region}, 40 } 41 resp, index, err := d.s.raftApply(structs.AllocUpdateDesiredTransitionRequestType, args) 42 return d.convertApplyErrors(resp, index, err) 43 } 44 45 // convertApplyErrors parses the results of a raftApply and returns the index at 46 // which it was applied and any error that occurred. Raft Apply returns two 47 // separate errors, Raft library errors and user returned errors from the FSM. 48 // This helper, joins the errors by inspecting the applyResponse for an error. 49 func (d drainerShim) convertApplyErrors(applyResp interface{}, index uint64, err error) (uint64, error) { 50 if applyResp != nil { 51 if fsmErr, ok := applyResp.(error); ok && fsmErr != nil { 52 return index, fsmErr 53 } 54 } 55 return index, err 56 }