gopkg.in/hashicorp/nomad.v0@v0.11.8/nomad/client_stats_endpoint.go (about)

     1  package nomad
     2  
     3  import (
     4  	"errors"
     5  	"time"
     6  
     7  	metrics "github.com/armon/go-metrics"
     8  	log "github.com/hashicorp/go-hclog"
     9  	nstructs "github.com/hashicorp/nomad/nomad/structs"
    10  
    11  	"github.com/hashicorp/nomad/client/structs"
    12  )
    13  
    14  // ClientStats is used to forward RPC requests to the targed Nomad client's
    15  // ClientStats endpoint.
    16  type ClientStats struct {
    17  	srv    *Server
    18  	logger log.Logger
    19  }
    20  
    21  func (s *ClientStats) Stats(args *nstructs.NodeSpecificRequest, reply *structs.ClientStatsResponse) error {
    22  	// We only allow stale reads since the only potentially stale information is
    23  	// the Node registration and the cost is fairly high for adding another hope
    24  	// in the forwarding chain.
    25  	args.QueryOptions.AllowStale = true
    26  
    27  	// Potentially forward to a different region.
    28  	if done, err := s.srv.forward("ClientStats.Stats", args, args, reply); done {
    29  		return err
    30  	}
    31  	defer metrics.MeasureSince([]string{"nomad", "client_stats", "stats"}, time.Now())
    32  
    33  	// Check node read permissions
    34  	if aclObj, err := s.srv.ResolveToken(args.AuthToken); err != nil {
    35  		return err
    36  	} else if aclObj != nil && !aclObj.AllowNodeRead() {
    37  		return nstructs.ErrPermissionDenied
    38  	}
    39  
    40  	// Verify the arguments.
    41  	if args.NodeID == "" {
    42  		return errors.New("missing NodeID")
    43  	}
    44  
    45  	// Check if the node even exists and is compatible with NodeRpc
    46  	snap, err := s.srv.State().Snapshot()
    47  	if err != nil {
    48  		return err
    49  	}
    50  
    51  	// Make sure Node is new enough to support RPC
    52  	_, err = getNodeForRpc(snap, args.NodeID)
    53  	if err != nil {
    54  		return err
    55  	}
    56  
    57  	// Get the connection to the client
    58  	state, ok := s.srv.getNodeConn(args.NodeID)
    59  	if !ok {
    60  
    61  		// Determine the Server that has a connection to the node.
    62  		srv, err := s.srv.serverWithNodeConn(args.NodeID, s.srv.Region())
    63  		if err != nil {
    64  			return err
    65  		}
    66  
    67  		if srv == nil {
    68  			return nstructs.ErrNoNodeConn
    69  		}
    70  
    71  		return s.srv.forwardServer(srv, "ClientStats.Stats", args, reply)
    72  	}
    73  
    74  	// Make the RPC
    75  	return NodeRpc(state.Session, "ClientStats.Stats", args, reply)
    76  }