open-nomad/command/agent/node_endpoint.go

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

221 lines
5.8 KiB
Go
Raw Normal View History

2015-09-06 21:18:11 +00:00
package agent
import (
"net/http"
"strings"
2018-02-23 23:56:36 +00:00
"github.com/hashicorp/nomad/api"
2015-09-06 21:18:11 +00:00
"github.com/hashicorp/nomad/nomad/structs"
)
func (s *HTTPServer) NodesRequest(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
if req.Method != "GET" {
return nil, CodedError(405, ErrInvalidMethod)
}
2015-09-06 21:31:17 +00:00
args := structs.NodeListRequest{}
if s.parse(resp, req, &args.Region, &args.QueryOptions) {
return nil, nil
}
args.Fields = &structs.NodeStubFields{}
// Parse resources field selection
2020-10-14 19:23:25 +00:00
resources, err := parseBool(req, "resources")
if err != nil {
return nil, err
2020-10-14 19:23:25 +00:00
}
if resources != nil {
args.Fields.Resources = *resources
}
// Parse OS
os, err := parseBool(req, "os")
if err != nil {
return nil, err
}
if os != nil {
args.Fields.OS = *os
}
2015-09-06 21:31:17 +00:00
var out structs.NodeListResponse
if err := s.agent.RPC("Node.List", &args, &out); err != nil {
2015-09-06 21:31:17 +00:00
return nil, err
}
setMeta(resp, &out.QueryMeta)
2015-09-07 17:03:10 +00:00
if out.Nodes == nil {
out.Nodes = make([]*structs.NodeListStub, 0)
}
2015-09-06 21:31:17 +00:00
return out.Nodes, nil
2015-09-06 21:18:11 +00:00
}
func (s *HTTPServer) NodeSpecificRequest(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
path := strings.TrimPrefix(req.URL.Path, "/v1/node/")
switch {
case strings.HasSuffix(path, "/evaluate"):
nodeName := strings.TrimSuffix(path, "/evaluate")
return s.nodeForceEvaluate(resp, req, nodeName)
case strings.HasSuffix(path, "/allocations"):
nodeName := strings.TrimSuffix(path, "/allocations")
return s.nodeAllocations(resp, req, nodeName)
case strings.HasSuffix(path, "/drain"):
nodeName := strings.TrimSuffix(path, "/drain")
return s.nodeToggleDrain(resp, req, nodeName)
2018-02-27 20:59:27 +00:00
case strings.HasSuffix(path, "/eligibility"):
nodeName := strings.TrimSuffix(path, "/eligibility")
return s.nodeToggleEligibility(resp, req, nodeName)
2017-10-26 06:51:53 +00:00
case strings.HasSuffix(path, "/purge"):
nodeName := strings.TrimSuffix(path, "/purge")
return s.nodePurge(resp, req, nodeName)
2015-09-06 21:18:11 +00:00
default:
return s.nodeQuery(resp, req, path)
}
}
func (s *HTTPServer) nodeForceEvaluate(resp http.ResponseWriter, req *http.Request,
nodeID string) (interface{}, error) {
if req.Method != "PUT" && req.Method != "POST" {
return nil, CodedError(405, ErrInvalidMethod)
}
args := structs.NodeEvaluateRequest{
NodeID: nodeID,
}
2017-09-07 23:56:15 +00:00
s.parseWriteRequest(req, &args.WriteRequest)
2015-09-06 21:18:11 +00:00
var out structs.NodeUpdateResponse
if err := s.agent.RPC("Node.Evaluate", &args, &out); err != nil {
2015-09-06 21:18:11 +00:00
return nil, err
}
setIndex(resp, out.Index)
return out, nil
}
func (s *HTTPServer) nodeAllocations(resp http.ResponseWriter, req *http.Request,
nodeID string) (interface{}, error) {
if req.Method != "GET" {
return nil, CodedError(405, ErrInvalidMethod)
}
args := structs.NodeSpecificRequest{
NodeID: nodeID,
}
if s.parse(resp, req, &args.Region, &args.QueryOptions) {
return nil, nil
}
var out structs.NodeAllocsResponse
if err := s.agent.RPC("Node.GetAllocs", &args, &out); err != nil {
2015-09-06 21:18:11 +00:00
return nil, err
}
setMeta(resp, &out.QueryMeta)
2015-09-07 17:03:10 +00:00
if out.Allocs == nil {
out.Allocs = make([]*structs.Allocation, 0)
}
for _, alloc := range out.Allocs {
alloc.SetEventDisplayMessages()
}
2015-09-06 21:18:11 +00:00
return out.Allocs, nil
}
func (s *HTTPServer) nodeToggleDrain(resp http.ResponseWriter, req *http.Request,
2015-09-07 03:03:59 +00:00
nodeID string) (interface{}, error) {
2015-09-06 21:18:11 +00:00
if req.Method != "PUT" && req.Method != "POST" {
return nil, CodedError(405, ErrInvalidMethod)
}
2018-02-23 23:56:36 +00:00
var drainRequest api.NodeUpdateDrainRequest
2018-02-27 17:40:17 +00:00
err := decodeBody(req, &drainRequest)
if err != nil {
return nil, CodedError(400, err.Error())
2015-09-07 02:35:05 +00:00
}
2015-09-07 03:03:59 +00:00
args := structs.NodeUpdateDrainRequest{
NodeID: nodeID,
MarkEligible: drainRequest.MarkEligible,
2021-05-07 17:58:40 +00:00
Meta: drainRequest.Meta,
2018-02-23 23:56:36 +00:00
}
if drainRequest.DrainSpec != nil {
args.DrainStrategy = &structs.DrainStrategy{
DrainSpec: structs.DrainSpec{
Deadline: drainRequest.DrainSpec.Deadline,
IgnoreSystemJobs: drainRequest.DrainSpec.IgnoreSystemJobs,
},
}
2015-09-07 03:03:59 +00:00
}
2017-09-07 23:56:15 +00:00
s.parseWriteRequest(req, &args.WriteRequest)
2015-09-07 03:03:59 +00:00
var out structs.NodeDrainUpdateResponse
if err := s.agent.RPC("Node.UpdateDrain", &args, &out); err != nil {
2015-09-07 03:03:59 +00:00
return nil, err
}
setIndex(resp, out.Index)
return out, nil
2015-09-06 21:18:11 +00:00
}
2018-02-27 20:59:27 +00:00
func (s *HTTPServer) nodeToggleEligibility(resp http.ResponseWriter, req *http.Request,
nodeID string) (interface{}, error) {
if req.Method != "PUT" && req.Method != "POST" {
return nil, CodedError(405, ErrInvalidMethod)
}
var eligibilityRequest structs.NodeUpdateEligibilityRequest
if err := decodeBody(req, &eligibilityRequest); err != nil {
2018-02-27 20:59:27 +00:00
return nil, CodedError(400, err.Error())
}
if eligibilityRequest.NodeID == "" {
eligibilityRequest.NodeID = nodeID
}
s.parseWriteRequest(req, &eligibilityRequest.WriteRequest)
2018-02-27 20:59:27 +00:00
var out structs.NodeEligibilityUpdateResponse
if err := s.agent.RPC("Node.UpdateEligibility", &eligibilityRequest, &out); err != nil {
2018-02-27 20:59:27 +00:00
return nil, err
}
setIndex(resp, out.Index)
return out, nil
2018-02-27 20:59:27 +00:00
}
2015-09-06 21:18:11 +00:00
func (s *HTTPServer) nodeQuery(resp http.ResponseWriter, req *http.Request,
nodeID string) (interface{}, error) {
if req.Method != "GET" {
return nil, CodedError(405, ErrInvalidMethod)
}
args := structs.NodeSpecificRequest{
NodeID: nodeID,
}
if s.parse(resp, req, &args.Region, &args.QueryOptions) {
return nil, nil
}
var out structs.SingleNodeResponse
if err := s.agent.RPC("Node.GetNode", &args, &out); err != nil {
2015-09-06 21:18:11 +00:00
return nil, err
}
setMeta(resp, &out.QueryMeta)
if out.Node == nil {
return nil, CodedError(404, "node not found")
}
return out.Node, nil
}
2017-10-26 06:51:53 +00:00
func (s *HTTPServer) nodePurge(resp http.ResponseWriter, req *http.Request, nodeID string) (interface{}, error) {
2017-10-27 17:43:57 +00:00
if req.Method != "PUT" && req.Method != "POST" {
2017-10-26 06:51:53 +00:00
return nil, CodedError(405, ErrInvalidMethod)
}
args := structs.NodeDeregisterRequest{
NodeID: nodeID,
}
s.parseWriteRequest(req, &args.WriteRequest)
var out structs.NodeUpdateResponse
if err := s.agent.RPC("Node.Deregister", &args, &out); err != nil {
return nil, err
}
setIndex(resp, out.Index)
return out, nil
}